🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25937

[PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone

#25937
From: Weiming Shi
Date: Wed, 02 Dec 2026 18:56
123 lines
15260 bytes
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);
@@ -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
Technical Details
Message-ID: <20261202185641.4035600-2-bestswngs@gmail.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376012-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376012-linux-kernel-netdev=dewy.ru
From: bestswngs@gmail.com (Weiming Shi)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone
Date: Wed,  2 Dec 2026 10:56:42 -0800
Organization: Usenet Network
Message-ID: <20261202185641.4035600-2-bestswngs@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2035640"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:k3S9lT5NO/3gJBJFLQOzBj9nOqM= sha256:Fad9agdtbtAXMnlUAcdcGLaAc0iSZgr7KVFTlAPWjdQ=
	sha1:4UMfIH0wRZbGo7FI9Z9LHOKyrks= sha256:PHtgKJNc8MuWsRym3IYqjohps5v4Enjq3C9BlnQHoAA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 085C7117700; Wed,  2 Dec 2026 21:58:07 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.549
X-Spam-Status: No, score=-2.549 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, FROM_LOCAL_NOVOWEL=0.5,
	HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id Ucg5n2O-YbVN;
	Wed,  2 Dec 2026 21:58:04 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-376012-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id BF5031176E4; Wed,  2 Dec 2026 21:57:53 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 9EEC72E51E; Wed,  2 Dec 2026 18:57:45 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796237864;
	b=CSZWYR5SUHH1OkmV49Otdr0d61DdPAG3mMoBQpPvA69F3BkULHaomjNkpYCqFxsCOsVE
	 4/P4WNHFxHlSdB9EuiYOo1ezZRBAIB5qyuCtna+5F15j0bGpNtjItR+oVVEamiAn1k6kw
	 5cKZzNRuhmojWnli0u9RgXbu+19ecvjgLxLZFnBJpcLyHQwsg4FS+/370eGQysmV2dRmf
	 0ShKxDZm+CbPAWmt4tz8luK70zkCr114VLcYoJsDbJp8xOw0QmL3xIvVngaBL+MkL0Um6
	 2CUJLPYSgIAAswefAVjEPMxgoEpfsClWL9+Uktv2Jvfi+SWxvhweyZioSVuPEZ2XZGg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796237864;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding;
	bh=G5aEq/uE3xUkrR0qeD2xHTyte/dOnK+nMpRLnASYTsE=;
	b=owwmM0IHGAAVbB8PXdws+W4mYMNBr5c4stQnVghr2CdmapHKzUdPZYNLB+WF6BNFnlGT
	 K3eS5iURTcSU0BslF/u9thLBUya6xid6vl7MzRhAjhKRvJoyzyJqzfJzAeHDIGRDNlPHC
	 m+JRyVJaOGuUfx522ajqVCbOHdNY3faKN9I/9CeoDuIOxi/r4GFtFWCgXImqgepuFra9n
	 EHcxZA5/NbX61CvWXbf9daVhIB3bOikROwl/hGod2E2GUGGs2VgG/Krh3f0S94RZlrixo
	 mnwWmOfvAg0roZ3JB/7B1v5eFG+2xGAu9fSGvDiUWVNwUFZRupaxEardDtG++17lQzg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=h19TlntN;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B6FA0606A335;
	Wed,  2 Dec 2026 18:57:44 +0000 (UTC)
X-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
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796237863;
	b=5idQQMzyy60MulC9AhBcsVvl0kGn3MORrMQuFvuOM4878ljKWTZH0xZ/rSW5IZ2WSBbD
	 F0/SvYaMUYG4E2jLY6ImXEjhtIi1qz9RVBlY2wlmhH1JhOuNtfRlrIcIXnaVFxR+AjLOZ
	 eqwM5gqQ5dtblUhD6RCY6x0Q/whQ+OF44ZjujRPiV3iR/xC4fZjcoS7ux3RdfhXCost3k
	 a5LmOSBoxH9nTCm6mJFo5c2G++B2ClfTE/pIGUggCitYZ317rBWJx7zHozm3wSgrMP1gj
	 kCn3T5DSfi2hVzPSvsoGLE8rLCGVUT3CI5KeSHKPz/IOHQ4mvRk+KyykIzFKXvVPCYg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796237863;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 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=2/ZfweChjN258C5RJrfgQARDzaeuyGUmbYHdKHbGBK4JSpugJ+T4CtTEHKLjGWAsdGni
	 st1V1+iOH70TrE5yLxnjpGD2USyYl4V7SFzRTXlJshMkIRfWZdQIhBHAvuuIt1GrtdXlC
	 Yjo2hGTmGGYPLOm4x4dQsyivEJhTTdGZ8f+WZM17s9zzjj5BMUjxz6JjdobMxjoUr5lke
	 dqJZdlESEvgxgg1nosRtQ2yP1K6lGjkvUzdYGTd1sOCJv3Cif2BpoC9mmjq103OgMf+I+
	 bntELV7rVt/bQdd0DbH6u/ofOF+VLq/griYb9CIMiv6liF/rzVzPAr6Q0ewuZspwL+g==
X-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
X-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 7120D606A32C
	for <netdev@vger.kernel.org>; Wed,  2 Dec 2026 18:57:43 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7120D606A32C
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-3866a784546so18641168eec.0
        for <netdev@vger.kernel.org>; Wed, 02 Dec 2026 10:57:43 -0800 (PST)
X-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=j8UzimG6ZlpPaYY6fod/VBNC31Sh5kGfnepA5OQz/BvrjqEDzfikm9eNxQGp0jE/zj
         sU7nM4wPawwFAC94s+itbLhZY23vjC8LuAO+r8/YRPRwC9fKGyIIcxIhUTPNg0oYFY4V
         sX1rZHXqEF54HpOuEoUPROqXttp2dWTpjoOaE4f5sfYntswTVsT/5qvxRn6cdoCNJLW0
         Lu8PKbULvax0rwWS+2OI5iFXRz/HJpeTyNxdCSCv/st3PGAR+ZKA6CAkZFbAlgOIjNCw
         H10lv0MnLof3c0I4Jxai20t7rM75iJhAu6VLWuY0EUffgH4hRPKeBJ32q1LpSIrvY6uc
         xbXA==
X-Gm-Message-State: APuU4Z8OxvQiP4bha3JDPerC3fG4NElloClxqh3JT9wR8hEmPX6u/v3I
	JAzeJyCEx71UcFgtMY2lbEIzDOO4MSirG3H7GK4t965kGK1wtbJ1sGeR
X-Gm-Gg: AZFdTZeCRkOKHs8bIxXUXOgFCg/8WN8n4BQg8hCk+twPRvFXfVsS1gfsd6cfdNKPjk9
	qyXliNl3HWx7evrQHFgYNxTksQKbG4yEkifLHSUQDk0z6Zyj9KuSuwGzbGyH6zPPXnilJIML5rP
	Gn/XDUUFIQmZprh3O9CH7DVM3TQw182htIfUZqUFYm3FlSGnV01N+aFelJrT4l1TSJXpaB+YLZG
	oXTRF/b6z9IrXjTEKj/8VSn83Le3+Rv0zx3qAwi69cxbaM8FwGWceqefg66+EnMXaY5vzLhKHEb
	DvO2eNOi2tp7jl1BAOt2YfqQwGaS2AEDXLZeC2d/cQSR6WRMcQQzu4bjCEnI7LIMutke6lYlpio
	03YZ5QyONmccPvfwksP2AAkBwu49HajsXfxJGEdlJ7albloC9klG6bxXogsDaDTzhrp0Gtzl0nl
	n2FleJ1JcWU7mqUsjXlw0QQZXnIkJEyNIAmP7UaUYz1qFEHCwh+1NwTj78CRkgHiJIDO7thL/JB
	PM4o9BSByyhaHVi+53g5cCZUNBCESWBQYJTeYcFGSctDjodQshcnIdkXh9iNkYnAEvlwbkZJ7V0
	jub3hrm54BElfHN1hSYzIbipAm1oNZbhM8k7897GVrun49R1T5ct637zKmE3mQlpybzrAUhl5Gt
	zcQ==
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)
X-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)
X-Cc: netdev@vger.kernel.org,
	bpf@vger.kernel.org,
	Xiang Mei <xmei5@asu.edu>,
	Weiming Shi <bestswngs@gmail.com>,
	co+c4bff1ab7d214997@bugs.sh
X-Mailer: git-send-email 2.56.1
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list