Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net: avoid clobbering active cork flowi union on datagram connect
@ 2026-11-19 19:21 Dominik Maier
  2026-11-23 21:18 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-19 19:21 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Kuniyuki Iwashima, Simon Horman, Willem de Bruijn, netdev,
	dmnk+artist, Dominik Maier

ip6_datagram_dst_update() and __ip4_datagram_connect() reuse
inet->cork.fl (and np->final in IPv6) as scratch storage for route
lookups to avoid stack-allocated flow structures in the common connect
path.

However, a UDP or raw socket may already hold pending corked frames on
sk->sk_write_queue queued via UDP_CORK or MSG_MORE. Connecting such a
socket (or refreshing its route via ip6_datagram_release_cb()) before
flushing the corked queue overwrites the active union flowi in place.
On a dual-stack AF_INET6 UDP socket holding a corked IPv4 datagram, a
subsequent IPv6 connect() overwrites inet->cork.fl.u.ip4 with flowi6
bytes; uncorking then transmits the pending IPv4 packet using the
aliased IPv6 address bytes as flowi4.saddr, allowing unprivileged IPv4
source address spoofing without CAP_NET_RAW. The symmetric overwrite of
inet->cork.fl.u.ip4 in __ip4_datagram_connect() when an IPv6 or IPv4
cork is active predates the IPv6 change (since <= v4.19).

Keep using inet->cork.fl and np->final on the common fast path when
sk->sk_write_queue is empty so no stack canary is introduced, and fall
back to noinline_for_stack helpers with stack-allocated flow keys when
corked frames are pending.

This issue was discovered with an AI-assisted code review tool (ARTIST)
and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without
kernel modifications or error injection; a standalone reproducer is
available upon maintainers' request. The fix was compile-tested with
-Werror (confirming no stack canary on the fast path) and checked with
checkpatch.pl --strict; an equivalent fix was verified against the
reproducer in QEMU/KVM.

Fixes: 4e6c91cf60f2 ("ipv6: use inet->cork.fl.u.ip6 and np->final in ip6_datagram_dst_update()")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
 net/ipv4/datagram.c | 28 +++++++++++++++++++++++-----
 net/ipv6/datagram.c | 26 ++++++++++++++++++++++----
 2 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c
index 7d25519..4fdad1d 100644
--- a/net/ipv4/datagram.c
+++ b/net/ipv4/datagram.c
@@ -16,17 +16,17 @@
 #include <net/tcp_states.h>
 #include <net/sock_reuseport.h>
 
-int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
+static int ____ip4_datagram_connect(struct sock *sk,
+				    struct sockaddr_unsized *uaddr,
+				    int addr_len, struct flowi4 *fl4)
 {
+	struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
 	struct inet_sock *inet = inet_sk(sk);
-	struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
-	struct flowi4 *fl4;
 	struct rtable *rt;
 	__be32 saddr;
 	int oif;
 	int err;
 
-
 	if (addr_len < sizeof(*usin))
 		return -EINVAL;
 
@@ -45,7 +45,6 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int
 	} else if (!oif) {
 		oif = READ_ONCE(inet->uc_index);
 	}
-	fl4 = &inet->cork.fl.u.ip4;
 	rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr, oif,
 			      sk->sk_protocol, inet->inet_sport,
 			      usin->sin_port, sk);
@@ -82,6 +81,25 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int
 out:
 	return err;
 }
+
+static noinline_for_stack int
+__ip4_datagram_connect_corked(struct sock *sk, struct sockaddr_unsized *uaddr,
+			      int addr_len)
+{
+	struct flowi4 fl4;
+
+	return ____ip4_datagram_connect(sk, uaddr, addr_len, &fl4);
+}
+
+int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
+			   int addr_len)
+{
+	if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
+		return __ip4_datagram_connect_corked(sk, uaddr, addr_len);
+
+	return ____ip4_datagram_connect(sk, uaddr, addr_len,
+					&inet_sk(sk)->cork.fl.u.ip4);
+}
 EXPORT_SYMBOL(__ip4_datagram_connect);
 
 int ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ff..dc64235 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -69,7 +69,8 @@ static void ip6_datagram_flow_key_init(struct flowi6 *fl6,
 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
 }
 
-int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
+static int __ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr,
+				     struct flowi6 *fl6, struct in6_addr *final)
 {
 	struct ip6_flowlabel *flowlabel = NULL;
 	struct inet_sock *inet = inet_sk(sk);
@@ -77,7 +78,6 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
 	struct ipv6_txoptions *opt;
 	struct in6_addr *final_p;
 	struct dst_entry *dst;
-	struct flowi6 *fl6;
 	int err = 0;
 
 	if (inet6_test_bit(SNDFLOW, sk) &&
@@ -86,12 +86,11 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
 		if (IS_ERR(flowlabel))
 			return -EINVAL;
 	}
-	fl6 = &inet_sk(sk)->cork.fl.u.ip6;
 	ip6_datagram_flow_key_init(fl6, sk);
 
 	rcu_read_lock();
 	opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
-	final_p = fl6_update_dst(fl6, opt, &np->final);
+	final_p = fl6_update_dst(fl6, opt, final);
 	rcu_read_unlock();
 
 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
@@ -119,6 +118,25 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
 	return err;
 }
 
+static noinline_for_stack int
+ip6_datagram_dst_update_corked(struct sock *sk, bool fix_sk_saddr)
+{
+	struct in6_addr final;
+	struct flowi6 fl6;
+
+	return __ip6_datagram_dst_update(sk, fix_sk_saddr, &fl6, &final);
+}
+
+int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
+{
+	if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
+		return ip6_datagram_dst_update_corked(sk, fix_sk_saddr);
+
+	return __ip6_datagram_dst_update(sk, fix_sk_saddr,
+					 &inet_sk(sk)->cork.fl.u.ip6,
+					 &inet6_sk(sk)->final);
+}
+
 void ip6_datagram_release_cb(struct sock *sk)
 {
 	struct dst_entry *dst;
-- 
2.56.1.750.gf70dc682cf-goog


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

* Re: [PATCH net] net: avoid clobbering active cork flowi union on datagram connect
  2026-11-19 19:21 [PATCH net] net: avoid clobbering active cork flowi union on datagram connect Dominik Maier
@ 2026-11-23 21:18 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 21:18 UTC (permalink / raw)
  To: dmnk
  Cc: davem, edumazet, kuba, pabeni, kuniyu, horms,
	willemdebruijn.kernel, netdev, dmnk+artist

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 · 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] Regression on AF_INET UDP sockets: once a datagram is corked, a
  later connect() leaves the socket sending all of its connected traffic…
- [Low] The Fixes: tag does not cover the IPv4 hunk.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it comes from
  the same root cause and limits what a connect-side guard can achieve.

--

net: avoid clobbering active cork flowi union on datagram connect

When sk->sk_write_queue holds pending corked frames, this commit stops
__ip4_datagram_connect() and ip6_datagram_dst_update() from doing the route
lookup in inet->cork.fl (and np->final). New noinline_for_stack helpers
handle that case with stack-allocated flow keys. The fast path with an
empty write queue still uses the cork flow.

> The symmetric overwrite of
> inet->cork.fl.u.ip4 in __ip4_datagram_connect() when an IPv6 or IPv4
> cork is active predates the IPv6 change (since <= v4.19).

[ ... ]

> Fixes: 4e6c91cf60f2 ("ipv6: use inet->cork.fl.u.ip6 and np->final in ip6_datagram_dst_update()")
> Cc: stable@vger.kernel.org

[Severity: Low]
Should the IPv4 hunk have its own Fixes: tag?

4e6c91cf60f2 only touches net/ipv6/datagram.c. git blame traces the
fl4 = &inet->cork.fl.u.ip4 assignment in __ip4_datagram_connect() to
3038eeac027d ("ipv4: Lock socket and use cork flow in
ip4_datagram_connect().") from 2011, so "since <= v4.19" is also not quite
accurate.

With only the IPv6 tag, stable tooling would limit the backport to trees
that contain 4e6c91cf60f2. The older trees that the message describes as
affected would not get the IPv4 change. The IPv4 question below also
applies before the backport scope is widened.

> diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c
> index 7d25519a6cdd3..4fdad1d2e42f1 100644
> --- a/net/ipv4/datagram.c
> +++ b/net/ipv4/datagram.c

[ ... ]

> @@ -82,6 +81,25 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int
>  out:
>  	return err;
>  }
> +
> +static noinline_for_stack int
> +__ip4_datagram_connect_corked(struct sock *sk, struct sockaddr_unsized *uaddr,
> +			      int addr_len)
> +{
> +	struct flowi4 fl4;
> +
> +	return ____ip4_datagram_connect(sk, uaddr, addr_len, &fl4);
> +}

[Severity: High]
On a plain AF_INET UDP socket, does this leave inet->cork.fl.u.ip4 holding
the old corked destination after connect()?

The route lookup goes into the stack fl4, which is then thrown away.
inet_daddr, inet_dport and sk_dst move to the new peer. cork.fl.u.ip4
still holds what udp_sendmsg() wrote while corking:

net/ipv4/udp.c:udp_sendmsg() {
    ...
	fl4 = &inet->cork.fl.u.ip4;
	fl4->daddr = daddr;
	fl4->saddr = saddr;
	fl4->fl4_dport = dport;
	fl4->fl4_sport = inet->inet_sport;
    ...
}

The commit message calls cork.fl scratch storage for connect. For IPv4 UDP,
though, the lockless fast path in udp_sendmsg() also uses it as the
connected flow whenever the cached route is still valid:

net/ipv4/udp.c:udp_sendmsg() {
    ...
	fl4 = &inet->cork.fl.u.ip4;
    ...
	if (connected)
		rt = dst_rtable(sk_dst_check(sk, 0));

	if (!rt) {
		...
		fl4 = &fl4_stack;
		...
	}
    ...
	saddr = fl4->saddr;
	if (!ipc.addr)
		daddr = ipc.addr = fl4->daddr;
    ...
		skb = ip_make_skb(sk, fl4, ip_generic_getfrag, msg, ulen,
    ...
			err = udp_send_skb(skb, fl4, &cork);
    ...
}

ip_make_skb()->__ip_make_skb()->ip_copy_addrs(iph, fl4) fills the IP
header from that flow. udp_send_skb() takes uh->dest and the checksum
addresses from the same fl4.

The sequence would be:

  sendto(B, MSG_MORE)      cork.fl.u.ip4 = B
  connect(C)               corked branch, cork.fl.u.ip4 stays B
  flush (UDP_CORK=0 or a send without MSG_MORE)
                           udp_push_pending_frames() sends to B,
                           cork.fl untouched
  send() ...               fast path builds packets for B's address
                           and port, sent over C's route

Before this patch, connect() always rewrote cork.fl.u.ip4, so only the one
pending datagram went to the wrong place. With the patch, it looks like
every later connected send() goes to B until the route goes stale or the
socket reconnects.

The dual-stack case in the commit message doesn't seem to reach this fast
path. For v4-mapped peers, udpv6_sendmsg() always passes an explicit
sockaddr_in to udp_sendmsg(), which forces connected=0.

Would it be better to make the connected fast path in udp_sendmsg() stop
depending on inet->cork.fl.u.ip4, or to resync it after the cork is
flushed, rather than skipping the write in connect()?

> +
> +int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
> +			   int addr_len)
> +{
> +	if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
> +		return __ip4_datagram_connect_corked(sk, uaddr, addr_len);
> +
> +	return ____ip4_datagram_connect(sk, uaddr, addr_len,
> +					&inet_sk(sk)->cork.fl.u.ip4);
> +}

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. It has
the same root cause, and a guard in __ip4_datagram_connect() can't cover
it, because no connect() happens while corked:

  connect(A)               cork.fl.u.ip4 and sk_dst = A
  sendto(B, MSG_MORE)      connected=0, sk_dst left alone,
                           corking writes B into cork.fl.u.ip4
  flush                    udp_push_pending_frames() sends to B,
                           cork.fl not restored
  send()                   connected=1, sk_dst_check() returns A's
                           route, fl4 stays &inet->cork.fl.u.ip4

That last send() builds the packet with B's saddr and daddr via
ip_copy_addrs() and B's port in udp_send_skb(), and sends it over A's
route. This keeps happening until the route goes stale or the socket
reconnects. It appears to date back to around 2011 (b6f21b268026,
9cce96df5b76, e474995f290f).

None of the functions changed by this patch run in this sequence. Could a
change in udp_sendmsg()'s connected fast path fix both this and the case
above? Options would be rebuilding or checking the flow from socket state,
or falling back to fl4_stack.

The dual-stack variant can't reach this path either, since udpv6_sendmsg()
passes an explicit sockaddr_in for v4-mapped destinations.

> EXPORT_SYMBOL(__ip4_datagram_connect);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119192147.2640881-1-dmnk%40google.com

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

end of thread, other threads:[~2026-11-23 21:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:21 [PATCH net] net: avoid clobbering active cork flowi union on datagram connect Dominik Maier
2026-11-23 21:18 ` netdev-bot+sashiko

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