Thread View: linux.kernel.netdev
1 messages
1 total messages
Started by Xuanqiang Luo
Tue, 17 Nov 2026 10:21
Re: [PATCH net] netfilter: nf_tables: don't unregister hooks that were never registered
Author: Xuanqiang Luo
Date: Tue, 17 Nov 2026 10:21
Date: Tue, 17 Nov 2026 10:21
142 lines
13406 bytes
13406 bytes
On 2026/11/17 at 17:54 Ma Xinmeng wrote:
> A basechain created while its table is dormant is accepted without
> registering its hook. Re-owning a PERSIST orphan table with DORMANT set
> then makes nf_tables_table_disable() clear DORMANT before
> nft_table_disable() walks the chains, so __nf_tables_unregister_hook()
> no longer sees the dormant state and calls nf_unregister_net_hook() for a
> hook that was never registered. This triggers WARN_ONCE("hook not found")
> in __nf_unregister_net_hook().
>
> Track whether a basechain's hook was actually registered and skip the
> unregister for chains whose hook was never registered. This preserves the
> existing "add a basechain to a dormant table" API.
>
> Signed-off-by: Ma Xinmeng <1564938642@qq.com>
> ---
> include/net/netfilter/nf_tables.h | 1 +
> net/netfilter/nf_tables_api.c | 29 +++++++++++++++++++++++------
> 2 files changed, 24 insertions(+), 6 deletions(-)
>
> diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
> index 9d5974823..fcd24502b 100644
> --- a/include/net/netfilter/nf_tables.h
> +++ b/include/net/netfilter/nf_tables.h
> @@ -1243,6 +1243,7 @@ struct nft_base_chain {
> const struct nft_chain_type *type;
> u8 policy;
> u8 flags;
> + bool registered;
> struct nft_stats __percpu *stats;
> struct nft_chain chain;
> struct flow_block flow_block;
> diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
> index 8b94b5e7d..25b4075c9 100644
> --- a/net/netfilter/nf_tables_api.c
> +++ b/net/netfilter/nf_tables_api.c
> @@ -428,21 +428,30 @@ static int nf_tables_register_hook(struct net *net,
> {
> struct nft_base_chain *basechain;
> const struct nf_hook_ops *ops;
> + int err;
>
> if (table->flags & NFT_TABLE_F_DORMANT ||
> !nft_is_base_chain(chain))
> return 0;
>
> basechain = nft_base_chain(chain);
> + if (basechain->registered)
> + return 0;
> +
> ops = &basechain->ops;
>
> if (basechain->type->ops_register)
> - return basechain->type->ops_register(net, ops);
> + err = basechain->type->ops_register(net, ops);
> + else if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
> + err = nft_netdev_register_hooks(net, &basechain->hook_list);
> + else
> + err = nf_register_net_hook(net, &basechain->ops);
>
> - if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
> - return nft_netdev_register_hooks(net, &basechain->hook_list);
> + if (err < 0)
> + return err;
>
> - return nf_register_net_hook(net, &basechain->ops);
> + basechain->registered = true;
> + return 0;
> }
>
> static void __nf_tables_unregister_hook(struct net *net,
> @@ -456,22 +465,30 @@ static void __nf_tables_unregister_hook(struct net *net,
> if (!nft_is_base_chain(chain))
> return;
> basechain = nft_base_chain(chain);
> + if (!basechain->registered)
> + return;
> +
> ops = &basechain->ops;
>
> /* must also be called for dormant tables */
> if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
> nft_netdev_unregister_hooks(net, table, &basechain->hook_list,
> release_netdev);
> + basechain->registered = false;
> return;
> }
>
> if (table->flags & NFT_TABLE_F_DORMANT)
> return;
>
> - if (basechain->type->ops_unregister)
> - return basechain->type->ops_unregister(net, ops);
> + if (basechain->type->ops_unregister) {
> + basechain->type->ops_unregister(net, ops);
> + basechain->registered = false;
> + return;
> + }
>
> nf_unregister_net_hook(net, &basechain->ops);
> + basechain->registered = false;
> }
>
> static void nf_tables_unregister_hook(struct net *net,
Thanks for the report, the trigger is clear.
I do not think we need a new field for this.
A dormant table never has any basechain hook registered. The real
problem is that nf_tables_commit() calls nf_tables_table_disable() for
a table that was already dormant.
Since table adoption added __NFT_TABLE_F_WAS_ORPHAN, having
__NFT_TABLE_F_UPDATE set no longer implies a dormant state change.
Could we instead check __NFT_TABLE_F_WAS_AWAKEN, which records an
awake-to-dormant transition? This also covers adoption combined with
such a transition.
Untested, but something like:
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ nf_tables_commit()
case NFT_MSG_NEWTABLE:
...
- if (table->flags & NFT_TABLE_F_DORMANT)
+ if (table->flags & __NFT_TABLE_F_WAS_AWAKEN)
nf_tables_table_disable(net,
table);
Also:
- Please add
Fixes: 31bf508be656 ("netfilter: nf_tables: Implement table adoption
support")
- netfilter fixes go through the nf tree: use [PATCH nf].
- A selftest in tools/testing/selftests/net/netfilter/ would be nice.
Thanks,
Xuanqiang
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads