Blob Blame History Raw
From: Andrii Nakryiko <andrii@kernel.org>
Date: Sat, 19 Mar 2022 17:19:11 -0700
Subject: libbpf: Avoid NULL deref when initializing map BTF info
Patch-mainline: v5.18-rc1
Git-commit: a8fee96202e279441d0e52d83eb100bd4a6d6272
References: jsc#PED-1377

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
Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
---
 tools/lib/bpf/libbpf.c |    3 +++
 1 file changed, 3 insertions(+)

--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4197,6 +4197,9 @@ static int bpf_map_find_btf_info(struct
 	__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.