From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796258821; b=kO8EwwXc6T0A7hU4RFLRPxvCFywHHNJLwMzopjbS8lIBFh4gjAh50NCwAQ86/WKXtvRX Z4nURNEUlgoR8HhZaa7DyBaSeTlEkmyIxRFM9IqMVyk4B+TZkTaWz13vgj6ttQBL+6lIr 3pZSWqcZUVgyxWbpOE/oKylO7BQvWYUE6rZbI5a/eEQoxk1LjYtnk14gXtEANapr7HkJn b+75HlaPljdhwcgxd4KZlnn84PUSV0L1VAp6/M0twdy4noqnZbqgqz4BKsmz8rYimQY0i VWRUZvGUTbQUpwGuwWEvL4l5J+YcxtP3/emtUTA/D5puvxRPHJyV7LMElS389WIMGZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796258821; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:Message-ID:Subject:From:To:Cc:Content-Type; bh=V79F4grD2p+pD1vnykmrQv9pXKZXx7uLsANvJqQYurM=; b=WJV3O1NqUfVcfF/UGt4RB9a0NUBu4ki8ZSkPd5L7/R9u1k+FDth7VpsUCjmpXF7i3TGD N/JYvSI6HE40lKPVL6v+6zKzI3O8aWaxYBVDLRf5vPhzXwgLlTe4LXIInkxzGKiETzc/4 W1TrDCLXXdiyzuOcrBopdZ1OvzJ+aAUagkt80r4+e1Gi0pRJ4cAqgFCJFqFjkPQTwfIPd xEkM/2nWPR/FTbRmP9yqVmcmp5cSwNW1TnaA82j/aRex7U3t0vSuE0I068eMC+2TjQrju B/20X/mz6L6czR4rmdzMsC53XPmkb07IHxK2NbDuvft9+x40+rG4AEURCmOI7wUjiPw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=FWjLT2Ry; arc=none smtp.remote-ip=209.85.215.199 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47AF0606A32C for ; Thu, 3 Dec 2026 00:47:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 47AF0606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=FWjLT2Ry Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cdd4dafdcf3so7203910a12.2 for ; Wed, 02 Dec 2026 16:47:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796258820; x=1796863620; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=V79F4grD2p+pD1vnykmrQv9pXKZXx7uLsANvJqQYurM=; b=FWjLT2RyLo23mkMW/4/IUDYGTTRn9pN7HV/VU6e4pte97+jHka7eMxlS0Rcewhbm6P DcsB+wKZsAU2W3s96PIFwQQKbrqA9zRmF/0mFwcjoH3P2lY3S4fcbZ18U/sFjFV2mnRa 0NHJx/ZnAuoJWcitq2uLeVdby6K4VUyWbYuuT82UPDD4RbOb6UHzncNnjU0+cOsUD6+Q uCVbJcVf4DSzXXXtt97jV+fcjMufg/H415WFNVBsMrgBbsWL/JSJmHKJoenDcGTYyQ+h aei6IQKH2fQYfFr0qHU1xcuPNSlVwKspA2sAeHoCXxkUIdzArNdGOJcd1p1Nw729euZ2 dYGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796258820; x=1796863620; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=V79F4grD2p+pD1vnykmrQv9pXKZXx7uLsANvJqQYurM=; b=ccLKl75Tnf+n9lvAh5nposKOopzba+tibh4bBThgZAatqHvKbuUHApk0EMEMej9Dzt QdwHDiO1BC3HisDqREBBOBUDvae+zwoX9TzH5McqnKCZJg8JYMdiO26TR1LwOoxPO+K/ j8bgk6qZqzJ+Wt3L8LgJJmhCz3+FZfbVna2gMo7ipEXuXxp/w/KL35Tb1bTnwjKgdFWK 1bkJ/h0fJ0hxJBTieOk9gBJIbLD5yeznwpMTVdn1v3aqxCk3VoRHE5zwrEjuzdq6yTdn ++fEL9NwLhxlv+rrRVsw/N7aX0PqU48z9LzWgnX4GTycGlMkrJI8nrkqZDSBkK28jbcJ vD/w== X-Forwarded-Encrypted: i=1; AL+hErGAkXaNEsHbcKn7JsSgCSd85pYmU5kywstRA/HP809wzIm2MXLXpdiOZKYSQ4y9C9COVGyNO/8=@vger.kernel.org X-Gm-Message-State: APuU4Z/Xu/nf2n7qKt1Ve9ksztl/cfLp6kQVUeXZxsDIzWpp61jhX8Yx W2lyiNks+MDlg7qkCYVlV27b1z5LoaWu4I+T4fJUgT0MCT2B962O516zR/8Ikdi+4kVqkI11LiY WuQieCA== X-Received: from pfde8.prod.google.com ([2002:aa7:8c48:0:b0:89e:d261:98c6]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:e593:b0:3cd:3e77:1dcd with SMTP id adf61e73a8af0-4014a271f38mr45985110637.1.1796258820338; Wed, 02 Dec 2026 16:47:00 -0800 (PST) Date: Thu, 3 Dec 2026 09:46:59 +0900 In-Reply-To: <20261202185641.4035600-2-bestswngs@gmail.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202185641.4035600-2-bestswngs@gmail.com> Message-ID: Subject: Re: [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone From: Kuniyuki Iwashima To: Weiming Shi Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Willem de Bruijn , netdev@vger.kernel.org, bpf@vger.kernel.org, Xiang Mei , co+c4bff1ab7d214997@bugs.sh Content-Type: text/plain; charset="us-ascii" 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: > > 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: > Signed-off-by: Weiming Shi > 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