Skip to contentSkip to conversation

libbpf: Fix segfault in light skeleton for objects without BTF

PATCH In review Kumar Kartikeya Dwivedi 2 participants 2 messages Sep 30, 2021

Subsystems (2)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

PATCH 1/1 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 67234743736a (libbpf: Generate loader program out of BPF ELF file.)
---
 tools/lib/bpf/libbpf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +2 -1
index 0f85d441fca3..b6b130171b86 100644
@@ -7031,7 +7031,8 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
 
 	if (obj->gen_loader) {
 		/* reset FDs */
-		btf__set_fd(obj->btf, -1);
+		if (obj->btf)
+			btf__set_fd(obj->btf, -1);
 		for (i = 0; i < obj->nr_maps; i++)
 			obj->maps[i].fd = -1;
 		if (!err)

--
2.33.0

maintainer
#1 lore raw
Quoted 13 lines · Kumar Kartikeya Dwivedi

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;
Quoted 7 lines

Segmentation fault (core dumped)

The same occurs for files compiled without BTF info, i.e. without clang's -g flag.

Fixes 67234743736a (libbpf: Generate loader program out of BPF ELF file.)

Looks like patchwork-bot is on vacation right now, this was applied thanks!