From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796342390; b=ak2zDM+Mr4w5o1kbSU5ggC0p1JxP2SWbFNfUOYcKtCwiU3PYYhcV4ikLFLlsksqSXj38 qKSjfxY16kwdMUM/XTXyXT3UvWbnNhquNNDxzEgly07/unFlpPi/86D4q1X4zXKg9Zpto xDEYph+Idhf1gRro9f/I8RdcXRvfe9WyThcN+NO9Ae6l2wSyt1xs5TqRimH0sV7sGFKgT IoZ8x75RVuR9+M/vL4JPmUX2wE7xMxOcCGLSZtojl+9T5jN8jbl3q8SussatFfvnoooN6 lkZYZeY73MdleCMtP1F7lxkhPU/LZkg61kiMrScQRZNI56clPfmMPeHFbmYiHbYy/rQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796342390; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=4E8Z6mTPnJjoKx0sR9Bb5oE4wR3Y2bRyxF2Hs+cpJ+o=; b=dgLizgpXwzXBFvJSUAhN7dcLt731XRhZqiyw8gvhuqM8AJf52wRXmUfaM3xOPizPPHfe nUK9JlmHlTydNa322HtY9t3IMXepvWq3dVvpIp/irtPi9NwQofql+cOLyTlI0o/jN86o1 7MCEE7cAZ/5QJTJXo323oPhkkQRNi1XhFvilDJNBOzqrm5uhpNjfUTOQrfGeEtbVrhmw2 RlBLViW5A3WFTETlIfdO+gJL5NBa1jfe9l34YmF5RqNSmTjX+gA4vM0NgYvsiLkglXniU jCS28TRWuoolrTIyP5JINrBJKzBDL5UYuOH82jeZAV4Kbl82AXuvHIMjawKrVYwwcNQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="iSik/wy+"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A50FB606A32C; Thu, 3 Dec 2026 23:59:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iSik/wy+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8DD441F000CE; Thu, 3 Dec 2026 23:59:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796342390; bh=4E8Z6mTPnJjoKx0sR9Bb5oE4wR3Y2bRyxF2Hs+cpJ+o=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=iSik/wy+Ld6lRUsaSxFX3LFAg51/UXkqUIy8iaZzGXwjForFDN5CZ84O3nZTFrs4M qcXaLa4vtTTHtf9ynN7obQxr3nJ9KaDhvA9Fgih8IAW7KKxO0o7OZiIcJuTsE/fLwV PgNms0wqCiO0IpcTd1PSA0C3lHmyhs4J/fe56SbxFXJzJ7NHN9UC6vILKp8cLUgBdl OCy2bH1lWq+ioA60jMLvku22GikisRQINCw3tHbzKR7cRSRQYKEWz7gjp+fL7alvJX lohSDMK8073R2wtC5aKDeU5DLH5qj+abfssgn8PTimDwVDNU+sOcBnK/zszmXxqT14 0olFVz7qy+mxg== Subject: Re: [PATCH net] netfilter: conntrack: serialize helper extension allocation 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 Date: Thu, 03 Dec 2026 23:59:49 +0000 Message-ID: <179634238903.3146.4855623999904602326@kernel.org> In-Reply-To: <20261201085814.82573-1-alex000young@gmail.com> References: <20261201085814.82573-1-alex000young@gmail.com> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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