* [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches
@ 2026-11-23 8:41 Nikolay Aleksandrov
2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-23 8:41 UTC (permalink / raw)
To: bpf
Cc: Nikolay Aleksandrov, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Kuniyuki Iwashima,
Willem de Bruijn, Martin KaFai Lau, Alexei Starovoitov,
Daniel Borkmann, John Fastabend, netdev
Hi,
Patch 01 fixes various mismatches (protocol, netns etc) that can happen
with assigned sockets which can lead to different corruptions. Patch 02
exercises such mismatches for IPv4 and IPv6. For more information please
check the commit messages. The problem was found during LLM review of
the sk assign egress patch-set.
Cheers,
Nik
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Simon Horman <horms@kernel.org>
CC: Kuniyuki Iwashima <kuniyu@google.com>
CC: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
CC: Martin KaFai Lau <kafai@fb.com>
CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: John Fastabend <john.fastabend@gmail.com>
CC: netdev@vger.kernel.org
Nikolay Aleksandrov (2):
bpf, net: validate assigned socket on receive
selftests/bpf: Test assigning a TCP listener to a UDP datagram
include/net/inet6_hashtables.h | 33 ++++++++++++-----
include/net/inet_hashtables.h | 34 +++++++++++++-----
include/net/inet_sock.h | 2 ++
net/ipv4/inet_hashtables.c | 13 +++++++
net/ipv4/udp.c | 2 +-
net/ipv6/udp.c | 2 +-
.../selftests/bpf/prog_tests/sk_assign.c | 36 +++++++++++++++++++
.../selftests/bpf/progs/test_sk_assign.c | 2 +-
8 files changed, 105 insertions(+), 19 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH bpf 1/2] bpf, net: validate assigned socket on receive 2026-11-23 8:41 [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov @ 2026-11-23 8:41 ` Nikolay Aleksandrov 2026-11-23 9:29 ` sashiko-bot ` (2 more replies) 2026-11-23 8:41 ` [PATCH bpf 2/2] selftests/bpf: test assigning a TCP listener to a UDP datagram Nikolay Aleksandrov 2026-11-23 9:40 ` [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov 2 siblings, 3 replies; 9+ messages in thread From: Nikolay Aleksandrov @ 2026-11-23 8:41 UTC (permalink / raw) To: bpf Cc: Nikolay Aleksandrov, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, Kuniyuki Iwashima, Willem de Bruijn, Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann, John Fastabend, netdev bpf_sk_assign accepts any hashed socket and the rx path uses it without checking against the packet which can also be rewritten after the assignment. A TCP socket can then reach UDP receive, AF_INET socket IPv6 receive, a v6only AF_INET6 socket IPv4 receive etc. A queued skb can also be delivered in another netns than the one checked at assignment, e.g. after an rx_handler forwards it to an upper device there. Check the socket where it is consumed: inet_steal_sock and inet6_steal_sock take the protocol of the caller and drop a prefetched socket on a mismatch. Like bpf_sk_lookup_assign, IPv4 also rejects v6only sockets which tcp_v6_conn_request would otherwise accept through tcp_v4_conn_request. A syncookie request socket left on the skb is released as well. The normal packet path is not affected: the checks run only after the prefetched test and the protocol is a constant at every call site. The full socket test before the listener test is dropped as only TCP accepts request and time wait sockets and they are never TCP_LISTEN. The release of a mismatched socket is intentionally out of line, when inlined it adds ~220 bytes to each of tcp_v4_rcv, tcp_v6_rcv, udp_rcv and udpv6_rcv on x86_64 for a path only a bad assignment takes. Tested on v7.4-rc3 with KASAN: a tcx ingress program on a veth assigned a tcp listener to an IPv4 udp datagram and udp receive used it: BUG: KASAN: null-ptr-deref in __udp_enqueue_schedule_skb+0x173/0x1300 Read of size 4 at addr 0000000000000008 by task test_progs/88 __udp_enqueue_schedule_skb (net/ipv4/udp.c:1697) udp_queue_rcv_one_skb (net/ipv4/udp.c:2365 net/ipv4/udp.c:2455) udp_unicast_rcv_skb (net/ipv4/udp.c:2627) udp_rcv (net/ipv4/udp.c:2687) ip_protocol_deliver_rcu (net/ipv4/ip_input.c:209) Kernel panic - not syncing: Fatal exception in interrupt The problem was found during review of bpf_sk_assign egress support patch-set. Fixes: cf7fbe660f2d ("bpf: Add socket assign support") Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org> --- CC: David S. Miller <davem@davemloft.net> CC: Eric Dumazet <edumazet@kernel.org> CC: Jakub Kicinski <kuba@kernel.org> CC: Paolo Abeni <pabeni@redhat.com> CC: Simon Horman <horms@kernel.org> CC: Kuniyuki Iwashima <kuniyu@google.com> CC: Willem de Bruijn <willemdebruijn.kernel@gmail.com> CC: Martin KaFai Lau <kafai@fb.com> CC: Alexei Starovoitov <ast@kernel.org> CC: Daniel Borkmann <daniel@iogearbox.net> CC: John Fastabend <john.fastabend@gmail.com> CC: netdev@vger.kernel.org include/net/inet6_hashtables.h | 33 +++++++++++++++++++++++++-------- include/net/inet_hashtables.h | 34 ++++++++++++++++++++++++++-------- include/net/inet_sock.h | 2 ++ net/ipv4/inet_hashtables.c | 13 +++++++++++++ net/ipv4/udp.c | 2 +- net/ipv6/udp.c | 2 +- 6 files changed, 68 insertions(+), 18 deletions(-) diff --git a/include/net/inet6_hashtables.h b/include/net/inet6_hashtables.h index 2cc5d416bbb5..9733d5b84bda 100644 --- a/include/net/inet6_hashtables.h +++ b/include/net/inet6_hashtables.h @@ -102,11 +102,23 @@ static inline struct sock *__inet6_lookup(const struct net *net, daddr, hnum, dif, sdif); } +static inline bool inet6_prefetched_sk_mismatch(const struct sock *sk, + const struct net *net, + u8 protocol) +{ + if (unlikely(sk->sk_family != AF_INET6 || !net_eq(sock_net(sk), net))) + return true; + if (!sk_fullsock(sk)) + return unlikely(protocol != IPPROTO_TCP); + return unlikely(sk->sk_protocol != protocol); +} + static inline struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff, const struct in6_addr *saddr, const __be16 sport, const struct in6_addr *daddr, const __be16 dport, - bool *refcounted, inet6_ehashfn_t *ehashfn) + bool *refcounted, inet6_ehashfn_t *ehashfn, + u8 protocol) { struct sock *sk, *reuse_sk; bool prefetched; @@ -115,16 +127,16 @@ struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff, if (!sk) return NULL; - if (!prefetched || !sk_fullsock(sk)) + if (!prefetched) return sk; - if (sk->sk_protocol == IPPROTO_TCP) { + if (unlikely(inet6_prefetched_sk_mismatch(sk, net, protocol))) + goto mismatch; + + if (protocol == IPPROTO_TCP) { if (sk->sk_state != TCP_LISTEN) return sk; - } else if (sk->sk_protocol == IPPROTO_UDP) { - if (sk->sk_state != TCP_CLOSE) - return sk; - } else { + } else if (sk->sk_state != TCP_CLOSE) { return sk; } @@ -140,6 +152,11 @@ struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff, WARN_ON_ONCE(*refcounted); return reuse_sk; + +mismatch: + inet_prefetched_sk_release(skb, sk, *refcounted); + *refcounted = false; + return NULL; } static inline struct sock *__inet6_lookup_skb(struct sk_buff *skb, int doff, @@ -153,7 +170,7 @@ static inline struct sock *__inet6_lookup_skb(struct sk_buff *skb, int doff, struct sock *sk; sk = inet6_steal_sock(net, skb, doff, &ip6h->saddr, sport, &ip6h->daddr, dport, - refcounted, inet6_ehashfn); + refcounted, inet6_ehashfn, IPPROTO_TCP); if (IS_ERR(sk)) return NULL; if (sk) diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h index 44b7e39a1245..ad8a7af17ddb 100644 --- a/include/net/inet_hashtables.h +++ b/include/net/inet_hashtables.h @@ -444,11 +444,24 @@ static inline struct sock *inet_lookup(struct net *net, return sk; } +static inline bool inet_prefetched_sk_mismatch(const struct sock *sk, + const struct net *net, + u8 protocol) +{ + if (unlikely(!net_eq(sock_net(sk), net))) + return true; + if (!sk_fullsock(sk)) + return unlikely(protocol != IPPROTO_TCP); + return unlikely(sk->sk_protocol != protocol || + (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))); +} + static inline struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff, const __be32 saddr, const __be16 sport, const __be32 daddr, const __be16 dport, - bool *refcounted, inet_ehashfn_t *ehashfn) + bool *refcounted, inet_ehashfn_t *ehashfn, + u8 protocol) { struct sock *sk, *reuse_sk; bool prefetched; @@ -457,16 +470,16 @@ struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff, if (!sk) return NULL; - if (!prefetched || !sk_fullsock(sk)) + if (!prefetched) return sk; - if (sk->sk_protocol == IPPROTO_TCP) { + if (unlikely(inet_prefetched_sk_mismatch(sk, net, protocol))) + goto mismatch; + + if (protocol == IPPROTO_TCP) { if (sk->sk_state != TCP_LISTEN) return sk; - } else if (sk->sk_protocol == IPPROTO_UDP) { - if (sk->sk_state != TCP_CLOSE) - return sk; - } else { + } else if (sk->sk_state != TCP_CLOSE) { return sk; } @@ -482,6 +495,11 @@ struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff, WARN_ON_ONCE(*refcounted); return reuse_sk; + +mismatch: + inet_prefetched_sk_release(skb, sk, *refcounted); + *refcounted = false; + return NULL; } static inline struct sock *__inet_lookup_skb(struct sk_buff *skb, @@ -496,7 +514,7 @@ static inline struct sock *__inet_lookup_skb(struct sk_buff *skb, struct sock *sk; sk = inet_steal_sock(net, skb, doff, iph->saddr, sport, iph->daddr, dport, - refcounted, inet_ehashfn); + refcounted, inet_ehashfn, IPPROTO_TCP); if (IS_ERR(sk)) return NULL; if (sk) diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h index 24cbabc24582..840ec8495f51 100644 --- a/include/net/inet_sock.h +++ b/include/net/inet_sock.h @@ -403,6 +403,8 @@ static inline unsigned int __inet_ehashfn(const __be32 laddr, struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener, bool attach_listener); +void inet_prefetched_sk_release(struct sk_buff *skb, struct sock *sk, + bool refcounted); static inline __u8 inet_sk_flowi_flags(const struct sock *sk) { diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 860145d810ed..fbd0e0e67798 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -521,6 +521,19 @@ void sock_gen_put(struct sock *sk) } EXPORT_SYMBOL_GPL(sock_gen_put); +/* release a prefetched socket that won't be used, drop the reference + * on @sk if @refcounted is set and orphan the skb to release any socket + * still attached to it (e.g. bpf syncookie request sock) + */ +void inet_prefetched_sk_release(struct sk_buff *skb, struct sock *sk, + bool refcounted) +{ + if (refcounted) + sock_gen_put(sk); + skb_orphan(skb); +} +EXPORT_SYMBOL_GPL(inet_prefetched_sk_release); + void sock_edemux(struct sk_buff *skb) { sock_gen_put(skb->sk); diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 2e5562f353f8..6841e447a14f 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2673,7 +2673,7 @@ int udp_rcv(struct sk_buff *skb) goto csum_error; sk = inet_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest, - &refcounted, udp_ehashfn); + &refcounted, udp_ehashfn, IPPROTO_UDP); if (IS_ERR(sk)) goto no_sk; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index a6886a0432f9..f51f2495f103 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1116,7 +1116,7 @@ INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb) /* Check if the socket is already available, e.g. due to early demux */ sk = inet6_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest, - &refcounted, udp6_ehashfn); + &refcounted, udp6_ehashfn, IPPROTO_UDP); if (IS_ERR(sk)) goto no_sk; -- 2.47.3 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive 2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov @ 2026-11-23 9:29 ` sashiko-bot 2026-11-23 9:39 ` Nikolay Aleksandrov 2026-11-23 9:38 ` bot+bpf-ci 2026-11-23 9:41 ` Jiayuan Chen 2 siblings, 1 reply; 9+ messages in thread From: sashiko-bot @ 2026-11-23 9:29 UTC (permalink / raw) To: Nikolay Aleksandrov, bpf Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] net: missing family check for non-fullsocks in inet[6]_prefetched_sk_mismatch() - [High] net: missing v4-mapped check for AF_INET6 sockets in inet[6]_prefetched_sk_mismatch() - [High] net: unvalidated syncookie request_sock left on skb->sk in inet[6]_steal_sock() - [Medium] bpf, net: sk mismatch check bypassed in inet_steal_sock() after __nf_queue() -- commit 2c31081df7649a22a023cb4c1f5a5c68555be5ac Author: Nikolay Aleksandrov <razor@blackwall.org> bpf, net: validate assigned socket on receive This commit validates prefetched sockets in inet_steal_sock() and inet6_steal_sock() against the caller's network namespace, address family, and transport protocol, releasing the socket on mismatch. > diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h > index 44b7e39a1245b..ad8a7af17ddbc 100644 > --- a/include/net/inet_hashtables.h > +++ b/include/net/inet_hashtables.h > @@ -444,11 +444,24 @@ static inline struct sock *inet_lookup(struct net *net, > return sk; > } > > +static inline bool inet_prefetched_sk_mismatch(const struct sock *sk, > + const struct net *net, > + u8 protocol) > +{ > + if (unlikely(!net_eq(sock_net(sk), net))) > + return true; > + if (!sk_fullsock(sk)) > + return unlikely(protocol != IPPROTO_TCP); [Severity: High] Does returning early for !sk_fullsock(sk) allow a request_sock or timewait socket of the opposite address family through? In inet_prefetched_sk_mismatch(), a native IPv6 TCP_NEW_SYN_RECV request_sock (with rsk_ops == &tcp6_request_sock_ops) or IPv6 TCP_TIME_WAIT socket passes without any family check. Conversely, in inet6_prefetched_sk_mismatch(), checking sk->sk_family != AF_INET6 is not sufficient for non-fullsocks because an IPv4 request_sock or timewait socket created on a dual-stack AF_INET6 listener inherits sk_family = AF_INET6 from sk_listener->sk_family in inet_reqsk_alloc() while using &tcp_request_sock_ops and storing struct ip_options_rcu in ireq->ireq_opt. When such an IPv4 request_sock is delivered to tcp_v6_rcv() -> tcp_check_req(), tcp_v6_syn_recv_sock() reads ireq->ipv6_opt (which shares a union with ireq->ireq_opt in struct inet_request_sock) and passes the struct ip_options_rcu pointer to ipv6_dup_options(). [Severity: Critical] This problem wasn't introduced by this patch, but in bpf_sk_assign() [1] can an unreferenced BTF socket pointer be unhashed, freed, and reallocated from a SLAB_TYPESAFE_BY_RCU cache between the pre-checks and refcount_inc_not_zero_acquire()? net/core/filter.c:bpf_sk_assign() { ... if (unlikely(dev_net(skb->dev) != sock_net(sk))) return -ENETUNREACH; if (sk_unhashed(sk)) return -EOPNOTSUPP; if (sk_is_refcounted(sk) && unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt))) return -ENOENT; } CPU0 (bpf_sk_assign) CPU1 -------------------- ---- checks sock_net(sk) checks !sk_unhashed(sk) reqsk unhashed and freed reallocated in tcp_get_cookie_sock(): req->rsk_listener = NULL refcount_set(&req->rsk_refcnt, 1) refcount_inc_not_zero_acquire() skb->sk = sk When tcp_v4_rcv() or tcp_v6_rcv() steals skb->sk, inet_steal_sock() accepts the TCP_NEW_SYN_RECV request_sock and tcp_v4_rcv() dereferences the NULL rsk_listener pointer. A reallocation during sk_clone() while sock_copy() temporarily has SOCK_RCU_FREE set before inet_csk_clone_lock() clears it also causes sk_is_refcounted(sk) to skip taking a reference that sock_pfree() or skb_steal_sock() later drops, underflowing sk_refcnt. > + return unlikely(sk->sk_protocol != protocol || > + (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))); > +} [Severity: High] Can a dual-stack AF_INET6 socket connected to a native IPv6 address still pass inet_prefetched_sk_mismatch(), and can an IPv4-mapped AF_INET6 socket still pass inet6_prefetched_sk_mismatch()? Unlike bpf_sk_lookup_assign(), bpf_sk_assign() accepts connected and connecting sockets. When ipv6_only_sock(sk) is 0, an AF_INET6 TCP socket connected to a native IPv6 address has icsk_af_ops = &ipv6_specific (.sk_rx_dst_set = inet6_sk_rx_dst_set) and sk_backlog_rcv = tcp_v6_do_rcv. If it is assigned to an IPv4 packet, inet_prefetched_sk_mismatch() allows it into tcp_v4_rcv(), where tcp_v4_do_rcv() calls inet6_sk_rx_dst_set() on the IPv4 skb, casting the IPv4 struct rtable to struct rt6_info in rt6_get_cookie(), or queues the IPv4 skb to sk_backlog for tcp_v6_do_rcv(). Similarly, an AF_INET6 socket connected to an IPv4-mapped address has sk_family == AF_INET6 with icsk_af_ops = &ipv6_mapped and sk_backlog_rcv = tcp_v4_do_rcv, so inet6_prefetched_sk_mismatch() allows it into tcp_v6_rcv(). > static inline > struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff, > const __be32 saddr, const __be16 sport, > const __be32 daddr, const __be16 dport, > - bool *refcounted, inet_ehashfn_t *ehashfn) > + bool *refcounted, inet_ehashfn_t *ehashfn, > + u8 protocol) > { > struct sock *sk, *reuse_sk; > bool prefetched; > > sk = skb_steal_sock(skb, refcounted, &prefetched); > if (!sk) > return NULL; > > - if (!prefetched || !sk_fullsock(sk)) > + if (!prefetched) > return sk; [Severity: Medium] Does returning when !prefetched is true bypass the mismatch check after a packet passes through NFQUEUE? When a packet carrying a prefetched SOCK_RCU_FREE socket (such as a TCP_LISTEN or UDP socket) is queued by __nf_queue(), __nf_queue() takes a reference on sk and replaces skb->destructor = sock_pfree with sock_edemux: net/netfilter/nf_queue.c:__nf_queue() { ... if (skb_sk_is_prefetched(skb)) { struct sock *sk = skb->sk; if (!sk_is_refcounted(sk)) { if (!refcount_inc_not_zero(&sk->sk_refcnt)) return -ENOTCONN; /* drop refcount on skb_orphan */ skb->destructor = sock_edemux; } } ... } On reinjection, skb_steal_sock() sees skb->destructor != sock_pfree and sets prefetched = false, so inet_steal_sock() and inet6_steal_sock() return the unvalidated socket before running inet[6]_prefetched_sk_mismatch(). > - if (sk->sk_protocol == IPPROTO_TCP) { > + if (unlikely(inet_prefetched_sk_mismatch(sk, net, protocol))) > + goto mismatch; [Severity: High] When skb->sk is a syncookie request_sock attached by bpf_sk_assign_tcp_reqsk(), skb_steal_sock() returns req->rsk_listener as sk while leaving the request_sock on skb->sk. Does validating only the listener sk leave a cross-family syncookie request_sock attached to skb->sk? If bpf_sk_assign_tcp_reqsk() attaches an IPv6 syncookie request_sock (rsk_ops == &tcp6_request_sock_ops) with a dual-stack AF_INET6 listener and the packet is then decapsulated or rewritten to IPv4, inet_prefetched_sk_mismatch() accepts the listener sk and leaves the IPv6 request_sock on skb->sk. cookie_v4_check() then stores an IPv4 struct ip_options_rcu pointer into ireq->ireq_opt, and when the request_sock is freed, tcp_v6_reqsk_destructor() calls kfree_rcu(inet_rsk(req)->ipv6_opt, rcu), writing struct rcu_head at the ipv6_txoptions offset (48) out of bounds of the ip_options_rcu allocation. [1] https://sashiko.dev/#/bug/linux-a29ff632-3911-4094-af19-2d49a80d3698 -- Sashiko AI review · https://sashiko.dev/#/patchset/20261123084138.723275-1-razor@blackwall.org?part=1 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive 2026-11-23 9:29 ` sashiko-bot @ 2026-11-23 9:39 ` Nikolay Aleksandrov 0 siblings, 0 replies; 9+ messages in thread From: Nikolay Aleksandrov @ 2026-11-23 9:39 UTC (permalink / raw) To: sashiko-reviews, bpf On 23/11/2026 11:29, sashiko-bot@kernel.org wrote: > Thank you for your contribution! Sashiko AI review found 4 potential > issues to consider: > - [High] net: missing family check for non-fullsocks in > inet[6]_prefetched_sk_mismatch() > - [High] net: missing v4-mapped check for AF_INET6 sockets in > inet[6]_prefetched_sk_mismatch() > - [High] net: unvalidated syncookie request_sock left on skb->sk in > inet[6]_steal_sock() Some of these are more valid mismatches that can happen, I will address them in v2. The critical issue below is pre-existing and will be fixed separately. [snip] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive 2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov 2026-11-23 9:29 ` sashiko-bot @ 2026-11-23 9:38 ` bot+bpf-ci 2026-11-23 9:41 ` Jiayuan Chen 2 siblings, 0 replies; 9+ messages in thread From: bot+bpf-ci @ 2026-11-23 9:38 UTC (permalink / raw) To: razor, bpf Cc: razor, davem, edumazet, kuba, pabeni, horms, kuniyu, willemdebruijn.kernel, kafai, ast, daniel, john.fastabend, netdev, ast, andrii, daniel, martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai [-- Attachment #1: Type: text/plain, Size: 3840 bytes --] > bpf, net: validate assigned socket on receive TL;DR: - Raw sockets with matching sk_protocol may pass the new check, possibly causing type confusion and out-of-bounds writes in UDP/TCP receive - Changelog says normal path is unaffected, but early-demuxed UDP packets run the new checks > The normal packet path is not affected: the checks run only after the > prefetched test and the protocol is a constant at every call site. The commit message says the normal packet path is not affected because the checks run only after the prefetched test. Is that accurate for UDP? skb_sk_is_prefetched() is skb->destructor == sock_pfree, and sock_pfree is also installed by udp_v4_early_demux() and udp_v6_early_demux(), which run on ordinary traffic when net.ipv4.udp_early_demux is enabled (the default). So early-demuxed UDP packets do reach inet_prefetched_sk_mismatch() or inet6_prefetched_sk_mismatch() in udp_rcv() and udpv6_rcv(). The early demux lookups already enforce netns and family, so no valid socket looks to be rejected, but could the changelog say that these packets run the new checks and why that is acceptable? > diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h > --- a/include/net/inet_hashtables.h > +++ b/include/net/inet_hashtables.h [ ... ] > @@ -444,11 +444,24 @@ static inline struct sock *inet_lookup(struct net *net, > return sk; > } > > +static inline bool inet_prefetched_sk_mismatch(const struct sock *sk, > + const struct net *net, > + u8 protocol) > +{ > + if (unlikely(!net_eq(sock_net(sk), net))) > + return true; > + if (!sk_fullsock(sk)) > + return unlikely(protocol != IPPROTO_TCP); > + return unlikely(sk->sk_protocol != protocol || > + (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))); > +} Does matching sk_protocol prove that this is really a TCP or UDP socket? For SOCK_RAW, inet_create() stores the user-chosen protocol in sk->sk_protocol, so socket(AF_INET, SOCK_RAW, IPPROTO_UDP) has sk_protocol == IPPROTO_UDP and sk_family == AF_INET. raw_hash_sk() hashes it and sets SOCK_RCU_FREE, so bpf_sk_assign() accepts it without a reference. A tc program can obtain such a socket without a lookup helper. On veth_xmit() -> __dev_forward_skb() -> skb_scrub_packet() the skb is not orphaned, so at the peer's tc ingress skb->sk is still the sender's raw socket, and __sk_buff->sk can be passed to bpf_sk_assign(). Then in udp_rcv(): inet_steal_sock() inet_prefetched_sk_mismatch() /* netns ok, full sock, sk_protocol == IPPROTO_UDP, not AF_INET6 */ passes, sk_state is TCP_CLOSE and sk_reuseport is clear, so the raw_sock is returned. udp_unicast_rcv_skb() and udp_queue_rcv_one_skb() then use it as a struct udp_sock, which is larger than raw_sock. That looks like the same type confusion as the KASAN report in the commit message. The same applies to SOCK_RAW with IPPROTO_TCP on the TCP receive path: inet_steal_sock() returns it because sk_state != TCP_LISTEN, and tcp_v4_rcv() then calls tcp_segs_in(tcp_sk(sk), skb), which writes beyond the raw_sock. AF_INET6 raw sockets pass inet6_prefetched_sk_mismatch() in the same way. The old code had no check at all, but the stated goal is to drop a prefetched socket that does not match the receiving protocol. Should the full socket branch also require sk_type == SOCK_STREAM for TCP and SOCK_DGRAM for UDP, and an inet family on IPv4? sk_is_tcp() and sk_is_udp() already check family, type and protocol together, and bpf_sk_assign_tcp_reqsk() checks sk->sk_type != SOCK_STREAM. [ ... ] --- AI reviewed your patch. Please fix the bug or email reply why it's not a bug. See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45143832636 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive 2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov 2026-11-23 9:29 ` sashiko-bot 2026-11-23 9:38 ` bot+bpf-ci @ 2026-11-23 9:41 ` Jiayuan Chen 2026-11-23 9:46 ` Nikolay Aleksandrov 2 siblings, 1 reply; 9+ messages in thread From: Jiayuan Chen @ 2026-11-23 9:41 UTC (permalink / raw) To: Nikolay Aleksandrov, bpf Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, Kuniyuki Iwashima, Willem de Bruijn, Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann, John Fastabend, netdev On 11/23/26 4:41 PM, Nikolay Aleksandrov wrote: > bpf_sk_assign accepts any hashed socket and the rx path uses it without > checking against the packet which can also be rewritten after the > assignment. A TCP socket can then reach UDP receive, AF_INET socket > IPv6 receive, a v6only AF_INET6 socket IPv4 receive etc. A queued skb can > also be delivered in another netns than the one checked at assignment, e.g. > after an rx_handler forwards it to an upper device there. > > Check the socket where it is consumed: inet_steal_sock and > inet6_steal_sock take the protocol of the caller and drop a prefetched > socket on a mismatch. Like bpf_sk_lookup_assign, IPv4 also rejects v6only > sockets which tcp_v6_conn_request would otherwise accept through > tcp_v4_conn_request. A syncookie request socket left on the skb is released > as well. Some old discussion is here: https://lore.kernel.org/netdev/20260403015851.148209-1-jiayuan.chen@linux.dev/ https://lore.kernel.org/netdev/20260608125846.157004-1-jiayuan.chen@linux.dev/ But the situation will change as time goes on. Hope the issue can be fixed. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive 2026-11-23 9:41 ` Jiayuan Chen @ 2026-11-23 9:46 ` Nikolay Aleksandrov 0 siblings, 0 replies; 9+ messages in thread From: Nikolay Aleksandrov @ 2026-11-23 9:46 UTC (permalink / raw) To: Jiayuan Chen, bpf Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, Kuniyuki Iwashima, Willem de Bruijn, Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann, John Fastabend, netdev On 23/11/2026 11:41, Jiayuan Chen wrote: > > > On 11/23/26 4:41 PM, Nikolay Aleksandrov wrote: >> bpf_sk_assign accepts any hashed socket and the rx path uses it without >> checking against the packet which can also be rewritten after the >> assignment. A TCP socket can then reach UDP receive, AF_INET socket >> IPv6 receive, a v6only AF_INET6 socket IPv4 receive etc. A queued skb can >> also be delivered in another netns than the one checked at assignment, e.g. >> after an rx_handler forwards it to an upper device there. >> >> Check the socket where it is consumed: inet_steal_sock and >> inet6_steal_sock take the protocol of the caller and drop a prefetched >> socket on a mismatch. Like bpf_sk_lookup_assign, IPv4 also rejects v6only >> sockets which tcp_v6_conn_request would otherwise accept through >> tcp_v4_conn_request. A syncookie request socket left on the skb is released >> as well. > > > Some old discussion is here: > https://lore.kernel.org/netdev/20260403015851.148209-1-jiayuan.chen@linux.dev/ > https://lore.kernel.org/netdev/20260608125846.157004-1-jiayuan.chen@linux.dev/ > > But the situation will change as time goes on. Hope the issue can be fixed. > Ah! Thanks a lot for the pointers, I see. I can just document this as a pre-existing problem in my patch-set and leave it for the time being, I don't see how it can be hit by accident, someone has to be malicious to do it and I think Alexei has a good point that there will always be a way to crash the kernel. Sorry for the noise. Thanks, Nik ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH bpf 2/2] selftests/bpf: test assigning a TCP listener to a UDP datagram 2026-11-23 8:41 [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov 2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov @ 2026-11-23 8:41 ` Nikolay Aleksandrov 2026-11-23 9:40 ` [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov 2 siblings, 0 replies; 9+ messages in thread From: Nikolay Aleksandrov @ 2026-11-23 8:41 UTC (permalink / raw) To: bpf Cc: Nikolay Aleksandrov, Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann, John Fastabend, netdev Assign the TCP listener from server_map to a UDP datagram for an unbound port. Without "bpf, net: validate assigned socket on receive" UDP receive uses the TCP socket and the kernel crashes. With it the datagram gets the normal lookup and the client sees ECONNREFUSED. Let packets the program does not parse pass, so the ICMP error reaches the client. Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org> --- CC: Martin KaFai Lau <kafai@fb.com> CC: Alexei Starovoitov <ast@kernel.org> CC: Daniel Borkmann <daniel@iogearbox.net> CC: John Fastabend <john.fastabend@gmail.com> CC: netdev@vger.kernel.org .../selftests/bpf/prog_tests/sk_assign.c | 36 +++++++++++++++++++ .../selftests/bpf/progs/test_sk_assign.c | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c index 10a0ab954b8a..804087a19aa9 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c @@ -199,6 +199,37 @@ prepare_addr(struct sockaddr *addr, int family, __u16 port, bool rewrite_addr) } } +static void +run_udp_to_tcp_listener(int server_map, int family) +{ + struct sockaddr_storage addr; + char buf[] = "testing"; + const int zero = 0; + int client = -1; + socklen_t len; + __s64 server; + + len = family == AF_INET ? sizeof(struct sockaddr_in) : + sizeof(struct sockaddr_in6); + prepare_addr((struct sockaddr *)&addr, family, BIND_PORT, false); + server = start_server_addr(SOCK_STREAM, &addr, len, NULL); + if (!ASSERT_GE(server, 0, "start_server_addr")) + return; + if (!ASSERT_OK(bpf_map_update_elem(server_map, &zero, &server, BPF_ANY), + "update_server_map")) + goto out; + + prepare_addr((struct sockaddr *)&addr, family, CONNECT_PORT, false); + client = connect_to_addr(SOCK_DGRAM, &addr, len, NULL); + if (ASSERT_GE(client, 0, "connect_to_addr") && + ASSERT_EQ(write(client, buf, sizeof(buf)), sizeof(buf), "write") && + ASSERT_LT(recv(client, buf, sizeof(buf), 0), 0, "recv")) + ASSERT_EQ(errno, ECONNREFUSED, "recv_errno"); + close(client); +out: + close(server); +} + struct test_sk_cfg { const char *name; int family; @@ -255,6 +286,11 @@ void test_sk_assign(void) goto cleanup; } + if (test__start_subtest("ipv4 udp to tcp listener")) + run_udp_to_tcp_listener(server_map, AF_INET); + if (test__start_subtest("ipv6 udp to tcp listener")) + run_udp_to_tcp_listener(server_map, AF_INET6); + for (i = 0; i < ARRAY_SIZE(tests) && !READ_ONCE(stop); i++) { struct test_sk_cfg *test = &tests[i]; const struct sockaddr *addr; diff --git a/tools/testing/selftests/bpf/progs/test_sk_assign.c b/tools/testing/selftests/bpf/progs/test_sk_assign.c index 3079244c7f96..4e8ae7b7d297 100644 --- a/tools/testing/selftests/bpf/progs/test_sk_assign.c +++ b/tools/testing/selftests/bpf/progs/test_sk_assign.c @@ -178,7 +178,7 @@ int bpf_sk_assign_test(struct __sk_buff *skb) tuple = get_tuple(skb, &ipv4, &tcp); if (!tuple) - return TC_ACT_SHOT; + return TC_ACT_OK; /* Note that the verifier socket return type for bpf_skc_lookup_tcp() * differs from bpf_sk_lookup_udp(), so even though the C-level type is -- 2.47.3 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches 2026-11-23 8:41 [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov 2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov 2026-11-23 8:41 ` [PATCH bpf 2/2] selftests/bpf: test assigning a TCP listener to a UDP datagram Nikolay Aleksandrov @ 2026-11-23 9:40 ` Nikolay Aleksandrov 2 siblings, 0 replies; 9+ messages in thread From: Nikolay Aleksandrov @ 2026-11-23 9:40 UTC (permalink / raw) To: bpf Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, Kuniyuki Iwashima, Willem de Bruijn, Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann, John Fastabend, netdev On 23/11/2026 10:41, Nikolay Aleksandrov wrote: > Hi, > Patch 01 fixes various mismatches (protocol, netns etc) that can happen > with assigned sockets which can lead to different corruptions. Patch 02 > exercises such mismatches for IPv4 and IPv6. For more information please > check the commit messages. The problem was found during LLM review of > the sk assign egress patch-set. > > Cheers, > Nik > > CC: David S. Miller <davem@davemloft.net> > CC: Eric Dumazet <edumazet@kernel.org> > CC: Jakub Kicinski <kuba@kernel.org> > CC: Paolo Abeni <pabeni@redhat.com> > CC: Simon Horman <horms@kernel.org> > CC: Kuniyuki Iwashima <kuniyu@google.com> > CC: Willem de Bruijn <willemdebruijn.kernel@gmail.com> > CC: Martin KaFai Lau <kafai@fb.com> > CC: Alexei Starovoitov <ast@kernel.org> > CC: Daniel Borkmann <daniel@iogearbox.net> > CC: John Fastabend <john.fastabend@gmail.com> > CC: netdev@vger.kernel.org > > Nikolay Aleksandrov (2): > bpf, net: validate assigned socket on receive > selftests/bpf: Test assigning a TCP listener to a UDP datagram > > include/net/inet6_hashtables.h | 33 ++++++++++++----- > include/net/inet_hashtables.h | 34 +++++++++++++----- > include/net/inet_sock.h | 2 ++ > net/ipv4/inet_hashtables.c | 13 +++++++ > net/ipv4/udp.c | 2 +- > net/ipv6/udp.c | 2 +- > .../selftests/bpf/prog_tests/sk_assign.c | 36 +++++++++++++++++++ > .../selftests/bpf/progs/test_sk_assign.c | 2 +- > 8 files changed, 105 insertions(+), 19 deletions(-) > Sashiko found a few more mismatch cases that can happen, I will address them in v2. Thanks, Nik --- pw-bot: cr ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-23 9:46 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-23 8:41 [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov 2026-11-23 8:41 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov 2026-11-23 9:29 ` sashiko-bot 2026-11-23 9:39 ` Nikolay Aleksandrov 2026-11-23 9:38 ` bot+bpf-ci 2026-11-23 9:41 ` Jiayuan Chen 2026-11-23 9:46 ` Nikolay Aleksandrov 2026-11-23 8:41 ` [PATCH bpf 2/2] selftests/bpf: test assigning a TCP listener to a UDP datagram Nikolay Aleksandrov 2026-11-23 9:40 ` [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox