BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiayuan Chen" <jiayuan.chen@linux.dev>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf v3 2/3] tcp: Skip cond_resched() in inet_csk_listen_stop() under BPF context
Date: Thu, 10 Sep 2026 11:44:28 +0000	[thread overview]
Message-ID: <20260910114428.7CFDF1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260910112736.153710-1-jiayuan.chen@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] bpf_sock_destroy() incorrectly relies on has_current_bpf_ctx() to bypass lock_sock(), leading to lockless socket state mutations and data corruption.
--

commit 5e3e5dd8730687f6f2e1d9b86f565fa03b6e3366
Author: Jiayuan Chen <jiayuan.chen@linux.dev>

tcp: Skip cond_resched() in inet_csk_listen_stop() under BPF context

This commit skips cond_resched() in inet_csk_listen_stop() when running
under a BPF context. It prevents a "sleeping function called from invalid
context" BUG when bpf_sock_destroy() triggers tcp_abort() from a BPF
iterator.

> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> index 6257459bcee24..6a30f11384547 100644
> --- a/net/ipv4/inet_connection_sock.c
> +++ b/net/ipv4/inet_connection_sock.c
[ ... ]
> @@ -1520,7 +1520,8 @@ void inet_csk_listen_stop(struct sock *sk)
>  		local_bh_enable();
>  		sock_put(child);
>  
> -		cond_resched();
> +		if (!has_current_bpf_ctx())
> +			cond_resched();

[Severity: High]
This is a pre-existing issue, but propagating the use of
has_current_bpf_ctx() highlights a deeper locking bug in
bpf_sock_destroy().

Does relying on has_current_bpf_ctx() in tcp_abort() and udp_abort()
lead to lockless socket state mutations and data corruption?

When a BPF program of type BPF_TRACE_ITER obtains a socket pointer via a
lookup helper like bpf_sk_lookup_tcp(), or operates in an iterator like
iter/task_file that doesn't inherently lock the target socket, passing
this socket to bpf_sock_destroy() calls tcp_abort() or udp_abort().

In those functions, the socket lock is currently bypassed:

net/ipv4/tcp.c:tcp_abort() {
    ...
    /* BPF context ensures sock locking. */
    if (!has_current_bpf_ctx())
        /* Don't race with userspace socket closes such as tcp_close. */
        lock_sock(sk);
    ...
}

Because has_current_bpf_ctx() returns true in BPF context, lock_sock(sk)
is bypassed. Modifying socket state (like accept queues, timers, and
flags) locklessly can lead to use-after-free and list/RB-tree corruption.

Since the underlying bug stems from the commit that added
bpf_sock_destroy(), should the lock acquisition be addressed structurally
for those code paths instead of relying on has_current_bpf_ctx()?

>  	}
>  	if (queue->fastopenq.rskq_rst_head) {
>  		/* Free all the reqs queued in rskq_rst_head. */

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260910112107.148770-1-jiayuan.chen@linux.dev?part=2

  reply	other threads:[~2026-09-10 11:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10 11:20 [PATCH bpf v3 0/3] bpf,tcp: Fix bpf_sock_destroy() on TIME_WAIT and listener socks Jiayuan Chen
2026-09-10 11:26 ` [PATCH bpf v3 1/3] bpf: Fix out-of-bounds read of sk_protocol in bpf_sock_destroy() Jiayuan Chen
2026-09-10 12:07   ` bot+bpf-ci
2026-09-10 12:20     ` Jiayuan Chen
2026-09-10 11:27 ` [PATCH bpf v3 2/3] tcp: Skip cond_resched() in inet_csk_listen_stop() under BPF context Jiayuan Chen
2026-09-10 11:44   ` sashiko-bot [this message]
2026-09-10 11:50     ` Jiayuan Chen
2026-09-10 11:28 ` [PATCH bpf v3 3/3] selftests/bpf: Test bpf_sock_destroy() on TIME_WAIT and listener socks Jiayuan Chen
2026-09-11  0:00 ` [PATCH bpf v3 0/3] bpf,tcp: Fix " patchwork-bot+netdevbpf

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=20260910114428.7CFDF1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jiayuan.chen@linux.dev \
    --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