Netfilter fixes for net
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter fixes for net
Hi,
The following patchset contains Netfilter fixes for net:
1) Missing #ifdef CONFIG_IP6_NF_IPTABLES in recent xt_socket fix.
2) Fix incorrect flow action array size in nf_tables.
3) Unregister flowtable hooks from netns exit path.
4) Fix missing limit object release, from Florian Westphal.
5) Memleak in nf_tables object update path, also from Florian.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git
Thanks.
----------------------------------------------------------------
The following changes since commit 143de8d97d79316590475dc2a84513c63c863ddf:
tipc: fix a bit overflow in tipc_crypto_key_rcv() (2022-02-13 12:12:25 +0000)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git HEAD
for you to fetch changes up to 33170d18fd2c5f660ebdad1a5436a611bd749320:
netfilter: nf_tables: fix memory leak during stateful obj update (2022-02-21 15:52:14 +0100)
----------------------------------------------------------------
Florian Westphal (2):
netfilter: nft_limit: fix stateful object memory leak
netfilter: nf_tables: fix memory leak during stateful obj update
Pablo Neira Ayuso (3):
netfilter: xt_socket: missing ifdef CONFIG_IP6_NF_IPTABLES dependency
netfilter: nf_tables_offload: incorrect flow offload action array size
netfilter: nf_tables: unregister flowtable hooks on netns exit
include/net/netfilter/nf_tables.h | 2 +- include/net/netfilter/nf_tables_offload.h | 2 -- net/netfilter/nf_tables_api.c | 14 +++++++++++--- net/netfilter/nf_tables_offload.c | 3 ++- net/netfilter/nft_dup_netdev.c | 6 ++++++ net/netfilter/nft_fwd_netdev.c | 6 ++++++ net/netfilter/nft_immediate.c | 12 +++++++++++- net/netfilter/nft_limit.c | 18 ++++++++++++++++++ net/netfilter/xt_socket.c | 2 ++ 9 files changed, 57 insertions(+), 8 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/5 netfilter: xt_socket: missing ifdef CONFIG_IP6_NF_IPTABLES dependency
nf_defrag_ipv6_disable() requires CONFIG_IP6_NF_IPTABLES.
--- net/netfilter/xt_socket.c | 2 ++ 1 file changed, 2 insertions(+)
net/netfilter/xt_socket.c +2 -0
@@ -220,8 +220,10 @@ static void socket_mt_destroy(const struct xt_mtdtor_param *par) {if (par->family ==NFPROTO_IPV4 )nf_defrag_ipv4_disable (par->net); +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) else if (par->family == NFPROTO_IPV6) nf_defrag_ipv6_disable(par->net); +#endif }static struct xt_match socket_mt_reg[]__read_mostly = {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/5 netfilter: nf_tables_offload: incorrect flow offload action array size
In reply to Pablo Neira Ayusoimmediate verdict expression needs to allocate one slot in the flow offload action array, however, immediate data expression does not need to do so.
fwd and dup expression need to allocate one slot, this is missing.
Add a new offload_action interface to report if this expression needs to allocate one slot in the flow offload action array.
--- include/net/netfilter/nf_tables.h | 2 +- include/net/netfilter/nf_tables_offload.h | 2 -- net/netfilter/nf_tables_offload.c | 3 ++- net/netfilter/nft_dup_netdev.c | 6 ++++++ net/netfilter/nft_fwd_netdev.c | 6 ++++++ net/netfilter/nft_immediate.c | 12 +++++++++++- 6 files changed, 26 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -905,9 +905,9 @@ struct nft_expr_ops {int (*offload)(struct nft_offload_ctx *ctx,struct nft_flow_rule *flow,const struct nft_expr *expr); +bool (*offload_action)(const struct nft_expr *expr);void (*offload_stats)(struct nft_expr *expr,const struct flow_stats *stats); -u32 offload_flags;const struct nft_expr_type *type;void *data; };
include/net/netfilter/nf_tables_offload.h +0 -2
@@ -67,8 +67,6 @@ struct nft_flow_rule {struct flow_rule *rule; }; -#define NFT_OFFLOAD_F_ACTION (1 << 0) -void nft_flow_rule_set_addr_type (struct nft_flow_rule *flow,enum flow_dissector_key_id addr_type);
net/netfilter/nf_tables_offload.c +2 -1
@@ -94,7 +94,8 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net, expr =nft_expr_first (rule);while (nft_expr_more (rule, expr)) { -if (expr->ops->offload_flags &NFT_OFFLOAD_F_ACTION ) +if (expr->ops->offload_action && + expr->ops->offload_action (expr)) num_actions++; expr =nft_expr_next (expr);
net/netfilter/nft_dup_netdev.c +6 -0
@@ -67,6 +67,11 @@ static int nft_dup_netdev_offload(struct nft_offload_ctx *ctx,return nft_fwd_dup_netdev_offload (ctx, flow,FLOW_ACTION_MIRRED , oif); } +static bool nft_dup_netdev_offload_action (const struct nft_expr *expr) +{ +return true; +} +static struct nft_expr_type nft_dup_netdev_type;static const struct nft_expr_ops nft_dup_netdev_ops = { .type = &nft_dup_netdev_type,
@@ -75,6 +80,7 @@ static const struct nft_expr_ops nft_dup_netdev_ops = { .init = nft_dup_netdev_init, .dump = nft_dup_netdev_dump, .offload = nft_dup_netdev_offload, + .offload_action = nft_dup_netdev_offload_action, };static struct nft_expr_type nft_dup_netdev_type__read_mostly = {
net/netfilter/nft_fwd_netdev.c +6 -0
@@ -79,6 +79,11 @@ static int nft_fwd_netdev_offload(struct nft_offload_ctx *ctx,return nft_fwd_dup_netdev_offload (ctx, flow,FLOW_ACTION_REDIRECT , oif); } +static bool nft_fwd_netdev_offload_action (const struct nft_expr *expr) +{ +return true; +} +struct nft_fwd_neigh {u8 sreg_dev;u8 sreg_addr;
@@ -222,6 +227,7 @@ static const struct nft_expr_ops nft_fwd_netdev_ops = { .dump = nft_fwd_netdev_dump, .validate = nft_fwd_validate, .offload = nft_fwd_netdev_offload, + .offload_action = nft_fwd_netdev_offload_action, }; static const struct nft_expr_ops *
net/netfilter/nft_immediate.c +11 -1
@@ -213,6 +213,16 @@ static int nft_immediate_offload(struct nft_offload_ctx *ctx,return 0 ; } +static bool nft_immediate_offload_action (const struct nft_expr *expr) +{ +const struct nft_immediate_expr *priv =nft_expr_priv (expr); + +if (priv->dreg ==NFT_REG_VERDICT ) +return true; + +return false; +} +static const struct nft_expr_ops nft_imm_ops = { .type = &nft_imm_type, .size =NFT_EXPR_SIZE (sizeof (struct nft_immediate_expr )),
@@ -224,7 +234,7 @@ static const struct nft_expr_ops nft_imm_ops = { .dump = nft_immediate_dump, .validate = nft_immediate_validate, .offload = nft_immediate_offload, - .offload_flags = NFT_OFFLOAD_F_ACTION, + .offload_action = nft_immediate_offload_action, };struct nft_expr_type nft_imm_type__read_mostly = {
Signature · 1 line
2.30.2
Re: [PATCH nf 2/5] netfilter: nf_tables_offload: incorrect flow offload action array size
Pablo Neira Ayuso wrote on Mon, Feb 21, 2022 at 05:17:54PM +0100:
Quoted 11 lines
immediate verdict expression needs to allocate one slot in the flow offload action array, however, immediate data expression does not need to do so.
fwd and dup expression need to allocate one slot, this is missing.
Add a new
offload_actioninterface to report if this expression needs to allocate one slot in the flow offload action array.Fixes be2861dc36d7 ("netfilter: nft_{fwd,dup}_netdev: add offload support")Reported-and-tested-by Nick Gregory <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
FYI this patch has been reported as fixing an arbitrary oob write on oss-security just now:
https://www.openwall.com/lists/oss-security/2022/02/21/2
So it might make sense to send it quickly to Linus and linux-stable for immediate backport as it will get some attention...
The poster didn't say anything about coordinating this with security handling people so it's safe to assume that wasn't done.
Thanks,
Signature · 1 line
Dominique
Pablo Neira Ayuso
maintainerPATCH 3/5 netfilter: nf_tables: unregister flowtable hooks on netns exit
In reply to Pablo Neira AyusoUnregister flowtable hooks before they are releases via nf_tables_flowtable_destroy() otherwise hook core reports UAF.
BUG: KASAN: use-after-free in nf_hook_entries_grow+0x5a7/0x700 net/netfilter/core.c:142 net/netfilter/core.c:142 Read of size 4 at addr ffff8880736f7438 by task syz-executor579/3666 CPU: 0 PID: 3666 Comm: syz-executor579 Not tainted 5.16.0-rc5-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] __dump_stack lib/dump_stack.c:88 [inline] lib/dump_stack.c:106 dump_stack_lvl+0x1dc/0x2d8 lib/dump_stack.c:106 lib/dump_stack.c:106 print_address_description+0x65/0x380 mm/kasan/report.c:247 mm/kasan/report.c:247 __kasan_report mm/kasan/report.c:433 [inline] __kasan_report mm/kasan/report.c:433 [inline] mm/kasan/report.c:450 kasan_report+0x19a/0x1f0 mm/kasan/report.c:450 mm/kasan/report.c:450 nf_hook_entries_grow+0x5a7/0x700 net/netfilter/core.c:142 net/netfilter/core.c:142 __nf_register_net_hook+0x27e/0x8d0 net/netfilter/core.c:429 net/netfilter/core.c:429 nf_register_net_hook+0xaa/0x180 net/netfilter/core.c:571 net/netfilter/core.c:571 nft_register_flowtable_net_hooks+0x3c5/0x730 net/netfilter/nf_tables_api.c:7232 net/netfilter/nf_tables_api.c:7232 nf_tables_newflowtable+0x2022/0x2cf0 net/netfilter/nf_tables_api.c:7430 net/netfilter/nf_tables_api.c:7430 nfnetlink_rcv_batch net/netfilter/nfnetlink.c:513 [inline] nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:634 [inline] nfnetlink_rcv_batch net/netfilter/nfnetlink.c:513 [inline] net/netfilter/nfnetlink.c:652 nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:634 [inline] net/netfilter/nfnetlink.c:652 nfnetlink_rcv+0x10e6/0x2550 net/netfilter/nfnetlink.c:652 net/netfilter/nfnetlink.c:652
__nft_release_hook() calls nft_unregister_flowtable_net_hooks() which only unregisters the hooks, then after RCU grace period, it is guaranteed that no packets add new entries to the flowtable (no flow offload rules and flowtable hooks are reachable from packet path), so it is safe to call nf_flow_table_free() which cleans up the remaining entries from the flowtable (both software and hardware) and it unbinds the flow_block.
--- net/netfilter/nf_tables_api.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nf_tables_api.c +3 -0
@@ -9636,10 +9636,13 @@ EXPORT_SYMBOL_GPL(__nft_release_basechain);static void __nft_release_hook (struct net *net,struct nft_table *table) { +struct nft_flowtable *flowtable;struct nft_chain *chain;list_for_each_entry (chain, &table->chains, list)nf_tables_unregister_hook (net, table, chain); +list_for_each_entry (flowtable, &table->flowtables, list) +nft_unregister_flowtable_net_hooks (net, &flowtable->hook_list); }static void __nft_release_hooks (struct net *net)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/5 netfilter: nft_limit: fix stateful object memory leak
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
We need to provide a destroy callback to release the extra fields.
--- net/netfilter/nft_limit.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
net/netfilter/nft_limit.c +18 -0
@@ -340,11 +340,20 @@ static int nft_limit_obj_pkts_dump(struct sk_buff *skb,return nft_limit_dump (skb, &priv->limit,NFT_LIMIT_PKTS ); } +static void nft_limit_obj_pkts_destroy (const struct nft_ctx *ctx, +struct nft_object *obj) +{ +struct nft_limit_priv_pkts *priv =nft_obj_data (obj); + +nft_limit_destroy (ctx, &priv->limit); +} +static struct nft_object_type nft_limit_obj_type;static const struct nft_object_ops nft_limit_obj_pkts_ops = { .type = &nft_limit_obj_type, .size =NFT_EXPR_SIZE (sizeof (struct nft_limit_priv_pkts )), .init = nft_limit_obj_pkts_init, + .destroy = nft_limit_obj_pkts_destroy, .eval = nft_limit_obj_pkts_eval, .dump = nft_limit_obj_pkts_dump, };
@@ -378,11 +387,20 @@ static int nft_limit_obj_bytes_dump(struct sk_buff *skb,return nft_limit_dump (skb, priv,NFT_LIMIT_PKT_BYTES ); } +static void nft_limit_obj_bytes_destroy (const struct nft_ctx *ctx, +struct nft_object *obj) +{ +struct nft_limit_priv *priv =nft_obj_data (obj); + +nft_limit_destroy (ctx, priv); +} +static struct nft_object_type nft_limit_obj_type;static const struct nft_object_ops nft_limit_obj_bytes_ops = { .type = &nft_limit_obj_type, .size =sizeof (struct nft_limit_priv ), .init = nft_limit_obj_bytes_init, + .destroy = nft_limit_obj_bytes_destroy, .eval = nft_limit_obj_bytes_eval, .dump = nft_limit_obj_bytes_dump, };
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/5 netfilter: nf_tables: fix memory leak during stateful obj update
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
stateful objects can be updated from the control plane. The transaction logic allocates a temporary object for this purpose.
The ->init function was called for this object, so plain kfree() leaks resources. We must call ->destroy function of the object.
nft_obj_destroy does this, but it also decrements the module refcount, but the update path doesn't increment it.
To avoid special-casing the update object release, do module_get for the update case too and release it via nft_obj_destroy().
--- net/netfilter/nf_tables_api.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +8 -3
@@ -6553,10 +6553,13 @@ static int nf_tables_updobj(const struct nft_ctx *ctx,struct nft_trans *trans;int err; +if (!try_module_get (type->owner)) +return -ENOENT ; + trans =nft_trans_alloc (ctx,NFT_MSG_NEWOBJ ,sizeof (struct nft_trans_obj ));if (!trans) -return -ENOMEM ; + goto err_trans; newobj =nft_obj_init (ctx, type, attr);if (IS_ERR (newobj)) {
@@ -6573,6 +6576,8 @@ static int nf_tables_updobj(const struct nft_ctx *ctx, err_free_trans:kfree (trans); +err_trans: +module_put (type->owner);return err; }
@@ -8185,7 +8190,7 @@ static void nft_obj_commit_update(struct nft_trans *trans)if (obj->ops->update) obj->ops->update (obj, newobj); -kfree (newobj); +nft_obj_destroy (&trans->ctx, newobj); }static void nft_commit_release (struct nft_trans *trans)
@@ -8976,7 +8981,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)break ;case NFT_MSG_NEWOBJ :if (nft_trans_obj_update (trans)) { -kfree (nft_trans_obj_newobj (trans)); +nft_obj_destroy (&trans->ctx,nft_trans_obj_newobj (trans));nft_trans_destroy (trans); }else { trans->ctx.table->use--;
Signature · 1 line
2.30.2
Jakub Kicinski
maintainerRe: [PATCH nf 5/5] netfilter: nf_tables: fix memory leak during stateful obj update
Quoted 60 lines · On Mon, 21 Feb 2022 17:17:57 +0100 Pablo Neira Ayuso wrote:
From: Florian Westphal <[email protected]>
stateful objects can be updated from the control plane. The transaction logic allocates a temporary object for this purpose.
The ->init function was called for this object, so plain
kfree()leaks resources. We must call ->destroy function of the object.
nft_obj_destroydoes this, but it also decrements the module refcount, but the update path doesn't increment it.To avoid special-casing the update object release, do
module_getfor the update case too and release it vianft_obj_destroy().Fixes d62d0ba97b58 ("netfilter: nf_tables: Introduce stateful object update operation")Cc Fernando Fernandez Mancera <[email protected]>Signed-off-by Florian Westphal <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>--- net/netfilter/nf_tables_api.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)net/netfilter/nf_tables_api.c +7
-2@@ -6553,10 +6553,13 @@ static int nf_tables_updobj(const struct nft_ctx *ctx,struct nft_trans *trans;int err; +if (!try_module_get (type->owner)) +return -ENOENT ; + trans =nft_trans_alloc (ctx,NFT_MSG_NEWOBJ ,sizeof (struct nft_trans_obj ));if (!trans) -return -ENOMEM ; + goto err_trans; newobj =nft_obj_init (ctx, type, attr);if (IS_ERR (newobj)) {@@ -6573,6 +6576,8 @@ static int nf_tables_updobj(const struct nft_ctx *ctx, err_free_trans:kfree (trans); +err_trans: +module_put (type->owner);return err; }@@ -8185,7 +8190,7 @@ static void nft_obj_commit_update(struct nft_trans *trans)if (obj->ops->update) obj->ops->update (obj, newobj); -kfree (newobj); +nft_obj_destroy (&trans->ctx, newobj); }static void nft_commit_release (struct nft_trans *trans)
net/netfilter/nf_tables_api.c static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) · line 8987
Quoted 8 lines
net/netfilter/nf_tables_api.c +1
-1@@ -8976,7 +8981,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)break ;case NFT_MSG_NEWOBJ :if (nft_trans_obj_update (trans)) { -kfree (nft_trans_obj_newobj (trans)); +nft_obj_destroy (&trans->ctx,nft_trans_obj_newobj (trans));nft_trans_destroy (trans); }else { trans->ctx.table->use--;
net/netfilter/nf_tables_api.c:6561:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
if (!trans)
^~~~~~
net/netfilter/nf_tables_api.c:6581:9: note: uninitialized use occurs here
return err;
^~~
net/netfilter/nf_tables_api.c:6561:2: note: remove the 'if' if its condition is always false
if (!trans)
^~~~~~~~~~~
net/netfilter/nf_tables_api.c:6554:9: note: initialize the variable 'err' to silence this warning
int err;
^
= 0Florian Westphal
maintainerRe: [PATCH nf 5/5] netfilter: nf_tables: fix memory leak during stateful obj update
Quoted 12 lines · Jakub Kicinski
net/netfilter/nf_tables_api.c static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) · line 8987
Quoted 10 lines
static void
nft_commit_release(struct nft_trans *trans)net/netfilter/nf_tables_api.c +1
-1@@ -8976,7 +8981,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)break ;case NFT_MSG_NEWOBJ :if (nft_trans_obj_update (trans)) { -kfree (nft_trans_obj_newobj (trans)); +nft_obj_destroy (&trans->ctx,nft_trans_obj_newobj (trans));nft_trans_destroy (trans); }else { trans->ctx.table->use--;
net/netfilter/nf_tables_api.c:6561:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
Quoted 11 lines
if (!trans)
^~~~~~
net/netfilter/nf_tables_api.c:6581:9: note: uninitialized use occurs here
return err;
^~~
net/netfilter/nf_tables_api.c:6561:2: note: remove the 'if' if its condition is always false
if (!trans)
^~~~~~~~~~~
net/netfilter/nf_tables_api.c:6554:9: note: initialize the variable 'err' to silence this warning
int err;
^I've sent a followup patch to this, no idea why gcc doesn't warn here. Looks like I should dump it in favor of clang.