]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
libbpf: Fix null pointer dereference in btf_dump__free on allocation failure
authorYuan Chen <chenyuan@kylinos.cn>
Wed, 18 Jun 2025 01:19:33 +0000 (09:19 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 23 Jun 2025 18:13:40 +0000 (11:13 -0700)
When btf_dump__new() fails to allocate memory for the internal hashmap
(btf_dump->type_names), it returns an error code. However, the cleanup
function btf_dump__free() does not check if btf_dump->type_names is NULL
before attempting to free it. This leads to a null pointer dereference
when btf_dump__free() is called on a btf_dump object.

Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250618011933.11423-1-chenyuan_fl@163.com
tools/lib/bpf/btf_dump.c

index 460c3e57fadb6420ab287dd1a15b1d3297c6bd29..0381f209920a660f44b5678706139c41df4184ad 100644 (file)
@@ -226,6 +226,9 @@ static void btf_dump_free_names(struct hashmap *map)
        size_t bkt;
        struct hashmap_entry *cur;
 
+       if (!map)
+               return;
+
        hashmap__for_each_entry(map, cur, bkt)
                free((void *)cur->pkey);