sock_map: fix a NULL pointer dereference in sock_map_link_update_prog()
Subsystems (5)
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.
--- net/core/sock_map.c | 4 ++++ 1 file changed, 4 insertions(+)
net/core/sock_map.c +4 -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 ; + goto out; + } ret =sock_map_prog_link_lookup (sockmap_link->map, &pprog, &plink, sockmap_link->attach_type);
Signature · 1 line
2.34.1
Yonghong Song
reviewerQuoted 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.
Reported-by Ruan Bonan <[email protected]>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]>Signed-off-by Cong Wang <[email protected]>--- net/core/sock_map.c | 4 ++++ 1 file changed, 4 insertions(+)net/core/sock_map.c +0
-0
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);
Martin KaFai Lau
reviewerQuoted 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.
Reported-by Ruan Bonan <[email protected]>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]>Signed-off-by Cong Wang <[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@@ -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
-ENOENTas the return error code? In this case,update_progfailed due tosockmap_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);
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