netfilter: nf_tables: double hook unregistration in netns path
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/2 netfilter: nf_tables: double hook unregistration in netns path
__nft_release_hooks() is called from pre_netns exit path which unregisters the hooks, then the NETDEV_UNREGISTER event is triggered which unregisters the hooks again.
[ 565.221461] WARNING: CPU: 18 PID: 193 at net/netfilter/core.c:495 __nf_unregister_net_hook+0x247/0x270 [...] [ 565.246890] CPU: 18 PID: 193 Comm: kworker/u64:1 Tainted: G E 5.18.0-rc7+ #27 [ 565.253682] Workqueue: netns cleanup_net [ 565.257059] RIP: 0010:__nf_unregister_net_hook+0x247/0x270 [...] [ 565.297120] Call Trace: [ 565.300900] <TASK> [ 565.304683] nf_tables_flowtable_event+0x16a/0x220 [nf_tables] [ 565.308518] raw_notifier_call_chain+0x63/0x80 [ 565.312386] unregister_netdevice_many+0x54f/0xb50
Unregister and destroy netdev hook from netns pre_exit via kfree_rcu so the NETDEV_UNREGISTER path see unregistered hooks.
Fixes 767d1216bff8 ("netfilter: nftables: fix possible UAF over chains from packet path in netns")
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- net/netfilter/nf_tables_api.c | 54 ++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +41 -13
@@ -222,12 +222,18 @@ static int nft_netdev_register_hooks(struct net *net, }static void nft_netdev_unregister_hooks (struct net *net, -struct list_head *hook_list) +struct list_head *hook_list, +bool release_netdev) { -struct nft_hook *hook; +struct nft_hook *hook, *next; -list_for_each_entry (hook, hook_list, list) +list_for_each_entry_safe (hook, next, hook_list, list) {nf_unregister_net_hook (net, &hook->ops); +if (release_netdev) { +list_del (&hook->list); +kfree_rcu (hook, rcu); + } + } } static int nf_tables_register_hook(struct net *net,
@@ -253,9 +259,10 @@ static int nf_tables_register_hook(struct net *net,return nf_register_net_hook (net, &basechain->ops); } -static void nf_tables_unregister_hook (struct net *net, -const struct nft_table *table, -struct nft_chain *chain) +static void __nf_tables_unregister_hook (struct net *net, +const struct nft_table *table, +struct nft_chain *chain, +bool release_netdev) {struct nft_base_chain *basechain;const struct nf_hook_ops *ops;
@@ -270,11 +277,19 @@ static void nf_tables_unregister_hook(struct net *net,return basechain->type->ops_unregister (net, ops);if (nft_base_chain_netdev (table->family, basechain->ops.hooknum)) -nft_netdev_unregister_hooks (net, &basechain->hook_list); +nft_netdev_unregister_hooks (net, &basechain->hook_list, + release_netdev);else nf_unregister_net_hook (net, &basechain->ops); } +static void nf_tables_unregister_hook (struct net *net, +const struct nft_table *table, +struct nft_chain *chain) +{ +return __nf_tables_unregister_hook (net, table, chain, false); +} +static void nft_trans_commit_list_add_tail (struct net *net,struct nft_trans *trans) {struct nftables_pernet *nft_net =nft_pernet (net);
@@ -7291,13 +7306,25 @@ static void nft_unregister_flowtable_hook(struct net *net, FLOW_BLOCK_UNBIND); } -static void nft_unregister_flowtable_net_hooks (struct net *net, -struct list_head *hook_list) +static void __nft_unregister_flowtable_net_hooks (struct net *net, +struct list_head *hook_list, +bool release_netdev) { -struct nft_hook *hook; +struct nft_hook *hook, *next; -list_for_each_entry (hook, hook_list, list) +list_for_each_entry_safe (hook, next, hook_list, list) {nf_unregister_net_hook (net, &hook->ops); +if (release_netdev) { +list_del (&hook->list); +kfree_rcu (hook); + } + } +} + +static void nft_unregister_flowtable_net_hooks (struct net *net, +struct list_head *hook_list) +{ +__nft_unregister_flowtable_net_hooks (net, hook_list, false); } static int nft_register_flowtable_net_hooks(struct net *net,
@@ -9741,9 +9768,10 @@ static void __nft_release_hook(struct net *net, struct nft_table *table)struct nft_chain *chain;list_for_each_entry (chain, &table->chains, list) -nf_tables_unregister_hook (net, table, chain); +__nf_tables_unregister_hook (net, table, chain, true);list_for_each_entry (flowtable, &table->flowtables, list) -nft_unregister_flowtable_net_hooks (net, &flowtable->hook_list); +__nft_unregister_flowtable_net_hooks (net, &flowtable->hook_list, + true); }static void __nft_release_hooks (struct net *net)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 netfilter: nf_tables: delete flowtable hooks via transaction list
Remove inactive bool field in nft_hook object that was introduced in abadb2f865d7 ("netfilter: nf_tables: delete devices from flowtable"). Move stale flowtable hooks to transaction list instead.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/net/netfilter/nf_tables.h | 1 - net/netfilter/nf_tables_api.c | 40 ++++++++----------------------- 2 files changed, 10 insertions(+), 31 deletions(-)
include/net/netfilter/nf_tables.h +0 -1
@@ -1090,7 +1090,6 @@ struct nft_stats {struct nft_hook {struct list_head list; -bool inactive;struct nf_hook_ops ops;struct rcu_head rcu; };
net/netfilter/nf_tables_api.c +10 -30
@@ -1914,7 +1914,6 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net, goto err_hook_dev; } hook->ops.dev = dev; - hook->inactive = false;return hook;
@@ -7602,7 +7601,8 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx, {const struct nlattr *const *nla = ctx->nla;struct nft_flowtable_hook flowtable_hook; -struct nft_hook *this, *hook; +struct nft_hook *this, *next, *hook; +LIST_HEAD (flowtable_del_list);struct nft_trans *trans;int err;
@@ -7611,13 +7611,13 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,if (err <0 )return err; -list_for_each_entry (this, &flowtable_hook.list, list) { +list_for_each_entry_safe (this, next, &flowtable_hook.list, list) { hook =nft_hook_list_find (&flowtable->hook_list, this);if (!hook) { err = -ENOENT ; goto err_flowtable_del_hook; } - hook->inactive = true; +list_move (&hook->list, &flowtable_del_list); } trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
@@ -7630,6 +7630,7 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,nft_trans_flowtable (trans) = flowtable;nft_trans_flowtable_update (trans) = true;INIT_LIST_HEAD (&nft_trans_flowtable_hooks (trans)); +list_splice (&flowtable_del_list, &nft_trans_flowtable_hooks (trans));nft_flowtable_hook_release (&flowtable_hook);nft_trans_commit_list_add_tail (ctx->net, trans);
@@ -7637,13 +7638,7 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,return 0 ; err_flowtable_del_hook: -list_for_each_entry (this, &flowtable_hook.list, list) { - hook =nft_hook_list_find (&flowtable->hook_list, this); -if (!hook) -break ; - - hook->inactive = false; - } +list_splice (&flowtable_del_list, &flowtable->hook_list);nft_flowtable_hook_release (&flowtable_hook);return err;
@@ -8327,9 +8322,8 @@ static void nft_commit_release(struct nft_trans *trans)nft_obj_destroy (&trans->ctx,nft_trans_obj (trans));break ;case NFT_MSG_DELFLOWTABLE : -if (nft_trans_flowtable_update (trans)) -nft_flowtable_hooks_destroy (&nft_trans_flowtable_hooks (trans)); -else +nft_flowtable_hooks_destroy (&nft_trans_flowtable_hooks (trans)); +if (!nft_trans_flowtable_update (trans))nf_tables_flowtable_destroy (nft_trans_flowtable (trans));break ; }
@@ -8549,17 +8543,6 @@ void nft_chain_del(struct nft_chain *chain)list_del_rcu (&chain->list); } -static void nft_flowtable_hooks_del (struct nft_flowtable *flowtable, -struct list_head *hook_list) -{ -struct nft_hook *hook, *next; - -list_for_each_entry_safe (hook, next, &flowtable->hook_list, list) { -if (hook->inactive) -list_move (&hook->list, hook_list); - } -} -static void nf_tables_module_autoload_cleanup (struct net *net) {struct nftables_pernet *nft_net =nft_pernet (net);
@@ -8904,8 +8887,6 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)break ;case NFT_MSG_DELFLOWTABLE :if (nft_trans_flowtable_update (trans)) { -nft_flowtable_hooks_del (nft_trans_flowtable (trans), - &nft_trans_flowtable_hooks (trans)); nf_tables_flowtable_notify(&trans->ctx, nft_trans_flowtable(trans), &nft_trans_flowtable_hooks(trans),
@@ -8986,7 +8967,6 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)struct nftables_pernet *nft_net =nft_pernet (net);struct nft_trans *trans, *next;struct nft_trans_elem *te; -struct nft_hook *hook;if (action ==NFNL_ABORT_VALIDATE &&nf_tables_validate (net) <0 )
@@ -9117,8 +9097,8 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)break ;case NFT_MSG_DELFLOWTABLE :if (nft_trans_flowtable_update (trans)) { -list_for_each_entry (hook, &nft_trans_flowtable (trans)->hook_list, list) - hook->inactive = false; +list_splice (&nft_trans_flowtable_hooks (trans), + &nft_trans_flowtable (trans)->hook_list); }else { trans->ctx.table->use++;nft_clear (trans->ctx.net,nft_trans_flowtable (trans));
Signature · 1 line
2.30.2