The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst
@ 2026-11-24 16:27 Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 1/4] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ralf Lici @ 2026-11-24 16:27 UTC (permalink / raw)
  To: netdev
  Cc: Ralf Lici, David Ahern, Ido Schimmel, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Kuniyuki Iwashima, Steffen Klassert, Herbert Xu, Florian Westphal,
	Xia Yan, Sabrina Dubroca, Antonio Quartulli, linux-kernel

Hi,

This series fixes the late cookie sampling race described in [1]: a dst
can be stored together with a cookie read after a FIB change that its
lookup did not see, and then passes dst_check until the next change.
With xfrm, a socket whose lookup ran just before a policy insertion
keeps its plain dst and leaks plaintext although the policy matches,
and a more specific blackhole route can be bypassed in the same way.

v4 returned the cookie from the lookup and converted selected cache
users to it. Following David's objection to growing the API and his RFC
[2], which moves validity into the dst, this revision keeps validity in
the dst but stamps it with the fn_sernum of the route's node, sampled
during the lookup, instead of the netns generation:

- Every holder is covered without being converted, including dst_cache
  users and the route and path dsts of xfrm bundles.
- Invalidation keeps its per-node scope, rather than every FIB change
  invalidating every cached IPv6 dst in the netns.
- Shared pcpu dsts are never re-stamped: a lookup only reuses one that
  carries its own stamp, and replaces it otherwise.

Patches 1 and 2 are writer-side fixes from v4: a fresh serial on default
router failure, and fib6_ifup and fib6_ifdown advancing the serial
after the ECMP rebalance. Patch 3 samples the stamp during the lookup
and patch 4 validates dsts against it.

I tested it in a VM with lockdep, PROVE_RCU and DEBUG_ATOMIC_SLEEP,
using reproducers for the races above, which I can share.

Not addressed here, and noted in the patches where relevant:

- PMTU and redirect exceptions keep being validated against the
  holder's cookie, so they keep the late sampling. Refreshing them
  needs PMTU updates serialized with the replacement, a follow-up.
- The stamp covers the lookup in one table: a fib rule inserted while
  the rules are evaluated can leave a lookup on the previous table.
- Deleting one route of an ECMP group rebalances the others without a
  new serial, changing the forwarding sysctl changes how gateways are
  scored without one, and deleting a nexthop-object route does not
  advance the generation. These predate this series.
- A replaced pcpu dst is released with dst_dev_put, so a holder that
  sends through it without validating it again, such as a corked UDP
  socket, loses those packets. This already happens when the route is
  removed, and now also when a FIB change in the route's subtree
  replaces the pcpu dst.
- xfrm_policy_insert advances the generation before unlinking the
  policy it replaces. I will send the fix separately to the ipsec tree.
- pcpu dsts dropped when their route is removed, in
  __fib6_drop_pcpu_from, still release the slot's reference without
  waiting for a grace period, as before.

[1] https://lore.kernel.org/netdev/20260901122501.482920-1-ralf@mandelbit.com/
[2] https://lore.kernel.org/netdev/20261123212623.106153-1-dsahern@kernel.org/

Thanks,

Ralf Lici
Mandelbit Srl

---
Changes since v4 https://lore.kernel.org/netdev/cover.1795166508.git.ralf@mandelbit.com/
- Keep validity in the dst instead of returning a cookie from the
  lookup, with a stamp sampled during the lookup (patches 3 and 4).
  Drop ip6_dst_lookup_flow_cookie, ip6_dst_store_cookie and the
  conversion of socket cache users, which are no longer needed, and the
  ip6_dst_check change for nexthop-object dsts, superseded by patch 4.
  (David)
- Mention the tunnel transmit error paths in patch 1. (Sashiko)
- The xfrm bundle cookies and the UDP rx dst cookie pairing reported on
  v4 no longer matter, since dsts are validated against their own
  stamp. (Sashiko)
- The xfrm_policy_insert ordering reported on v4 will be fixed
  separately in the ipsec tree. (Sashiko)
- Reword the fib6_ifup and fib6_ifdown patch to refer to the lookup
  stamps.

Changes since v3 https://lore.kernel.org/netdev/cover.1794906743.git.ralf@mandelbit.com/
- Rebase onto current net-next.
- Split v3 2/3 into the writer ordering (2/5), the ip6_dst_check change
  (3/5) and the new API (4/5), each with its own explanation. (David)
- Reword the kdoc of ip6_dst_lookup_flow_cookie to say that it performs
  the xfrm policy lookup (David) and narrow it to changes that advance
  the IPv6 generation. (Sashiko)
- Document the ordering of fib6_new_sernum that the helper relies on and
  name it in the barrier comments. (Xia)
- Note the per-call generation advance in 1/5 and widen the
  rt6_exception_net comment. (Sashiko)
- Drop the Fixes tag from the conversion patch. (Sashiko)
- List the known limitations and pre-existing issues above.

Changes since v2 https://lore.kernel.org/all/cover.1794384146.git.ralf@mandelbit.com/
- Rebase onto current net-next, preserving the connected-only cache
  lookup introduced by f778e80b3daf ("ipv6: fix cached route reuse for
  control messages"). (Jakub)
- Move the serial update after rt6_multipath_rebalance in fib6_ifup and
  fib6_ifdown. (Sashiko)
- Add Fixes tags and improve wording. (Sashiko)
- Clarify that this is the groundwork for fixing the race and a partial
  conversion to the new API: other users will be covered in a follow-up
  series.

Changes since v1 https://lore.kernel.org/netdev/cover.1793806941.git.ralf@mandelbit.com/
- Fix kdoc return statements in patch 2/3. (NIPA)

Ralf Lici (4):
  ipv6: stamp the default route node with a fresh serial on link failure
  ipv6: advance the serial after rebalancing in fib6_ifup and
    fib6_ifdown
  ipv6: sample the route's serial during the lookup
  ipv6: validate dsts against the state their lookup found

 include/net/ip6_fib.h |  10 +++
 net/ipv6/ip6_fib.c    |   4 ++
 net/ipv6/route.c      | 151 +++++++++++++++++++++++++++++++++++-------
 3 files changed, 140 insertions(+), 25 deletions(-)


base-commit: c2621b518b38ed38e280e008aa33e6c2d302f8cc
-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net-next v5 1/4] ipv6: stamp the default route node with a fresh serial on link failure
  2026-11-24 16:27 [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst Ralf Lici
@ 2026-11-24 16:27 ` Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 2/4] ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown Ralf Lici
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ralf Lici @ 2026-11-24 16:27 UTC (permalink / raw)
  To: netdev
  Cc: Ralf Lici, David Ahern, Ido Schimmel, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Kuniyuki Iwashima, Steffen Klassert, Herbert Xu, Florian Westphal,
	Xia Yan, Sabrina Dubroca, Antonio Quartulli, linux-kernel

ip6_link_failure invalidates the cached dsts of an RTF_DEFAULT route by
writing -1 into the node's fn_sernum. Once the node already holds -1,
another link failure leaves the serial unchanged, so dsts cached with
that cookie can survive subsequent failures.

The assignment also leaves net->ipv6.fib6_sernum unchanged, making the
invalidation invisible to lookups watching the namespace generation.

Use fib6_update_sernum under tb6_lock to allocate and stamp a fresh
serial. This makes repeated invalidations distinguishable and allows the
lookup stamps introduced later in this series to detect them.

Advancing the namespace generation also invalidates unrelated dsts using
nexthop objects, which are validated against that generation. This
happens once per ip6_link_failure call: once per queued skb when the
neighbour of the default router fails, and once per packet on the
tunnel transmit error paths that call dst_link_failure, some of which a
remote host can trigger with ICMP errors. These are error paths and add
no cost to the fast path.

Found while reviewing the IPv6 routing code to address the dst-cache
race.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
Changes since v4 https://lore.kernel.org/netdev/79fea581787dd175005d81090d246e982a07cc51.1795166508.git.ralf@mandelbit.com/
- Mention the tunnel transmit error paths. (Sashiko)

Changes since v3 https://lore.kernel.org/netdev/5b59512a6c51c7a5d7c7b4e16728de3c522d9a1e.1794906743.git.ralf@mandelbit.com/
- Note that the generation advances once per ip6_link_failure call.
  (Sashiko)
- Widen the rt6_exception_net comment. (Sashiko)

Changes since v2 https://lore.kernel.org/all/143461e2c94a20060ff4c7d6e5eb46c47ac46aa0.1794384146.git.ralf@mandelbit.com/
- Add a Fixes tag referencing the initial git import. (Sashiko)
- Clarify that the nexthop-object invalidation is a harmless side
  effect, rather than a deficiency being fixed. (Sashiko)

No changes since v1 https://lore.kernel.org/netdev/5b039a5a24808f2678eedd283c29f8dae514ef5e.1793806941.git.ralf@mandelbit.com/

 net/ipv6/route.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 4a4c5ace63c5..bbe6d67bb696 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1531,9 +1531,11 @@ static struct rt6_info *rt6_make_pcpu_route(struct net *net,
  */
 static DEFINE_SPINLOCK(rt6_exception_lock);
 
-/* Exceptions are accounted to the netns their dst was allocated in.
- * Unlike dev_net(dst->dev), it does not change when the device moves to
- * another netns or when dst_dev_put() points the dst to blackhole_netdev.
+/* Return the netns the dst was allocated in, which owns the fib6 table of
+ * its route. Exceptions are accounted to it, and ip6_link_failure advances
+ * its generation. Unlike dev_net(dst->dev), it does not change when the
+ * device moves to another netns or when dst_dev_put() points the dst to
+ * blackhole_netdev.
  */
 static struct net *rt6_exception_net(const struct rt6_info *rt)
 {
@@ -2873,13 +2875,13 @@ static void ip6_link_failure(struct sk_buff *skb)
 			rt6_remove_exception_rt(rt);
 		} else {
 			struct fib6_info *from;
-			struct fib6_node *fn;
 
 			from = rcu_dereference(rt->from);
-			if (from) {
-				fn = rcu_dereference(from->fib6_node);
-				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
-					WRITE_ONCE(fn->fn_sernum, -1);
+			if (from && from->fib6_table &&
+			    (rt->rt6i_flags & RTF_DEFAULT)) {
+				spin_lock_bh(&from->fib6_table->tb6_lock);
+				fib6_update_sernum(rt6_exception_net(rt), from);
+				spin_unlock_bh(&from->fib6_table->tb6_lock);
 			}
 		}
 		rcu_read_unlock();
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v5 2/4] ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown
  2026-11-24 16:27 [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 1/4] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
@ 2026-11-24 16:27 ` Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 3/4] ipv6: sample the route's serial during the lookup Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found Ralf Lici
  3 siblings, 0 replies; 5+ messages in thread
From: Ralf Lici @ 2026-11-24 16:27 UTC (permalink / raw)
  To: netdev
  Cc: Ralf Lici, David Ahern, Ido Schimmel, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Kuniyuki Iwashima, Sagarika Sharma, Steffen Klassert, Herbert Xu,
	Florian Westphal, Xia Yan, Sabrina Dubroca, Antonio Quartulli,
	linux-kernel

fib6_ifup and fib6_ifdown update the node serial before calling
rt6_multipath_rebalance. A lookup that runs between the two can select a
nexthop using the old ECMP upper bounds and read the new serial as its
cookie. The cached dst then passes dst_check although it was selected
from the old state, until the next relevant change.

Update the serial after rt6_multipath_rebalance, so that it is published
only after the routing state it describes. fib6_new_sernum allocates the
serial with a fully ordered atomic_try_cmpxchg, which orders the
rebalance before the serial update. The invalidation scope is unchanged:
fib6_ifup still updates the node and its ancestors, fib6_ifdown only the
node.

The lookup stamps introduced later in this series rely on this order: a
lookup that reads the new generation must also observe the routing
change it describes.

Reported by Sashiko while reviewing an earlier revision of this series.

Fixes: d7dedee184e7 ("ipv6: Calculate hash thresholds for IPv6 nexthops")
Fixes: 4bc852006b62 ("ipv6: update route serial number on NETDEV_CHANGE")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
Reviewed-by: Xia Yan <yanxia@cmss.chinamobile.com>
---
Changes since v4 https://lore.kernel.org/netdev/363e8614dfd168dcaf90cf7f158e12d587ec8355.1795166508.git.ralf@mandelbit.com/
- Reword the reference to the lookup stamps.

New in v4
- Split out of v3 2/3 (David):
  https://lore.kernel.org/netdev/7af19082325ce83699c5fd82de4925e52ed7dff2.1794906743.git.ralf@mandelbit.com/
- Add Fixes tags.
- Carry Xia's Reviewed-by from v3 2/3, the code is unchanged.

 net/ipv6/route.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index bbe6d67bb696..98e3ca0644ab 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -4913,8 +4913,8 @@ static int fib6_ifup(struct fib6_info *rt, void *p_arg)
 	if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
-		fib6_update_sernum_upto_root(net, rt);
 		rt6_multipath_rebalance(rt);
+		fib6_update_sernum_upto_root(net, rt);
 	}
 
 	return 0;
@@ -5017,8 +5017,8 @@ static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
 			}
 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
 						   RTNH_F_LINKDOWN);
-			fib6_update_sernum(net, rt);
 			rt6_multipath_rebalance(rt);
+			fib6_update_sernum(net, rt);
 		}
 		return -2;
 	case NETDEV_CHANGE:
@@ -5026,8 +5026,8 @@ static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
 			break;
 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
-		fib6_update_sernum(net, rt);
 		rt6_multipath_rebalance(rt);
+		fib6_update_sernum(net, rt);
 		break;
 	}
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v5 3/4] ipv6: sample the route's serial during the lookup
  2026-11-24 16:27 [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 1/4] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 2/4] ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown Ralf Lici
@ 2026-11-24 16:27 ` Ralf Lici
  2026-11-24 16:27 ` [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found Ralf Lici
  3 siblings, 0 replies; 5+ messages in thread
From: Ralf Lici @ 2026-11-24 16:27 UTC (permalink / raw)
  To: netdev
  Cc: Ralf Lici, David Ahern, Ido Schimmel, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Kuniyuki Iwashima, Steffen Klassert, Herbert Xu, Florian Westphal,
	Xia Yan, Sabrina Dubroca, Antonio Quartulli, linux-kernel

ip6_dst_check validates a dst against a cookie that its holder reads
with rt6_get_cookie after the lookup has returned. For routes without
their own sernum, this is the current fn_sernum of the route's node, so
a FIB change or xfrm policy insertion between the lookup and the read
pairs an old dst with a new serial, and the stale dst keeps passing
dst_check until another relevant change.

Sample that state during the lookup instead. ip6_pol_route and
ip6_pol_route_lookup read the IPv6 generation before walking the FIB
and, once the route and path are selected, the fn_sernum of the route's
node followed by the generation again. If the generation changed in
between, or the route was unlinked meanwhile, the selection may predate
the change and the sample is 0. Store the sample in struct fib6_result
and stamp every dst built from the result with it.

The sample covers the lookup in one table. With fib rules, the table is
selected before the first generation read, so a rule inserted while the
rules are evaluated can leave the lookup on the previous table with a
stamp that still validates. Closing that needs the generation read
before rule selection and passed down to the table lookups, which is
left for a follow-up.

Writers that change the selection without advancing the serial are not
covered either: deleting one route of an ECMP group makes fib6_del_route
rebalance the remaining ones without a new serial, and changing the
forwarding sysctl changes how gateways are scored without one, so a
lookup racing with either can keep its previous path. Both cases
predate this series.

Nothing uses the stamp yet, the following patch does.

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)

 include/net/ip6_fib.h | 10 +++++++++
 net/ipv6/ip6_fib.c    |  4 ++++
 net/ipv6/route.c      | 49 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/include/net/ip6_fib.h b/include/net/ip6_fib.h
index 232289b439c3..75da95b2349b 100644
--- a/include/net/ip6_fib.h
+++ b/include/net/ip6_fib.h
@@ -208,6 +208,10 @@ struct rt6_info {
 	struct dst_entry		dst;
 	struct fib6_info __rcu		*from;
 	int				sernum;
+	/* fn_sernum of the route's node, sampled by the lookup that built
+	 * this dst, 0 if the lookup raced with a generation change
+	 */
+	u32				cookie;
 
 	struct rt6key			rt6i_dst;
 	struct rt6key			rt6i_src;
@@ -225,6 +229,12 @@ struct fib6_result {
 	u32			fib6_flags;
 	u8			fib6_type;
 	struct rt6_info		*rt6;
+	/* sampled by rt6_lookup_stamp: the IPv6 generation and the route
+	 * node's fn_sernum, both 0 if the generation changed during the
+	 * lookup
+	 */
+	int			gen;
+	u32			cookie;
 };
 
 #define for_each_fib6_node_rt_rcu(fn)					\
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index e1d49897d6ab..c94968a8fcbd 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -123,6 +123,10 @@ static void fib6_walkers_skip_rt(struct net *net, struct fib6_info *rt)
 	fib6_walkers_replace_leaf(net, rt, next);
 }
 
+/* The successful atomic_try_cmpxchg is fully ordered: it publishes the
+ * routing change before the new generation, and the new generation before
+ * the nodes stamped with it. rt6_lookup_stamp relies on both.
+ */
 static int fib6_new_sernum(struct net *net)
 {
 	int new, old = atomic_read(&net->ipv6.fib6_sernum);
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 98e3ca0644ab..8b98ee945c58 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1216,11 +1216,48 @@ static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
 		rt->rt6i_flags |= RTF_GATEWAY;
 	}
 	rt6_set_from(rt, f6i);
+	rt->cookie = res->cookie;
 #ifdef CONFIG_IPV6_SUBTREES
 	rt->rt6i_src = f6i->fib6_src;
 #endif
 }
 
+static int rt6_lookup_gen(const struct net *net)
+{
+	/* the acquire pairs with the full barrier in fib6_new_sernum */
+	return atomic_read_acquire(&net->ipv6.fib6_sernum);
+}
+
+/* Record in @res the state the selected route was found under: the
+ * generation read by rt6_lookup_gen and the fn_sernum of the route's
+ * node. If a FIB change advanced the generation during the lookup, the
+ * selection may predate it, so record 0, which never validates.
+ */
+static void rt6_lookup_stamp(const struct net *net, struct fib6_result *res,
+			     int gen)
+{
+	bool linked;
+	u32 cookie;
+
+	/* a route unlinked by a concurrent deletion has no node left, so no
+	 * dst built from it should validate: treat it like a generation change
+	 */
+	linked = fib6_get_cookie_safe(res->f6i, &cookie);
+
+	/* keep the FIB and fn_sernum reads before the final generation read,
+	 * pairs with the full barrier in fib6_new_sernum
+	 */
+	smp_rmb();
+	if (unlikely(!linked || rt_genid_ipv6(net) != gen)) {
+		res->gen = 0;
+		res->cookie = 0;
+		return;
+	}
+
+	res->gen = gen;
+	res->cookie = cookie;
+}
+
 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
 					struct in6_addr *saddr)
 {
@@ -1291,8 +1328,11 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
 	struct fib6_result res = {};
 	struct fib6_node *fn;
 	struct rt6_info *rt;
+	int gen;
 
 	rcu_read_lock();
+	/* read before walking the FIB, rt6_lookup_stamp checks it again */
+	gen = rt6_lookup_gen(net);
 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
 restart:
 	res.f6i = rcu_dereference(fn->leaf);
@@ -1311,11 +1351,13 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
 		dst_hold(&rt->dst);
 		goto out;
 	} else if (res.fib6_flags & RTF_REJECT) {
+		rt6_lookup_stamp(net, &res, gen);
 		goto do_create;
 	}
 
 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
 			 fl6->flowi6_oif != 0, skb, flags);
+	rt6_lookup_stamp(net, &res, gen);
 
 	/* Search through exception table */
 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
@@ -2278,10 +2320,16 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
 	struct rt6_info *rt = NULL;
 	bool have_oif_match;
 	int strict = 0;
+	int gen;
 
 	WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
 		     !rcu_read_lock_held());
 
+	/* read before the forwarding mode and the FIB, rt6_lookup_stamp
+	 * checks it again
+	 */
+	gen = rt6_lookup_gen(net);
+
 	strict |= flags & RT6_LOOKUP_F_IFACE;
 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
 	if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
@@ -2296,6 +2344,7 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
 	have_oif_match = fl6->flowi6_iif == LOOPBACK_IFINDEX &&
 			 oif == res.nh->fib_nh_dev->ifindex;
 	fib6_select_path(net, &res, fl6, oif, have_oif_match, skb, strict);
+	rt6_lookup_stamp(net, &res, gen);
 
 	/*Search through exception table */
 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found
  2026-11-24 16:27 [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst Ralf Lici
                   ` (2 preceding siblings ...)
  2026-11-24 16:27 ` [PATCH net-next v5 3/4] ipv6: sample the route's serial during the lookup Ralf Lici
@ 2026-11-24 16:27 ` Ralf Lici
  3 siblings, 0 replies; 5+ messages in thread
From: Ralf Lici @ 2026-11-24 16:27 UTC (permalink / raw)
  To: netdev
  Cc: Ralf Lici, David Ahern, Ido Schimmel, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Kuniyuki Iwashima, Steffen Klassert, Herbert Xu, Florian Westphal,
	Xia Yan, Sabrina Dubroca, Antonio Quartulli,
	Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
	linux-rt-devel, linux-kernel

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-24 16:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 16:27 [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst Ralf Lici
2026-11-24 16:27 ` [PATCH net-next v5 1/4] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
2026-11-24 16:27 ` [PATCH net-next v5 2/4] ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown Ralf Lici
2026-11-24 16:27 ` [PATCH net-next v5 3/4] ipv6: sample the route's serial during the lookup Ralf Lici
2026-11-24 16:27 ` [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found Ralf Lici

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox