🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #6412

[PATCH nf] netfilter: nf_nat: do not link a conntrack twice into nat_bysource

#6412
From: Vimal Agrawal
Date: Thu, 22 Oct 2026 10:59
116 lines
15526 bytes
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
   ...

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
Technical Details
Message-ID: <20261022105943.4003353-1-vimal.agrawal@sophos.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-350523-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-350523-linux-kernel-netdev=dewy.ru
From: avimalin@gmail.com (Vimal Agrawal)
Newsgroups: linux.kernel.netdev
Subject: [PATCH nf] netfilter: nf_nat: do not link a conntrack twice into nat_bysource
Date: Thu, 22 Oct 2026 10:59:43 +0000
Organization: Usenet Network
Message-ID: <20261022105943.4003353-1-vimal.agrawal@sophos.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="193754"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:P/WTjio7g4OpJOMNTqNKyluBI4I= sha256:0j1HueOpfEJL8skP6MNr/n0Vs2yj8SvhasozK1Kz51g=
	sha1:+Xw/sO62EqWw9BlF5IIpHY3pucM= sha256:HRo0szULsOz+mKRiv5Jc2q1RKhi82scAOdQONEi8oqs=
List-ID: <netdev.vger.kernel.org>
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5576711124F; Thu, 22 Oct 2026 14:02:56 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: 1.51
X-Spam-Level: *
X-Spam-Status: No, score=1.51 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FORGED_GMAIL_RCVD=1,
	FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001,
	HEADER_FROM_DIFFERENT_DOMAINS=0.25, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_SBL_CSS=3.558, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=no autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id ngAC-GlS7iFD;
	Thu, 22 Oct 2026 14:02:53 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-350523-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id BB34C11124D; Thu, 22 Oct 2026 14:02:40 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id E4961351FA; Thu, 22 Oct 2026 11:02:11 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1792666930;
	b=YHsI3v7Unrayyx7autd4mtVo2HMeXoL9XtrRu7Ejz8mEs5JxPg0ZCOMhXoL3zcXgZNO/
	 B62KkGPe+0yzVbIWWJQb71oG7lIxnPVaGFk327jdG7NexTePPo7qdVOxvku4FFHl3iUz1
	 XGycs65NyWCG+/379aD7Fan20qlD0t0vIMADSjMKJKQ1RsLXZbRocwGFUTARrUgdY9jrH
	 mr4LFnz/gQ+/c22IxtbkwrM33eQHv2NHg5u3yf0pb4CtYpUX01yFQOMHdv3O4fiK3Zr2t
	 tVecMil2Ikvoh479A6zOngSFXgNJdwqyDjOB83Jgj6pBcT6z1gVsiBBR5Jmt2WfUpUA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792666930;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
	 X-Gm-Gg:X-Received:Received:From:X-Google-Original-From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:List-Help:
	 List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=QRHuvewQDkW7Qs/NCBaG8NxQX27dKGA8PbOiyV1o5wA=;
	b=ounlK4Eba0zofhe75qKS+eGS37nhMD388rA19Gn2zbw7bOsCqLIuAsHGbhxIPk2CiAug
	 SEKbwTczhLHUhCAm4Z8S9Y+MyLUO0JQbi8VLYmB/mGxYta5XoKoq0RFQgPMgYyMVsT+OZ
	 j633WaW5O8CDlX5KQ96s+lzTdoI3z3AdeOrK3BwAyppjJMGGCDe3mvgwj8ufJdNWULL5n
	 60651tawHQdfk5WtF0IrneEz3dQdE3c0DVxFhtjTXryzRNtsnfGnxd9IE6QPtT8Z94Eru
	 Ew72ptnHcgmeIK9phFCoQriTEV5ui26j0raAcd8s+zsrBi4wBsm29ICtvyhs2dIVJbg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=MqVndisA;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id A2FA3606A328;
	Thu, 22 Oct 2026 11:02:10 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=MqVndisA
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.219.46
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1792666929;
	b=wb8Bjaq2whsjG+Cm59VqRvMwimqmUIfOiyZKJWsM+QscWgE4hbP3lMQOK2qM1bqVQylA
	 8rJVOyln9nYdvPf5MWQnwRfJwgBpC8eSZ16P4KM3UlmHu/B7zpLtNL23llPdpeCg7niUB
	 F6WUkJAGcDu37TxsiW4xI7rrFmhZMszBd+IyXjuNZDGfRyVJs06vUQuvYwTF6vAUsadYm
	 GHXBCSl3FTzabYq5z072ZpXgu4muhUAcHnEVlThbJvDL8raFHmDefG83i/XnPkvPVPn11
	 DK0fi1IG55mMvSyoLy4C+LrOLe+6I8TYmonSH8jAmx3WVxynZs6zf2euzx3CMyjwi+g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792666929;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
	 From:X-Google-Original-From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 MIME-Version:Content-Transfer-Encoding;
	bh=QRHuvewQDkW7Qs/NCBaG8NxQX27dKGA8PbOiyV1o5wA=;
	b=MNtHbKnwVaMcSrliQksuIQFPUjB0nx5LappI/s4ur1UhdAJ6HQQennxGrPx9EflhE+NT
	 uUwC+FFvXn47+kUSbvAO72WPqeZ+xH++BTGDpRMR8nd/MznWVY6IgFdYtVZD6vYR3zIrm
	 BCydpYbDCor7RUkC+E6x82AO8XFDU94MmwbwyTC2ru7wsM+HMXkRPtFAHBVvpFR6QCnjh
	 L1SMak8GsWd3w3hfoIJm1CGcBbnV90zqcPq/j2c3LU9M1m0LEwaynyH4D5z+mVnIyI7wc
	 k+VO81dHPj1/C17U01WzukJAX1NJhB5nDXy25oGE4wU+pa8uJ1tkNTjlDhVEGABTnEA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=MqVndisA;
	arc=none smtp.remote-ip=209.85.219.46
X-Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70EAE606A31A
	for <netdev@vger.kernel.org>; Thu, 22 Oct 2026 11:02:09 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 70EAE606A31A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-91b8b456dbeso18590426d6.2
        for <netdev@vger.kernel.org>; Thu, 22 Oct 2026 04:02:09 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1792666928; x=1793271728; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:message-id:date:subject:cc
         :to:from:from:to:cc:subject:date:message-id:reply-to:content-type;
        bh=QRHuvewQDkW7Qs/NCBaG8NxQX27dKGA8PbOiyV1o5wA=;
        b=MqVndisAcnNte2/6UioG0DG0DwjYzQiSHzvKaAL8eqlaL1AlFqw1fHMfS7Tp8fTEGo
         S++ORIIyyNfQnBTWLVDPOmjLsmBH30ueivMH6u31kIUsbu4VCWwDFR5Qj0B0uVOpz6Be
         79uvyEP9ZPy/88Y4Z/qfHqbou4BaIAwIGMAGSMRD61lhwU20YP7qHEcjHpK20KUt9R4I
         fQ/WN9HRzK+4fpAC8tWdmZXw1Enzs+xSFiW91veffNDck1Xg88sqUeaQdGOYzyF15FsC
         Ew3qOkBJqT5SLv8vm3xebMSJu82q7SJ9Un9S+TKe/Io/xhdOQROaKfdHT2X2IuEiTa5p
         h0lA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1792666928; x=1793271728;
        h=content-transfer-encoding:mime-version:message-id:date:subject:cc
         :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=QRHuvewQDkW7Qs/NCBaG8NxQX27dKGA8PbOiyV1o5wA=;
        b=vZmkwWOTVFJ3ebpxoU27C62+ywtNp0R6cLcftnVoDy8/Fvv5JLdJEK0p0sT/fMdDgv
         +uG4nkTdlop6XN7oJJrfY6f3D0LfuYUdL6PjLf19XLFenoFo852umECaot/UQ4U+rb78
         5whIvrMkh5h5ngTYkTYLFrFdW2zk42vgk6EBNRHdbedeONBvRFJlEI2I08n9NwAuTotD
         AXq6KHh3UpMRq7xcZzg+pz8fq4gIa2H/Vchq++fzaZPq0GouBoTeQN7S6NoLKsQ/uSXo
         0bLxtn6OawWzD2Sb5S5ZxMZhE7u2Nu2vRrjF7IBdxsz5SY53yAigB73C51eC549g4dOg
         6gmw==
X-Forwarded-Encrypted: i=1; AKwUvBzgSpPtZxl1+UmifGmKp0IDrObKhMk1///qCqDQRKpXBG8IYvWhvYr2tldJIdg307SKa1XgVoI=@vger.kernel.org
X-Gm-Message-State: AFq9FYIuqE0GkqnZoWDI05yzJmmlcooRXExoQSGOO/k+GzOxIgUO8zjd
	oGxfRImgNjZtTYnnMoMuEI1VCrOtWkhP9NM1r093iqzFakh9mfRJxE4o
X-Gm-Gg: AYBFou0M5clkR5EPMYCUHIjLTNhgAKgb567NDwKVN634XyGaW5AS0fMxch/NGh21xJ9
	FfZkOQcgVY7Sbu9AzwWOzE460L6EnDlv3/YNe+FuP1c6lnVmQ+tvNA3+0AJl5AdDlBJCcw6u8TA
	BUcRR5aU/k+nfs+ijN6V4r5GRa/NYEgYBcSQbAzGXATqhkT1MLrYmlk0Nt1LW/m9VuuLNTPPgoA
	SSYw5ojtOhvCAXPrVldKYDd7HLABzBj7hFYQEkep5qmZoZUL7NYX4pV2vYRiGrf2l5ZpDZElPZg
	9Wqg+h9uL8lvw6sOBvMwtYoKvGFqLWuIE4KRKIfgYg8B/JBOExKA2BOIMYxOBZVfkPZp2Xy2MOF
	TMTWYUMWpBHcQ966WUod88B0awyFnA9uV5lbstCFzkBGdzspI90+u2x8oy0/Qxae0oL3D8wEN5p
	WcgcmMdy8LtCNe5mRR+qL1Jl89zuUKQ7PXh9yfJMJC3Rlnag54oyRAGWjX6uVG20rSpJXd0Y2Kh
	ZJRB5MEV60rByZOX6IJMdMkgQTBX/NB7yr6i/obmE0VbtUHYpNg51novN2mCaQVLaFaNNms4Lo9
	/MTxLlQioB73puQnNAnD8+9YrMDAZNtAYsJwCynPe6Yz6jbLFeGH
X-Received: by 2002:ad4:5ca7:0:b0:914:4464:41a9 with SMTP id 6a1803df08f44-91c12eff859mr82758566d6.29.1792666928067;
        Thu, 22 Oct 2026 04:02:08 -0700 (PDT)
X-Received: from b2r3-prox-24-ubuntu18.. ([66.198.16.131])
        by smtp.gmail.com with ESMTPSA id 6a1803df08f44-91c10b76143sm48333796d6.14.2026.10.22.04.02.07
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 22 Oct 2026 04:02:07 -0700 (PDT)
X-Google-Original-From: Vimal Agrawal <vimal.agrawal@sophos.com>
X-Cc: phil@nwl.cc,
	coreteam@netfilter.org,
	netdev@vger.kernel.org,
	Anirudh Gupta <anirudh.gupta@sophos.com>,
	avimalin@gmail.com,
	Vimal Agrawal <vimal.agrawal@sophos.com>
X-Mailer: git-send-email 2.43.0
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list