Skip to contentSkip to conversation

sock_map: fix a NULL pointer dereference in sock_map_link_update_prog()

PATCH Applied Cong Wang 4 participants 4 messages Oct 26, 2024 – Oct 29, 2024

Subsystems (5)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 sock_map: fix a NULL pointer dereference in sock_map_link_update_prog()

From: Cong Wang <[email protected]>

The following race condition could trigger a NULL pointer dereference:

sock_map_link_detach(): sock_map_link_update_prog():
mutex_lock(&sockmap_mutex);
...
sockmap_link->map = NULL;
mutex_unlock(&sockmap_mutex);

   				   mutex_lock(&sockmap_mutex);
				   ...
				   sock_map_prog_link_lookup(sockmap_link->map);
				   mutex_unlock(&sockmap_mutex);

<continue>

Fix it by adding a NULL pointer check. In this specific case, it makes no sense to update a link which is being released.

Fixes 699c23f02c65 ("bpf: Add bpf_link support for sk_msg and sk_skb progs")
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jakub Sitnicki <[email protected]>
---
 net/core/sock_map.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/sock_map.c +4 -0
index 07d6aa4e39ef..9fca4db52f57 100644
@@ -1760,6 +1760,10 @@ static int sock_map_link_update_prog(struct bpf_link *link,
 		ret = -EINVAL;
 		goto out;
 	}
+	if (!sockmap_link->map) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	ret = sock_map_prog_link_lookup(sockmap_link->map, &pprog, &plink,
 					sockmap_link->attach_type);
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 33 lines · Cong Wang

From: Cong Wang <[email protected]>

The following race condition could trigger a NULL pointer dereference:

sock_map_link_detach(): sock_map_link_update_prog():

    mutex_lock(&sockmap_mutex);
    ...
    sockmap_link->map = NULL;
    mutex_unlock(&sockmap_mutex);
    				   mutex_lock(&sockmap_mutex);
				   ...
				   sock_map_prog_link_lookup(sockmap_link->map);
				   mutex_unlock(&sockmap_mutex);
    <continue>

Fix it by adding a NULL pointer check. In this specific case, it makes no sense to update a link which is being released.

Fixes 699c23f02c65 ("bpf: Add bpf_link support for sk_msg and sk_skb progs")
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jakub Sitnicki <[email protected]>
---
  net/core/sock_map.c | 4 ++++
  1 file changed, 4 insertions(+)
net/core/sock_map.c +0 -0
index 07d6aa4e39ef..9fca4db52f57 100644

net/core/sock_map.c static int sock_map_link_update_prog(struct bpf_link *link, · line 1764

Quoted 5 lines
net/core/sock_map.c +2 -0
@@ -1760,6 +1760,10 @@ static int sock_map_link_update_prog(struct bpf_link *link,
  		ret = -EINVAL;
  		goto out;
  	}
+	if (!sockmap_link->map) {
+		ret = -EINVAL;

Thanks for the fix. Maybe we should use -ENOENT as the return error code? In this case, update_prog failed due to sockmap_link->map == NULL which is equivalent to no 'entry' to update.

Quoted 5 lines
net/core/sock_map.c +2 -0
+		goto out;
+	}
 
  	ret = sock_map_prog_link_lookup(sockmap_link->map, &pprog, &plink,
  					sockmap_link->attach_type);
#2 lore raw
Quoted 30 lines · Yonghong Song
Quoted 28 lines · Cong Wang

From: Cong Wang <[email protected]>

The following race condition could trigger a NULL pointer dereference:

sock_map_link_detach():        sock_map_link_update_prog():
    mutex_lock(&sockmap_mutex);
    ...
    sockmap_link->map = NULL; mutex_unlock(&sockmap_mutex); mutex_lock(&sockmap_mutex);
                   ...
                   sock_map_prog_link_lookup(sockmap_link->map); mutex_unlock(&sockmap_mutex); <continue>

Fix it by adding a NULL pointer check. In this specific case, it makes no sense to update a link which is being released.

Fixes 699c23f02c65 ("bpf: Add bpf_link support for sk_msg and sk_skb progs")
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jakub Sitnicki <[email protected]>

---
  net/core/sock_map.c | 4 ++++

  1 file changed, 4 insertions(+)
Quoted 15 lines

net/core/sock_map.c static int sock_map_link_update_prog(struct bpf_link · line 1765

Quoted 11 lines
net/core/sock_map.c +2 -0
index 07d6aa4e39ef..9fca4db52f57 100644
@@ -1760,6 +1760,10 @@ static int sock_map_link_update_prog(struct bpf_link 
 *link,
           ret = -EINVAL;
           goto out;
       }
+    if (!sockmap_link->map) {
+        ret = -EINVAL;

Thanks for the fix. Maybe we should use -ENOENT as the return error code? In this case, update_prog failed due to sockmap_link->map == NULL which is equivalent to no 'entry' to update.

The fix lgtm. Regarding the error value, the tcx/bpf_struct_ops/cgroup's update_prog uses -ENOLINK. I changed it to -ENOLINK for consistency. Applied. Thanks.

Quoted 6 lines
Quoted 5 lines
net/core/sock_map.c +2 -0
+        goto out;
+    }
       ret = sock_map_prog_link_lookup(sockmap_link->map, &pprog, &plink,
                       sockmap_link->attach_type);
#3 lore raw
In reply to Cong Wang
This patch was applied to bpf/bpf.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Martin KaFai Lau <[email protected]>:

Quoted 15 lines · On Sat, 26 Oct 2024 11:55:22 -0700 you wrote:

From: Cong Wang <[email protected]>

The following race condition could trigger a NULL pointer dereference:

sock_map_link_detach(): sock_map_link_update_prog():

   mutex_lock(&sockmap_mutex);
   ...
   sockmap_link->map = NULL;
   mutex_unlock(&sockmap_mutex);
   				   mutex_lock(&sockmap_mutex);
				   ...
				   sock_map_prog_link_lookup(sockmap_link->map);
				   mutex_unlock(&sockmap_mutex);
   <continue>
Quoted 1 line

[...]

Here is the summary with links:
- [bpf] sock_map: fix a NULL pointer dereference in sock_map_link_update_prog()

    https://git.kernel.org/bpf/bpf/c/740be3b9a6d7

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html