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 --- 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 #include -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