Skip to contentSkip to conversation

libbpf: add null pointer check in bpf_object__init_user_btf_maps()

PATCH Applied Quentin Monnet 4 participants 7 messages Mar 12, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: add null pointer check in bpf_object__init_user_btf_maps()

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is no .BTF section.

Add a null pointer check and error out if the pointer is null. The final bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Fixes abd29c931459 ("libbpf: allow specifying map definitions using BTF")
Cc Andrii Nakryiko <[email protected]>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
index 223be01dc466..19c0c40e8a80 100644
@@ -2140,6 +2140,10 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
 		return -EINVAL;
 	}
 
+	if (!obj->btf) {
+		pr_warn("failed to retrieve BTF for map");
+		return -EINVAL;
+	}
 	nr_types = btf__get_nr_types(obj->btf);
 	for (i = 1; i <= nr_types; i++) {
 		t = btf__type_by_id(obj->btf, i);
Signature · 1 line
2.20.1
Tested-by on v1 1/1
#1 lore raw
Quoted 33 lines · Quentin Monnet

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is no .BTF section.

Add a null pointer check and error out if the pointer is null. The final bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Fixes abd29c931459 ("libbpf: allow specifying map definitions using BTF")
Cc Andrii Nakryiko <[email protected]>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 223be01dc466..19c0c40e8a80 100644

tools/lib/bpf/libbpf.c static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, · line 2149

Quoted 10 lines
tools/lib/bpf/libbpf.c +4 -0
@@ -2140,6 +2140,10 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
 		return -EINVAL;
 	}
 
+	if (!obj->btf) {
+		pr_warn("failed to retrieve BTF for map");
+		return -EINVAL;
+	}
 	nr_types = btf__get_nr_types(obj->btf);
 	for (i = 1; i <= nr_types; i++) {
 		t = btf__type_by_id(obj->btf, i);

Thanks!

maintainer Applied
#2 lore raw
In reply to Quentin Monnet
Quoted 15 lines · Quentin Monnet

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is no .BTF section.

Quoted 8 lines

Add a null pointer check and error out if the pointer is null. The final bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Fixes abd29c931459 ("libbpf: allow specifying map definitions using BTF")
Cc Andrii Nakryiko <[email protected]>

Applied to bpf-next, thanks! Note ...

Quoted 9 lines
---
  tools/lib/bpf/libbpf.c | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 223be01dc466..19c0c40e8a80 100644

tools/lib/bpf/libbpf.c static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, · line 2144

Quoted 5 lines
tools/lib/bpf/libbpf.c +2 -0
@@ -2140,6 +2140,10 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
  		return -EINVAL;
  	}
 
+	if (!obj->btf) {
+		pr_warn("failed to retrieve BTF for map");

I've added a '\n' here, otherwise it looks like:

[...]

  LINK     _bpftool
  CLANG    skeleton/profiler.bpf.o
  GEN      profiler.skel.h

libbpf: failed to retrieve BTF for mapError: failed to open BPF object file: 0 Makefile:129: recipe for target 'profiler.skel.h' failed

Fixed version:

LINK _bpftool
GEN profiler.skel.h
libbpf: failed to retrieve BTF for map
Error: failed to open BPF object file: 0 Makefile:129: recipe for target 'profiler.skel.h' failed

Thanks,
Daniel

#3 lore raw

2020-03-12 16:37 UTC+0100 ~ Daniel Borkmann <[email protected]>

Quoted 32 lines · Quentin Monnet
Quoted 19 lines

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

     ./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is no .BTF section.

Add a null pointer check and error out if the pointer is null. The final bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Quoted 5 lines
Fixes abd29c931459 ("libbpf: allow specifying map definitions using

BTF")

Applied to bpf-next, thanks! Note ...

Quoted 4 lines

---
  tools/lib/bpf/libbpf.c | 4 ++++

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

tools/lib/bpf/libbpf.c static int · line 2144

Quoted 10 lines
tools/lib/bpf/libbpf.c +1 -0
index 223be01dc466..19c0c40e8a80 100644
@@ -2140,6 +2140,10 @@ static int
 bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
           return -EINVAL;
       }
   +    if (!obj->btf) {
+        pr_warn("failed to retrieve BTF for map");

I've added a '\n' here

Sorry about that, thank you Daniel!
Quentin

#4 lore raw
In reply to Daniel Borkmann
Quoted 21 lines · Daniel Borkmann
Quoted 19 lines · Quentin Monnet

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is no .BTF section.

Add a null pointer check and error out if the pointer is null. The final bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Quoted 6 lines
Quoted 4 lines
Fixes abd29c931459 ("libbpf: allow specifying map definitions using BTF")
Cc Andrii Nakryiko <[email protected]>

Applied to bpf-next, thanks! Note ...

I don't think this is the right fix. The problem was in my 5327644614a1 ("libbpf: Relax check whether BTF is mandatory") commit. I've removed "mandatory" status of BTF if .maps is present. But that's not right. We have the need for BTF at two levels: for libbpf itself and for kernel, those are overlapping, but not exactly the same. BTF is needed for libbpf when .maps, .struct_ops and externs are present. But kernel needs it only for when .struct_ops are present. Right now those checks are conflated together. Proper fix would be to separate them. Can we please undo this patch? I'll post a proper fix shortly.

Quoted 35 lines
Quoted 10 lines
---
  tools/lib/bpf/libbpf.c | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 223be01dc466..19c0c40e8a80 100644

tools/lib/bpf/libbpf.c static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, · line 2144

Quoted 5 lines
tools/lib/bpf/libbpf.c +2 -0
@@ -2140,6 +2140,10 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
              return -EINVAL;
      }
 
+     if (!obj->btf) {
+             pr_warn("failed to retrieve BTF for map");

I've added a '\n' here, otherwise it looks like:

[...]

   LINK     _bpftool
   CLANG    skeleton/profiler.bpf.o
   GEN      profiler.skel.h

libbpf: failed to retrieve BTF for mapError: failed to open BPF object file: 0 Makefile:129: recipe for target 'profiler.skel.h' failed

Fixed version:

LINK _bpftool
GEN profiler.skel.h
libbpf: failed to retrieve BTF for map
Error: failed to open BPF object file: 0 Makefile:129: recipe for target 'profiler.skel.h' failed

Thanks,
Daniel

maintainer
#5 lore raw
Quoted 28 lines · Andrii Nakryiko
Quoted 20 lines · Daniel Borkmann
Quoted 19 lines · Quentin Monnet

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is no .BTF section.

Add a null pointer check and error out if the pointer is null. The final bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Quoted 6 lines
Quoted 4 lines
Fixes abd29c931459 ("libbpf: allow specifying map definitions using BTF")
Cc Andrii Nakryiko <[email protected]>

Applied to bpf-next, thanks! Note ...

Quoted 9 lines

I don't think this is the right fix. The problem was in my 5327644614a1 ("libbpf: Relax check whether BTF is mandatory") commit. I've removed "mandatory" status of BTF if .maps is present. But that's not right. We have the need for BTF at two levels: for libbpf itself and for kernel, those are overlapping, but not exactly the same. BTF is needed for libbpf when .maps, .struct_ops and externs are present. But kernel needs it only for when .struct_ops are present. Right now those checks are conflated together. Proper fix would be to separate them. Can we please undo this patch? I'll post a proper fix shortly.

Ok, please send a proper fix for 5327644614a1 then. Tossed off the tree.

#6 lore raw

2020-03-12 19:21 UTC+0100 ~ Daniel Borkmann <[email protected]>

Quoted 44 lines · Andrii Nakryiko
Quoted 33 lines
Quoted 23 lines · Daniel Borkmann
Quoted 22 lines · Quentin Monnet

When compiling bpftool with clang 7, after the addition of its recent "bpftool prog profile" feature, Michal reported a segfault. This occurred while the build process was attempting to generate the skeleton needed for the profiling program, with the following command:

      ./_bpftool gen skeleton skeleton/profiler.bpf.o > profiler.skel.h

Tracing the error showed that bpf_object__init_user_btf_maps() does no verification on obj->btf before passing it to btf__get_nr_types(), where
btf is dereferenced. Libbpf considers BTF information should be here because of the presence of a ".maps" section in the object file (hence the check on "obj->efile.btf_maps_shndx < 0" fails and we do not exit from the function early), but it was unable to load BTF info as there is
no .BTF section.

Add a null pointer check and error out if the pointer is null. The final
bpftool executable still fails to build, but at least we have a proper error and no more segfault.

Quoted 7 lines
Quoted 5 lines
Fixes abd29c931459 ("libbpf: allow specifying map definitions using

BTF")

Applied to bpf-next, thanks! Note ...

Quoted 9 lines

I don't think this is the right fix. The problem was in my 5327644614a1 ("libbpf: Relax check whether BTF is mandatory") commit. I've removed "mandatory" status of BTF if .maps is present. But that's not right. We have the need for BTF at two levels: for libbpf itself and for kernel, those are overlapping, but not exactly the same. BTF is needed for libbpf when .maps, .struct_ops and externs are present. But kernel needs it only for when .struct_ops are present. Right now those checks are conflated together. Proper fix would be to separate them. Can we please undo this patch? I'll post a proper fix shortly.

Quoted 1 line

Ok, please send a proper fix for 5327644614a1 then. Tossed off the tree.

I suspected there was something like this and was only mildly satisfied with my solution to be honest... Thank you Andrii for taking over!

Quentin