From: Kuniyuki Iwashima <kuniyu@google.com>
To: Weiming Shi <bestswngs@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
Willem de Bruijn <willemb@google.com>,
netdev@vger.kernel.org, bpf@vger.kernel.org,
Xiang Mei <xmei5@asu.edu>,
co+c4bff1ab7d214997@bugs.sh
Subject: Re: [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone
Date: Thu, 3 Dec 2026 09:46:59 +0900 [thread overview]
Message-ID: <axC67g5oniWxSE4f@google.com> (raw)
In-Reply-To: <20261202185641.4035600-2-bestswngs@gmail.com>
On Wed, 02 Dec 2026 10:56:42 -0800, Weiming Shi wrote:
> sk_clone() allocates newsk via sk_prot_alloc() without __GFP_ZERO. With
> init_on_alloc off, sk_refcnt, sk_state, and sk_bpf_storage are
> uninitialized in security_sk_alloc(). sock_copy() skips sk_refcnt and
> calls security_sk_clone() before newsk->sk_bpf_storage is cleared. Early
> BPF hooks can call bpf_sk_storage_get() on newsk. It shifts by sk_state
> in sk_fullsock(), dereferences sk_bpf_storage, and calls
> refcount_inc_not_zero(&newsk->sk_refcnt). Uninitialized sk_refcnt can
> cause early free after sk_clone() sets it to 2, or refcount saturation
> and underflow in sock_put():
>
> refcount_t: underflow; use-after-free.
> kernel BUG at lib/refcount.c:37!
> Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
> RIP: 0010:refcount_warn_saturate (lib/refcount.c:37)
> Call Trace:
> <IRQ>
> bpf_sk_storage_get_tracing (net/core/bpf_sk_storage.c:261)
> bpf_prog_f686ef25e9cfe862_skst_fe+0x83/0xd3
> bpf_trampoline_6442635922+0x4c/0xb4
> sk_filter_charge (net/core/filter.c:1255)
> sk_clone (net/core/sock.c:2594)
> inet_csk_clone_lock (net/ipv4/inet_connection_sock.c:1217)
> tcp_create_openreq_child (net/ipv4/tcp_minisocks.c:550)
> tcp_v4_syn_recv_sock (net/ipv4/tcp_ipv4.c:1700)
> tcp_check_req (net/ipv4/tcp_minisocks.c:934)
> tcp_v4_rcv (net/ipv4/tcp_ipv4.c:2173)
> Kernel panic - not syncing: Fatal exception
>
> Initialize sk_refcnt, sk_state, and sk_bpf_storage in sk_prot_alloc()
> when __GFP_ZERO is not set. Clear nsk->sk_bpf_storage in sock_copy()
> before security_sk_clone(). Remove the conditional refcount_set() in
> bpf_sk_storage_clone().
>
> Fixes: 30897832d8b9 ("bpf: Allow local storage to be used from LSM programs")
> Reported-by: <co+c4bff1ab7d214997@bugs.sh>
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>
> Assisted-by: LLM
> ---
> include/net/bpf_sk_storage.h | 7 +++++++
> net/core/bpf_sk_storage.c | 7 -------
> net/core/sock.c | 9 ++++++---
> 3 files changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/include/net/bpf_sk_storage.h b/include/net/bpf_sk_storage.h
> index 2926f1f00d65..6435c319ef11 100644
> --- a/include/net/bpf_sk_storage.h
> +++ b/include/net/bpf_sk_storage.h
> @@ -29,6 +29,10 @@ struct sk_buff;
> struct nlattr;
>
> #ifdef CONFIG_BPF_SYSCALL
> +static inline void bpf_sk_storage_init(struct sock *sk)
> +{
> + RCU_INIT_POINTER(sk->sk_bpf_storage, NULL);
> +}
> int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk);
> struct bpf_sk_storage_diag *
> bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs);
> @@ -38,6 +42,9 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
> int stg_array_type,
> unsigned int *res_diag_size);
> #else
> +static inline void bpf_sk_storage_init(struct sock *sk)
> +{
> +}
> static inline int bpf_sk_storage_clone(const struct sock *sk,
> struct sock *newsk)
> {
> diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
> index 1c59300af05c..02203b89e16e 100644
> --- a/net/core/bpf_sk_storage.c
> +++ b/net/core/bpf_sk_storage.c
> @@ -164,13 +164,6 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
> if (!sk_storage || hlist_empty(&sk_storage->list))
> goto out;
>
> - /*
> - * The maps lead to newsk as soon as an element is linked, but
> - * sk_clone() sets sk_refcnt only later. Until then it holds whatever
> - * the slab object held before: no reference can be had yet.
> - */
> - refcount_set(&newsk->sk_refcnt, 0);
> -
> hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
> struct bpf_local_storage_elem *copy_selem;
> struct bpf_local_storage_map *smap;
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 5675053625e6..8ae7b6391cd1 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);
> #ifdef CONFIG_SECURITY_NETWORK
> nsk->sk_security = sptr;
> security_sk_clone(osk, nsk);
Simply move security_sk_clone() to the end of
inet_csk_clone_lock() and before security_sctp_sk_clone().
Passing a not-fully-initialised socket is too error-prone.
---
pw-bot: cr
prev parent reply other threads:[~2026-12-03 0:47 UTC|newest]
Thread overview: 2+ 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 [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=axC67g5oniWxSE4f@google.com \
--to=kuniyu@google.com \
--cc=bestswngs@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=co+c4bff1ab7d214997@bugs.sh \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemb@google.com \
--cc=xmei5@asu.edu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.