Skip to contentSkip to conversation

bpf: Fix NULL pointer dereference in bpf_sk_storage_clone and diag paths

PATCH Superseded Weiming Shi 3 participants 3 messages Apr 20 – Apr 21

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Fix NULL pointer dereference in bpf_sk_storage_clone and diag paths

bpf_selem_unlink_nofail() sets SDATA(selem)->smap to NULL before removing the selem from the storage hlist. A concurrent RCU reader in bpf_sk_storage_clone() can observe the selem still on the list with smap already NULL, causing a NULL pointer dereference.

 general protection fault, probably for non-canonical address 0xdffffc000000000a:
 KASAN: null-ptr-deref in range [0x0000000000000050-0x0000000000000057]
 RIP: 0010:bpf_sk_storage_clone+0x1cd/0xaa0 net/core/bpf_sk_storage.c:174
 Call Trace:
  <IRQ>
  sk_clone+0xfed/0x1980 net/core/sock.c:2591
  inet_csk_clone_lock+0x30/0x760 net/ipv4/inet_connection_sock.c:1222
  tcp_create_openreq_child+0x35/0x2680 net/ipv4/tcp_minisocks.c:571
  tcp_v4_syn_recv_sock+0x123/0xf90 net/ipv4/tcp_ipv4.c:1729
  tcp_check_req+0x8e1/0x2580 include/net/tcp.h:855
  tcp_v4_rcv+0x1845/0x3b80 net/ipv4/tcp_ipv4.c:2347

While at it, also add NULL checks in bpf_sk_storage_diag_put_all() and diag_get() which have the same unprotected dereference pattern and could theoretically hit the same race during an inet_diag dump.

Fixes 5d800f87d0a5 ("bpf: Support lockless unlink when freeing map or local storage")
---
 net/core/bpf_sk_storage.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/core/bpf_sk_storage.c +5 -1
index f8338acebf077..3b487280f50fa 100644
@@ -172,7 +172,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
 		struct bpf_map *map;
 
 		smap = rcu_dereference(SDATA(selem)->smap);
-		if (!(smap->map.map_flags & BPF_F_CLONE))
+		if (!smap || !(smap->map.map_flags & BPF_F_CLONE))
 			continue;
 
 		/* Note that for lockless listeners adding new element
@@ -547,6 +547,8 @@ static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb)
 		return -EMSGSIZE;
 
 	smap = rcu_dereference(sdata->smap);
+	if (!smap)
+		goto errout;
 	if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id))
 		goto errout;
 
@@ -599,6 +601,8 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,
 	saved_len = skb->len;
 	hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
 		smap = rcu_dereference(SDATA(selem)->smap);
+		if (!smap)
+			continue;
 		diag_size += nla_value_size(smap->map.value_size);
 
 		if (nla_stgs && diag_get(SDATA(selem), skb))
Signature · 1 line
2.43.0
#1 lore raw
Quoted 14 lines · Weiming Shi
net/core/bpf_sk_storage.c +1 -1
index f8338acebf077..3b487280f50fa 100644
@@ -172,7 +172,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
 		struct bpf_map *map;
 
 		smap = rcu_dereference(SDATA(selem)->smap);
-		if (!(smap->map.map_flags & BPF_F_CLONE))
+		if (!smap || !(smap->map.map_flags & BPF_F_CLONE))
 			continue;
 
 		/* Note that for lockless listeners adding new element

net/core/bpf_sk_storage.c static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb) · line 551

Quoted 5 lines
net/core/bpf_sk_storage.c +2 -0
@@ -547,6 +547,8 @@ static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb)
 		return -EMSGSIZE;
 
 	smap = rcu_dereference(sdata->smap);
+	if (!smap)
+		goto errout;

You need to study it more thoroughly and the code around it instead of rushing to fix a problem discovered by AI/bot (?).

This is now treated as an -EMSGSIZE error by diag_get().

net/core/bpf_sk_storage.c static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb, · line 608

Quoted 12 lines
net/core/bpf_sk_storage.c +2 -0
 	if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id))
 		goto errout;
 
@@ -599,6 +601,8 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,
 	saved_len = skb->len;
 	hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
 		smap = rcu_dereference(SDATA(selem)->smap);
+		if (!smap)
+			continue;
 		diag_size += nla_value_size(smap->map.value_size);
 
 		if (nla_stgs && diag_get(SDATA(selem), skb))

... and here it will eventually return an -EMSGSIZE to the user space which is incorrect. Pass the smap to diag_get instead.

pw-bot: cr

#2 lore raw
In reply to Weiming Shi
Sashiko AI review found 1 potential issue(s): - [Medium] diag_get() returns -EMSGSIZE when a storage element is concurrently deleted, which falsely signals an out-of-buffer condition to the netlink…

Sashiko AI review found 1 potential issue(s):
- [Medium] diag_get() returns -EMSGSIZE when a storage element is concurrently deleted, which falsely signals an out-of-buffer condition to the netlink subsystem. --

commit 522b6760e7969af1ad0bb15843d5c06084d686f7
Author: Weiming Shi <[email protected]>

bpf: Fix NULL pointer dereference in bpf_sk_storage_clone and diag paths

This commit addresses a NULL pointer dereference in bpf_sk_storage_clone and diagnostic paths by adding checks for when a concurrent RCU reader observes a storage element with a NULL smap.

Quoted 4 lines
net/core/bpf_sk_storage.c +0 -0
index 14eb7812bda4a..3e21d79558154 100644

[ ... ]

net/core/bpf_sk_storage.c static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb) · line 548

Quoted 6 lines
net/core/bpf_sk_storage.c +2 -0
@@ -544,6 +544,8 @@ static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb)
 		return -EMSGSIZE;
 
 	smap = rcu_dereference(sdata->smap);
+	if (!smap)
+		goto errout;

When a storage element is concurrently deleted, smap evaluates to NULL here and goes to errout, which returns -EMSGSIZE.

In netlink diagnostics, doesn't -EMSGSIZE specifically signal an out-of-buffer condition?

Could propagating -EMSGSIZE for a concurrently deleted element cause socket dumps to unnecessarily abort the current message and retry, or cause exact-match diagnostic queries to falsely fail with an EMSGSIZE error returned to userspace?

Would it be better to return 0 after cancelling the nested attribute to gracefully skip the deleted element, or perhaps pass the already validated smap from the caller directly into diag_get()?

Quoted 2 lines
net/core/bpf_sk_storage.c +0 -0
 	if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id))
 		goto errout;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1