Skip to contentSkip to conversation

net: init sk_refcnt, sk_state, sk_bpf_storage on clone

PATCH In review Weiming Shi 2 participants 2 messages Dec 2 – Dec 3

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: init sk_refcnt, sk_state, sk_bpf_storage on clone

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")
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(-)
include/net/bpf_sk_storage.h +7 -0
index 2926f1f00d65..6435c319ef11 100644
@@ -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)
 {
net/core/bpf_sk_storage.c +0 -7
index 1c59300af05c..02203b89e16e 100644
@@ -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;
net/core/sock.c +6 -3
index 5675053625e6..8ae7b6391cd1 100644
@@ -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
Signature · 1 line
2.56.1
#1 lore raw
Quoted 91 lines · Weiming Shi

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")
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(-)
include/net/bpf_sk_storage.h +7 -0
index 2926f1f00d65..6435c319ef11 100644
@@ -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)
 {
net/core/bpf_sk_storage.c +0 -7
index 1c59300af05c..02203b89e16e 100644
@@ -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;
net/core/sock.c +0 -0
index 5675053625e6..8ae7b6391cd1 100644

net/core/sock.c static void sock_copy(struct sock *nsk, const struct sock *osk) · line 2266

Quoted 7 lines
net/core/sock.c +1 -0
@@ -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);

Simply move security_sk_clone() to the end of inet_csk_clone_lock() and before security_sctp_sk_clone().

Passing a not-fully-initialised socket is too error-prone.

---
pw-bot: cr