From: Weiming Shi <bestswngs@gmail.com>
To: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Willem de Bruijn <willemb@google.com>,
Neal Cardwell <ncardwell@google.com>,
Matthieu Baerts <matttbe@kernel.org>,
Mat Martineau <martineau@kernel.org>,
Geliang Tang <geliang@kernel.org>,
Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
Xin Long <lucien.xin@gmail.com>
Cc: netdev@vger.kernel.org, mptcp@lists.linux.dev,
linux-sctp@vger.kernel.org, bpf@vger.kernel.org,
co+c4bff1ab7d214997@bugs.sh, Xiang Mei <xmei5@asu.edu>,
Weiming Shi <bestswngs@gmail.com>
Subject: [PATCH net v2] net: init sk_refcnt, sk_state, sk_bpf_storage on clone
Date: Sat, 5 Dec 2026 09:20:58 -0800 [thread overview]
Message-ID: <20261205172057.53933-2-bestswngs@gmail.com> (raw)
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:
<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. 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: <co+c4bff1ab7d214997@bugs.sh>
Assisted-by: LLM
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
---
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
reply other threads:[~2026-12-05 17:21 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205172057.53933-2-bestswngs@gmail.com \
--to=bestswngs@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=co+c4bff1ab7d214997@bugs.sh \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=geliang@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-sctp@vger.kernel.org \
--cc=lucien.xin@gmail.com \
--cc=marcelo.leitner@gmail.com \
--cc=martineau@kernel.org \
--cc=matttbe@kernel.org \
--cc=mptcp@lists.linux.dev \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemb@google.com \
--cc=xmei5@asu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox