🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #21946

[PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found

#21946
From: Ralf Lici
Date: Tue, 24 Nov 2026 16:27
180 lines
16238 bytes
Validate dsts in ip6_dst_check against the stamp of the lookup that
built them instead of the cookie passed by their holder, which may have
been read after a FIB change the lookup did not see. A stamp of 0,
recorded when the generation changed during the lookup or the route was
unlinked, never validates. Holders are covered without changes,
including dst_cache users and the route and path dsts of xfrm bundles,
and invalidation keeps its per-node scope: a FIB change only invalidates
dsts of routes under the nodes it stamps.

pcpu dsts are shared by all their holders, so they are never
re-stamped, which would revalidate a stale dst for every holder. A pcpu
dst is reused only if it carries the stamp of the current lookup.
Otherwise it is replaced, so a lookup that selected a route before a FIB
change cannot return a pcpu dst that another lookup rebuilt after it.
On PREEMPT_RT, a pcpu dst that another task installed while this lookup
built its own follows the same rule.

A replaced pcpu dst is released with dst_dev_put as before, and since
pcpu dsts of nexthop-object routes are validated against the IPv6
generation, now the one read before the lookup, they are also rejected
once released. Lookups may have read the slot without taking a
reference, and an xfrm bundle holding the dst as its child drops its
reference without waiting for a grace period, so the slot's reference
is dropped only after one (__fib6_drop_pcpu_from is unchanged).

Not covered yet: a holder that sends through a released dst without
validating it again, such as a corked UDP socket, loses those packets,
now also when a FIB change in the route's subtree replaces the pcpu
dst. Exceptions keep being validated against the holder's cookie, and
so keep the late sampling, as refreshing them needs their PMTU updates
serialized with the replacement.

Suggested-by: David Ahern <dsahern@kernel.org>
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
New in v5
- Replaces the lookup cookie API and its users from v4. (David)

 net/ipv6/route.c | 78 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 64 insertions(+), 14 deletions(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 8b98ee945c58..be9bf7a4e780 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1501,7 +1501,7 @@ static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
 	pcpu_rt->rt6i_flags |= RTF_PCPU;
 
 	if (f6i->nh)
-		pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
+		pcpu_rt->sernum = res->gen;
 
 	return pcpu_rt;
 }
@@ -1511,6 +1511,33 @@ static bool rt6_is_valid(const struct rt6_info *rt6)
 	return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
 }
 
+static bool rt6_stamp_matches(const struct rt6_info *rt,
+			      const struct fib6_result *res)
+{
+	if (rt->sernum || res->f6i->nh)
+		return res->gen && rt->sernum == res->gen;
+
+	return res->cookie && rt->cookie == res->cookie;
+}
+
+static void rt6_pcpu_release_rcu(struct rcu_head *head)
+{
+	struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
+
+	dst_release(dst);
+}
+
+/* Release a pcpu dst taken out of its slot. Lookups may have read the slot
+ * without taking a reference, so drop the slot's own only after a grace
+ * period: an xfrm bundle holding the dst as its child releases its
+ * reference without waiting for one.
+ */
+static void rt6_pcpu_release(struct rt6_info *rt)
+{
+	dst_dev_put(&rt->dst);
+	call_rcu(&rt->dst.rcu_head, rt6_pcpu_release_rcu);
+}
+
 /* It should be called with rcu_read_lock() acquired */
 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
 {
@@ -1518,16 +1545,19 @@ static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
 
 	pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
 
-	if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
+	/* A pcpu dst is shared by all its holders, so it is never re-stamped.
+	 * Reuse it only if it was built under the state this lookup found,
+	 * otherwise replace it: a dst rebuilt by another lookup after a FIB
+	 * change must not be returned to a lookup that selected before it.
+	 */
+	if (pcpu_rt && !rt6_stamp_matches(pcpu_rt, res)) {
 		struct rt6_info *prev, **p;
 
 		p = this_cpu_ptr(res->nh->rt6i_pcpu);
 		/* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */
 		prev = xchg(p, NULL);
-		if (prev) {
-			dst_dev_put(&prev->dst);
-			dst_release(&prev->dst);
-		}
+		if (prev)
+			rt6_pcpu_release(prev);
 
 		pcpu_rt = NULL;
 	}
@@ -1550,13 +1580,21 @@ static struct rt6_info *rt6_make_pcpu_route(struct net *net,
 		/*
 		 * Another task on this CPU already installed a pcpu_rt.
 		 * This can happen on PREEMPT_RT where preemption is possible.
-		 * Free our allocation and return the existing one.
+		 * Free our allocation and return the existing one if it was
+		 * built under the state this lookup found, otherwise replace
+		 * it as rt6_get_pcpu_route does.
 		 */
 		WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RT));
 
-		dst_dev_put(&pcpu_rt->dst);
-		dst_release(&pcpu_rt->dst);
-		return prev;
+		if (rt6_stamp_matches(prev, res)) {
+			dst_dev_put(&pcpu_rt->dst);
+			dst_release(&pcpu_rt->dst);
+			return prev;
+		}
+
+		prev = xchg(p, pcpu_rt);
+		if (prev)
+			rt6_pcpu_release(prev);
 	}
 
 	if (res->f6i->fib6_destroying) {
@@ -2864,11 +2902,16 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
 	struct dst_entry *dst_ret;
 	struct fib6_info *from;
 	struct rt6_info *rt;
+	bool exception;
 
 	rt = dst_rt6_info(dst);
 
+	/* a nexthop-object pcpu dst replaced by another lookup has been
+	 * released with dst_dev_put and must fail even if its stamp is current
+	 */
 	if (rt->sernum)
-		return rt6_is_valid(rt) ? dst : NULL;
+		return (READ_ONCE(rt->dst.obsolete) == DST_OBSOLETE_FORCE_CHK &&
+			rt6_is_valid(rt)) ? dst : NULL;
 
 	rcu_read_lock();
 
@@ -2879,9 +2922,16 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
 
 	from = rcu_dereference(rt->from);
 
-	if (from && (rt->rt6i_flags & RTF_PCPU ||
-	    unlikely(rt->dst.rt_uncached_list)))
-		dst_ret = rt6_dst_from_check(rt, from, cookie);
+	/* Exceptions are validated against the holder's cookie, every other
+	 * dst against the stamp of the lookup that built it rather than the
+	 * cookie its holder read afterwards, which may postdate a FIB change
+	 * the lookup did not see.
+	 */
+	exception = (rt->rt6i_flags & RTF_CACHE) && !rt->dst.rt_uncached_list;
+	if (from && !exception)
+		dst_ret = rt->cookie ?
+				  rt6_dst_from_check(rt, from, rt->cookie) :
+				  NULL;
 	else
 		dst_ret = rt6_check(rt, from, cookie);
 
-- 
2.56.0
Technical Details
Message-ID: <4b2bf5de6c9837fee8db88d639db0e27e6d687b9.1795537128.git.ralf@mandelbit.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370573-linux-kernel-netdev=dewy.ru
References: <cover.1795537128.git.ralf@mandelbit.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370573-linux-kernel-netdev=dewy.ru
From: ralf@mandelbit.com (Ralf Lici)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found
Date: Tue, 24 Nov 2026 17:27:10 +0100
Organization: Usenet Network
Message-ID: <4b2bf5de6c9837fee8db88d639db0e27e6d687b9.1795537128.git.ralf@mandelbit.com>
References: <cover.1795537128.git.ralf@mandelbit.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="4179942"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:nNhF9W5SK3ZG3vyuFqPbkDCzkHg= sha256:6x+37i7M4XFYrHgwZnqeA67tlBUJmDUB9vsCRlAhDDQ=
	sha1:SlJ+LpPb0Dfm/DEKNnKCSIhAhAY= sha256:tXJq0+wIAp2UdhJawjwbjZYhL/wayGW55Q2UnGHnYIc=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 2DCD6125E29; Tue, 24 Nov 2026 19:28:27 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 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, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=mandelbit.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 sfllU2s6RiJx;
	Tue, 24 Nov 2026 19:28:24 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-370573-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 9613F125E2B; Tue, 24 Nov 2026 19:28:12 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id A1A673CC6B; Tue, 24 Nov 2026 16:27:46 +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=1795537665;
	b=VYC6U53InI/MHe+yiuVcdQjQbR2qbCVaav0jjjOLFUPHbRL/KZjlrIzOoOT3jfL3LwpM
	 Ww4a3YhjiiPY21+HiUhFiEhwsrTbUz3PwzVZMbI7hn/G+35JSFDLpnX2LCL0IsrK7NOEC
	 jYieLR3pk5BVGO79UJdaTsyCLOho+Y2elHwhqP7ulPhFjyhFevSIga822VU3NbT58uYqv
	 TmldmjPNf/F8VNWn7H8hrzSWIK04ZYSHrIKd8owFFbxNnfWvwCJdTldi2I9+DulaHT/qi
	 3j9I/4/e6K6kxglPCFtYytRf95xewBHf1QbamuhfYEp5mp71Aq3c6XzktdGfG1XVT3Q==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795537665;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To:
	 Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding:X-Rspamd-Queue-Id;
	bh=Jrpxc1Q6pgjfCQC9LCXv5AaAKRJs/kdvERD9Fgr0nfc=;
	b=ewgFjHAYz/8rKrSYhrXIrl5zPlg3Hr+5H/i5MEYSHdL9E+5onz8o60L9bF8pBmD+or6L
	 XD9vierirdJP5vf1faFADsYmzRVfyiF488LJ8jwhXkka7nDkbmAkDKgHzHUeH1WSY5ISz
	 B6nocEeH4j/XzZkbWz3ZlRAgj6L8dKtZ8YwkU4CSy36asBxabeYrKZFJHp7Pvam3e3Fsn
	 2VZXg4GYo0L9Uiq6HHKo1PnkRfh2gPxfj2g/xPLgxaU6bFoBQ0OclREHqSABNmgUJrO3i
	 FZ2xvAFcdgK7jtLU41o5nj+EKFUuTY+f+aP+SFSweOUsXSmImgDPWTlDwkhGsM5TP/A==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=cc4d7QZP;
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.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 16EF1606A332;
	Tue, 24 Nov 2026 16:27:45 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=cc4d7QZP
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.62
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795537664;
	b=0zjwc3Kl5ds3ENO6HKlPT5kXJvO+OdcOZwGccMVVT8mAOi03/JOFMwli7jsLYx0mqCb5
	 07tJtj36NrMUyRsBSz3xPEWfJxdeogaZafSy5YgMs/t9lx68wd3Pazdb86z3sYuW6LDa/
	 NKoCTQJYzOzlrbspRv4mewaN1af9ZD6x2s5hxcDLeDa4gFjQjUVdsI8/NTBvlf5/n6IKx
	 yGfr41R2DLnoorEGLp9ZO5CYvX9LlriEPMMqqmuDbDDcQhDOGCCSVPZdSK+QfZZ7rs1jb
	 SVhtArdZ51GLG2zU1Qv7GGSMEJQw2uG/qwqDgv/fwWIi3qQH3sGM62WxsC/3j3EWADw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795537664;
	h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date:
	 Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding:X-Rspamd-Queue-Id;
	bh=Jrpxc1Q6pgjfCQC9LCXv5AaAKRJs/kdvERD9Fgr0nfc=;
	b=SzUlYG4D90DQpfbrRxmcs444aDhKMu72YJ/9nT5zE+ikbi/4eQypXhemE3aRN0j5T7hQ
	 THbUu/cK/zQ8YtdaiTv4lGrSZ2TL3ZR5X1UtS1cfa5nMjj1nsht2IpeBJFMd1KvBIZ3D2
	 +qOIh/5reolGNNGK7sTfKvVHlJerwJS4CzoRlvPqG8J9/XA1YuKZD6jGIDmCbZWph4ZR2
	 EkYlxQVPc8uZmhHe7AiZgdK6T/vxxkpDh/g8ny6gB++HERX4nUqFLb/ZpcA7WSbrbHzw6
	 L0t6WI5RVD3z9vE/4nIidO8Oo8Nkak6kNY4TLemsJgcvk3Vzup8y6QQ7y06IKBkkPuQ==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=cc4d7QZP;
	arc=none smtp.remote-ip=195.10.208.62
X-Received: from mout-b-202.mailbox.org (mout-b-202.mailbox.org [195.10.208.62])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86F0E606A329;
	Tue, 24 Nov 2026 16:27:43 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 86F0E606A329
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com
X-Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by mout-b-202.mailbox.org (Postfix) with ESMTPS id 4jYpBG3f7szKnsH;
	Tue, 24 Nov 2026 17:27:34 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com;
	s=MBO0001; t=1795537654;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=Jrpxc1Q6pgjfCQC9LCXv5AaAKRJs/kdvERD9Fgr0nfc=;
	b=cc4d7QZPuxOeD8POei5YQZbOALG+AOMebhNI5laNKtPtXKqVDBp0sU1gT1tUvKlffxcXSM
	OZg/EMz6Iu72bQSFozrs9ecmFfFEG4ad4DdqD/44xQjwzeJ9i9YLbWiLVjpS6vtRE/h2Jj
	rWTMkwxT4rrNwy0SC3G4TRDwi2WSvH4O+QocH9HqUbvDAmv1RJTilRagRjj4/5Ka5WXcqt
	bq9RJRtf/+vfRaku41lCj6b88pqHTPHw5rOlbTNnnTjLiyzkVOM29+k5ucWxBfsoE3Mwgc
	47/hadLTlAbYQtYwEqNxSpWYUB/lQchj+ht0atZkypsxCem/k8r0A7lpA8RfOA==
X-Authentication-Results: outgoing_mbo_mout;
	dkim=none;
	spf=pass (outgoing_mbo_mout: domain of ralf@mandelbit.com designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=ralf@mandelbit.com
X-Cc: Ralf Lici <ralf@mandelbit.com>,
	David Ahern <dsahern@kernel.org>,
	Ido Schimmel <idosch@nvidia.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Steffen Klassert <steffen.klassert@secunet.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Florian Westphal <fw@strlen.de>,
	Xia Yan <yanxia@cmss.chinamobile.com>,
	Sabrina Dubroca <sd@queasysnail.net>,
	Antonio Quartulli <antonio@mandelbit.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Clark Williams <clrkwllms@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	linux-rt-devel@lists.linux.dev,
	linux-kernel@vger.kernel.org
X-Mailer: git-send-email 2.56.0
X-In-Reply-To: <cover.1795537128.git.ralf@mandelbit.com>
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
X-Rspamd-Queue-Id: 4jYpBG3f7szKnsH