Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: alex000young@gmail.com
Cc: pablo@netfilter.org, fw@strlen.de, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	willemb@google.com, netfilter-devel@vger.kernel.org,
	coreteam@netfilter.org, netdev@vger.kernel.org
Subject: Re: [PATCH net] netfilter: conntrack: serialize helper extension allocation
Date: Thu, 03 Dec 2026 23:59:49 +0000	[thread overview]
Message-ID: <179634238903.3146.4855623999904602326@kernel.org> (raw)
In-Reply-To: <20261201085814.82573-1-alex000young@gmail.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.

Critical: 0 · High: 3 · Medium: 1 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] nf_ct_helper_ext_add() (net/netfilter/nf_conntrack_helper.c:187)
  now takes spin_lock_bh(&ct->lock) around nf_ct_ext_add().
- [High] nf_ct_helper_ext_add() now locks ct->lock for every caller, and
  some callers pass conntrack templates.
- [High] The patch serializes only one of many writers of ct->ext, so the
  bug class it claims to close is still reachable.
- [Medium] The commit message states as fact that "Two CPUs can add the
  helper extension to the same unconfirmed conntrack when shared skbs…
- [Low] The commit message says the lock "also covers publishing
  IPS_HELPER_BIT and initializing the expectation list, so a concurrent…
- [Low] nf_ct_helper_ext_add() is EXPORT_SYMBOL_GPL and has callers well
  beyond nft_ct: - xt_CT.c:94 (template, GFP_KERNEL_ACCOUNT) -…
- [Low] The patch says ct->lock covers publishing IPS_HELPER_BIT, but it
  keeps the non-atomic __set_bit(IPS_HELPER_BIT, &ct->status)…

--

netfilter: conntrack: serialize helper extension allocation

This patch wraps nf_ct_ext_add() in nf_ct_helper_ext_add() with
spin_lock_bh(&ct->lock). The goal is to stop two CPUs from calling
krealloc() on the same ct->ext when shared skbs are evaluated
concurrently. The lock is also meant to cover setting IPS_HELPER_BIT
and initializing the expectation list.

> Two CPUs can add the helper extension to the same unconfirmed conntrack
> when shared skbs are evaluated concurrently.  nf_ct_ext_add() then calls
> krealloc() on the same ct->ext from both CPUs.

[Severity: Medium]
Has this been seen outside a test harness? The notes below the --- line
say the reproducer was a QEMU harness that forced two CPUs into
nf_ct_helper_ext_add(). They also say "the exact shared-skbs network
path has not yet been shown without the test harness". Those notes
won't make it into git history.

nf_ct_ext_add() assumes an unconfirmed conntrack has a single owner:

net/netfilter/nf_conntrack_extend.c:nf_ct_ext_add() {
    ...
	/* Conntrack must not be confirmed to avoid races on reallocation. */
	WARN_ON(nf_ct_is_confirmed(ct));
    ...
}

Where an unconfirmed conntrack can be shared, the sharing has been
handled at that point. For example, br_nf_local_in() does:

	if (WARN_ON_ONCE(refcount_read(&nfct->use) != 1)) {
		nf_reset_ct(skb);
		return NF_ACCEPT;
	}

Could the commit message name the real path where two skbs end up
sharing one unconfirmed conntrack? Would it be better to fix that path
instead of adding a lock to a generic exported helper?

> Serialize nf_ct_helper_ext_add() with the conntrack lock.

[Severity: Low]
nf_ct_helper_ext_add() is EXPORT_SYMBOL_GPL, so this changes the calling
context for every caller, not only nft_ct. The callers include:

  xt_ct_set_helper()
  nf_ct_add_helper(), used by act_ct and openvswitch
  ctnetlink
  init_conntrack()
  __nf_ct_try_assign_helper()

Should the commit message mention the template callers and the callers
that pass GFP_KERNEL_ACCOUNT? The new lock affects them both, as
described below.

> The lock also
> covers publishing IPS_HELPER_BIT and initializing the expectation list,
> so a concurrent caller observes a fully initialized helper extension.

[Severity: Low]
Does the lock give concurrent readers this guarantee? The readers of
IPS_HELPER_BIT don't take ct->lock. nft_ct_helper_obj_eval() does:

	if (test_bit(IPS_HELPER_BIT, &ct->status))
		return;

and __nf_ct_try_assign_helper() does:

	if (test_bit(IPS_HELPER_BIT, &ct->status))
		return 0;

nfct_help(ct) is also read without the lock, in nft_ct_expect_obj_eval()
and __nf_ct_try_assign_helper().

In addition, every caller sets help->helper with rcu_assign_pointer()
after nf_ct_helper_ext_add() has dropped the lock. The expect_data
writes in nft_ct_expect_obj_eval() also happen outside the lock.

Since those readers never take ct->lock, can't they still see the
extension before it is fully initialized?

> diff --git a/net/netfilter/nf_conntrack_helper.c b/net/netfilter/nf_conntrack_helper.c
> index c30ae3f203bee..4d2dd88e74f36 100644
> --- a/net/netfilter/nf_conntrack_helper.c
> +++ b/net/netfilter/nf_conntrack_helper.c
> @@ -184,11 +184,13 @@ nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
>  {
>  	struct nf_conn_help *help;
>  
> +	spin_lock_bh(&ct->lock);
>  	help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);

[Severity: High]
Can this sleep while holding a spinlock with BHs disabled?
nf_ct_ext_add() passes gfp straight to krealloc():

	new = krealloc(ct->ext, alloc, gfp);

Several callers pass GFP_KERNEL_ACCOUNT:

net/netfilter/xt_CT.c:xt_ct_set_helper() {
    ...
	help = nf_ct_helper_ext_add(ct, GFP_KERNEL_ACCOUNT);
    ...
}

net/netfilter/nf_conntrack_ovs.c:nf_ct_add_helper() {
    ...
	help = nf_ct_helper_ext_add(ct, GFP_KERNEL_ACCOUNT);
    ...
}

tc act_ct tcf_ct_fill_params() and the openvswitch ct(helper=...)
action setup both reach nf_ct_add_helper().

So installing an iptables CT --helper rule does:

xt_ct_tg_check()
  xt_ct_set_helper()
    nf_ct_helper_ext_add(ct, GFP_KERNEL_ACCOUNT)
      spin_lock_bh(&ct->lock)
      nf_ct_ext_add()
        krealloc(..., GFP_KERNEL_ACCOUNT)

With CONFIG_DEBUG_ATOMIC_SLEEP, might_alloc() in the slab path would
print "sleeping function called from invalid context" every time.
Under memory pressure the allocation could really sleep in atomic
context. Before this patch these callers held no lock here.

Any CAP_NET_ADMIN user in a network namespace can trigger this
deterministically by installing a rule.

[Severity: High]
Is ct->lock initialized for conntrack templates? Templates come from
nf_ct_tmpl_alloc(). It kzalloc()s the object and sets only a few fields:

net/netfilter/nf_conntrack_core.c:nf_ct_tmpl_alloc() {
    ...
	tmpl->status = IPS_TEMPLATE;
	write_pnet(&tmpl->ct_net, net);
	nf_ct_zone_add(tmpl, zone);
	refcount_set(&tmpl->ct_general.use, 1);

	return tmpl;
}

The only spin_lock_init(&ct->lock) seems to be in __nf_conntrack_alloc(),
and templates don't go through it.

These paths all pass a template into nf_ct_helper_ext_add():

  xt_ct_tg_check()->xt_ct_set_helper()
  tcf_ct_fill_params()->nf_ct_add_helper()
  the openvswitch ct action setup->nf_ct_add_helper()

So they would now lock a spinlock that was never initialized.

With CONFIG_DEBUG_SPINLOCK this would report "spinlock bad magic". With
lockdep it would report "trying to register non-static key" and turn
lockdep off for the whole system.

Some architectures don't treat a zeroed lock word as unlocked, for
example parisc's ldcw-based spinlock. Could spin_lock_bh() spin forever
there?

Nothing locked a template's ct->lock before this patch.

>  	if (help) {
>  		__set_bit(IPS_HELPER_BIT, &ct->status);

[Severity: Low]
__set_bit() is a non-atomic read-modify-write of ct->status. Other
ct->status writers, such as the NAT setup bits and IPS_SEQ_ADJUST_BIT,
don't take ct->lock and update the word atomically.

Under the concurrency described in the commit message, could this
__set_bit() lose a bit that another CPU sets at the same time, or lose
IPS_HELPER_BIT itself? Would set_bit() fit better with the other
writers?

>  		INIT_HLIST_HEAD(&help->expectations);
>  	}
> +	spin_unlock_bh(&ct->lock);
>  	return help;

[Severity: High]
If two CPUs really can process the same unconfirmed conntrack at once,
does serializing only this caller close the race? Many other paths still
call nf_ct_ext_add() and krealloc() ct->ext without ct->lock:

  nfct_seqadj_ext_add(), called in nft_ct_helper_obj_eval() right after
  the helper is added
  nf_ct_timeout_ext_add() in nft_ct_timeout_obj_eval()
  nf_ct_ecache_ext_add() in nft_ct_set_eval()
  nf_ct_nat_ext_add()
  synproxy, labels, act_ct and others

Lockless readers like nfct_help(), nf_ct_ext_find() and
__nf_ct_ext_exist() are also unchanged.

Here is one interleaving in nft_ct_helper_obj_eval():

CPU A                                CPU B
                                     test_bit(IPS_HELPER_BIT) is false
nf_ct_helper_ext_add()
  spin_lock_bh(&ct->lock)            spin_lock_bh(&ct->lock) waits
  ...
  spin_unlock_bh(&ct->lock)          lock acquired
                                     nf_ct_ext_add()
                                       old = ct->ext
nfct_seqadj_ext_add()
  krealloc(ct->ext) frees old
                                       __nf_ct_ext_exist(old, id)

Wouldn't CPU B then read freed memory through old->offset? Two of the
unlocked ext adds above can still double-free ct->ext exactly as the
commit message describes.

Also, help points into ct->ext and is returned after spin_unlock_bh().
Callers then write through it:

	rcu_assign_pointer(help->helper, to_assign);

This happens in nft_ct_helper_obj_eval(), xt_ct_set_helper() and
nf_ct_add_helper(). nft_ct_expect_obj_eval() writes expect_data the
same way. If another CPU's unlocked nf_ct_ext_add() moves ct->ext, can
those writes land in freed memory?

Would it be better to stop concurrent processing of one unconfirmed
conntrack where it becomes shared, as br_nf_local_in() does? The
alternative would be to serialize every ext add and user, rather than
locking a single wrapper.

>  }
>  EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201085814.82573-1-alex000young%40gmail.com

      parent reply	other threads:[~2026-12-03 23:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  8:58 [PATCH net] netfilter: conntrack: serialize helper extension allocation zhuorao yang
2026-12-01  9:21 ` Pablo Neira Ayuso
2026-12-03 23:59 ` netdev-bot+sashiko [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=179634238903.3146.4855623999904602326@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=alex000young@gmail.com \
    --cc=coreteam@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=willemb@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox