Skip to contentSkip to conversation

libbpf: avoid NULL deref when initializing map BTF info

PATCH Applied Andrii Nakryiko 4 participants 4 messages Mar 20, 2022 – Mar 21, 2022

Subsystems (2)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 libbpf: avoid NULL deref when initializing map BTF info

If BPF object doesn't have an BTF info, don't attempt to search for BTF types describing BPF map key or value layout.

Fixes 262cfb74ffda ("libbpf: Init btf_{key,value}_type_id on internal map open")
---
 tools/lib/bpf/libbpf.c | 3 +++
 1 file changed, 3 insertions(+)
tools/lib/bpf/libbpf.c +3 -0
index 7526419e59e0..2efe9431c1ba 100644
@@ -4197,6 +4197,9 @@ static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
 	__u32 key_type_id = 0, value_type_id = 0;
 	int ret;
 
+	if (!obj->btf)
+		return -ENOENT;
+
 	/* if it's BTF-defined map, we don't need to search for type IDs.
 	 * For struct_ops map, it does not need btf_key_type_id and
 	 * btf_value_type_id.
Signature · 1 line
2.30.2
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Andrii Nakryiko

If BPF object doesn't have an BTF info, don't attempt to search for BTF types describing BPF map key or value layout.

Fixes 262cfb74ffda ("libbpf: Init btf_{key,value}_type_id on internal map open")

So without this patch, we may have segfault or other errors, right? It would be good to specify that in the commit message.

The code change looks good to me.

Quoted 18 lines
---
  tools/lib/bpf/libbpf.c | 3 +++
  1 file changed, 3 insertions(+)
tools/lib/bpf/libbpf.c +3 -0
index 7526419e59e0..2efe9431c1ba 100644
@@ -4197,6 +4197,9 @@ static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
  	__u32 key_type_id = 0, value_type_id = 0;
  	int ret;
 
+	if (!obj->btf)
+		return -ENOENT;
+
  	/* if it's BTF-defined map, we don't need to search for type IDs.
  	 * For struct_ops map, it does not need btf_key_type_id and
  	 * btf_value_type_id.
#2 lore raw
Quoted 29 lines · Yonghong Song
Quoted 5 lines · Andrii Nakryiko

If BPF object doesn't have an BTF info, don't attempt to search for BTF types describing BPF map key or value layout.

Fixes 262cfb74ffda ("libbpf: Init btf_{key,value}_type_id on internal map open")

So without this patch, we may have segfault or other errors, right? It would be good to specify that in the commit message.

The code change looks good to me.

Quoted 12 lines
---
  tools/lib/bpf/libbpf.c | 3 +++
  1 file changed, 3 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 7526419e59e0..2efe9431c1ba 100644
@@ -4197,6 +4197,9 @@ static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
      __u32 key_type_id = 0, value_type_id = 0;
      int ret;
 
Quoted 3 lines

tools/lib/bpf/libbpf.c static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map) · line 4202

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+     if (!obj->btf)
+             return -ENOENT;
+

yeah, it was SIGSEGV, which I only saw in libbpf CI on old 5.5 kernel. I'll include a bit more specifics for future fixes, sorry.

Quoted 3 lines
Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
      /* if it's BTF-defined map, we don't need to search for type IDs.
       * For struct_ops map, it does not need btf_key_type_id and
       * btf_value_type_id.
#3 lore raw
In reply to Andrii Nakryiko
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 8 lines · On Sat, 19 Mar 2022 17:19:11 -0700 you wrote:

If BPF object doesn't have an BTF info, don't attempt to search for BTF types describing BPF map key or value layout.

Fixes 262cfb74ffda ("libbpf: Init btf_{key,value}_type_id on internal map open")
---
 tools/lib/bpf/libbpf.c | 3 +++
 1 file changed, 3 insertions(+)

Here is the summary with links:
- [bpf-next] libbpf: avoid NULL deref when initializing map BTF info

    https://git.kernel.org/bpf/bpf-next/c/a8fee96202e2

You are awesome, thank you!

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