From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796237864; b=1jKWsXPjsAnAq+5aisZmr2rbeOFPI/LbVmKMimwXL6lX0+TpIoo7y50+NqkGwLp0x6/N ZfvFsE7KtGPPvOHTKS0UMnytBqan22pYsxda1xa2UKRvtI4NQ/sNuic/BD3bWTUVqGgzP ZVpymepiXGOWZgxRM50PKavVM64F5Oexjb3yCJuxNoJILHilBRvelADtp2AJQT0C+G9lr 85ZGI9GToCdHHSyVFp75CczF2XgtNIT8qaphcfVn8ixidm7qrU5EeuRbTWNUXl7bKiQ3Z P9XEwVJgQdIuzM++mbcPfHCGmZwJTJbhii4ZSZRrCPg+AeDEpJ2qi16hYJgsbFq5Nwg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796237864; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=G5aEq/uE3xUkrR0qeD2xHTyte/dOnK+nMpRLnASYTsE=; b=itqnv7dB8OPImYFcTNqLzph5OUl6uH2SQy1nbuHQ94xKCWkjATizmh5HJjbTrbnriHwp 5Z53FYOgWkyJp9wuWeRWYoGU/hey5ktO+PhQQcLLiP/m7Du1dW2t7mYygThMIYEvUiorO eergnKBpA37yOqCub1yG+jQ+GfGVk3qsYKrSH9gzm6Fd+SX7Socdzmp4vMvldXP2AEcmE Puo5F5R0GkFYh02RDLjJ8bENGhe1IXloEFceQRu7wFf5JIPCI2tWWD1R4Xjuofl4KZSHd UPx6RjmLV8uJV/+y3vmCTsR4evMQeQ5mdmqDgcY9DKBLdwDJ/gvcwStgT3vzK1If16Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=h19TlntN; arc=none smtp.remote-ip=74.125.82.173 Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173]) (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 7123F606A32D for ; Wed, 2 Dec 2026 18:57:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7123F606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=h19TlntN Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-386ab9ab619so20405492eec.1 for ; Wed, 02 Dec 2026 10:57:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796237863; x=1796842663; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=G5aEq/uE3xUkrR0qeD2xHTyte/dOnK+nMpRLnASYTsE=; b=h19TlntNeURY/bWZxA6SLafiFD3mcVC30REleyGAfrSlcliGlfryKv2Ft5Z4aTaBfQ qymky3k7R1LskqRj8LOmWKlnZwAc5gExT4/3SVJJok1eny1mnYeMCCq3EcE2H0p+uPoR rEycJbjscqB0EwgtNiA8PEQOtIo+N0qAmeYm49+57zvG2jst14WP/Sn77DlsPz7d1ZGe CP9jjppqD7vfk66z359M0ukcWszjTngP2DCUFcI5+eTR8UWKJ/qXiW3GMqAxQzWVKZhz EcoiTSSgLt4vrspyg02erVc9dyyS7B+ha09fCryVDMrRK3aDrqDzeGyjQPgAjAakWcF+ 2Eqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796237863; x=1796842663; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=G5aEq/uE3xUkrR0qeD2xHTyte/dOnK+nMpRLnASYTsE=; b=ttFgYEiDqSqXxFpVHWp6fK1T4b7CZeMp3bGTCsQcc+0Hb8Hxdj/TIhh/NSXroOuEpz v+bRteWhAEkYTHkkfFdiM/tqiSQ2UOhI3iXUW4yqaWnxzOSVO+Z0A2Yrx3mtaeUZkPfc omEzH85l1Y1KCJDZLUI88Ef4BCkvyEwQzGQsc8cb6wdRwlH0moGVoVc/k5cIGK3O2+FA OAu8IKJVEYu8kbMTEvrMlLQo1MBgv/z2hv0RuxLXclu4NmkJJEmFcQL/LTD5iikCL18Y vqKbyaNIg5b6SqvG2Jxi9n/FrN5lBrOllYrH1a6XvjwimD2KYngDjF+ZtXZPEHPwVKTh UzJQ== X-Forwarded-Encrypted: i=1; AL+hErFZp7TVtCsP2+2z5sVBcEXZ20USr2cFU3wUW3l+EQFupDko2vTENc4Jz+gDslJzHQyHD/0=@vger.kernel.org X-Gm-Message-State: APuU4Z/Dv6PNELFHYD4fo1qdSEoB2t1W+hdbOsgRjmSrqUi35h+f4ByU 4AGKshrh+TjUnJbVWMHbYzwP4u/nHDjl7n89SOV5xM547dLYUZB/jZzG X-Gm-Gg: AZFdTZe1K0fcjtzIcUJDWgsJD8Dnu3qgewsv3vEuDOLbPlE311AnEVlwhPelnpZkzrf H+o+352RhCqjAhREbEfxuHNm+5/fdZA9gYtZz3/bm1/ke/1d+TGR4whv8np+K4p8IIegI32MKvp jTcFbSLtOJUR4jKgWAJbtOp+CYmc7L3h3T+a8SpIKSQn0YTb5lpqkQbf2duB4LLpAYlkgiIHWUu f0F8CO0d29DxfS1M65ZeFgRh5sfKRlHTM0aWzU2C2X0gTAE/8wxGe2P9zRx0dE1QQ3LvhjjLjNP flxhjFotfbuXcoKxTzEKTP0GxIDyHmuFh8cMSRgGfijFb99V+1BFJhXxHXNMK8AARArNzyLM5J3 cJv8eLrw5Gu8nRthNCH1SICzOCDr1agu0B/+H+hm3tdZvKmmgllj2yW7BZK0bPGVk1/8dwoHfK3 YSfctv0OxIOcHXsM5Xc6d7WSDM5y73pk5n58rSmAPTUds83bDF7XsjMvHvc4PT9kbJ2zqmoSX0R qAvpy25/banHkGun//yhBtZIv40UbEPfw2KpyYJhRcWKEkUSioNdKc+WJhTs/rF/4hvojSAKu5T ynk4ImsPbftk3eFVD/lZ1qAD3j2IHHx8wrfmWpQOWKLty8PRRoaf3Ec37v1UNCkhEL1rK58mIU2 lnA== X-Received: by 2002:a05:7301:1a13:b0:387:4034:c9b6 with SMTP id 5a478bee46e88-3874034cdd2mr183315eec.28.1796237862480; Wed, 02 Dec 2026 10:57:42 -0800 (PST) Received: from Terra.tail0ac356.ts.net ([198.176.50.208]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385672c6f31sm118252206eec.15.2026.12.02.10.57.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 10:57:41 -0800 (PST) From: Weiming Shi To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Kuniyuki Iwashima , Willem de Bruijn Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Xiang Mei , Weiming Shi , co+c4bff1ab7d214997@bugs.sh Subject: [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone Date: Wed, 2 Dec 2026 10:56:42 -0800 Message-ID: <20261202185641.4035600-2-bestswngs@gmail.com> X-Mailer: git-send-email 2.56.1 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 Content-Transfer-Encoding: 8bit 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); @@ -2283,6 +2284,11 @@ static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority, sk = kmalloc(prot->obj_size, priority); if (sk != NULL) { + if (!(priority & __GFP_ZERO)) { + refcount_set(&sk->sk_refcnt, 0); + WRITE_ONCE(sk->sk_state, 0); + bpf_sk_storage_init(sk); + } if (security_sk_alloc(sk, family, priority)) goto out_free; @@ -2523,9 +2529,6 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority, sock_copy(newsk, sk); newsk->sk_prot_creator = prot; -#ifdef CONFIG_BPF_SYSCALL - RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL); -#endif #if IS_ENABLED(CONFIG_INET_PSP) DEBUG_NET_WARN_ON_ONCE(rcu_access_pointer(sk->psp_assoc)); #endif -- 2.56.1