All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xuanqiang Luo <xuanqiang.luo@linux.dev>
To: Ma Xinmeng <1564938642@qq.com>
Cc: netfilter-devel@vger.kernel.org, netdev@vger.kernel.org,
	Phil Sutter <phil@nwl.cc>,
	Pablo Neira Ayuso <pablo@netfilter.org>,
	Florian Westphal <fw@strlen.de>
Subject: Re: [PATCH net] netfilter: nf_tables: don't unregister hooks that were never registered
Date: Tue, 17 Nov 2026 19:21:07 +0900	[thread overview]
Message-ID: <1273d905-a93d-4d56-a831-cecd1dd948c9@linux.dev> (raw)
In-Reply-To: <tencent_19A48DA487BBFCFD990AC5A8010C663EA007@qq.com>

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

      parent reply	other threads:[~2026-11-17 10:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-11-17  9:21 ` Ma Xinmeng
2026-11-17 10:21 ` Xuanqiang Luo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1273d905-a93d-4d56-a831-cecd1dd948c9@linux.dev \
    --to=xuanqiang.luo@linux.dev \
    --cc=1564938642@qq.com \
    --cc=fw@strlen.de \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.