net: avoid clobbering active cork flowi union on datagram connect
Subsystems (2)
PATCH 1/1 net: avoid clobbering active cork flowi union on datagram connect
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.
--- net/ipv4/datagram.c | 28 +++++++++++++++++++++++----- net/ipv6/datagram.c | 26 ++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-)
net/ipv4/datagram.c +23 -5
@@ -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)
net/ipv6/datagram.c +22 -4
@@ -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;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewThank 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: 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.
Quoted 3 lines
The symmetric overwrite of
inet->cork.fl.u.ip4in__ip4_datagram_connect()when an IPv6 or IPv4 cork is active predates the IPv6 change (since <= v4.19).
[ ... ]
Quoted 2 lines
Fixes 4e6c91cf60f2 ("ipv6: use inet->cork.fl.u.ip6 and np->final in ip6_datagram_dst_update()")
[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.
Quoted 4 lines
net/ipv4/datagram.c +0
-0
[ ... ]
net/ipv4/datagram.c int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int · line 92
Quoted 13 lines
net/ipv4/datagram.c +9
-0@@ -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()?
net/ipv4/datagram.c int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int · line 102
Quoted 10 lines
net/ipv4/datagram.c +10
-0+ +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.
Quoted 1 line
net/ipv4/datagram.c +0
-0EXPORT_SYMBOL (__ip4_datagram_connect);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119192147.2640881-1-dmnk%40google.com