Fix libbpf and selftest issues detected by ASAN
Subsystems (6)
v2 cover Fix libbpf and selftest issues detected by ASAN
Add necessary infra to build selftests with ASAN (or any other sanitizer). Fix a bunch of found memory leaks and other memory access issues.
v1->v2:
- don't add ASAN flavor, but allow extra flags for build (Alexei);
- fix few more found issues, which somehow were missed first time.
Andrii Nakryiko (11):
selftests/bpf: ensure test flavors use correct skeletons selftests/bpf: add SAN_CFLAGS param to selftests build to allow
sanitizers
selftests/bpf: convert test_hashmap into test_progs test
libbpf: fix memory leak and possible double-free in hashmap__clear selftests/bpf: fix memory leak in test selector selftests/bpf: fix memory leak in extract_build_id() selftests/bpf: fix invalid memory reads in core_relo selftest
libbpf: fix huge memory leak in libbpf_find_vmlinux_btf_id() selftests/bpf: disable ASAN instrumentation for mmap()'ed memory read selftests/bpf: fix bpf_link leak in ns_current_pid_tgid selftest selftests/bpf: add runqslower binary to .gitignore
tools/lib/bpf/hashmap.c | 7 +
tools/lib/bpf/libbpf.c | 5 +-
tools/testing/selftests/bpf/.gitignore | 4 +-
tools/testing/selftests/bpf/Makefile | 11 +-
.../selftests/bpf/prog_tests/core_reloc.c | 2 +-
.../{test_hashmap.c => prog_tests/hashmap.c} | 280 +++++++++---------
.../bpf/prog_tests/ns_current_pid_tgid.c | 5 +-
.../selftests/bpf/prog_tests/perf_buffer.c | 5 +
tools/testing/selftests/bpf/test_progs.c | 21 +-
9 files changed, 181 insertions(+), 159 deletions(-)
rename tools/testing/selftests/bpf/{test_hashmap.c => prog_tests/hashmap.c} (53%)Signature · 1 line
2.24.1
PATCH v2 1/11 selftests/bpf: ensure test flavors use correct skeletons
Ensure that test runner flavors include their own skeletons from <flavor>/ directory. Previously, skeletons generated for no-flavor test_progs were used. Apart from fixing correctness, this also makes it possible to compile only flavors individually:
$ make clean && make test_progs-no_alu32 ... now succeeds ...
--- tools/testing/selftests/bpf/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile +1 -1
@@ -324,7 +324,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ $(TRUNNER_BPF_SKELS) \ $$(BPFOBJ) | $(TRUNNER_OUTPUT) $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) - cd $$(@D) && $$(CC) $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) + cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ %.c \
Signature · 1 line
2.24.1
PATCH v2 2/11 selftests/bpf: add SAN_CFLAGS param to selftests build to allow sanitizers
In reply to Andrii NakryikoAdd ability to specify extra compiler flags with SAN_CFLAGS for compilation of all user-space C files. This allows to build all of selftest programs with, e.g., custom sanitizer flags, without requiring support for such sanitizers from anyone compiling selftest/bpf.
As an example, to compile everything with AddressSanitizer, one would do:
$ make clean && make SAN_CFLAGS="-fsanitize=address"
For AddressSanitizer to work, one needs appropriate libasan shared library installed in the system, with version of libasan matching what GCC links against. E.g., GCC8 needs libasan5, while GCC7 uses libasan4.
For CentOS 7, to build everything successfully one would need to:
$ sudo yum install devtoolset-8-gcc devtoolset-libasan-devel $ scl enable devtoolset-8 bash # set up environment
For Arch Linux to run selftests, one would need to install gcc-libs package to get libasan.so.5:
$ sudo pacman -S gcc-libs
N.B. EXTRA_CFLAGS name wasn't used, because it's also used by libbpf's Makefile and this causes few issues:
1. default "-g -Wall" flags are overriden;
2. compiling shared library with AddressSanitizer generates a bunch of symbols
like: "_GLOBAL__sub_D_00099_0_btf_dump.c", "_GLOBAL__sub_D_00099_0_bpf.c", etc, which screws up versioned symbols check.
--- tools/testing/selftests/bpf/Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +4 -3
@@ -20,9 +20,10 @@ CLANG ?= clang LLC ?= llc LLVM_OBJCOPY ?= llvm-objcopy BPF_GCC ?= $(shell command -v bpf-gcc;) -CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) -I$(CURDIR) \ - -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) -I$(TOOLSINCDIR) \ - -I$(APIDIR) \ +SAN_CFLAGS ?= +CFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS) \ + -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ + -I$(TOOLSINCDIR) -I$(APIDIR) \ -Dbpf_prog_load=bpf_prog_test_load \ -Dbpf_load_program=bpf_test_load_program LDLIBS += -lcap -lelf -lz -lrt -lpthread
Signature · 1 line
2.24.1
PATCH v2 3/11 selftests/bpf: convert test_hashmap into test_progs test
In reply to Andrii NakryikoFold stand-alone test_hashmap test into test_progs.
---
tools/testing/selftests/bpf/.gitignore | 2 -
tools/testing/selftests/bpf/Makefile | 2 +-
.../{test_hashmap.c => prog_tests/hashmap.c} | 280 +++++++++---------
3 files changed, 140 insertions(+), 144 deletions(-)
rename tools/testing/selftests/bpf/{test_hashmap.c => prog_tests/hashmap.c} (53%)tools/testing/selftests/bpf/.gitignore +0 -2
@@ -30,8 +30,6 @@ test_tcpnotify_user test_libbpf test_tcp_check_syncookie_user test_sysctl -test_hashmap -test_btf_dump test_current_pid_tgid_new_ns xdping test_cpp
tools/testing/selftests/bpf/Makefile +1 -1
@@ -33,7 +33,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage \ - test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ + test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \ test_progs-no_alu32 \ test_current_pid_tgid_new_ns
tools/testing/selftests/bpf/prog_tests/hashmap.c renamed from tools/testing/selftests/bpf/test_hashmap.c +139 -141
484 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v2 4/11 libbpf: fix memory leak and possible double-free in hashmap__clear
In reply to Andrii NakryikoFix memory leak in hashmap_clear() not freeing hashmap_entry structs for each of the remaining entries. Also NULL-out bucket list to prevent possible double-free between hashmap__clear() and hashmap__free().
Running test_progs-asan flavor clearly showed this problem.
--- tools/lib/bpf/hashmap.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/lib/bpf/hashmap.c +7 -0
@@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,void hashmap__clear (struct hashmap *map) { +struct hashmap_entry *cur, *tmp; +int bkt; + +hashmap__for_each_entry_safe (map, cur, tmp, bkt) { +free (cur); + }free (map->buckets); + map->buckets =NULL ; map->cap = map->cap_bits = map->sz =0 ; }
Signature · 1 line
2.24.1
PATCH v2 5/11 selftests/bpf: fix memory leak in test selector
In reply to Andrii NakryikoFree test selector substrings, which were strdup()'ed.
--- tools/testing/selftests/bpf/test_progs.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/test_progs.c +16 -4
@@ -420,6 +420,18 @@ static int libbpf_print_fn(enum libbpf_print_level level,return 0 ; } +static void free_str_set (const struct str_set *set) +{ +int i; + +if (!set) +return ; + +for (i =0 ; i < set->cnt; i++) +free ((void *)set->strs[i]); +free (set->strs); +} +static int parse_str_list (const char *s,struct str_set *set) {char *input, *state =NULL , *next, **tmp, **strs =NULL ;
@@ -756,11 +768,11 @@ int main(int argc, char **argv)fprintf (stdout,"Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n" , env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt); -free (env.test_selector.blacklist.strs); -free (env.test_selector.whitelist.strs); +free_str_set (&env.test_selector.blacklist); +free_str_set (&env.test_selector.whitelist);free (env.test_selector.num_set); -free (env.subtest_selector.blacklist.strs); -free (env.subtest_selector.whitelist.strs); +free_str_set (&env.subtest_selector.blacklist); +free_str_set (&env.subtest_selector.whitelist);free (env.subtest_selector.num_set);return env.fail_cnt ?EXIT_FAILURE :EXIT_SUCCESS ;
Signature · 1 line
2.24.1
PATCH v2 6/11 selftests/bpf: fix memory leak in extract_build_id()
In reply to Andrii Nakryikogetline() allocates string, which has to be freed.
--- tools/testing/selftests/bpf/test_progs.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/test_progs.c +1 -0
@@ -351,6 +351,7 @@ int extract_build_id(char *build_id, size_t size) len = size;memcpy (build_id, line, len); build_id[len] ='\0' ; +free (line);return 0 ; err:fclose (fp);
Signature · 1 line
2.24.1
PATCH v2 7/11 selftests/bpf: fix invalid memory reads in core_relo selftest
In reply to Andrii NakryikoAnother one found by AddressSanitizer. input_len is bigger than actually initialized data size.
--- tools/testing/selftests/bpf/prog_tests/core_reloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +1 -1
@@ -392,7 +392,7 @@ static struct core_reloc_test_case test_cases[] = { .input = STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal) { .a = 42, }, - .input_len = sizeof(struct core_reloc_existence), + .input_len = sizeof(struct core_reloc_existence___minimal), .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) { .a_exists = 1, .b_exists = 0,
Signature · 1 line
2.24.1
PATCH v2 8/11 libbpf: fix huge memory leak in libbpf_find_vmlinux_btf_id()
In reply to Andrii NakryikoBTF object wasn't freed.
--- tools/lib/bpf/libbpf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +4 -1
@@ -6672,6 +6672,7 @@ int libbpf_find_vmlinux_btf_id(const char *name, enum bpf_attach_type attach_type) {struct btf *btf; +int err; btf =libbpf_find_kernel_btf ();if (IS_ERR (btf)) {
@@ -6679,7 +6680,9 @@ int libbpf_find_vmlinux_btf_id(const char *name,return -EINVAL ; } -return __find_vmlinux_btf_id (btf, name, attach_type); + err =__find_vmlinux_btf_id (btf, name, attach_type); +btf__free (btf); +return err; }static int libbpf_find_prog_btf_id (const char *name,__u32 attach_prog_fd)
Signature · 1 line
2.24.1
PATCH v2 9/11 selftests/bpf: disable ASAN instrumentation for mmap()'ed memory read
In reply to Andrii NakryikoAddressSanitizer assumes that all memory dereferences are done against memory allocated by sanitizer's malloc()/free() code and not touched by anyone else. Seems like this doesn't hold for perf buffer memory. Disable instrumentation on perf buffer callback function.
--- tools/testing/selftests/bpf/prog_tests/perf_buffer.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/testing/selftests/bpf/prog_tests/perf_buffer.c +5 -0
@@ -6,6 +6,11 @@#include <test_progs.h> #include "bpf/libbpf_internal.h" +/* AddressSanitizer sometimes crashes due to data dereference below, due to +* this being mmap()'ed memory. Disable instrumentation with +* no_sanitize_address attribute +*/ +__attribute__((no_sanitize_address))static void on_sample (void *ctx,int cpu,void *data,__u32 size) {int cpu_data = *(int *)data, duration =0 ;
Signature · 1 line
2.24.1
PATCH v2 10/11 selftests/bpf: fix bpf_link leak in ns_current_pid_tgid selftest
In reply to Andrii NakryikoIf condition is inverted, but it's also just not necessary.
--- tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c +1 -4
@@ -80,9 +80,6 @@ void test_ns_current_pid_tgid(void) "User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid)) goto cleanup; cleanup: -if (!link) { -bpf_link__destroy (link); - link =NULL ; - } +bpf_link__destroy (link);bpf_object__close (obj); }
Signature · 1 line
2.24.1
PATCH v2 11/11 selftests/bpf: add runqslower binary to .gitignore
In reply to Andrii NakryikoWith recent changes, runqslower is being copied into selftests/bpf root directory. So add it into .gitignore.
--- tools/testing/selftests/bpf/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/.gitignore +1 -1
@@ -37,4 +37,4 @@ test_cpp /no_alu32 /bpf_gcc /tools - +/runqslower
Signature · 1 line
2.24.1
Alexei Starovoitov
AppliedQuoted 6 lines · Andrii Nakryiko
Add necessary infra to build selftests with ASAN (or any other sanitizer). Fix a bunch of found memory leaks and other memory access issues.
v1->v2:
- don't add ASAN flavor, but allow extra flags for build (Alexei);
- fix few more found issues, which somehow were missed first time.
Applied, Thanks