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

libbpf: Fix skel_internal.h to set errno on loader retval < 0

When the loader indicates an internal error (result of a checked bpf
system call), it returns the result in attr.test.retval. However, tests
that rely on ASSERT_OK_PTR on NULL (returned from light skeleton) may
miss that NULL denotes an error if errno is set to 0. This would result
in skel pointer being NULL, while ASSERT_OK_PTR returning 1, leading to
a SEGV on dereference of skel, because libbpf_get_error relies on the
assumption that errno is always set in case of error for ptr == NULL.

In particular, this was observed for the ksyms_module test. When
executed using `./test_progs -t ksyms`, prior tests manipulated errno
and the test didn't crash when it failed at ksyms_module load, while
using `./test_progs -t ksyms_module` crashed due to errno being
untouched.

Fixes: 67234743736a (libbpf: Generate loader program out of BPF ELF file.)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210927145941.1383001-11-memxor@gmail.com

authored by

Kumar Kartikeya Dwivedi and committed by
Alexei Starovoitov
e68ac008 161ecd53

+4 -2
+4 -2
tools/lib/bpf/skel_internal.h
··· 105 105 err = skel_sys_bpf(BPF_PROG_RUN, &attr, sizeof(attr)); 106 106 if (err < 0 || (int)attr.test.retval < 0) { 107 107 opts->errstr = "failed to execute loader prog"; 108 - if (err < 0) 108 + if (err < 0) { 109 109 err = -errno; 110 - else 110 + } else { 111 111 err = (int)attr.test.retval; 112 + errno = -err; 113 + } 112 114 goto out; 113 115 } 114 116 err = 0;