Netdev List
 help / color / mirror / Atom feed
From: Nikolay Aleksandrov <razor@blackwall.org>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: daniel@iogearbox.net, ayush.sawal@chelsio.com,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, ast@kernel.org,
	andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com,
	martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev,
	jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev,
	john.fastabend@gmail.com, sdf@fomichev.me, horms@kernel.org,
	kuniyu@google.com, willemb@google.com, jhs@mojatatu.com,
	jiri@resnulli.us, sd@queasysnail.net, paul@isovalent.com,
	alasdair.mcwilliam@isovalent.com, netdev@vger.kernel.org,
	martin.lau@kernel.org, mason@kernel.org
Subject: Re: [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets
Date: Thu, 19 Nov 2026 17:36:05 +0200	[thread overview]
Message-ID: <ada68cd6-93dd-474c-a11c-6a851d9fe3c3@blackwall.org> (raw)
In-Reply-To: <a4c31b033613fa51016f2cf42f90e3f2c61d0e9a8c1012c20e5074a0e92bbe95@mail.kernel.org>

On 19/11/2026 17:19, bot+bpf-ci@kernel.org wrote:
>> bpf: allow bpf_sk_assign on tc egress with refcounted sockets
> 
> TL;DR:
> - bpf_sk_assign() on egress may hit BUG_ON in skb_orphan() for test-run skbs
>    with skb->sk but no destructor
> - skb_do_redirect() may keep a borrowed listener via syncookie reqsk, a
>    possible use-after-free; also possible socket double-put
> 

oh well, both are good and valid catches, I will take care of them for v2
my local Sashiko run missed these too

Thanks,
  Nik

>> diff --git a/net/core/filter.c b/net/core/filter.c
>> --- a/net/core/filter.c
>> +++ b/net/core/filter.c
>> @@ -2559,14 +2559,17 @@ int skb_do_redirect(struct sk_buff *skb)
>>   	if (unlikely(!dev))
>>   		goto out_drop;
>>
>> +#ifdef CONFIG_INET
>>   	/*
>> -	 * Prefetched sockets may borrow a listener under RCU. Orphan them
>> -	 * before redirects that can outlive this section. Peer ingress
>> -	 * stays in the same RCU section.
>> +	 * Prefetched listeners may be borrowed under RCU. Drop borrowed
>> +	 * assignments before redirects that can outlive this section.
>> +	 * Peer ingress stays in the same RCU section.
>>   	 */
>> -	if (skb_sk_is_prefetched(skb) &&
>> +	if (skb->destructor == sock_pfree &&
>> +	    !sk_is_refcounted(skb->sk) &&
>>   	    (!(flags & BPF_F_PEER) || (flags & BPF_F_EGRESS)))
>>   		skb_orphan(skb);
>> +#endif
> 
> Can this leave a borrowed listener behind a redirect?
> 
> bpf_sk_assign_tcp_reqsk() installs a syncookie request sock whose
> rsk_listener is a borrowed SOCK_RCU_FREE listener:
> 
>      req = inet_reqsk_alloc(ops, sk, false);   /* no ref on sk */
>      req->rsk_listener = sk;
>      skb->sk = req_to_sk(req);
>      skb->destructor = sock_pfree;
> 
> skb->sk is a TCP_NEW_SYN_RECV request sock, so sk_is_refcounted(skb->sk)
> is true and the new condition is false.  The skb then keeps the reqsk
> across bpf_redirect(ifindex, BPF_F_INGRESS) and non-peer egress
> redirects.  For example:
> 
>      sch_handle_ingress()
>        skb_do_redirect()                    /* no orphan */
>          __bpf_redirect_common()
>            __bpf_rx_skb()
>              dev_forward_skb_nomtu()
>                netif_rx_internal()          /* queued past the RCU section */
> 
> Later tcp_v4_rcv() -> __inet_lookup_skb() -> inet_steal_sock() ->
> skb_steal_sock() takes the syncookie branch:
> 
>      *refcounted = false;
>      sk = req->rsk_listener;
>      req->rsk_listener = NULL;
>      return sk;
> 
> and inet_steal_sock() dereferences that listener.  If the listener was
> closed and its SOCK_RCU_FREE grace period elapsed while the skb sat in
> the backlog or a qdisc, is this a use-after-free?
> 
> The updated comment says borrowed listener assignments are dropped, and the
> commit message describes "sock_pfree + !sk_is_refcounted" as "the true
> no-refcount case from ingress", but the reqsk case above is not covered.
> 
> Separately, refcounted sock_pfree assignments made at ingress now survive
> non-peer redirects into egress.  complete_skb() and
> chcr_ktls_copy_record_in_skb() treat any destructor other than
> sock_efree/sock_pfree_ref as charged to sk_wmem_alloc, and chcr copies
> sock_pfree to nskb without sock_hold().  Would that double-put the socket
> when both skbs are freed?  The commit message says only sock_pfree_ref
> sockets are meant to be preserved; would testing
> skb->destructor == sock_pfree alone, as before, keep that and close both
> problems?
> 
> [ ... ]
> 
>> @@ -8115,21 +8118,26 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
>>
>>   BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
>>   {
>> +	bool egress = !skb_at_tc_ingress(skb);
>> +	bool refcounted;
>> +
>>   	if (!sk || flags != 0)
>>   		return -EINVAL;
>> -	if (!skb_at_tc_ingress(skb))
>> +	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
>> +		       skb_is_decrypted(skb)))
>>   		return -EOPNOTSUPP;
>> +	refcounted = egress || sk_is_refcounted(sk);
>>   	if (unlikely(dev_net(skb->dev) != sock_net(sk)))
>>   		return -ENETUNREACH;
>>   	if (sk_unhashed(sk))
>>   		return -EOPNOTSUPP;
>> -	if (sk_is_refcounted(sk) &&
>> +	if (refcounted &&
>>   	    unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
>>   		return -ENOENT;
>>
>>   	skb_orphan(skb);
>>   	skb->sk = sk;
>> -	skb->destructor = sock_pfree;
>> +	skb->destructor = egress ? sock_pfree_ref : sock_pfree;
> 
> Can skb_orphan() hit BUG_ON(skb->sk) here?
> 
> Removing the !skb_at_tc_ingress() rejection lets every non-ingress skb
> reach skb_orphan(), including skbs that have skb->sk set and no
> destructor.  bpf_prog_test_run_skb() builds exactly such an skb:
> 
>      sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
>      skb = slab_build_skb(data);
>      skb->sk = sk;          /* no destructor */
> 
> tc_at_ingress is zero there and convert___skb_to_skb() never sets it, so
> skb_at_tc_ingress() is false and the new egress branch is taken.  A tc
> program that does bpf_sk_lookup_tcp(skb, &tuple, ..., BPF_F_CURRENT_NETNS,
> 0) on an established socket and then bpf_sk_assign(skb, sk, 0) passes
> sk_fullsock(), sk_is_refcounted(), skb_is_decrypted(), the netns check and
> sk_unhashed(), and refcount_inc_not_zero_acquire() succeeds.  Then:
> 
>      static __always_inline void skb_orphan(struct sk_buff *skb)
>      {
>          if (skb->destructor) {
>              ...
>          } else {
>              BUG_ON(skb->sk);   /* skb->sk == dummy sk */
>          }
>      }
> 
> Before this patch the same program got -EOPNOTSUPP and never reached
> skb_orphan().  The selftest added later in the series (probe_timewait in
> sk_assign_netkit) goes through bpf_prog_test_run_opts() but only avoids
> the BUG because its socket is in TIME_WAIT and fails sk_fullsock().  A
> later patch in the series that accepts listeners does not add a guard
> either.
> 
> Could the egress branch reject skbs that have skb->sk set but no
> destructor (or the test-run context) before calling skb_orphan()?
> 
> 
> ---
> 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/44548126067


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

Thread overview: 7+ 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: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:36     ` Nikolay Aleksandrov [this message]
2026-11-19 14:22 ` [PATCH bpf-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress 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=ada68cd6-93dd-474c-a11c-6a851d9fe3c3@blackwall.org \
    --to=razor@blackwall.org \
    --cc=alasdair.mcwilliam@isovalent.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=ayush.sawal@chelsio.com \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=paul@isovalent.com \
    --cc=sd@queasysnail.net \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=willemb@google.com \
    --cc=yonghong.song@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