ipv6: tie the dst cookie to the lookup that returned the dst
Subsystems (4)
v4 cover ipv6: tie the dst cookie to the lookup that returned the dst
Hi,
This series implements a first part of the model proposed in [1] to address the late cookie sampling race: 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.
The case that made me post it as a series is xfrm. Adding a policy restamps every fib6 node, so a socket whose lookup ran just before the insertion keeps its plain dst and leaks plaintext although the policy matches. I reproduced it for TCP connect and connected UDP, and the same window lets a more specific blackhole route be bypassed for routes using nexthop objects. The ovpn patch [2] hit the same pattern in dst_cache_set_ip6.
Patches 1-3 are the prerequisites: writers that publish a routing change before advancing the IPv6 generation, and an ip6_dst_check that compares the cookie of nexthop-object dsts. Patch 4 adds the lookup-cookie API and patch 5 converts tcp_v6_connect, ip6_datagram_dst_update and ip6_sk_dst_lookup_flow to it.
Compared to [1], the fallback cookie is 0 instead of the pre-lookup generation, so fib6_flush_trees is left alone, and nexthop-object dsts are checked in ip6_dst_check instead of passing the generation down to ip6_rt_pcpu_alloc.
I am targeting net-next to get feedback on the lookup-cookie API and its incremental adoption before extending the conversion across the remaining cache users. I am not requesting stable backports at this stage.
Known limitations and pre-existing issues, not addressed here:
- The conversion is partial. The remaining ip6_dst_store users (inet6_csk_route_socket, inet6_sk_rebuild_header and tcp_v6_syn_recv_sock) and the other cache users (dst_cache, sctp, ipvs, flowtable, sk_rx_dst) still sample the cookie late.
- xfrm bundles ignore the returned cookie. Their route and path cookies are still sampled late in xfrm_bundle_create. I plan to apply the same generation bracketing inside xfrm as a follow-up.
- xfrm_policy_insert unlinks a replaced policy after rt_genid_bump_ipv6, so a lookup after the bump can still match the old policy. I will send a separate fix.
- Route deletion does not advance the generation, and ip6_dst_check does not look at rt->from for nexthop-object dsts, so these are not invalidated by deletion. This predates the series and the kdoc of the new helper does not claim otherwise.
- A newly linked fib6 node holds fn_sernum 0 until fib6_add stamps it, so a dst stored with the fallback cookie 0 can pass a check in that window. It fails as soon as the node is stamped.
I tested it in a VM with lockdep, PROVE_RCU and DEBUG_ATOMIC_SLEEP, using a debug-only delay after the lookup to hit the window: no warnings, and the plaintext and blackhole bypass are gone in these test cases.
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
Thanks,
Ralf Lici
Mandelbit Srl
---
Changes since v3 https://lore.kernel.org/netdev/[email protected]/
- 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/[email protected]/
- 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/[email protected]/
- Fix kdoc return statements in patch 2/3. (NIPA)
Ralf Lici (5):
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: validate the cookie of nexthop-object dsts in ip6_dst_check
ipv6: add ip6_dst_lookup_flow_cookie
ipv6: use lookup cookies in selected socket dst cache paths
include/net/ip6_route.h | 18 ++++++++---- include/net/ipv6.h | 13 +++++++++ net/ipv6/datagram.c | 6 ++-- net/ipv6/ip6_fib.c | 4 +++ net/ipv6/ip6_output.c | 63 ++++++++++++++++++++++++++++++++++++++--- net/ipv6/route.c | 38 +++++++++++++------------ net/ipv6/tcp_ipv6.c | 5 ++-- 7 files changed, 116 insertions(+), 31 deletions(-)
base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e
Signature · 1 line
2.56.0
PATCH v4 1/5 ipv6: stamp the default route node with a fresh serial on link failure
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-cookie generation checks 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, so once per queued skb when the neighbour of the default router fails. 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.
---
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(-)
net/ipv6/route.c +10 -8
@@ -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 ();
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net-next v4 1/5] ipv6: stamp the default route node with a fresh serial on link failure
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The patch makes ip6_link_failure() (net/ipv6/route.c) do much more work for an RTF_DEFAULT, non-RTF_CACHE dst.
--
ipv6: stamp the default route node with a fresh serial on link failure
This commit changes ip6_link_failure() so that it no longer writes -1 into fn_sernum of an RTF_DEFAULT route's node. It now calls fib6_update_sernum() under tb6_lock, which allocates a fresh serial and advances the netns-wide fib6_sernum generation. The changelog says the extra cost happens once per queued skb when the neighbour of the default router fails.
Quoted 4 lines
net/ipv6/route.c +0
-0
[ ... ]
Quoted 6 lines
net/ipv6/route.c +0
-1@@ -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;
net/ipv6/route.c static void ip6_link_failure(struct sk_buff *skb) · line 2884
Quoted 10 lines
net/ipv6/route.c +5
-4from =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);
[Severity: Low]
Is the cost of this path limited to neighbour failure, as the commit message says?
This happens once per ip6_link_failure call, so once per queued skb when the neighbour of the default router fails.
Several tunnel transmit error paths call dst_link_failure(skb) once per packet. For an IPv6 inner packet the inner route's dst is still attached, so they reach ip6_link_failure():
ipip6_tunnel_xmit() in net/ipv6/sit.c:
tx_error_icmp:
dst_link_failure(skb);
This one is taken when ip_route_output_flow() fails or when the underlay route is not unicast or local.
ip6_tnl_xmit() in net/ipv6/ip6_tunnel.c:
tx_err_link_failure:
DEV_STATS_INC(dev, tx_carrier_errors); dst_link_failure(skb);
This one is taken when ip6_route_output() returns dst->error, when xfrm_lookup() fails, or when ip6_tnl_xmit_ctl() fails. skb_dst_set() runs later than this, so skb_dst is still the inner IPv6 route here.
ip_tunnel_xmit() in net/ipv4/ip_tunnel.c also reaches tx_error_icmp in the NBMA case when the IPv6 next hop is not v4-compatible.
There are also the err_count paths in ip_tunnel_xmit(), ipip6_tunnel_xmit() and ip6_tnl_xmit():
WRITE_ONCE(tunnel->err_count, err_count - 1); dst_link_failure(skb);
These run once per ICMP error received for the tunnel, so a remote host can drive them.
RA-learned default routes carry RTF_DEFAULT, for example a default router learned over ISATAP/sit, gretap or ip6gretap. pcpu copies keep the flag through ip6_rt_copy_init().
While the underlay is broken, wouldn't every IPv6 packet sent through such a route take tb6_lock with BH disabled? Each one would also run fib6_new_sernum()'s atomic_try_cmpxchg on net->ipv6.fib6_sernum.
tb6_lock is the same lock that route insert/delete, GC and PMTU exception insertion use. Each generation bump also invalidates every nexthop-object dst in the netns, because rt6_is_valid() checks those against the netns generation. Later in the series, ip6_dst_lookup_flow_cookie() returns cookie 0 to socket lookups that race with a bump.
These are error paths where the traffic is already being dropped. Also, rt6_insert_exception() already takes tb6_lock and bumps the generation on remotely triggered PMTU exceptions. Still, the cost here seems to scale with packets on these tunnel paths, not with neighbour queue length.
Could the changelog mention these callers? Would it be worth rate-limiting the bump, or skipping it when the node's serial has already moved past the one the dst cached?
Quoted 3 lines
net/ipv6/route.c +0
-0} }rcu_read_unlock ();
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795166508.git.ralf%40mandelbit.com
PATCH v4 2/5 ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown
In reply to Ralf Licifib6_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-cookie generation checks 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.
---
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(-)
net/ipv6/route.c +3 -3
@@ -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 ; }
Signature · 1 line
2.56.0
PATCH v4 3/5 ipv6: validate the cookie of nexthop-object dsts in ip6_dst_check
In reply to Ralf LiciDsts of routes using nexthop objects record the IPv6 generation at allocation in rt->sernum. ip6_dst_check validates them with rt6_is_valid, which compares rt->sernum with the current generation, and ignores the cookie supplied by the caller.
If the generation advances between route selection and dst allocation, the dst records the new generation and passes rt6_is_valid although it was selected from the old state. A caller that sampled the generation before its lookup can detect this only if ip6_dst_check compares the cookie as well.
Require the cookie to match rt->sernum. Existing callers obtain the cookie with rt6_get_cookie, which returns rt->sernum for these dsts, so their validation is unchanged. ip6_dst_lookup_flow_cookie, added in the next patch, returns the generation read before the lookup as the cookie for these dsts.
---
New in v4
- Split out of v3 2/3 (David):
https://lore.kernel.org/netdev/7af19082325ce83699c5fd82de4925e52ed7dff2.1794906743.git.ralf@mandelbit.com/
- Carry Xia's Reviewed-by from v3 2/3, the code is unchanged.
net/ipv6/route.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/route.c +1 -1
@@ -2819,7 +2819,7 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst, rt =dst_rt6_info (dst);if (rt->sernum) -return rt6_is_valid (rt) ? dst :NULL ; +return (rt6_is_valid (rt) && cookie == rt->sernum) ? dst :NULL ;rcu_read_lock ();
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net-next v4 3/5] ipv6: validate the cookie of nexthop-object dsts in ip6_dst_check
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] In net/ipv6/route.c, ip6_dst_check() (lines 2821-2822) now returns a nexthop-object per-CPU dst (rt->sernum != 0) only if `cookie…
--
ipv6: validate the cookie of nexthop-object dsts in ip6_dst_check
This patch makes ip6_dst_check() compare the cookie passed by the caller with rt->sernum for dsts of nexthop-object routes. Before, it only checked rt->sernum against the current IPv6 generation. The commit message says existing callers are unaffected because rt6_get_cookie() returns rt->sernum for these dsts.
net/ipv6/route.c INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst, · line 2822
Quoted 10 lines
net/ipv6/route.c +1
-1@@ -2819,7 +2819,7 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst, rt =dst_rt6_info (dst);if (rt->sernum) -return rt6_is_valid (rt) ? dst :NULL ; +return (rt6_is_valid (rt) && cookie == rt->sernum) ? dst :NULL ;
[Severity: Low]
Is "their validation is unchanged" true for the IPv6 UDP rx dst cache? Until now the cookie was ignored for these dsts, so a cached dst and its cookie could come from different lookups without any effect. With this check they have to match.
In udp6_sk_rx_dst_set(), the dst and the cookie are written separately:
net/ipv6/udp.c:udp6_sk_rx_dst_set() {
if (udp_sk_rx_dst_set(sk, dst))
sk->sk_rx_dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
}udp_sk_rx_dst_set() updates only the pointer, using xchg(). Nothing serializes softirq writers running on different CPUs, so this ordering looks possible:
CPU A
xchg(sk_rx_dst, dst1) dst1->sernum == g1
IPv6 generation moves to g2
CPU B
xchg(sk_rx_dst, dst2) dst2->sernum == g2
sk_rx_dst_cookie = g2
CPU A
sk_rx_dst_cookie = g1
The socket now holds dst2 with cookie g1. After that, every call to udp_v6_early_demux() fails:
dst = dst_check(dst, sk->sk_rx_dst_cookie);
rt6_is_valid(dst2) is true, but g1 != g2. udp_v6_early_demux() does not clear sk_rx_dst when this check fails.
The full input lookup on CPU B then returns the same per-CPU dst2. So in __udp6_lib_rcv() this condition is false:
if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) udp6_sk_rx_dst_set(sk, dst);
That means the stale cookie is never corrected.
Could this leave early demux disabled for that socket on that CPU? Each packet would pay for a full FIB lookup until the generation advances again or a packet arrives on a CPU with a different per-CPU dst.
The window between the xchg() and the cookie store is short, but a hardirq can stretch it. On PREEMPT_RT, softirqs run in threads, so preemption can too. Later patches in the series do not touch this UDP rx path.
ip6_dst_store() has a similar split-store pattern. There, a failed sk_dst_check() resets the cache, so it recovers. TCP rx dst writers are serialized by the socket lock, so any mismatch there is only transient.
Quoted 3 lines
net/ipv6/route.c +0
-0rcu_read_lock ();
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795166508.git.ralf%40mandelbit.com
PATCH v4 4/5 ipv6: add ip6_dst_lookup_flow_cookie
In reply to Ralf LiciPersistent dst caches read the validation cookie with rt6_get_cookie after route lookup has returned. For routes without their own sernum, this reads the current fn_sernum of the associated node. A concurrent FIB change or xfrm policy insertion can therefore pair an old dst with a new serial, allowing subsequent dst_check calls to accept the stale result until another relevant change.
Add ip6_dst_lookup_flow_cookie to return the dst together with its validation cookie. Like ip6_dst_lookup_flow, it performs the route lookup followed by the xfrm policy lookup. Sample the cookie between two reads of the IPv6 generation and return cookie 0 if the generation changed. Both FIB changes and xfrm policy insertion advance the generation. This lets the route dst serve the current packet while preventing its subsequent reuse through cache validation. For dsts of routes using nexthop objects, return the generation read before the lookup, which ip6_dst_check compares with rt->sernum. xfrm bundles retain their existing validation through their own route and path cookies.
The generation checks rely on writers publishing routing changes before advancing the generation, and advancing it before stamping the nodes. Both follow from the fully ordered atomic_try_cmpxchg in fib6_new_sernum. Document this there and name it as the pairing side of the read barriers in the helper.
The following patch converts selected socket cache users, while other persistent caches are left for follow-up conversions.
---
Changes since v3 https://lore.kernel.org/netdev/7af19082325ce83699c5fd82de4925e52ed7dff2.1794906743.git.ralf@mandelbit.com/
- Move the writer ordering and the ip6_dst_check change to separate
patches. (David)
- Say in the kdoc that the helper performs the xfrm policy lookup.
(David)
- Narrow the kdoc to changes that advance the IPv6 generation. (Sashiko)
- Document the full ordering of fib6_new_sernum and name it as the
pairing side of the read barriers. (Xia)
Changes since v2 https://lore.kernel.org/all/e48cac0cdf9f3adcedecbbfff4a314a98bc22d01.1794384146.git.ralf@mandelbit.com/
- Move the serial update after rt6_multipath_rebalance in fib6_ifup and
fib6_ifdown. (Sashiko)
- Clarify that this introduces the new API while the following patch
converts *some* users to it while leaving the rest as follow-up.
Changes since v1 https://lore.kernel.org/netdev/e40f48e925a0950eac99a3f084e485e82cabe88f.1793806941.git.ralf@mandelbit.com/
- Fix kdoc return statement in ip6_dst_lookup_flow and
ip6_dst_lookup_flow_cookie. (NIPA)
include/net/ipv6.h | 13 ++++++++++ net/ipv6/ip6_fib.c | 4 +++ net/ipv6/ip6_output.c | 57 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-)
include/net/ipv6.h +13 -0
@@ -1062,6 +1062,11 @@ int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,#if IS_ENABLED (CONFIG_IPV6 )struct dst_entry *ip6_dst_lookup_flow (struct net *net,const struct sock *sk,struct flowi6 *fl6,const struct in6_addr *final_dst); +struct dst_entry *ip6_dst_lookup_flow_cookie (struct net *net, +const struct sock *sk, +struct flowi6 *fl6, +const struct in6_addr *final_dst, +u32 *cookie);#else static inline struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
@@ -1069,6 +1074,14 @@ static inline struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struc {return ERR_PTR (-EAFNOSUPPORT ); } + +static inline struct dst_entry * +ip6_dst_lookup_flow_cookie (struct net *net,const struct sock *sk, +struct flowi6 *fl6,const struct in6_addr *final_dst, +u32 *cookie) +{ +return ERR_PTR (-EAFNOSUPPORT ); +}#endif struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
net/ipv6/ip6_fib.c +4 -0
@@ -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. ip6_dst_lookup_flow_cookie relies on both. +*/ static int fib6_new_sernum (struct net *net) {int new, old =atomic_read (&net->ipv6.fib6_sernum);
net/ipv6/ip6_output.c +55 -2
69 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 2 · Medium: 0 · Low: 0
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] ip6_dst_lookup_flow_cookie() (net/ipv6/ip6_output.c) only closes the lookup/cookie race when the returned dst is a plain route.
- [High] The generation check in ip6_dst_lookup_flow_cookie() is only sound if every writer publishes its full routing/policy change before…
--
ipv6: add ip6_dst_lookup_flow_cookie
This adds ip6_dst_lookup_flow_cookie(). Like ip6_dst_lookup_flow(), it does the route and xfrm policy lookup, and it also returns the dst validation cookie. The cookie is sampled between two reads of the IPv6 generation and forced to 0 if the generation changed. A comment is also added to fib6_new_sernum() describing the writer side ordering that the helper relies on.
net/ipv6/ip6_fib.c static void fib6_walkers_skip_rt(struct net *net, struct fib6_info *rt) · line 129
Quoted 12 lines
net/ipv6/ip6_fib.c +4
-0@@ -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. ip6_dst_lookup_flow_cookie relies on both. +*/
[Severity: High]
Does xfrm_policy_insert() follow this ordering when it replaces an existing policy (excl == 0)?
In xfrm_policy_insert(), the IPv6 generation is advanced before the replaced policy is unlinked:
net/xfrm/xfrm_policy.c:xfrm_policy_insert() {
...
__xfrm_policy_link(policy, dir);
...
else
rt_genid_bump_ipv6(net);
if (delpol) {
xfrm_policy_requeue(delpol, policy);
__xfrm_policy_unlink(delpol, dir);
}
...
}Nothing advances the generation after the unlink. xfrm_policy_kill(delpol) only does atomic_inc(&policy->genid), and that only affects bundles.
Suppose the new policy has a larger priority value than the old one, and a later entry in the chain has priority <= the new priority. Then xfrm_policy_insert_list() links the new policy behind that entry, which puts it after the old policy. During this window, xfrm_policy_lookup_bytype() takes the first match, so it keeps selecting the old policy.
Can this sequence happen?
CPU2 xfrm_policy_insert() CPU1 ip6_dst_lookup_flow_cookie()
__xfrm_policy_link(P_new)
rt_genid_bump_ipv6() /* G+1 */
gen0 = G+1 xfrm_lookup() matches P_old (allow, no templates), returns dst_orig untransformed *cookie = rt6_get_cookie(rt) = G+1 gen1 = G+1, cookie is kept
__xfrm_policy_unlink(P_old)
The socket's cached plain route would then keep passing ip6_dst_check(). Traffic would go out in plaintext even though P_new requires IPsec or blocks the flow. This lasts until an unrelated IPv6 generation change.
The following patch, "ipv6: use lookup cookies in selected socket dst cache paths", relies on lookup cookies to prevent this kind of plaintext bypass. No later patch in the series changes xfrm_policy_insert().
Quoted 4 lines
net/ipv6/ip6_output.c +0
-0
[ ... ]
Quoted 4 lines
net/ipv6/ip6_output.c +0
-0@@ -1300,6 +1299,60 @@ struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, st }EXPORT_SYMBOL_GPL (ip6_dst_lookup_flow);
[ ... ]
Quoted 20 lines
net/ipv6/ip6_output.c +20
-0+struct dst_entry *ip6_dst_lookup_flow_cookie (struct net *net, +const struct sock *sk, +struct flowi6 *fl6, +const struct in6_addr *final_dst, +u32 *cookie) +{ +const struct rt6_info *rt; +struct dst_entry *dst; +int gen0, gen1; + + gen0 =rt_genid_ipv6 (net); +/* read the generation before looking at the FIB or xfrm policies, +* pairs with the full barrier in fib6_new_sernum +*/ +smp_rmb (); + + dst =ip6_dst_lookup_flow (net, sk, fl6, final_dst); +if (IS_ERR (dst)) +return dst; +
net/ipv6/ip6_output.c struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, st · line 1350
Quoted 10 lines
net/ipv6/ip6_output.c +10
-0+ rt =dst_rt6_info (dst); + *cookie = rt->sernum ? gen0 :rt6_get_cookie (rt); + +/* keep the FIB and cookie reads before the final generation read, +* pairs with the full barrier in fib6_new_sernum +*/ +smp_rmb (); + gen1 =rt_genid_ipv6 (net); +if (unlikely (gen0 != gen1)) + *cookie =0 ;
[Severity: High]
If ip6_dst_lookup_flow() returns an xfrm bundle, does zeroing the cookie here change anything?
xfrm_dst_check() never reads the cookie:
net/xfrm/xfrm_policy.c:xfrm_dst_check() {
...
if (READ_ONCE(dst->obsolete) < 0 && !stale_bundle(dst))
return dst;
...
}xfrm_bundle_ok() checks only dst_check(path, path_cookie) and dst_check(xdst->route, route_cookie), plus the policy and state genids.
Those two cookies are sampled with rt6_get_cookie() after the route and policy lookups, which is inside the gen0..gen1 window. That is the same late sampling that the commit message describes as the bug:
net/ipv6/xfrm6_policy.c:xfrm6_fill_dst() {
...
xdst->route_cookie = rt6_get_cookie(rt);
...
}net/xfrm/xfrm_policy.c:xfrm_init_path() {
...
path->path_cookie = rt6_get_cookie(dst_rt6_info(dst));
...
}Could this interleaving occur?
1. A reader (tcp_v6_connect(), ip6_datagram_dst_update() or ip6_sk_dst_lookup_flow() after the following patch) reads gen0 = G, looks up route R, and xfrm_lookup() selects the existing policy P_old.
2. A higher precedence policy P_new with a different selector is inserted, so P_old is not killed and its genid does not change. xfrm_policy_insert() calls rt_genid_bump_ipv6() -> fib6_flush_trees(), which sets fib6_sernum to G+1 and stamps every node with G+1.
3. The reader's xfrm_bundle_create() then reads route_cookie and path_cookie as G+1.
4. Since gen1 != gen0, *cookie is set to 0, but the bundle ignores it. The socket caches the bundle. Later dst_check() calls pass because fn_sernum is G+1 and P_old->genid has not changed.
The flow would keep using P_old until some unrelated generation change. That means the old tunnel or SA stays in use, even if P_new is a block policy. A concurrent fib6_add() of a more specific route could pair the old xdst->route with a new serial in the same way.
The commit message says:
xfrm bundles retain their existing validation through their own route and path cookies.
That validation depends on the same racy sampling this patch is meant to fix. The cookie value alone cannot invalidate a bundle, so does the gen0 != gen1 case need something more? For example, a result telling the caller not to cache the dst, or marking the bundle obsolete.
Quoted 4 lines
net/ipv6/ip6_output.c +4
-0+ +return dst; +} +EXPORT_SYMBOL_GPL (ip6_dst_lookup_flow_cookie);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795166508.git.ralf%40mandelbit.com
Re: [PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie
Quoted 11 lines · On Tue, 24 Nov 2026 10:26:54 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 2 · Medium: 0 · Low: 0
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).
Quoted 5 lines
- [High]
ip6_dst_lookup_flow_cookie()(net/ipv6/ip6_output.c) only closes the lookup/cookie race when the returned dst is a plain route.
- [High] The generation check inip6_dst_lookup_flow_cookie()is only sound if every writer publishes its full routing/policy change before…
Both points are addressed in the next revision, which drops ip6_dst_lookup_flow_cookie and moves the validation into the dst:
- The route and path dsts of xfrm bundles are now validated against their own lookup stamp, so the late route_cookie and path_cookie no longer matter.
- The xfrm_policy_insert ordering will be fixed separately in the ipsec tree.
pw-bot: cr
Signature · 2 lines
Ralf Lici Mandelbit Srl
PATCH v4 5/5 ipv6: use lookup cookies in selected socket dst cache paths
In reply to Ralf Licitcp_v6_connect, ip6_datagram_dst_update and ip6_sk_dst_lookup_flow obtain a dst and sample its validation cookie afterwards. If a matching xfrm policy is inserted between lookup and cookie sampling, the plain route can be cached with a post-insertion serial. Subsequent packets then pass dst validation and bypass the new policy until another relevant FIB change.
Store the cookie returned by ip6_dst_lookup_flow_cookie in these paths instead. Add ip6_dst_store_cookie to accept the supplied cookie, retaining ip6_dst_store for unconverted callers.
This is a partial conversion. Established TCP route refills, header rebuilds and accepted-socket setup still use late cookie sampling. Those paths and other persistent caches, including SCTP and dst_cache users, are left for follow-up conversions.
This race was found while investigating how mutable socket routing properties affect the dst cache in ovpn's UDP TX path.
The socket-cache case was then reproduced in a VM by adding a debug-only kernel delay after the route and policy lookup, then inserting a matching xfrm policy before cookie sampling. TCP connect and connected UDP sockets continued sending plaintext after the policy insertion. With lookup cookies, the stale cached route was rejected on reuse.
---
Changes since v3 https://lore.kernel.org/netdev/60907e12930f38ba6c45c4ccfdca7abee068b765.1794906743.git.ralf@mandelbit.com/
- Drop the Fixes tag: this targets net-next and no backport is
requested. (Sashiko)
Changes since v2 https://lore.kernel.org/all/43917b9119e13e54dcde0e2941d3f4a1fe255df7.1794384146.git.ralf@mandelbit.com/
- Add a Fixes tag referencing the initial git import. (Sashiko)
- Clearly state that this is only a partial conversion to the new API.
No changes since v1 https://lore.kernel.org/netdev/832892eaa7d2371f20459c3134c72914e42970b0.1793806941.git.ralf@mandelbit.com/
include/net/ip6_route.h | 18 +++++++++++++----- net/ipv6/datagram.c | 6 ++++-- net/ipv6/ip6_output.c | 6 ++++-- net/ipv6/route.c | 12 ++++++------ net/ipv6/tcp_ipv6.c | 5 +++-- 5 files changed, 30 insertions(+), 17 deletions(-)
include/net/ip6_route.h +13 -5
@@ -252,13 +252,13 @@ static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)/* * Store a destination cache entry in a socket */ -static inline void ip6_dst_store (struct sock *sk,struct dst_entry *dst, -bool daddr_set, -bool saddr_set) +static inline void ip6_dst_store_cookie (struct sock *sk,struct dst_entry *dst, +u32 cookie,bool daddr_set, +bool saddr_set) {struct ipv6_pinfo *np =inet6_sk (sk); - np->dst_cookie =rt6_get_cookie (dst_rt6_info (dst)); + np->dst_cookie = cookie;sk_setup_caps (sk, dst); np->daddr_cache = daddr_set;#ifdef CONFIG_IPV6_SUBTREES
@@ -266,8 +266,16 @@ static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst,#endif } +static inline void ip6_dst_store (struct sock *sk,struct dst_entry *dst, +bool daddr_set, +bool saddr_set) +{ +ip6_dst_store_cookie (sk, dst,rt6_get_cookie (dst_rt6_info (dst)), + daddr_set, saddr_set); +} +void ip6_sk_dst_store_flow (struct sock *sk,struct dst_entry *dst, -const struct flowi6 *fl6); +const struct flowi6 *fl6,u32 cookie);static inline bool ipv6_unicast_destination (const struct sk_buff *skb) {
net/ipv6/datagram.c +4 -2
@@ -78,6 +78,7 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)struct in6_addr *final_p;struct dst_entry *dst;struct flowi6 *fl6; +u32 cookie;int err =0 ; if (inet6_test_bit(SNDFLOW, sk) &&
@@ -94,7 +95,8 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) final_p =fl6_update_dst (fl6, opt, &np->final);rcu_read_unlock (); - dst =ip6_dst_lookup_flow (sock_net (sk), sk, fl6, final_p); + dst =ip6_dst_lookup_flow_cookie (sock_net (sk), sk, fl6, final_p, + &cookie);if (IS_ERR (dst)) { err =PTR_ERR (dst); goto out;
@@ -112,7 +114,7 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) } } -ip6_sk_dst_store_flow (sk, dst, fl6); +ip6_sk_dst_store_flow (sk, dst, fl6, cookie); out:fl6_sock_release (flowlabel);
net/ipv6/ip6_output.c +4 -2
@@ -1376,6 +1376,7 @@ struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, bool connected) {struct dst_entry *dst; +u32 cookie;if (connected) { dst =sk_dst_check (sk,inet6_sk (sk)->dst_cookie);
@@ -1384,9 +1385,10 @@ struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,return dst; } - dst =ip6_dst_lookup_flow (sock_net (sk), sk, fl6, final_dst); + dst =ip6_dst_lookup_flow_cookie (sock_net (sk), sk, fl6, final_dst, + &cookie);if (connected && !IS_ERR (dst)) -ip6_sk_dst_store_flow (sk,dst_clone (dst), fl6); +ip6_sk_dst_store_flow (sk,dst_clone (dst), fl6, cookie);return dst; }
net/ipv6/route.c +6 -6
@@ -3050,19 +3050,19 @@ void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) }void ip6_sk_dst_store_flow (struct sock *sk,struct dst_entry *dst, -const struct flowi6 *fl6) +const struct flowi6 *fl6,u32 cookie) {#ifdef CONFIG_IPV6_SUBTREES struct ipv6_pinfo *np =inet6_sk (sk);#endif -ip6_dst_store (sk, dst, -ipv6_addr_equal (&fl6->daddr, &sk->sk_v6_daddr), +ip6_dst_store_cookie (sk, dst, cookie, +ipv6_addr_equal (&fl6->daddr, &sk->sk_v6_daddr), #ifdef CONFIG_IPV6_SUBTREES -ipv6_addr_equal (&fl6->saddr, &np->saddr) ? - true : +ipv6_addr_equal (&fl6->saddr, &np->saddr) ? + true : #endif - false); + false); } static bool ip6_redirect_nh_match(const struct fib6_result *res,
net/ipv6/tcp_ipv6.c +3 -2
@@ -145,6 +145,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,struct dst_entry *dst;struct flowi6 *fl6;int addr_type; +u32 cookie;int err;if (addr_len <SIN6_LEN_RFC2133 )
@@ -288,7 +289,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,*/ ip6_ecmp_set_mp_hash (net, fl6, sk->sk_txhash); - dst =ip6_dst_lookup_flow (net, sk, fl6, final_p); + dst =ip6_dst_lookup_flow_cookie (net, sk, fl6, final_p, &cookie);if (IS_ERR (dst)) { err =PTR_ERR (dst); goto failure;
@@ -312,7 +313,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr, inet->inet_rcv_saddr =LOOPBACK4_IPV6 ; sk->sk_gso_type =SKB_GSO_TCPV6 ; -ip6_dst_store (sk, dst, false, false); +ip6_dst_store_cookie (sk, dst, cookie, false, false); icsk->icsk_ext_hdr_len =0 ;if (opt)
Signature · 1 line
2.56.0