Skip to contentSkip to conversation

bpf: Check IS_ERR for the bpf_map_get() return value

PATCH Applied Martin KaFai Lau 4 participants 4 messages Mar 24, 2023

Subsystems (2)
lore
Share & tools
lore

Martin KaFai Lau

author reviewer
#0 lore raw

PATCH 1/1 bpf: Check IS_ERR for the bpf_map_get() return value

From: Martin KaFai Lau <[email protected]>

This patch fixes a mistake in checking NULL instead of checking IS_ERR for the bpf_map_get() return value.

It also fixes the return value in link_update_map() from -EINVAL to PTR_ERR(*_map).

Reported-by [email protected]
Fixes 68b04864ca42 ("bpf: Create links for BPF struct_ops maps.")
Fixes aef56f2e918b ("bpf: Update the struct_ops of a bpf_link.")
---
 kernel/bpf/bpf_struct_ops.c | 4 ++--
 kernel/bpf/syscall.c        | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/bpf_struct_ops.c +2 -2
index 6401deca3b56..d3f0a4825fa6 100644
@@ -871,8 +871,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
 	int err;
 
 	map = bpf_map_get(attr->link_create.map_fd);
-	if (!map)
-		return -EINVAL;
+	if (IS_ERR(map))
+		return PTR_ERR(map);
 
 	st_map = (struct bpf_struct_ops_map *)map;
 
kernel/bpf/syscall.c +2 -2
index b4d758fa5981..a09597c95029 100644
@@ -4689,12 +4689,12 @@ static int link_update_map(struct bpf_link *link, union bpf_attr *attr)
 
 	new_map = bpf_map_get(attr->link_update.new_map_fd);
 	if (IS_ERR(new_map))
-		return -EINVAL;
+		return PTR_ERR(new_map);
 
 	if (attr->link_update.flags & BPF_F_REPLACE) {
 		old_map = bpf_map_get(attr->link_update.old_map_fd);
 		if (IS_ERR(old_map)) {
-			ret = -EINVAL;
+			ret = PTR_ERR(old_map);
 			goto out_put;
 		}
 	} else if (attr->link_update.old_map_fd) {
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw

This fix makes sense to me.
Thank you for fixing this.

Quoted 51 lines · Martin KaFai Lau

From: Martin KaFai Lau <[email protected]>

This patch fixes a mistake in checking NULL instead of checking IS_ERR for the bpf_map_get() return value.

It also fixes the return value in link_update_map() from -EINVAL to PTR_ERR(*_map).

Reported-by [email protected]
Fixes 68b04864ca42 ("bpf: Create links for BPF struct_ops maps.")
Fixes aef56f2e918b ("bpf: Update the struct_ops of a bpf_link.")
---
  kernel/bpf/bpf_struct_ops.c | 4 ++--
  kernel/bpf/syscall.c        | 4 ++--
  2 files changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/bpf_struct_ops.c +2 -2
index 6401deca3b56..d3f0a4825fa6 100644
@@ -871,8 +871,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
  	int err;
 
  	map = bpf_map_get(attr->link_create.map_fd);
-	if (!map)
-		return -EINVAL;
+	if (IS_ERR(map))
+		return PTR_ERR(map);
 
  	st_map = (struct bpf_struct_ops_map *)map;
 
kernel/bpf/syscall.c +2 -2
index b4d758fa5981..a09597c95029 100644
@@ -4689,12 +4689,12 @@ static int link_update_map(struct bpf_link *link, union bpf_attr *attr)
 
  	new_map = bpf_map_get(attr->link_update.new_map_fd);
  	if (IS_ERR(new_map))
-		return -EINVAL;
+		return PTR_ERR(new_map);
 
  	if (attr->link_update.flags & BPF_F_REPLACE) {
  		old_map = bpf_map_get(attr->link_update.old_map_fd);
  		if (IS_ERR(old_map)) {
-			ret = -EINVAL;
+			ret = PTR_ERR(old_map);
  			goto out_put;
  		}
  	} else if (attr->link_update.old_map_fd) {
Acked-by on v1 1/1
#2 lore raw
In reply to Martin KaFai Lau
Quoted 5 lines · Martin KaFai Lau

From: Martin KaFai Lau <[email protected]>

This patch fixes a mistake in checking NULL instead of checking IS_ERR for the bpf_map_get() return value.

Quoted 7 lines

It also fixes the return value in link_update_map() from -EINVAL to PTR_ERR(*_map).

Reported-by [email protected]
Fixes 68b04864ca42 ("bpf: Create links for BPF struct_ops maps.")
Fixes aef56f2e918b ("bpf: Update the struct_ops of a bpf_link.")
Quoted 42 lines
---
  kernel/bpf/bpf_struct_ops.c | 4 ++--
  kernel/bpf/syscall.c        | 4 ++--
  2 files changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/bpf_struct_ops.c +2 -2
index 6401deca3b56..d3f0a4825fa6 100644
@@ -871,8 +871,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
  	int err;
 
  	map = bpf_map_get(attr->link_create.map_fd);
-	if (!map)
-		return -EINVAL;
+	if (IS_ERR(map))
+		return PTR_ERR(map);
 
  	st_map = (struct bpf_struct_ops_map *)map;
 
kernel/bpf/syscall.c +2 -2
index b4d758fa5981..a09597c95029 100644
@@ -4689,12 +4689,12 @@ static int link_update_map(struct bpf_link *link,  
 union bpf_attr *attr)
 
  	new_map = bpf_map_get(attr->link_update.new_map_fd);
  	if (IS_ERR(new_map))
-		return -EINVAL;
+		return PTR_ERR(new_map);
 
  	if (attr->link_update.flags & BPF_F_REPLACE) {
  		old_map = bpf_map_get(attr->link_update.old_map_fd);
  		if (IS_ERR(old_map)) {
-			ret = -EINVAL;
+			ret = PTR_ERR(old_map);
  			goto out_put;
  		}

} else if (attr->link_update.old_map_fd) {
--
2.34.1

#3 lore raw
In reply to Martin KaFai Lau
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Fri, 24 Mar 2023 11:42:41 -0700 you wrote:

From: Martin KaFai Lau <[email protected]>

This patch fixes a mistake in checking NULL instead of checking IS_ERR for the bpf_map_get() return value.

It also fixes the return value in link_update_map() from -EINVAL to PTR_ERR(*_map).

[...]

Here is the summary with links:
- [bpf-next] bpf: Check IS_ERR for the bpf_map_get() return value

    https://git.kernel.org/bpf/bpf-next/c/55fbae05476d

You are awesome, thank you!

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