diff mbox series

[bpf-next] libbpf: avoid NULL deref when initializing map BTF info

Message ID 20220320001911.3640917-1-andrii@kernel.org (mailing list archive)
State Accepted
Commit a8fee96202e279441d0e52d83eb100bd4a6d6272
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: avoid NULL deref when initializing map BTF info | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 1 blamed authors not CCed: delyank@fb.com; 7 maintainers not CCed: netdev@vger.kernel.org songliubraving@fb.com yhs@fb.com john.fastabend@gmail.com kafai@fb.com kpsingh@kernel.org delyank@fb.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary

Commit Message

Andrii Nakryiko March 20, 2022, 12:19 a.m. UTC
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")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/libbpf.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Yonghong Song March 20, 2022, 3:37 a.m. UTC | #1
On 3/19/22 5:19 PM, Andrii Nakryiko 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")
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

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.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   tools/lib/bpf/libbpf.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 7526419e59e0..2efe9431c1ba 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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.
patchwork-bot+netdevbpf@kernel.org March 21, 2022, 2 a.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

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")
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> ---
>  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!
Andrii Nakryiko March 21, 2022, 4:58 p.m. UTC | #3
On Sat, Mar 19, 2022 at 8:37 PM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 3/19/22 5:19 PM, Andrii Nakryiko 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")
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
>
> 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.
>
> Acked-by: Yonghong Song <yhs@fb.com>
>
> > ---
> >   tools/lib/bpf/libbpf.c | 3 +++
> >   1 file changed, 3 insertions(+)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index 7526419e59e0..2efe9431c1ba 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -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;
> > +

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.

> >       /* 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.
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 7526419e59e0..2efe9431c1ba 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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.