From: Fernando Fernandez Mancera <fmancera@suse.de>
To: Vimal Agrawal <avimalin@gmail.com>,
netfilter-devel@vger.kernel.org, pablo@netfilter.org,
Florian Westphal <fw@strlen.de>
Cc: phil@nwl.cc, coreteam@netfilter.org, netdev@vger.kernel.org,
Anirudh Gupta <anirudh.gupta@sophos.com>,
Vimal Agrawal <vimal.agrawal@sophos.com>
Subject: Re: [PATCH nf] netfilter: nf_nat: do not link a conntrack twice into nat_bysource
Date: Wed, 28 Oct 2026 00:16:55 +0100 [thread overview]
Message-ID: <17a53ec0-7493-4f60-97b0-da6012412056@suse.de> (raw)
In-Reply-To: <CALkUMdSvyg9eOUfsZPy-uvtwdednnavtX3k8BU4mbxYzkr5NhA@mail.gmail.com>
On 10/27/26 7:56 AM, Vimal Agrawal wrote:
> Hi Florian,
>
> Can you please review this patch?
>
We are all a bit overwhelmed with submissions and this kind of pings
does not help IMHO.
> Vimal
>
> On Thu, Oct 22, 2026 at 4:32 PM Vimal Agrawal <avimalin@gmail.com> wrote:
>>
>> From: Vimal Agrawal <vimal.agrawal@sophos.com>
>>
>> nf_nat_setup_info() assumes it owns an unconfirmed conntrack, but the
>> clones made by br_flood() share one (skb_clone() only takes a
>> reference). That is safe while they are handled one after another on
>> the same CPU. If one clone is sent to NFQUEUE and another is not, the
>> queued one is reinjected later from process context by nf_reinject()
>> (BH enabled, any CPU) and can run NAT concurrently with the other.
>>
>> Both callers can then reach hlist_add_head_rcu(&ct->nat_bysource, ...).
>> Linking a node twice makes it point to itself, so a walk of that bucket
>> never ends. Also, "ct->status |= IPS_SRC_NAT" is a plain
>> read-modify-write outside nf_nat_locks[]; it can erase a concurrently
>> set IPS_SRC_NAT_DONE, and nf_nat_cleanup_conntrack() then leaves the
>> node of a freed conntrack in the bucket.
>>
>> We hit this on 6.6.49 (tainted, out-of-tree modules) as a general
>> protection fault in the bucket walk, from a packet reinjected from
>> NFQUEUE:
>>
>> general protection fault, probably for non-canonical address
>> 0xabeb549edb10a8d4: 0000 [#1] SMP NOPTI
>> RIP: 0010:nf_nat_setup_info+0x8b6/0xba0 [nf_nat]
>> nf_nat_masquerade_ipv4+0x11d/0x200 [nf_nat]
>> ipt_do_table+0x2b9/0x410 [ip_tables]
>> nf_nat_inet_fn+0x14f/0x260 [nf_nat]
>> nf_nat_ipv4_out+0x4d/0xe0 [nf_nat]
>> nf_reinject+0x177/0x1d0
>> ...
>>
Do you happen to have a reproducer that could trigger this on a
non-tainted kernel? I would like to reproduce this to verify what is
going on.
The idea is sounds but I would like to understand the problem better and
if possible add a fixes tag.
Thanks,
Fernando.
>> Use set_bit() for IPS_SRC_NAT/IPS_DST_NAT. Test IPS_SRC_NAT_DONE under
>> nf_nat_locks[] before linking and set it only after the node is
>> linked. A losing caller returns NF_ACCEPT, as before, without linking
>> again. The early nf_nat_initialized() check is unchanged, and two
>> callers that both create a real mapping are not covered.
>>
>> Link: https://lore.kernel.org/netfilter-devel/CALkUMdQ_6aU8Sy8vTORReQ2=vr4TskbkhZQt3WRazwUMTNEOtg@mail.gmail.com/
>> Signed-off-by: Vimal Agrawal <vimal.agrawal@sophos.com>
>> Reviewed-by: Anirudh Gupta <anirudh.gupta@sophos.com>
>> ---
>> net/netfilter/nf_nat_core.c | 41 +++++++++++++++++++++++++++++--------
>> 1 file changed, 32 insertions(+), 9 deletions(-)
>>
>> diff --git a/net/netfilter/nf_nat_core.c b/net/netfilter/nf_nat_core.c
>> index cc8e1e81006d..3a89c894a520 100644
>> --- a/net/netfilter/nf_nat_core.c
>> +++ b/net/netfilter/nf_nat_core.c
>> @@ -791,11 +791,14 @@ nf_nat_setup_info(struct nf_conn *ct,
>> nf_ct_invert_tuple(&reply, &new_tuple);
>> nf_conntrack_alter_reply(ct, &reply);
>>
>> - /* Non-atomic: we own this at the moment. */
>> + /* Atomic: a concurrent caller on the same unconfirmed ct may
>> + * set IPS_SRC_NAT_DONE (set_bit() below) at the same
>> + * time, and a plain read-modify-write here could erase it.
>> + */
>> if (maniptype == NF_NAT_MANIP_SRC)
>> - ct->status |= IPS_SRC_NAT;
>> + set_bit(IPS_SRC_NAT_BIT, &ct->status);
>> else
>> - ct->status |= IPS_DST_NAT;
>> + set_bit(IPS_DST_NAT_BIT, &ct->status);
>>
>> if (nfct_help(ct) && !nfct_seqadj(ct))
>> if (!nfct_seqadj_ext_add(ct))
>> @@ -806,21 +809,41 @@ nf_nat_setup_info(struct nf_conn *ct,
>> unsigned int srchash;
>> spinlock_t *lock;
>>
>> + /* Concurrent callers can reach here for the same,
>> + * still-unconfirmed ct (skb_clone()/GSO segmentation/
>> + * nf_ct_attach() can share one ct across skbs processed on
>> + * different CPUs before it is confirmed). The DONE bit is
>> + * tested and set under this lock, and only after the node is
>> + * linked, so a set bit always means the node is in the hash
>> + * (nf_nat_cleanup_conntrack() relies on that). Only the first
>> + * caller links the node; a losing caller does not link it
>> + * again, which would corrupt the list.
>> + *
>> + * The winner sets the mapping (ct->status/reply tuple) before
>> + * taking this lock, so it is final once the DONE bit is seen.
>> + * The loser's packet is therefore accepted: nf_nat_packet()
>> + * applies the winner's mapping to it. This is the common case
>> + * for bridge-flooded broadcasts (e.g. DHCP) racing with a
>> + * packet reinjected from NFQUEUE (nf_reinject() runs the
>> + * remaining hooks with BH enabled), where the mapping is an
>> + * identity null binding on both sides.
>> + */
>> srchash = hash_by_src(net, nf_ct_zone(ct),
>> &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
>> lock = &nf_nat_locks[srchash % CONNTRACK_LOCKS];
>> spin_lock_bh(lock);
>> + if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status)) {
>> + spin_unlock_bh(lock);
>> + return NF_ACCEPT;
>> + }
>> hlist_add_head_rcu(&ct->nat_bysource,
>> &nf_nat_bysource[srchash]);
>> + set_bit(IPS_SRC_NAT_DONE_BIT, &ct->status);
>> spin_unlock_bh(lock);
>> + } else {
>> + set_bit(IPS_DST_NAT_DONE_BIT, &ct->status);
>> }
>>
>> - /* It's done. */
>> - if (maniptype == NF_NAT_MANIP_DST)
>> - ct->status |= IPS_DST_NAT_DONE;
>> - else
>> - ct->status |= IPS_SRC_NAT_DONE;
>> -
>> return NF_ACCEPT;
>> }
>> EXPORT_SYMBOL(nf_nat_setup_info);
>> --
>> 2.43.0
>>
>
next prev parent reply other threads:[~2026-10-27 23:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 10:59 [PATCH nf] netfilter: nf_nat: do not link a conntrack twice into nat_bysource Vimal Agrawal
2026-10-27 6:56 ` Vimal Agrawal
2026-10-27 23:16 ` Fernando Fernandez Mancera [this message]
2026-12-03 11:50 ` Florian Westphal
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=17a53ec0-7493-4f60-97b0-da6012412056@suse.de \
--to=fmancera@suse.de \
--cc=anirudh.gupta@sophos.com \
--cc=avimalin@gmail.com \
--cc=coreteam@netfilter.org \
--cc=fw@strlen.de \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
--cc=phil@nwl.cc \
--cc=vimal.agrawal@sophos.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