Skip to content

Commit 4729445

Browse files
kkdwivediborkmann
authored andcommitted
libbpf: Fix segfault in light skeleton for objects without BTF
When fed an empty BPF object, bpftool gen skeleton -L crashes at btf__set_fd() since it assumes presence of obj->btf, however for the sequence below clang adds no .BTF section (hence no BTF). Reproducer: $ touch a.bpf.c $ clang -O2 -g -target bpf -c a.bpf.c $ bpftool gen skeleton -L a.bpf.o /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ /* THIS FILE IS AUTOGENERATED! */ struct a_bpf { struct bpf_loader_ctx ctx; Segmentation fault (core dumped) The same occurs for files compiled without BTF info, i.e. without clang's -g flag. Fixes: 6723474 (libbpf: Generate loader program out of BPF ELF file.) Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 30e29a9 commit 4729445

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6894,7 +6894,8 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
68946894

68956895
if (obj->gen_loader) {
68966896
/* reset FDs */
6897-
btf__set_fd(obj->btf, -1);
6897+
if (obj->btf)
6898+
btf__set_fd(obj->btf, -1);
68986899
for (i = 0; i < obj->nr_maps; i++)
68996900
obj->maps[i].fd = -1;
69006901
if (!err)

0 commit comments

Comments
 (0)