* [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
@ 2020-03-12 14:03 Quentin Monnet
2020-03-12 14:22 ` Michal Rostecki
2020-03-12 15:37 ` Daniel Borkmann
0 siblings, 2 replies; 7+ messages in thread
From: Quentin Monnet @ 2020-03-12 14:03 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann
Cc: bpf, netdev, Quentin Monnet, Andrii Nakryiko, Michal Rostecki
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 <andriin@fb.com>
Reported-by: Michal Rostecki <mrostecki@opensuse.org>
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
---
tools/lib/bpf/libbpf.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 223be01dc466..19c0c40e8a80 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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);
--
2.20.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
2020-03-12 14:03 [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps() Quentin Monnet
@ 2020-03-12 14:22 ` Michal Rostecki
2020-03-12 15:37 ` Daniel Borkmann
1 sibling, 0 replies; 7+ messages in thread
From: Michal Rostecki @ 2020-03-12 14:22 UTC (permalink / raw)
To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann
Cc: bpf, netdev, Andrii Nakryiko
On 3/12/20 3:03 PM, Quentin Monnet wrote:
> 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 <andriin@fb.com>
> Reported-by: Michal Rostecki <mrostecki@opensuse.org>
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> ---
> tools/lib/bpf/libbpf.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 223be01dc466..19c0c40e8a80 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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);
>
Tested-by: Michal Rostecki <mrostecki@opensuse.org>
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
2020-03-12 14:03 [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps() Quentin Monnet
2020-03-12 14:22 ` Michal Rostecki
@ 2020-03-12 15:37 ` Daniel Borkmann
2020-03-12 16:50 ` Quentin Monnet
2020-03-12 17:54 ` Andrii Nakryiko
1 sibling, 2 replies; 7+ messages in thread
From: Daniel Borkmann @ 2020-03-12 15:37 UTC (permalink / raw)
To: Quentin Monnet, Alexei Starovoitov
Cc: bpf, netdev, Andrii Nakryiko, Michal Rostecki
On 3/12/20 3:03 PM, Quentin Monnet wrote:
> 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 <andriin@fb.com>
> Reported-by: Michal Rostecki <mrostecki@opensuse.org>
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Applied to bpf-next, thanks! Note ...
> ---
> tools/lib/bpf/libbpf.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 223be01dc466..19c0c40e8a80 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
2020-03-12 15:37 ` Daniel Borkmann
@ 2020-03-12 16:50 ` Quentin Monnet
2020-03-12 17:54 ` Andrii Nakryiko
1 sibling, 0 replies; 7+ messages in thread
From: Quentin Monnet @ 2020-03-12 16:50 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov
Cc: bpf, netdev, Andrii Nakryiko, Michal Rostecki
2020-03-12 16:37 UTC+0100 ~ Daniel Borkmann <daniel@iogearbox.net>
> On 3/12/20 3:03 PM, Quentin Monnet wrote:
>> 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 <andriin@fb.com>
>> Reported-by: Michal Rostecki <mrostecki@opensuse.org>
>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>
> Applied to bpf-next, thanks! Note ...
>
>> ---
>> tools/lib/bpf/libbpf.c | 4 ++++
>> 1 file changed, 4 insertions(+)
>>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 223be01dc466..19c0c40e8a80 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
2020-03-12 15:37 ` Daniel Borkmann
2020-03-12 16:50 ` Quentin Monnet
@ 2020-03-12 17:54 ` Andrii Nakryiko
2020-03-12 18:21 ` Daniel Borkmann
1 sibling, 1 reply; 7+ messages in thread
From: Andrii Nakryiko @ 2020-03-12 17:54 UTC (permalink / raw)
To: Daniel Borkmann
Cc: Quentin Monnet, Alexei Starovoitov, bpf, Networking,
Andrii Nakryiko, Michal Rostecki
On Thu, Mar 12, 2020 at 8:38 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> On 3/12/20 3:03 PM, Quentin Monnet wrote:
> > 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 <andriin@fb.com>
> > Reported-by: Michal Rostecki <mrostecki@opensuse.org>
> > Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>
> 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.
>
> > ---
> > tools/lib/bpf/libbpf.c | 4 ++++
> > 1 file changed, 4 insertions(+)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index 223be01dc466..19c0c40e8a80 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
2020-03-12 17:54 ` Andrii Nakryiko
@ 2020-03-12 18:21 ` Daniel Borkmann
2020-03-12 18:34 ` Quentin Monnet
0 siblings, 1 reply; 7+ messages in thread
From: Daniel Borkmann @ 2020-03-12 18:21 UTC (permalink / raw)
To: Andrii Nakryiko
Cc: Quentin Monnet, Alexei Starovoitov, bpf, Networking,
Andrii Nakryiko, Michal Rostecki
On 3/12/20 6:54 PM, Andrii Nakryiko wrote:
> On Thu, Mar 12, 2020 at 8:38 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
>> On 3/12/20 3:03 PM, Quentin Monnet wrote:
>>> 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 <andriin@fb.com>
>>> Reported-by: Michal Rostecki <mrostecki@opensuse.org>
>>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>>
>> 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.
Ok, please send a proper fix for 5327644614a1 then. Tossed off the tree.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps()
2020-03-12 18:21 ` Daniel Borkmann
@ 2020-03-12 18:34 ` Quentin Monnet
0 siblings, 0 replies; 7+ messages in thread
From: Quentin Monnet @ 2020-03-12 18:34 UTC (permalink / raw)
To: Daniel Borkmann, Andrii Nakryiko
Cc: Alexei Starovoitov, bpf, Networking, Andrii Nakryiko, Michal Rostecki
2020-03-12 19:21 UTC+0100 ~ Daniel Borkmann <daniel@iogearbox.net>
> On 3/12/20 6:54 PM, Andrii Nakryiko wrote:
>> On Thu, Mar 12, 2020 at 8:38 AM Daniel Borkmann <daniel@iogearbox.net>
>> wrote:
>>> On 3/12/20 3:03 PM, Quentin Monnet wrote:
>>>> 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 <andriin@fb.com>
>>>> Reported-by: Michal Rostecki <mrostecki@opensuse.org>
>>>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>>>
>>> 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.
>
> 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
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2020-03-12 18:35 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-12 14:03 [PATCH bpf] libbpf: add null pointer check in bpf_object__init_user_btf_maps() Quentin Monnet
2020-03-12 14:22 ` Michal Rostecki
2020-03-12 15:37 ` Daniel Borkmann
2020-03-12 16:50 ` Quentin Monnet
2020-03-12 17:54 ` Andrii Nakryiko
2020-03-12 18:21 ` Daniel Borkmann
2020-03-12 18:34 ` Quentin Monnet
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).