Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210930061634.1840768-1-memxor@gmail.com

authored by

Kumar Kartikeya Dwivedi and committed by
Daniel Borkmann
4729445b 30e29a9a

+2 -1
+2 -1
tools/lib/bpf/libbpf.c
··· 6894 6894 6895 6895 if (obj->gen_loader) { 6896 6896 /* reset FDs */ 6897 - btf__set_fd(obj->btf, -1); 6897 + if (obj->btf) 6898 + btf__set_fd(obj->btf, -1); 6898 6899 for (i = 0; i < obj->nr_maps; i++) 6899 6900 obj->maps[i].fd = -1; 6900 6901 if (!err)