From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796491273; b=yFaZDaTGZf53Xz1VJDzSslnN8roLifW8N4sJa8o1JOUjSIAtR5vpZir4GL0c2Y19HV7p WRoFKajwp/lksGceSWkMNb34l5qjjXXCoICHeQj+D68ROpeBwU3XroT97gqL+Vo5exQLQ wcHFIcUfKJyOI4PEAlExKK/qsIPebIFbCFmLHsYKECrCdn77AZcl5BwT01RG9n5yJvT1j QeF9tcFTDxf6o/EWzYcTNyVnb2nxSBgeG+0AYXwdy5TY3KiXJO1kll2xrCX9ikcwEUZvL mQ4WMKTKTJPMKsPqIKZIxBq+pQMyeOEIWZ3y9lotAx2FZTTwaeSnptoAeUkxsN0YmGg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796491273; 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=Qahst9PiwbeXI1nHthgJvOnVqcpfpW9pOw3dIWZZsFM=; b=yNssf5nmyeXirKP5V70V0nDf66p2JSzHY6UDp0LmUXzGcDc0ku9IhZszPMuydeLKRP/Y 6SLRw3Q2n/ZsOOsMYfL+mAtmK/j7VqeM+u1k85QCVDX4EtBqLEfyAyV/6Lij1rzGWo9Ft I1S0TKIGwd6zbKyXd7w9b9MNj6BMHdfn9FD7PN2h5oiw6VaJjmcPp6QxPVGUDu+jLFTQf ybF3W7k6BTBqRTFSfY2kPv2jVnBGOvwbRjdgoBe7RSq/OihnIKJZbxRJJIFcD1ie/XLv4 YfIJsUFArFGqaMcH1aQH2+0QC2WVNos07mEvRSat/f1+N7/4CowfBRFlpOOGd62f31w== 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=RqnznwnJ; arc=none smtp.remote-ip=74.125.82.46 Received: from mail-dl1-f46.google.com (mail-dl1-f46.google.com [74.125.82.46]) (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 B60FE606A322 for ; Sat, 5 Dec 2026 17:21:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B60FE606A322 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=RqnznwnJ Received: by mail-dl1-f46.google.com with SMTP id a92af1059eb24-170135e43c7so4848230c88.0 for ; Sat, 05 Dec 2026 09:21:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796491271; x=1797096071; 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=Qahst9PiwbeXI1nHthgJvOnVqcpfpW9pOw3dIWZZsFM=; b=RqnznwnJn3tl49/bSaeSQZKjJBCjUoG51lW4OkDbI0GnRQ4rTnOwtZFTsCrXWtJDIX 4Kk1bzrz4NZigLAMdzfC7Hu0vF/YiEj+QjW5V44zZV7Lm3sBhc3uzy262tqBMq+QFRHI xXIs/7noHeSCRBQ1VXPEGi6trhfGdyG5rVLKbaQH8RrFyEq7a5hNUfvfALaSOaYE8zB0 JRs+p1t4+gjITlw26riUCEKRYtJeh+84NjQSfV95OrQalOggp+Xz5DmFQFhAEbI+YUKR XMg+8g+Qwi9nhVxVf9aJyGy27MNzW8VDrmdg0E55Qxd75LRd/NtvLWFGH/QZI1dH1w8m pSWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796491271; x=1797096071; 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=Qahst9PiwbeXI1nHthgJvOnVqcpfpW9pOw3dIWZZsFM=; b=kjItOk0W12pzqwv5fjScIO86nYW2RG6bsigiCGSH4iINa3iMNkeRffSOPoKi8MGvLr bR+z84GdZfgulvAitFA+WSrQ+9lRZeKPkOLBl6IrWJPsGP15KedeKrGywTSZhMsF5oNy Pu6gbXR7RQQwBvo4OunwUur02nAInf04qNUkou/cBHo+fCFlwQAPVstPVoKCWyyPXSkJ EadTIuRnZySQqi1SyEBFfi670Q/NpiCv+k4gB4XHPZfcO1q4dRZGNS+m+TvX3HHS74rt 9WP/VRWJNBs197dEERC5Cc8M7oV9M/nqUernuivmJSAkLX21odcRtY5rowy5HQHXqdAo 6Asg== X-Gm-Message-State: APuU4Z/qgSiCx0hI6+aEGi70YTA4tmaaKaE0R8gBjhba3WQl+m32m2Ps wG3wt+jMFSjKdwt8+gypY9l2GStt7lPpx4HkgoBqxwg1qZYP/k5UEptc X-Gm-Gg: AZFdTZehfiEz4aIGYbvKzfclyZQLXPWD+9BR2sKcn7pxaJKtYKRO+Z5IdCOySUf+k3F qD453jvS42jAb7Wy2F5wSDL9PJiVzn8YzQ2+Z8HqwWLKPU33ApBkJ+zUKG6T31WoWOC+kMJoEzw 9qgHKJ9680u5Yg+SrG5Bs8bZNfx5akgFb4dxxzyMMsAt3dbIoA5RgeCyaHv6Pra4bKah3ywmZeA Xeou03JZo3AROQdraGjBSmIkIYqDvOYvW9wmBN3o/Mf2BCh25UH9QqS/NaydHH6tvM6PeXSxJJ+ tr+M3RxSGV6EozkcIIDomvQ/ovBq0zaYwTK5mFf3lfsh/wlVOeLBvVgaFRx+clV1Xl8Y7iyX4ae N0npmQUcXYHby0h4igtFG//Ih6lNEQ+svbeLSXlVPLIyxNJCu+aqya+DF3BGxi4Y+daXOm3hX6j nFl7eOStVw/dcdYqJPaF0HozkMNswnzg2LD9UqZZoGCu6w22lZNvyOMnIvdXMoHtzxPnYndBTk9 W0U3LwOxiJJEU2xs1y+WpbbjtFcy0MuUU0xM/22vijV7Z7hnMvndahA9TnsyYkI7L+k/skn7Qll WfyHBuQY46QvK/3YYkVo1LbS4W0BDN5u3DtS8VWtO3nS62nPlzYlupARcpHv2e+6vunOogBLRXM +q/5X4kayTZMYeBaqIXn8K1g= X-Received: by 2002:a05:7022:7f0e:b0:1b3:b3de:9232 with SMTP id a92af1059eb24-1b5d70ccbdcmr5215680c88.9.1796491270536; Sat, 05 Dec 2026 09:21:10 -0800 (PST) Received: from Terra.tail0ac356.ts.net ([198.176.50.208]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d6455949sm18562166c88.9.2026.12.05.09.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 09:21:09 -0800 (PST) From: Weiming Shi To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Kuniyuki Iwashima , Willem de Bruijn , Neal Cardwell , Matthieu Baerts , Mat Martineau , Geliang Tang , Marcelo Ricardo Leitner , Xin Long Cc: netdev@vger.kernel.org, mptcp@lists.linux.dev, linux-sctp@vger.kernel.org, bpf@vger.kernel.org, co+c4bff1ab7d214997@bugs.sh, Xiang Mei , Weiming Shi Subject: [PATCH net v2] net: init sk_refcnt, sk_state, sk_bpf_storage on clone Date: Sat, 5 Dec 2026 09:20:58 -0800 Message-ID: <20261205172057.53933-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() while newsk is still half-initialized and newsk->sk_bpf_storage still points to the parent's storage. 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. Remove the conditional refcount_set() in bpf_sk_storage_clone(). Move security_sk_clone() out of sock_copy() to after the clone is initialized and before it is exposed: before security_inet_csk_clone() in inet_csk_clone_lock() and mptcp_sk_clone_init(), and before sctp_sock_migrate() in sctp_clone_sock(). Fixes: 30897832d8b9 ("bpf: Allow local storage to be used from LSM programs") Reported-by: Assisted-by: LLM Signed-off-by: Weiming Shi --- v2: - Move security_sk_clone() out of sock_copy() into inet_csk_clone_lock(), mptcp_sk_clone_init(), and sctp_clone_sock() (Kuniyuki Iwashima). Place it before sctp_sock_migrate() in sctp_clone_sock(), since migration already exposes and unlocks newsk via assoc->base.sk. - Document the caller requirement in sk_clone(). v1: https://lore.kernel.org/netdev/20261202185641.4035600-2-bestswngs@gmail.com/ include/net/bpf_sk_storage.h | 7 +++++++ net/core/bpf_sk_storage.c | 7 ------- net/core/sock.c | 12 ++++++++---- net/ipv4/inet_connection_sock.c | 1 + net/mptcp/protocol.c | 1 + net/sctp/socket.c | 2 ++ 6 files changed, 19 insertions(+), 11 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..a4c0075aa4c4 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2262,7 +2262,6 @@ static void sock_copy(struct sock *nsk, const struct sock *osk) #ifdef CONFIG_SECURITY_NETWORK nsk->sk_security = sptr; - security_sk_clone(osk, nsk); #endif } @@ -2283,6 +2282,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; @@ -2507,6 +2511,8 @@ static void sk_init_common(struct sock *sk) * * If @lock is true, the clone is locked by bh_lock_sock(), and * caller must unlock socket even in error path by bh_unlock_sock(). + * Caller must call security_sk_clone() once the clone is initialized + * and before exposing it to concurrent readers. */ struct sock *sk_clone(const struct sock *sk, const gfp_t priority, bool lock) @@ -2523,9 +2529,7 @@ 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 + bpf_sk_storage_init(newsk); #if IS_ENABLED(CONFIG_INET_PSP) DEBUG_NET_WARN_ON_ONCE(rcu_access_pointer(sk->psp_assoc)); #endif diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index aa0678a1823d..68ad8e198110 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -1277,6 +1277,7 @@ struct sock *inet_csk_clone_lock(const struct sock *sk, inet_clone_ulp(req, newsk, priority); + security_sk_clone(sk, newsk); security_inet_csk_clone(newsk, req); return newsk; diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index bba84018cb67..990133baa18b 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -3767,6 +3767,7 @@ struct sock *mptcp_sk_clone_init(const struct sock *sk, msk->subflow_id = 2; sock_reset_flag(nsk, SOCK_RCU_FREE); + security_sk_clone(sk, nsk); security_inet_csk_clone(nsk, req); /* this can't race with mptcp_close(), as the msk is diff --git a/net/sctp/socket.c b/net/sctp/socket.c index 85ac4952e9cb..9e8c3bdc2083 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -4963,6 +4963,8 @@ static struct sock *sctp_clone_sock(struct sock *sk, sk_sockets_allocated_inc(newsk); sock_prot_inuse_add(sock_net(sk), newsk->sk_prot, 1); + security_sk_clone(sk, newsk); + err = sctp_sock_migrate(sk, newsk, asoc, type); if (err) goto out_release; -- 2.56.1