Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone
@ 2026-12-02 18:56 Weiming Shi
  0 siblings, 0 replies; only message in thread
From: Weiming Shi @ 2026-12-02 18:56 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Kuniyuki Iwashima, Willem de Bruijn
  Cc: netdev, bpf, Xiang Mei, Weiming Shi, co+c4bff1ab7d214997

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02 18:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:56 [PATCH net] net: init sk_refcnt, sk_state, sk_bpf_storage on clone Weiming Shi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox