Skip to contentSkip to conversation

Fix libbpf and selftest issues detected by ASAN

PATCH Applied Andrii Nakryiko 2 participants 13 messages Apr 29, 2020

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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 ...
Fixes 74b5a5968fe8 ("selftests/bpf: Replace test_progs and test_maps w/ general rule")
---
 tools/testing/selftests/bpf/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile +1 -1
index 4e654d41c7af..01c95f8278c7 100644
@@ -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
#2 lore raw

PATCH v2 2/11 selftests/bpf: add SAN_CFLAGS param to selftests build to allow sanitizers

In reply to Andrii Nakryiko

Add 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
index 01c95f8278c7..887f06a514ee 100644
@@ -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
#3 lore raw

PATCH v2 3/11 selftests/bpf: convert test_hashmap into test_progs test

In reply to Andrii Nakryiko

Fold 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
index c30079c86998..16b9774d8b68 100644
@@ -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
index 887f06a514ee..10f12a5aac20 100644
@@ -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
#4 lore raw

PATCH v2 4/11 libbpf: fix memory leak and possible double-free in hashmap__clear

In reply to Andrii Nakryiko

Fix 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
index 54c30c802070..cffb96202e0d 100644
@@ -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
#5 lore raw

PATCH v2 5/11 selftests/bpf: fix memory leak in test selector

In reply to Andrii Nakryiko

Free test selector substrings, which were strdup()'ed.

Fixes b65053cd94f4 ("selftests/bpf: Add whitelist/blacklist of test names to test_progs")
---
 tools/testing/selftests/bpf/test_progs.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/test_progs.c +16 -4
index b521e0a512b6..86d0020c9eec 100644
@@ -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
#6 lore raw

PATCH v2 6/11 selftests/bpf: fix memory leak in extract_build_id()

In reply to Andrii Nakryiko

getline() allocates string, which has to be freed.

Cc Song Liu <[email protected]>
Fixes 81f77fd0deeb ("bpf: add selftest for stackmap with BPF_F_STACK_BUILD_ID")
---
 tools/testing/selftests/bpf/test_progs.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/test_progs.c +1 -0
index 86d0020c9eec..93970ec1c9e9 100644
@@ -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
#7 lore raw

PATCH v2 7/11 selftests/bpf: fix invalid memory reads in core_relo selftest

In reply to Andrii Nakryiko

Another one found by AddressSanitizer. input_len is bigger than actually initialized data size.

Fixes c7566a69695c ("selftests/bpf: Add field existence CO-RE relocs tests")
---
 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
index 31e177adbdf1..084ed26a7d78 100644
@@ -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
#8 lore raw

PATCH v2 8/11 libbpf: fix huge memory leak in libbpf_find_vmlinux_btf_id()

In reply to Andrii Nakryiko

BTF object wasn't freed.

Cc KP Singh <[email protected]>
Fixes a6ed02cac690 ("libbpf: Load btf_vmlinux only once per object.")
---
 tools/lib/bpf/libbpf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +4 -1
index 8e1dc6980fac..a0f943e6b7bb 100644
@@ -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
#9 lore raw

PATCH v2 9/11 selftests/bpf: disable ASAN instrumentation for mmap()'ed memory read

In reply to Andrii Nakryiko

AddressSanitizer 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
index 1450ea2dd4cc..a122ce3b360e 100644
@@ -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
#10 lore raw

PATCH v2 10/11 selftests/bpf: fix bpf_link leak in ns_current_pid_tgid selftest

In reply to Andrii Nakryiko

If condition is inverted, but it's also just not necessary.

Cc Carlos Neira <[email protected]>
Fixes 1c1052e0140a ("tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid.")
---
 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
index 542240e16564..e74dc501b27f 100644
@@ -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
#11 lore raw

PATCH v2 11/11 selftests/bpf: add runqslower binary to .gitignore

In reply to Andrii Nakryiko

With recent changes, runqslower is being copied into selftests/bpf root directory. So add it into .gitignore.

Cc Veronika Kabatova <[email protected]>
Fixes b26d1e2b6028 ("selftests/bpf: Copy runqslower to OUTPUT directory")
---
 tools/testing/selftests/bpf/.gitignore | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/.gitignore +1 -1
index 16b9774d8b68..3ff031972975 100644
@@ -37,4 +37,4 @@ test_cpp
 /no_alu32
 /bpf_gcc
 /tools
-
+/runqslower
Signature · 1 line
2.24.1
#12 lore raw
In reply to Andrii Nakryiko
Quoted 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