Netdev List
 help / color / mirror / Atom feed
* [PATCH net] netfilter: nf_tables: don't unregister hooks that were never registered
@ 2026-11-17  8:54 Ma Xinmeng
  2026-11-17  9:03 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Ma Xinmeng @ 2026-11-17  8:54 UTC (permalink / raw)
  To: Pablo Neira Ayuso, Florian Westphal; +Cc: netfilter-devel, netdev, Phil Sutter

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,
-- 
2.56.0.windows.4.niugit


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] netfilter: nf_tables: don't unregister hooks that were never registered
  2026-11-17  8:54 [PATCH net] netfilter: nf_tables: don't unregister hooks that were never registered Ma Xinmeng
@ 2026-11-17  9:03 ` netdev-bot+sinfo
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sinfo @ 2026-11-17  9:03 UTC (permalink / raw)
  To: Ma Xinmeng
  Cc: Pablo Neira Ayuso, Florian Westphal, netfilter-devel, netdev,
	Phil Sutter

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17  9:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  8:54 [PATCH net] netfilter: nf_tables: don't unregister hooks that were never registered Ma Xinmeng
2026-11-17  9:03 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox