Netdev List
 help / color / mirror / Atom feed
From: Nikolay Aleksandrov <razor@blackwall.org>
To: bpf@vger.kernel.org
Cc: Nikolay Aleksandrov <razor@blackwall.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	Martin KaFai Lau <kafai@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	netdev@vger.kernel.org
Subject: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive
Date: Mon, 23 Nov 2026 10:41:37 +0200	[thread overview]
Message-ID: <20261123084138.723275-2-razor@blackwall.org> (raw)
In-Reply-To: <20261123084138.723275-1-razor@blackwall.org>

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


  reply	other threads:[~2026-11-23  8:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  8:41 [PATCH bpf 0/2] bpf, net: fix assigned socket mismatches Nikolay Aleksandrov
2026-11-23  8:41 ` Nikolay Aleksandrov [this message]
2026-11-23  8:41 ` [PATCH bpf 2/2] selftests/bpf: test assigning a TCP listener to a UDP datagram Nikolay Aleksandrov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123084138.723275-2-razor@blackwall.org \
    --to=razor@blackwall.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemdebruijn.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox