diff mbox series

[bpf] libbpf: Fix segfault in static linker for objects without BTF

Message ID 20210924023725.70228-1-memxor@gmail.com (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [bpf] libbpf: Fix segfault in static linker for objects without BTF | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf
netdev/subject_prefix success Link
netdev/cc_maintainers fail 3 blamed authors not CCed: yhs@fb.com andrii@kernel.org ast@kernel.org; 12 maintainers not CCed: nathan@kernel.org kpsingh@kernel.org john.fastabend@gmail.com yhs@fb.com daniel@iogearbox.net clang-built-linux@googlegroups.com ndesaulniers@google.com andrii@kernel.org songliubraving@fb.com netdev@vger.kernel.org ast@kernel.org kafai@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 2 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: line length of 84 exceeds 80 columns
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 0
netdev/header_inline success Link
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf success VM_Test

Commit Message

Kumar Kartikeya Dwivedi Sept. 24, 2021, 2:37 a.m. UTC
When a BPF object is compiled without BTF info (without -g),
trying to link such objects using bpftool causes a SIGSEGV due to
btf__get_nr_types accessing obj->btf which is NULL. Fix this by
checking for the NULL pointer, and return error.

Reproducer:
$ cat a.bpf.c
extern int foo(void);
int bar(void) { return foo(); }
$ cat b.bpf.c
int foo(void) { return 0; }
$ clang -O2 -target bpf -c a.bpf.c
$ clang -O2 -target bpf -c b.bpf.c
$ bpftool gen obj out a.bpf.o b.bpf.o
Segmentation fault (core dumped)

After fix:
$ bpftool gen obj out a.bpf.o b.bpf.o
libbpf: failed to find BTF info for object 'a.bpf.o'
Error: failed to link 'a.bpf.o': Unknown error -22 (-22)

Fixes: a46349227cd8 (libbpf: Add linker extern resolution support for functions and global variables)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 tools/lib/bpf/linker.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

--
2.33.0

Comments

Andrii Nakryiko Sept. 24, 2021, 11:35 p.m. UTC | #1
On Thu, Sep 23, 2021 at 7:39 PM Kumar Kartikeya Dwivedi
<memxor@gmail.com> wrote:
>
> When a BPF object is compiled without BTF info (without -g),
> trying to link such objects using bpftool causes a SIGSEGV due to
> btf__get_nr_types accessing obj->btf which is NULL. Fix this by
> checking for the NULL pointer, and return error.
>
> Reproducer:
> $ cat a.bpf.c
> extern int foo(void);
> int bar(void) { return foo(); }
> $ cat b.bpf.c
> int foo(void) { return 0; }
> $ clang -O2 -target bpf -c a.bpf.c
> $ clang -O2 -target bpf -c b.bpf.c
> $ bpftool gen obj out a.bpf.o b.bpf.o
> Segmentation fault (core dumped)
>
> After fix:
> $ bpftool gen obj out a.bpf.o b.bpf.o
> libbpf: failed to find BTF info for object 'a.bpf.o'
> Error: failed to link 'a.bpf.o': Unknown error -22 (-22)
>
> Fixes: a46349227cd8 (libbpf: Add linker extern resolution support for functions and global variables)
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>  tools/lib/bpf/linker.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>

Applied to bpf, thanks.

[...]
diff mbox series

Patch

diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 10911a8cad0f..2df880cefdae 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -1649,11 +1649,17 @@  static bool btf_is_non_static(const struct btf_type *t)
 static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name,
 			     int *out_btf_sec_id, int *out_btf_id)
 {
-	int i, j, n = btf__get_nr_types(obj->btf), m, btf_id = 0;
+	int i, j, n, m, btf_id = 0;
 	const struct btf_type *t;
 	const struct btf_var_secinfo *vi;
 	const char *name;

+	if (!obj->btf) {
+		pr_warn("failed to find BTF info for object '%s'\n", obj->filename);
+		return -EINVAL;
+	}
+
+	n = btf__get_nr_types(obj->btf);
 	for (i = 1; i <= n; i++) {
 		t = btf__type_by_id(obj->btf, i);