inet6_csk_xmit(), inet6_sk_rebuild_header() and the redirect handling in tcp_v6_err() take the socket's cached route from __sk_dst_check() and use it as an IPv6 route without checking that it is one. The IPv4 ICMP handlers can cache an IPv4 route in an IPv6 socket. An ICMPv4 Fragmentation Needed makes the kernel look up the socket that sent the quoted packet by its IPv4 address fields, without checking its family, and tcp_v4_mtu_reduced() caches an rtable in it. The socket's next transmit passes that rtable to ip6_xmit(), which reads rt6i_idev 32 bytes past the end of the 176-byte object: BUG: KASAN: slab-out-of-bounds in ip6_xmit+0x141/0x1020 Read of size 8 at addr ff110000110c19d0 by task v4mapped_rtable/239 If not NULL, the value read is used as an inet6_dev pointer for a per-CPU statistics increment. This is remotely reachable in at least three ways. A TCP child that cookie_v6_check() creates from an ACK with a v4mapped source address is hashed by its IPv4 address fields (2^29 guesses for the cookie). An ordinary IPv6 connection is found the same way if its hash equals the hash of those fields (2^32 attempts, as long as nothing leaks the keys). The third way is an L2TP tunnel socket over UDP/IPv6 that is not bound to a specific address, which is found by port alone. The first was tested on an nginx server running on Ubuntu 24.04. A remote reproducer guessed the cookie in 152 minutes and the kernel panicked on a pointer it read out of bounds. The second one was also confirmed but with the keys given to the reproducer. The third one was confirmed too, no brute force required. ip6_sk_dst_check() has rejected non-IPv6 routes since commit a963a37d384d ("ipv6: ip6_sk_dst_check() must not assume ipv6 dst"). Fix this by adding inet6_sk_dst_check(), which does the same and also drops the route, and using it in those three places. I found the second and third remote routes during an LLM-based audit for sibling bugs of netdev patches with KASAN logs. Hyunwoo Kim found the first route. This patch prevents all three routes from causing the out-of-bounds read. Fixes: 80d0a69fc577 ("ipv4: Add helper inet_csk_update_pmtu().") Cc: stable@vger.kernel.org Reported-by: Hyunwoo Kim Link: https://lore.kernel.org/netdev/avFvW7nn7GIZmZ-a@v4bel/ Signed-off-by: Juan Agustin Niccolini --- I have reproducers that are available to the network maintainers on request. I have one that causes the KASAN slab-out-of-bounds when given a SYN cookie for the v4mapped tuple. I also have one that brute forces it over the network and triggers the same report, and another one that causes the oob read through route 2 when given the keys. One for the L2TP case is also available. I tested the fix on mainline 8e5ca834ee39 with KASAN, comparing kernels built with and without the patch. Without it, the reproducers for all three routes trigger the slab-out-of-bounds report in ip6_xmit(). With it, none do: the forged ICMP still reaches the socket, the IPv4 route is dropped on the next transmit, and the connection keeps working. I also measured the cost on the transmit path. With default offloads there is no measurable difference. In the worst case I could set up (an IPv6 loopback stream with offloads off and an MSS of 1440), throughput was 0.6% lower (95% interval 0.4% to 0.8%, 20 boots per kernel). Regarding Hyunwoo's patch, it fixes route 1 but not the other 2. It is still worth applying since it prevents spoofing an IPv4 address and that is a bug of its own. Regarding "as long as nothing leaks the keys" from above, net_hash_mix already leaks through an unrelated bug that I submitted a patch for before: https://lore.kernel.org/all/20261120211711.75786-1-juanagusnicco@gmail.com/ I don't know of any attacks that recover the other ones remotely. include/net/ip6_route.h | 12 ++++++++++++ net/ipv6/af_inet6.c | 2 +- net/ipv6/inet6_connection_sock.c | 2 +- net/ipv6/tcp_ipv6.c | 5 +---- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h index 95f936be32c6..526e88c00b33 100644 --- a/include/net/ip6_route.h +++ b/include/net/ip6_route.h @@ -266,6 +266,18 @@ static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst, #endif } +/* Drop the cached route if an IPv4 ICMP handler stored an IPv4 one. */ +static inline struct dst_entry *inet6_sk_dst_check(struct sock *sk) +{ + struct dst_entry *dst = __sk_dst_check(sk, inet6_sk(sk)->dst_cookie); + + if (dst && unlikely(dst->ops->family != AF_INET6)) { + __sk_dst_reset(sk); + return NULL; + } + return dst; +} + void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, const struct flowi6 *fl6); diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c index b85125c3f14b..b745f183a940 100644 --- a/net/ipv6/af_inet6.c +++ b/net/ipv6/af_inet6.c @@ -801,7 +801,7 @@ int inet6_sk_rebuild_header(struct sock *sk) struct dst_entry *dst; struct flowi6 *fl6; - dst = __sk_dst_check(sk, np->dst_cookie); + dst = inet6_sk_dst_check(sk); if (dst) return 0; diff --git a/net/ipv6/inet6_connection_sock.c b/net/ipv6/inet6_connection_sock.c index 3e4ce8cb478e..4d6f27b3e038 100644 --- a/net/ipv6/inet6_connection_sock.c +++ b/net/ipv6/inet6_connection_sock.c @@ -101,7 +101,7 @@ int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl_unused struct dst_entry *dst; int res; - dst = __sk_dst_check(sk, np->dst_cookie); + dst = inet6_sk_dst_check(sk); if (unlikely(!dst)) { dst = inet6_csk_route_socket(sk, fl6); if (IS_ERR(dst)) { diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 5c7ed38f1f56..676e1333d587 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -410,7 +410,6 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, const struct tcphdr *th = (struct tcphdr *)(skb->data+offset); struct net *net = dev_net_rcu(skb->dev); struct request_sock *fastopen; - struct ipv6_pinfo *np; struct tcp_sock *tp; __u32 seq, snd_una; struct sock *sk; @@ -470,11 +469,9 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, goto out; } - np = tcp_inet6_sk(sk); - if (type == NDISC_REDIRECT) { if (!sock_owned_by_user(sk)) { - struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie); + struct dst_entry *dst = inet6_sk_dst_check(sk); if (dst) dst->ops->redirect(dst, sk, skb); base-commit: e9d78fc6741ac06d438cf64e314c28e57014432a -- 2.53.0