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-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress
Date: Thu, 19 Nov 2026 15:24:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-223378@kernel.org> (raw)
In-Reply-To: <20261119142231.2612515-5-razor@blackwall.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] bpf: BUG_ON(skb->sk) in skb_orphan() via bpf_sk_assign() in
  bpf_prog_test_run_skb()
- [High] bpf: cross-netns socket leak on tc egress in bpf_sk_assign()
- [High] net: uncharged sock_wfree() on frag_list after egress
  bpf_sk_assign()
- [High] bpf: sock_pfree_ref ownership and sk_wmem_alloc corruption on
  egress
--

commit e7a6cd63b8b9259d47c7f6ec283ab9c14f268028
Author: Daniel Borkmann <daniel@iogearbox.net>

bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress

This commit removes the sk_is_refcounted() restriction in bpf_sk_assign()
on TC egress so that full SOCK_RCU_FREE sockets, such as TCP listeners,
can be assigned with an skb-owned reference.

> diff --git a/net/core/filter.c b/net/core/filter.c
> index feeb752f7a008..4dea2cc84712a 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -8123,8 +8123,7 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
>  
>  	if (!sk || flags != 0)
>  		return -EINVAL;
> -	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
> -		       skb_is_decrypted(skb)))
> +	if (egress && (!sk_fullsock(sk) || skb_is_decrypted(skb)))
>  		return -EOPNOTSUPP;
>  	refcounted = egress || sk_is_refcounted(sk);
>  	if (unlikely(dev_net(skb->dev) != sock_net(sk)))

[Severity: High]
Can calling bpf_sk_assign() during BPF_PROG_TEST_RUN trigger BUG_ON(skb->sk)
in skb_orphan()?

In bpf_prog_test_run_skb(), the test skb is built with skb->tc_at_ingress
set to 0, skb->sk pointing to a dummy socket, and skb->destructor left as
NULL:

net/bpf/test_run.c:bpf_prog_test_run_skb() {
	...
	skb = slab_build_skb(data);
	if (!skb) {
		ret = -ENOMEM;
		goto out;
	}
	skb->sk = sk;
	...
}

Because skb_at_tc_ingress(skb) is false, egress is true in bpf_sk_assign().
With this change, passing any hashed full socket in the current netns
(such as a TCP listener or UDP socket from bpf_sk_lookup_tcp() or
bpf_sk_lookup_udp()) now passes the egress check and reaches
skb_orphan(skb), which hits BUG_ON(skb->sk) when skb->destructor is NULL.

[Severity: High]
Can assigning a socket on TC egress leak skb->sk across a network
namespace boundary?

When bpf_sk_assign() runs in sch_handle_egress() on a veth or netkit
device, skb->dev is still in the sender's network namespace, so
dev_net(skb->dev) == sock_net(sk) succeeds and sets skb->destructor to
sock_pfree_ref.  When the packet crosses into the peer's network
namespace, skb_scrub_packet() does not orphan skb->sk, and ip_rcv_core()
and ip6_rcv_core() skip skb_orphan() because skb_sk_is_prefetched(skb) is
true.

In the receiving namespace, inet_steal_sock() and inet6_steal_sock()
return the prefetched socket without checking net_eq(net, sock_net(sk)),
delivering the peer namespace's packet to the sender namespace's TCP
listener or UDP socket (and caching the peer namespace's dst_entry in
sk->sk_rx_dst), while ip_forward() and ip6_forward() drop the packet if
it is routed because skb->sk is non-NULL.

[Severity: High]
Can IP fragmentation after an egress bpf_sk_assign() underflow
sk->sk_wmem_alloc on a frag_list skb?

bpf_sk_assign() attaches sk to skb with destructor sock_pfree_ref and only
takes an sk_refcnt reference without charging sk->sk_wmem_alloc.  If an
skb carrying a frag_list later enters the fastpath in ip_do_fragment() or
ip6_fragment() (for example, after traversing a VRF device with a TC
egress program attached), each fragment is given sock_wfree without
checking is_skb_wmem(skb):

net/ipv4/ip_output.c:ip_do_fragment() {
	...
			BUG_ON(frag->sk);
			if (skb->sk) {
				frag->sk = skb->sk;
				frag->destructor = sock_wfree;
			}
			skb->truesize -= frag->truesize;
	...
}

When those fragments are freed, sock_wfree() subtracts their uncharged
truesize from sk->sk_wmem_alloc, which underflows sk_wmem_alloc on a
SOCK_RCU_FREE listening socket (preventing sk_free() from freeing the
socket on close) or triggers a premature __sk_free() while the socket is
still in use.

[Severity: High]
Can egress paths that assume skb->sk is a write-charged socket corrupt
sock_pfree_ref ownership or leak sk->sk_wmem_alloc?

In skb_expand_head() (reachable after TC egress via bpf_redirect_neigh()
or VRF output when headroom is expanded), is_skb_wmem(skb) is false for
sock_pfree_ref:

net/core/skbuff.c:skb_expand_head() {
	...
	if (skb_shared(skb) || !is_skb_wmem(skb)) {
		struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);

		if (unlikely(!nskb))
			goto fail;

		if (sk)
			skb_set_owner_w(nskb, sk);
		consume_skb(skb);
		skb = nskb;
	}
	...
}

skb_set_owner_w() replaces sock_pfree_ref with sock_wfree and charges
sk->sk_wmem_alloc, while consume_skb(skb) drops the sk_refcnt reference;
because nskb->destructor is now sock_wfree, skb_sk_is_prefetched(nskb) is
false and the receive path orphans the assigned socket.

Similarly, esp_output_head() and esp6_output_head() check
if (sk && sk_fullsock(sk)) rather than is_skb_wmem(skb) before calling
refcount_add(tailen, &sk->sk_wmem_alloc), adding write memory charges that
sock_pfree_ref() never releases and preventing sk_free() from freeing the
socket when closed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119142231.2612515-1-razor@blackwall.org?part=4

  reply	other threads:[~2026-11-19 15:24 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:22 [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 1/8] bpf: Orphan prefetched sk on skb_do_redirect() Nikolay Aleksandrov
2026-11-19 14:52   ` sashiko-bot
2026-11-19 15:01     ` Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 2/8] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets Nikolay Aleksandrov
2026-11-19 15:19   ` bot+bpf-ci
2026-11-19 15:28   ` sashiko-bot
2026-11-19 14:22 ` [PATCH bpf-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress Nikolay Aleksandrov
2026-11-19 15:24   ` sashiko-bot [this message]
2026-11-19 14:22 ` [PATCH bpf-next 5/8] selftests/bpf: Delete assign_reuse namespace when opening it fails Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 6/8] selftests/bpf: Cover egress socket assignment with reuseport Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 7/8] selftests/bpf: Exercise netkit socket assignment to a host proxy Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 8/8] selftests/bpf: Check assigned socket lifetime across queues and drops 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-223378@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