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=1796237863; b=5idQQMzyy60MulC9AhBcsVvl0kGn3MORrMQuFvuOM4878ljKWTZH0xZ/rSW5IZ2WSBbD F0/SvYaMUYG4E2jLY6ImXEjhtIi1qz9RVBlY2wlmhH1JhOuNtfRlrIcIXnaVFxR+AjLOZ eqwM5gqQ5dtblUhD6RCY6x0Q/whQ+OF44ZjujRPiV3iR/xC4fZjcoS7ux3RdfhXCost3k a5LmOSBoxH9nTCm6mJFo5c2G++B2ClfTE/pIGUggCitYZ317rBWJx7zHozm3wSgrMP1gj kCn3T5DSfi2hVzPSvsoGLE8rLCGVUT3CI5KeSHKPz/IOHQ4mvRk+KyykIzFKXvVPCYg== 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== 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 7120D606A32C for ; Wed, 2 Dec 2026 18:57:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7120D606A32C 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-3866a784546so18641168eec.0 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=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) 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: netdev@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