BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Weiming Shi" <bestswngs@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone
Date: Thu, 03 Dec 2026 18:57:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-248448@kernel.org> (raw)
In-Reply-To: <20261202185641.4035600-2-bestswngs@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] bpf, sockmap: race in tcp_bpf_clone() leaves psock
  callbacks on child socket [1]
--

commit 2f699c239f62a4c51ffb60a012b53a93896cb087
Author: Weiming Shi <bestswngs@gmail.com>

net: init sk_refcnt, sk_state, sk_bpf_storage on clone

This commit initializes sk_refcnt, sk_state, and sk_bpf_storage in
sk_prot_alloc() when __GFP_ZERO is not set, and clears nsk->sk_bpf_storage
in sock_copy() before security_sk_clone().

> diff --git a/net/core/sock.c b/net/core/sock.c
> index 5ed63d2d8976f..15ffe7db9dc01 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -2260,6 +2260,7 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)
>  		      prot->obj_size - offsetof(struct sock, sk_dontcopy_end),
>  		      /* alloc is larger than struct, see sk_prot_alloc() */);
>  
> +	bpf_sk_storage_init(nsk);

[Severity: Critical]
This problem wasn't introduced by this patch, but can a race between
sock_copy() and sk_psock_drop() [1] leave sk_psock callbacks on a
cloned child TCP socket?

When a TCP socket is inserted into a sockmap or sockhash and later
transitions to TCP_LISTEN (e.g., via tcp_disconnect() followed by
inet_listen()), passive connection setup clones the listener in
sk_clone_lock() without holding the listener socket lock or
sk_callback_lock.  sock_copy() copies sk_prot (before sk_dontcopy_begin) in
the first memcpy() and sk_data_ready / sk_write_space (after
sk_dontcopy_end) in the second unsafe_memcpy(), while sk_psock_drop()
restores sk_prot before restoring the callbacks:

CPU0                              CPU1
sk_psock_drop()                   sk_clone_lock()
  sk_psock_restore_proto()
    sock_replace_proto()
                                    sock_copy()
                                      memcpy()        // restored sk_prot
                                      unsafe_memcpy() // psock callbacks
  sk_psock_stop_verdict()
    sk->sk_data_ready = ...
                                    tcp_bpf_clone()   // skips reset

net/ipv4/tcp_bpf.c:tcp_bpf_clone() {
	struct proto *prot = newsk->sk_prot;

	if (!is_insidevar(prot, tcp_bpf_prots))
		return;
    ...
	newsk->sk_data_ready = sock_def_readable;
	newsk->sk_write_space = sk_stream_write_space;
}

Because newsk->sk_prot is already restored to the base TCP protocol,
tcp_bpf_clone() returns early without resetting newsk->sk_data_ready and
newsk->sk_write_space.  The child socket retains
sk_psock_verdict_data_ready() without an attached sk_psock, stalling data
delivery, and inserting that child into a sockmap saves
sk_psock_verdict_data_ready() as psock->saved_data_ready, causing
unbounded recursion and a kernel stack overflow when data arrives.

[1] https://sashiko.dev/#/bug/linux-2fa20aa5-ab8d-4c5c-a262-b22090716944

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202185641.4035600-2-bestswngs@gmail.com?part=1

      parent reply	other threads:[~2026-12-03 18:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 18:56 [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone Weiming Shi
2026-12-03  0:46 ` Kuniyuki Iwashima
2026-12-03 17:18   ` Weiming Shi
2026-12-03 18:57 ` sashiko-bot [this message]

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-248448@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bestswngs@gmail.com \
    --cc=bpf@vger.kernel.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