selftests/bpf: fix segfault of test_progs when prog loading failed
Subsystems (5)
PATCH 1/1 selftests/bpf: fix segfault of test_progs when prog loading failed
The test_progs subtests, test_spin_lock() and test_map_lock(), requires BTF present to run successfully.
Currently, when BTF failed to load, test_progs will segfault,
$ ./test_progs ... 12: (bf) r1 = r8 13: (85) call bpf_spin_lock#93 map 'hash_map' has to have BTF in order to use bpf_spin_lock
libbpf: -- END LOG --
libbpf: failed to load program 'map_lock_demo'
libbpf: failed to load object './test_map_lock.o' test_map_lock:bpf_prog_load errno 13 Segmentation fault
The segfault is caused by uninitialized variable "obj", which is used in bpf_object__close(obj), when bpf prog failed to load.
Initializing variable "obj" to NULL in two occasions fixed the problem.
$ ./test_progs ... Summary: 219 PASSED, 2 FAILED
--- tools/testing/selftests/bpf/prog_tests/map_lock.c | 2 +- tools/testing/selftests/bpf/prog_tests/spinlock.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/map_lock.c +1 -1
@@ -37,7 +37,7 @@ void test_map_lock(void)const char *file ="./test_map_lock.o" ;int prog_fd, map_fd[2 ], vars[17 ] = {};pthread_t thread_id[6 ]; -struct bpf_object *obj; +struct bpf_object *obj =NULL ;int err =0 , key =0 , i;void *ret;
tools/testing/selftests/bpf/prog_tests/spinlock.c +1 -1
@@ -5,7 +5,7 @@ void test_spinlock(void) {const char *file ="./test_spin_lock.o" ;pthread_t thread_id[4 ]; -struct bpf_object *obj; +struct bpf_object *obj =NULL ;int prog_fd;int err =0 , i;void *ret;
Signature · 1 line
2.17.1
Quoted 23 lines · Yonghong Song
The
test_progssubtests,test_spin_lock()andtest_map_lock(), requires BTF present to run successfully.
Currently, when BTF failed to load,test_progswill segfault,$ ./test_progs ... 12: (bf) r1 = r8 13: (85) call bpf_spin_lock#93 map 'hash_map' has to have BTF in order to use bpf_spin_locklibbpf: -- END LOG --
libbpf: failed to load program 'map_lock_demo'
libbpf: failed to load object './test_map_lock.o' test_map_lock:bpf_prog_load errno 13 Segmentation faultThe segfault is caused by uninitialized variable "obj", which is used in
bpf_object__close(obj), when bpf prog failed to load.Initializing variable "obj" to NULL in two occasions fixed the problem.
Quoted 8 lines
$ ./test_progs ... Summary: 219 PASSED, 2 FAILEDFixes b4d4556c3266 ("selftests/bpf: add bpf_spin_lock verifier tests")Fixes ba72a7b4badb ("selftests/bpf: test for BPF_F_LOCK")Reported-by Daniel Borkmann <[email protected]>Signed-off-by Yonghong Song <[email protected]>
Quoted 34 lines
---
tools/testing/selftests/bpf/prog_tests/map_lock.c| 2 +-tools/testing/selftests/bpf/prog_tests/spinlock.c| 2 +-2 files changed, 2 insertions(+), 2 deletions(-)tools/testing/selftests/bpf/prog_tests/map_lock.c +1
-1@@ -37,7 +37,7 @@ void test_map_lock(void)const char *file ="./test_map_lock.o" ;int prog_fd, map_fd[2 ], vars[17 ] = {};pthread_t thread_id[6 ]; -struct bpf_object *obj; +struct bpf_object *obj =NULL ;int err =0 , key =0 , i;void *ret;tools/testing/selftests/bpf/prog_tests/spinlock.c +1
-1@@ -5,7 +5,7 @@ void test_spinlock(void) {const char *file ="./test_spin_lock.o" ;pthread_t thread_id[4 ]; -struct bpf_object *obj; +struct bpf_object *obj =NULL ;int prog_fd;int err =0 , i;void *ret;Signature · 1 line
2.17.1
Quoted 19 lines · Yonghong Song
The
test_progssubtests,test_spin_lock()andtest_map_lock(), requires BTF present to run successfully.
Currently, when BTF failed to load,test_progswill segfault,$ ./test_progs ... 12: (bf) r1 = r8 13: (85) call bpf_spin_lock#93 map 'hash_map' has to have BTF in order to use bpf_spin_locklibbpf: -- END LOG --
libbpf: failed to load program 'map_lock_demo'
libbpf: failed to load object './test_map_lock.o' test_map_lock:bpf_prog_load errno 13 Segmentation faultThe segfault is caused by uninitialized variable "obj", which is used in
bpf_object__close(obj), when bpf prog failed to load.Initializing variable "obj" to NULL in two occasions fixed the problem.
Quoted 8 lines
$ ./test_progs ... Summary: 219 PASSED, 2 FAILEDFixes b4d4556c3266 ("selftests/bpf: add bpf_spin_lock verifier tests")Fixes ba72a7b4badb ("selftests/bpf: test for BPF_F_LOCK")Reported-by Daniel Borkmann <[email protected]>Signed-off-by Yonghong Song <[email protected]>
Applied, thanks!