BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nikolay Aleksandrov" <razor@blackwall.org>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf 1/2] bpf, net: validate assigned socket on receive
Date: Mon, 23 Nov 2026 09:29:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-228972@kernel.org> (raw)
In-Reply-To: <20261123084138.723275-2-razor@blackwall.org>

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

  reply	other threads:[~2026-11-23  9:29 UTC|newest]

Thread overview: 9+ 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 ` [PATCH bpf 1/2] bpf, net: validate assigned socket on receive Nikolay Aleksandrov
2026-11-23  9:29   ` sashiko-bot [this message]
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

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=sashiko-outbox-228972@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=razor@blackwall.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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