net: avoid clobbering active cork flowi union on datagram connect
Subsystems (4)
PATCH v2 1/1 net: avoid clobbering active cork flowi union on datagram connect
ip6_datagram_dst_update() and __ip4_datagram_connect() use inet->cork.fl (and np->final in IPv6) for route lookups to avoid stack-allocated flow structures on the common connect path.
When a UDP or raw socket already has corked frames queued on sk->sk_write_queue via UDP_CORK or MSG_MORE, calling connect() (or refreshing the route in ip6_datagram_release_cb()) overwrites the active union flowi in place. On a dual-stack AF_INET6 UDP socket with a corked IPv4 datagram, an IPv6 connect() overwrites inet->cork.fl.u.ip4 with flowi6 bytes, and uncorking sends the pending IPv4 packet with the aliased IPv6 bytes as saddr without CAP_NET_RAW (and symmetrically for a corked IPv6 datagram followed by a v4-mapped connect()).
Keep using inet->cork.fl and np->final when sk->sk_write_queue is empty, and fall back to noinline_for_stack helpers with stack-allocated flow keys when corked frames are queued. Once a UDP cork finishes on a connected socket, restore the connected flow in inet->cork.fl (keeping sk_dst_cache warm on normal IPv4 and IPv6 UDP paths, and resetting sk_dst_cache only on cross-family IPv4 cork completion on a native-IPv6 socket or non-UDP datagram callers) so subsequent connected sends and L2TP encapsulation never reuse the corked flow.
---
v2:
- Restore the connected flow in inet->cork.fl when a UDP cork finishes (keeping sk_dst_cache warm on IPv4 and IPv6 UDP paths, and resetting sk_dst_cache on cross-family or non-UDP datagram cases), keeping both udp_sendmsg()'s lockless fast path and L2TP encapsulation from sending with the corked flow.
- Add Fixes: 3038eeac027d for __ip4_datagram_connect() and move the testing note below ---.
- v1: https://lore.kernel.org/netdev/[email protected]/
Found with AI-assisted code review; tested in QEMU across dual-stack IPv4/IPv6 cork + connect sequences and connected AF_INET UDP cork/send fast paths (confirmed tail-call fast path with no stack frame growth).
include/net/ipv6.h | 1 + include/net/udp.h | 21 +++++++++++++++++++++ net/ipv4/datagram.c | 32 +++++++++++++++++++++++++++----- net/ipv4/udp.c | 8 ++++++-- net/ipv6/datagram.c | 33 +++++++++++++++++++++++++++------ net/ipv6/udp.c | 19 +++++++++++++++++-- 6 files changed, 99 insertions(+), 15 deletions(-)
include/net/ipv6.h +1 -0
@@ -1151,6 +1151,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr,int ip6_datagram_connect (struct sock *sk,struct sockaddr_unsized *addr,int addr_len);int ip6_datagram_connect_v6_only (struct sock *sk,struct sockaddr_unsized *addr,int addr_len); +void ip6_datagram_flow_key_init (struct flowi6 *fl6,const struct sock *sk);int ip6_datagram_dst_update (struct sock *sk,bool fix_sk_saddr);void ip6_datagram_release_cb (struct sock *sk);
include/net/udp.h +21 -0
@@ -417,6 +417,27 @@ INDIRECT_CALLABLE_DECLARE(int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags));void udp_splice_eof (struct socket *sock);int udp_push_pending_frames (struct sock *sk); +static inline void udp_cork_restore_connected (struct sock *sk) +{ +struct inet_sock *inet =inet_sk (sk); +struct flowi4 *fl4 = &inet->cork.fl.u.ip4; + +if (sk->sk_state !=TCP_ESTABLISHED ) +return ; + +#if IS_ENABLED (CONFIG_IPV6 ) +if (sk->sk_family ==AF_INET6 && + !ipv6_addr_v4mapped (&sk->sk_v6_daddr)) { +sk_dst_reset (sk); +return ; + } +#endif +WRITE_ONCE (fl4->daddr,READ_ONCE (inet->inet_daddr)); +WRITE_ONCE (fl4->saddr, inet->inet_saddr); +WRITE_ONCE (fl4->fl4_dport, inet->inet_dport); +WRITE_ONCE (fl4->fl4_sport, inet->inet_sport); +} +void udp_flush_pending_frames (struct sock *sk);int udp_cmsg_send (struct sock *sk,struct msghdr *msg,u16 *gso_size);void udp4_hwcsum (struct sk_buff *skb,__be32 src,__be32 dst);
net/ipv4/datagram.c +27 -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,29 @@ 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; +int err; + + err =____ip4_datagram_connect (sk, uaddr, addr_len, &fl4); +if (!err && !sk_is_udp (sk)) +sk_dst_reset (sk); +return err; +} + +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/ipv4/udp.c +6 -2
@@ -1034,8 +1034,9 @@ void udp_flush_pending_frames(struct sock *sk)if (up->pending) { up->len =0 ; -WRITE_ONCE (up->pending,0 );ip_flush_pending_frames (sk); +udp_cork_restore_connected (sk); +WRITE_ONCE (up->pending,0 ); } }
@@ -1213,6 +1214,7 @@ int udp_push_pending_frames(struct sock *sk) out: up->len =0 ; +udp_cork_restore_connected (sk);WRITE_ONCE (up->pending,0 );return err; }
@@ -1496,8 +1498,10 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)udp_flush_pending_frames (sk); else if (!corkreq) err =udp_push_pending_frames (sk); - else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) + else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) { +udp_cork_restore_connected (sk);WRITE_ONCE (up->pending,0 ); + }release_sock (sk); out:
net/ipv6/datagram.c +27 -6
@@ -38,8 +38,7 @@ static bool ipv6_mapped_addr_any(const struct in6_addr *a)return ipv6_addr_v4mapped (a) && (a->s6_addr32[3 ] ==0 ); } -static void ip6_datagram_flow_key_init (struct flowi6 *fl6, -const struct sock *sk) +void ip6_datagram_flow_key_init (struct flowi6 *fl6,const struct sock *sk) {const struct inet_sock *inet =inet_sk (sk);const struct ipv6_pinfo *np =inet6_sk (sk);
@@ -69,7 +68,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 +77,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 +85,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 +117,29 @@ 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; +int err; + + err =__ip6_datagram_dst_update (sk, fix_sk_saddr, &fl6, &final); +if (!err && !sk_is_udp (sk)) +sk_dst_reset (sk); +return err; +} + +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;
net/ipv6/udp.c +17 -2
@@ -1267,6 +1267,17 @@ void udp_v6_early_demux(struct sk_buff *skb)/* * Throw away all pending data and cancel the corking. Socket is locked. */ +static void udp_v6_cork_restore_connected (struct sock *sk) +{ +if (sk->sk_state !=TCP_ESTABLISHED ) +return ; + +if (ipv6_addr_v4mapped (&sk->sk_v6_daddr)) +udp_cork_restore_connected (sk); +else +ip6_datagram_flow_key_init (&inet_sk (sk)->cork.fl.u.ip6, sk); +} +static void udp_v6_flush_pending_frames (struct sock *sk) {struct udp_sock *up =udp_sk (sk);
@@ -1275,8 +1286,9 @@ static void udp_v6_flush_pending_frames(struct sock *sk)udp_flush_pending_frames (sk); else if (up->pending) { up->len =0 ; -WRITE_ONCE (up->pending,0 );ip6_flush_pending_frames (sk); +udp_v6_cork_restore_connected (sk); +WRITE_ONCE (up->pending,0 ); } }
@@ -1463,6 +1475,7 @@ static int udp_v6_push_pending_frames(struct sock *sk) &inet_sk(sk)->cork.base); out: up->len =0 ; +udp_v6_cork_restore_connected (sk);WRITE_ONCE (up->pending,0 );return err; }
@@ -1749,8 +1762,10 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)udp_v6_flush_pending_frames (sk); else if (!corkreq) err =udp_v6_push_pending_frames (sk); - else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) + else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) { +udp_v6_cork_restore_connected (sk);WRITE_ONCE (up->pending,0 ); + }if (err >0 ) err =inet6_test_bit (RECVERR6 , sk) ?net_xmit_errno (err) :0 ;
Signature · 1 line
2.56.1.831.g720f3f5937-goog