libbpf: Avoid NULL deref when initializing map BTF info
authorAndrii Nakryiko <andrii@kernel.org>
Sun, 20 Mar 2022 00:19:11 +0000 (17:19 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 21 Mar 2022 01:53:04 +0000 (18:53 -0700)
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>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220320001911.3640917-1-andrii@kernel.org
tools/lib/bpf/libbpf.c

index 7526419..2efe943 100644 (file)
@@ -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.