Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796258822; b=MRZzBtMDspnLfZAbIQcMynR2nH6hxDOZggwAwLQOcs7Ar8Nv5dcfcqXcQki0saVd5pco NeBULl05Zu8MB35XrOOC8qq+D1MSv4nqsdKA8EhK6IrGwTUXBKfTwM6hNaFiBFfZxNFuN OHHQzw3BFzFy39OQ/VwcyPsfzz/f43PnblpBFvnm0TEwncb+NCyK8dnvEs7Jto/FOFwpi MkR3CeB5Ff16DeKdkTILqzOTMScAPNcTHvHVvYKSrOszZXYB/7JWO+XV8xrG/G3L3/CvF hWxHSOVbn2RZyxKnjIA+7gWP4G3e+8NPfshPRdyBq437vzyAcyHb7ZImQHZ2EzsdRrg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796258822; 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=WiqzStMielV9NZOinpSmcVGgaRB1KeVCByW8H0DUdTRD+EZ4Tb7Xh3MVOlloNWJsAcdy efrvfS/ACSXc+vSVRxQ4OVjEVbWC37P725jKFTwvnmCm/3bj5WsrNn6ot9H9R3WmXTwOV T8G+H+DgivGj/Fhdt4K92ih6R98Jp4XJk5OJhRez8Ws4TlOtYmHVYM9QmVJW3eojTDBuj SsByTBKgW4c/dtBwuc2xBU6X2SwyCUDctCD3zauG1V+GkZ5sJAnS4XZIxbm/9USSKWL5D 8oSUHB6pnqGlw53RUMFs5O/BBCR/NiSavkESCDJazjVOeHSrVC/VmX+mw1UadB8W5Ag== 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.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 45DD4606A322 for ; Thu, 3 Dec 2026 00:47:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45DD4606A322 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-f198.google.com with SMTP id 41be03b00d2f7-cc73895c7d5so8565120a12.1 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=wfQIdgNuJ6jWeTKP7oKEDxK/BMP00qK04pTBO71ZY3Klas5yuzl7HkGzdP/3jXScs0 xtw2PyVrkMoW/u6x8RvdA7ILOKvqACr22w6kdk0kX+HEdpZDLA2vdkbzQSrxuZqEXF02 rpWtTYFTL10aBAzzbZFG0/Nd5exkXPdBkvN3OI27o2GXnNMAicV4iEbAWvWsCFn3BUDb Vwt6wVQgEynlFVDPo8A+Ruzn4YmwFUeYmI6eLVybcEZlcrrjy3PFX4lLY4CWL3gfOjhn VfzlWG1ewDeYSV7csIik9NDxSel8chc4qg/6Nam23Ms2b5L73UHGqV1DNXYdBJr7jpkt MN5A== X-Forwarded-Encrypted: i=1; AL+hErEBTV4ydaAUAjDqYCBAjM0kMsuuqRBCSdNB4rVMsyKEOL8JhHzGiJ96QWjV8WpIXrxy2O0=@vger.kernel.org X-Gm-Message-State: APuU4Z/vrtU0SOCDsWbwO3wjmRXcuSgV6TJZsvppxzrhiIqv7zpmTo0J B0XrWTmGFqTCSSJb6k0HuWo1TgD6KBY/9WR5DTwvhjOhgZp3lQumJMqd5G6G+NHP4OeIaY9Jx0i 7uUeOjw== 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: bpf@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