Kuniyuki Iwashima <kuni…@google.com> 于2026年12月2日周三 16:47写道:
>
> 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+c…@bugs.sh>
> > Signed-off-by: Weiming Shi <best…@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
Thanks for your review. I will send v2.
Best,
Weiming Shi