🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Ralf Lici Wed, 11 Nov 2026 08:22
[PATCH net-next v2 3/3] ipv6: use the lookup cookie when caching the dst in sockets
#14430
Author: Ralf Lici
Date: Wed, 11 Nov 2026 08:22
181 lines
15526 bytes
tcp_v6_connect, ip6_datagram_dst_update and ip6_sk_dst_lookup_flow store
the dst in the socket and read its cookie afterwards. If an xfrm policy
matching the flow is added after the route and policy lookup but before
the cookie is read, the plain route found without the policy is cached
with a post-insertion serial. Later packets pass the validation in
__sk_dst_check and skip xfrm, so a socket that should be using IPsec
keeps sending plaintext until the FIB changes again. It affects TCP and
connected UDP sockets alike.

Store the cookie returned by ip6_dst_lookup_flow_cookie instead. Add
ip6_dst_store_cookie for this and keep ip6_dst_store for callers that
have no lookup cookie.

Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
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(-)

diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
index 95f936be32c6..2684f6069a5e 100644
--- a/include/net/ip6_route.h
+++ b/include/net/ip6_route.h
@@ -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)
 {
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ffbf983..c18609bab366 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -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);
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index 1b273714f7cc..2288102710b3 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -1370,14 +1370,16 @@ struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
 					 bool connected)
 {
 	struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
+	u32 cookie;
 
 	dst = ip6_sk_dst_check(sk, dst, fl6);
 	if (dst)
 		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;
 }
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 84af9626c738..ec7433b4fac8 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -3042,19 +3042,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,
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index b1dec5d86d63..25404656069c 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -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)
-- 
2.56.0
Re: [PATCH net-next v2 3/3] ipv6: use the lookup cookie when caching the dst in sockets
#16582
Author: netdev-bot+sashiko
Date: Sun, 15 Nov 2026 08:56
148 lines
14020 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 1

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] Incomplete fix: on the TCP side only tcp_v6_connect() was
  converted.
- [Medium] The same bug class remains in the SCTP per-transport dst
  cache.
- [Low] The commit fixes a user-visible security problem: 'a socket that
  should be using IPsec keeps sending plaintext until the FIB changes…

--

ipv6: use the lookup cookie when caching the dst in sockets

This commit changes tcp_v6_connect(), ip6_datagram_dst_update() and
ip6_sk_dst_lookup_flow() to store the cookie returned by
ip6_dst_lookup_flow_cookie(). Before, they read rt6_get_cookie() after
the route and xfrm lookup. It adds ip6_dst_store_cookie() for this, and
ip6_dst_store() stays as a wrapper that still reads the cookie after the
lookup.

> Store the cookie returned by ip6_dst_lookup_flow_cookie instead. Add
> ip6_dst_store_cookie for this and keep ip6_dst_store for callers that
> have no lookup cookie.
>
> Signed-off-by: Ralf Lici <ralf@mandelbit.com>

[Severity: Low]
This isn't a bug, but should this have a Fixes: tag and Cc: stable? The
commit message describes a socket that "keeps sending plaintext" when it
should be using IPsec.

The patch also calls ip6_dst_lookup_flow_cookie(). That helper comes from
the previous patch in the series ("ipv6: add ip6_dst_lookup_flow_cookie"),
which also changes ip6_dst_check() behaviour for nexthop-object dsts. So
this patch can't be backported on its own. Could the dependency be noted
for stable?

> diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
> index 95f936be32c6c..2684f6069a5e1 100644
> --- a/include/net/ip6_route.h
> +++ b/include/net/ip6_route.h

[ ... ]

> @@ -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);
> +}

[Severity: High]
Is the TCP side of this fix complete? The commit message says this
"affects TCP and connected UDP sockets alike", but tcp_v6_connect() is
the only TCP path converted. Other TCPv6 paths still call
ip6_dst_lookup_flow() and then ip6_dst_store(), so they still read the
cookie after xfrm_lookup_route() has returned.

Here is inet6_csk_route_socket() in net/ipv6/inet6_connection_sock.c:

	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);

	if (!IS_ERR(dst))
		ip6_dst_store(sk, dst, false, false);

inet6_csk_xmit() reaches this whenever __sk_dst_check(sk, np->dst_cookie)
fails. inet6_csk_update_pmtu() also reaches it through
tcp_v6_mtu_reduced(), and a remote peer can trigger that path with
ICMPv6 Packet Too Big.

inet6_sk_rebuild_header() in net/ipv6/af_inet6.c has the same
lookup/store pair. It is ipv6_specific.rebuild_header, and tcp_connect()
calls it right after tcp_v6_connect(). Suppose
ip6_dst_lookup_flow_cookie() saw the generation move and returned cookie
0. Then __sk_dst_check() fails immediately, and the lookup is redone
through the unconverted path inside the same connect() call. The
retransmit path also reaches inet6_sk_rebuild_header().

tcp_v6_syn_recv_sock() in net/ipv6/tcp_ipv6.c gets its dst from
inet6_csk_route_req(). That function calls ip6_dst_lookup_flow() without
capturing a cookie. tcp_v6_syn_recv_sock() then runs
tcp_create_openreq_child() before it does:

	ip6_dst_store(newsk, dst, false, false);

So every passively opened IPv6 TCP socket has the same window.

An xfrm policy insertion goes through
rt_genid_bump_ipv6()->__fib6_flush_trees(), which invalidates every
cached socket dst. Established sockets therefore re-route through
inet6_csk_route_socket() right after a policy change.

Suppose another policy is inserted during that re-lookup, which can
happen when an IKE daemon installs several policies back to back. Can
the plain route then be cached with the post-insertion serial? If so,
inet6_csk_xmit() would keep accepting it and send plaintext.

The commit message says ip6_dst_store is kept "for callers that have no
lookup cookie". Each of these callers does its own ip6_dst_lookup_flow(),
though, so each could use ip6_dst_lookup_flow_cookie() with
ip6_dst_store_cookie(). For the passive-open path, inet6_csk_route_req()
would need to return the cookie.

The kernel-doc of ip6_dst_lookup_flow_cookie() also says "A caller caching
the dst should use this cookie when validating it with dst_check."

[Severity: Medium]
Does the same window remain in the SCTP per-transport dst cache?
sctp_v6_get_dst() in net/sctp/ipv6.c does its route and policy lookup
through ip6_dst_lookup_flow() and stores the result in t->dst. Only after
that does it read the cookie:

	rt = dst_rt6_info(dst);
	t->dst_cookie = rt6_get_cookie(rt);

sctp_transport_dst_check() later validates t->dst against that cookie.
If an xfrm policy insertion restamps fn_sernum between the lookup and
this read, could an SCTP association over IPv6 keep using the route that
was found without the policy?

dst_cache_set_ip6() in net/core/dst_cache.c has the same pattern. IPv6
UDP tunnels and ip6_tunnel use it:

	dst_cache_per_cpu_dst_set(idst, dst,
				  rt6_get_cookie(dst_rt6_info(dst)));

The previous patch in the series describes the problem as affecting all
"callers that keep a dst in a persistent cache". Should these sites also
be converted to ip6_dst_lookup_flow_cookie(), or at least mentioned in
the commit message?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794384146.git.ralf%40mandelbit.com
Re: [PATCH net-next v2 3/3] ipv6: use the lookup cookie when caching the dst in sockets
#17003
Author: Ralf Lici
Date: Mon, 16 Nov 2026 09:02
133 lines
14727 bytes
On Sun, 15 Nov 2026 08:56:33 +0000, netdev-bot+sashiko@kernel.org wrote:
> > Store the cookie returned by ip6_dst_lookup_flow_cookie instead. Add
> > ip6_dst_store_cookie for this and keep ip6_dst_store for callers that
> > have no lookup cookie.
> >
> > Signed-off-by: Ralf Lici <ralf@mandelbit.com>
>
> [Severity: Low]
> This isn't a bug, but should this have a Fixes: tag and Cc: stable? The
> commit message describes a socket that "keeps sending plaintext" when it
> should be using IPsec.
>
> The patch also calls ip6_dst_lookup_flow_cookie(). That helper comes from
> the previous patch in the series ("ipv6: add ip6_dst_lookup_flow_cookie"),
> which also changes ip6_dst_check() behaviour for nexthop-object dsts. So
> this patch can't be backported on its own. Could the dependency be noted
> for stable?
>

The same historical and target-tree considerations discussed in my reply
to Sashiko's review in patch 1/3 apply here as well. The late cookie
sampling in the socket cache is already present in the initial Git
import. I can clarify that attribution when revising the changelog.

> > diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
> > index 95f936be32c6c..2684f6069a5e1 100644
> > --- a/include/net/ip6_route.h
> > +++ b/include/net/ip6_route.h
>
> [ ... ]
>
> > @@ -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);
> > +}
>
> [Severity: High]
> Is the TCP side of this fix complete? The commit message says this
> "affects TCP and connected UDP sockets alike", but tcp_v6_connect() is
> the only TCP path converted. Other TCPv6 paths still call
> ip6_dst_lookup_flow() and then ip6_dst_store(), so they still read the
> cookie after xfrm_lookup_route() has returned.
>
> Here is inet6_csk_route_socket() in net/ipv6/inet6_connection_sock.c:
>
> 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
>
> 	if (!IS_ERR(dst))
> 		ip6_dst_store(sk, dst, false, false);
>
> inet6_csk_xmit() reaches this whenever __sk_dst_check(sk, np->dst_cookie)
> fails. inet6_csk_update_pmtu() also reaches it through
> tcp_v6_mtu_reduced(), and a remote peer can trigger that path with
> ICMPv6 Packet Too Big.
>
> inet6_sk_rebuild_header() in net/ipv6/af_inet6.c has the same
> lookup/store pair. It is ipv6_specific.rebuild_header, and tcp_connect()
> calls it right after tcp_v6_connect(). Suppose
> ip6_dst_lookup_flow_cookie() saw the generation move and returned cookie
> 0. Then __sk_dst_check() fails immediately, and the lookup is redone
> through the unconverted path inside the same connect() call. The
> retransmit path also reaches inet6_sk_rebuild_header().
>
> tcp_v6_syn_recv_sock() in net/ipv6/tcp_ipv6.c gets its dst from
> inet6_csk_route_req(). That function calls ip6_dst_lookup_flow() without
> capturing a cookie. tcp_v6_syn_recv_sock() then runs
> tcp_create_openreq_child() before it does:
>
> 	ip6_dst_store(newsk, dst, false, false);
>
> So every passively opened IPv6 TCP socket has the same window.
>
> An xfrm policy insertion goes through
> rt_genid_bump_ipv6()->__fib6_flush_trees(), which invalidates every
> cached socket dst. Established sockets therefore re-route through
> inet6_csk_route_socket() right after a policy change.
>
> Suppose another policy is inserted during that re-lookup, which can
> happen when an IKE daemon installs several policies back to back. Can
> the plain route then be cached with the post-insertion serial? If so,
> inet6_csk_xmit() would keep accepting it and send plaintext.
>
> The commit message says ip6_dst_store is kept "for callers that have no
> lookup cookie". Each of these callers does its own ip6_dst_lookup_flow(),
> though, so each could use ip6_dst_lookup_flow_cookie() with
> ip6_dst_store_cookie(). For the passive-open path, inet6_csk_route_req()
> would need to return the cookie.
>
> The kernel-doc of ip6_dst_lookup_flow_cookie() also says "A caller caching
> the dst should use this cookie when validating it with dst_check."
>
> [Severity: Medium]
> Does the same window remain in the SCTP per-transport dst cache?
> sctp_v6_get_dst() in net/sctp/ipv6.c does its route and policy lookup
> through ip6_dst_lookup_flow() and stores the result in t->dst. Only after
> that does it read the cookie:
>
> 	rt = dst_rt6_info(dst);
> 	t->dst_cookie = rt6_get_cookie(rt);
>
> sctp_transport_dst_check() later validates t->dst against that cookie.
> If an xfrm policy insertion restamps fn_sernum between the lookup and
> this read, could an SCTP association over IPv6 keep using the route that
> was found without the policy?
>
> dst_cache_set_ip6() in net/core/dst_cache.c has the same pattern. IPv6
> UDP tunnels and ip6_tunnel use it:
>
> 	dst_cache_per_cpu_dst_set(idst, dst,
> 				  rt6_get_cookie(dst_rt6_info(dst)));
>
> The previous patch in the series describes the problem as affecting all
> "callers that keep a dst in a persistent cache". Should these sites also
> be converted to ip6_dst_lookup_flow_cookie(), or at least mentioned in
> the commit message?
>

Both points concern conversions explicitly deferred in the cover letter.
The same scope discussion in my reply to patch 2/3 applies here: these
paths are follow-up work, pending feedback on the lookup-cookie API and
its incremental introduction.

-- 
Ralf Lici
Mandelbit Srl
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads