Skip to content

Commit 8feedae

Browse files
author
Alexei Starovoitov
committed
Merge branch 'selftests-bpf-fixes-for-userspace-asan'
Ihor Solodrai says: ==================== selftests/bpf: Fixes for userspace ASAN This series includes various fixes aiming to enable test_progs run with userspace address sanitizer on BPF CI. The first five patches add a simplified implementation of strscpy() to selftests/bpf and then replace strcpy/strncpy usages across the tests with it. See relevant discussions [1][2]. Patch #6 fixes the selftests/bpf/test_progs build with: SAN_CFLAGS="-fsanitize=address -fno-omit-frame-pointer" The subsequent patches fix bugs reported by the address sanitizer on attempt to run the tests. [1] https://lore.kernel.org/bpf/CAADnVQ+9uw2_o388j43EWiAPdMB=3FLx2jq-9zRSvqrv-wgRag@mail.gmail.com/ [2] https://lore.kernel.org/bpf/20260220182011.802116-1-ihor.solodrai@linux.dev/ --- v3->v4: - combine strscpy and ASAN series into one (Alexei) - make the count arg of strscpy() optional via macro and fixup relevant call sites (Alexei) - remove strscpy_cat() from this series (Alexei) v3: https://lore.kernel.org/bpf/20260220222604.1155148-1-ihor.solodrai@linux.dev/ v2->v3: - rebase on top of "selftests/bpf: Add and use strscpy()" - https://lore.kernel.org/bpf/20260220182011.802116-1-ihor.solodrai@linux.dev/ - uprobe_multi_test.c: memset static struct child at the beginning of a test *and* zero out child->thread in release_child (patch #9, Mykyta) - nits in test_sysctl.c (patch #11, Eduard) - bpftool_helpers.c: update to use strscpy (patch #14, Alexei) - add __asan_on_error handler to still dump test logs even with ASAN build (patch #15, Mykyta) v2: https://lore.kernel.org/bpf/20260218003041.1156774-1-ihor.solodrai@linux.dev/ v1->v2: - rebase on bpf (v1 was targeting bpf-next) - add ASAN flag handling in selftests/bpf/Makefile (Eduard) - don't override SIGSEGV handler in test_progs with ASAN (Eduard) - add error messages in detect_bpftool_path (Mykyta) - various nits (Eduard, Jiri, Mykyta, Alexis) v1: https://lore.kernel.org/bpf/20260212011356.3266753-1-ihor.solodrai@linux.dev/ ==================== Link: https://patch.msgid.link/20260223190736.649171-1-ihor.solodrai@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 7dff99b + 4c9d078 commit 8feedae

47 files changed

Lines changed: 290 additions & 163 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tools/bpf/resolve_btfids/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ $(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
6565
LIBELF_FLAGS := $(shell $(HOSTPKG_CONFIG) libelf --cflags 2>/dev/null)
6666
LIBELF_LIBS := $(shell $(HOSTPKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
6767

68+
ZLIB_LIBS := $(shell $(HOSTPKG_CONFIG) zlib --libs 2>/dev/null || echo -lz)
69+
ZSTD_LIBS := $(shell $(HOSTPKG_CONFIG) libzstd --libs 2>/dev/null || echo -lzstd)
70+
6871
HOSTCFLAGS_resolve_btfids += -g \
6972
-I$(srctree)/tools/include \
7073
-I$(srctree)/tools/include/uapi \
@@ -73,7 +76,7 @@ HOSTCFLAGS_resolve_btfids += -g \
7376
$(LIBELF_FLAGS) \
7477
-Wall -Werror
7578

76-
LIBS = $(LIBELF_LIBS) -lz
79+
LIBS = $(LIBELF_LIBS) $(ZLIB_LIBS) $(ZSTD_LIBS)
7780

7881
export srctree OUTPUT HOSTCFLAGS_resolve_btfids Q HOSTCC HOSTLD HOSTAR
7982
include $(srctree)/tools/build/Makefile.include
@@ -83,7 +86,7 @@ $(BINARY_IN): fixdep FORCE prepare | $(OUTPUT)
8386

8487
$(BINARY): $(BPFOBJ) $(SUBCMDOBJ) $(BINARY_IN)
8588
$(call msg,LINK,$@)
86-
$(Q)$(HOSTCC) $(BINARY_IN) $(KBUILD_HOSTLDFLAGS) -o $@ $(BPFOBJ) $(SUBCMDOBJ) $(LIBS)
89+
$(Q)$(HOSTCC) $(BINARY_IN) $(KBUILD_HOSTLDFLAGS) $(EXTRA_LDFLAGS) -o $@ $(BPFOBJ) $(SUBCMDOBJ) $(LIBS)
8790

8891
clean_objects := $(wildcard $(OUTPUT)/*.o \
8992
$(OUTPUT)/.*.o.cmd \

tools/bpf/resolve_btfids/main.c

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
226226
}
227227

228228
static struct btf_id *__btf_id__add(struct rb_root *root,
229-
char *name,
229+
const char *name,
230230
enum btf_id_kind kind,
231231
bool unique)
232232
{
@@ -250,25 +250,33 @@ static struct btf_id *__btf_id__add(struct rb_root *root,
250250
id = zalloc(sizeof(*id));
251251
if (id) {
252252
pr_debug("adding symbol %s\n", name);
253-
id->name = name;
253+
id->name = strdup(name);
254+
if (!id->name) {
255+
free(id);
256+
return NULL;
257+
}
254258
id->kind = kind;
255259
rb_link_node(&id->rb_node, parent, p);
256260
rb_insert_color(&id->rb_node, root);
257261
}
258262
return id;
259263
}
260264

261-
static inline struct btf_id *btf_id__add(struct rb_root *root, char *name, enum btf_id_kind kind)
265+
static inline struct btf_id *btf_id__add(struct rb_root *root,
266+
const char *name,
267+
enum btf_id_kind kind)
262268
{
263269
return __btf_id__add(root, name, kind, false);
264270
}
265271

266-
static inline struct btf_id *btf_id__add_unique(struct rb_root *root, char *name, enum btf_id_kind kind)
272+
static inline struct btf_id *btf_id__add_unique(struct rb_root *root,
273+
const char *name,
274+
enum btf_id_kind kind)
267275
{
268276
return __btf_id__add(root, name, kind, true);
269277
}
270278

271-
static char *get_id(const char *prefix_end)
279+
static int get_id(const char *prefix_end, char *buf, size_t buf_sz)
272280
{
273281
/*
274282
* __BTF_ID__func__vfs_truncate__0
@@ -277,28 +285,28 @@ static char *get_id(const char *prefix_end)
277285
*/
278286
int len = strlen(prefix_end);
279287
int pos = sizeof("__") - 1;
280-
char *p, *id;
288+
char *p;
281289

282290
if (pos >= len)
283-
return NULL;
291+
return -1;
284292

285-
id = strdup(prefix_end + pos);
286-
if (id) {
287-
/*
288-
* __BTF_ID__func__vfs_truncate__0
289-
* id = ^
290-
*
291-
* cut the unique id part
292-
*/
293-
p = strrchr(id, '_');
294-
p--;
295-
if (*p != '_') {
296-
free(id);
297-
return NULL;
298-
}
299-
*p = '\0';
300-
}
301-
return id;
293+
if (len - pos >= buf_sz)
294+
return -1;
295+
296+
strcpy(buf, prefix_end + pos);
297+
/*
298+
* __BTF_ID__func__vfs_truncate__0
299+
* buf = ^
300+
*
301+
* cut the unique id part
302+
*/
303+
p = strrchr(buf, '_');
304+
p--;
305+
if (*p != '_')
306+
return -1;
307+
*p = '\0';
308+
309+
return 0;
302310
}
303311

304312
static struct btf_id *add_set(struct object *obj, char *name, enum btf_id_kind kind)
@@ -335,17 +343,31 @@ static struct btf_id *add_set(struct object *obj, char *name, enum btf_id_kind k
335343

336344
static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
337345
{
338-
char *id;
346+
char id[KSYM_NAME_LEN];
339347

340-
id = get_id(name + size);
341-
if (!id) {
348+
if (get_id(name + size, id, sizeof(id))) {
342349
pr_err("FAILED to parse symbol name: %s\n", name);
343350
return NULL;
344351
}
345352

346353
return btf_id__add(root, id, BTF_ID_KIND_SYM);
347354
}
348355

356+
static void btf_id__free_all(struct rb_root *root)
357+
{
358+
struct rb_node *next;
359+
struct btf_id *id;
360+
361+
next = rb_first(root);
362+
while (next) {
363+
id = rb_entry(next, struct btf_id, rb_node);
364+
next = rb_next(&id->rb_node);
365+
rb_erase(&id->rb_node, root);
366+
free(id->name);
367+
free(id);
368+
}
369+
}
370+
349371
static void bswap_32_data(void *data, u32 nr_bytes)
350372
{
351373
u32 cnt, i;
@@ -1547,6 +1569,11 @@ int main(int argc, const char **argv)
15471569
out:
15481570
btf__free(obj.base_btf);
15491571
btf__free(obj.btf);
1572+
btf_id__free_all(&obj.structs);
1573+
btf_id__free_all(&obj.unions);
1574+
btf_id__free_all(&obj.typedefs);
1575+
btf_id__free_all(&obj.funcs);
1576+
btf_id__free_all(&obj.sets);
15501577
if (obj.efile.elf) {
15511578
elf_end(obj.efile.elf);
15521579
close(obj.efile.fd);

tools/include/linux/args.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
2323

2424
/* Concatenate two parameters, but allow them to be expanded beforehand. */
25+
#ifndef __CONCAT
2526
#define __CONCAT(a, b) a ## b
27+
#endif
28+
#ifndef CONCATENATE
2629
#define CONCATENATE(a, b) __CONCAT(a, b)
30+
#endif
2731

2832
#endif /* _LINUX_ARGS_H */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*arena*
2+
task_local_data
3+
uprobe_multi_test

tools/testing/selftests/bpf/Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ ifneq ($(wildcard $(GENHDR)),)
2727
endif
2828

2929
BPF_GCC ?= $(shell command -v bpf-gcc;)
30+
ifdef ASAN
31+
SAN_CFLAGS ?= -fsanitize=address -fno-omit-frame-pointer
32+
else
3033
SAN_CFLAGS ?=
34+
endif
3135
SAN_LDFLAGS ?= $(SAN_CFLAGS)
3236
RELEASE ?=
3337
OPT_FLAGS ?= $(if $(RELEASE),-O2,-O0)
@@ -326,8 +330,8 @@ $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
326330
$(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
327331
$(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
328332
ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" \
329-
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \
330-
EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \
333+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \
334+
EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \
331335
OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
332336
LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \
333337
LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \
@@ -338,8 +342,8 @@ $(CROSS_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
338342
$(BPFOBJ) | $(BUILD_DIR)/bpftool
339343
$(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
340344
ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) \
341-
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \
342-
EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \
345+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \
346+
EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \
343347
OUTPUT=$(BUILD_DIR)/bpftool/ \
344348
LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/ \
345349
LIBBPF_DESTDIR=$(SCRATCH_DIR)/ \
@@ -404,6 +408,7 @@ $(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids \
404408
$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids \
405409
CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
406410
LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
411+
EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \
407412
OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
408413

409414
# Get Clang's default includes on this system, as opposed to those seen by

tools/testing/selftests/bpf/benchs/bench_trigger.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ static void trigger_fentry_setup(void)
230230
static void attach_ksyms_all(struct bpf_program *empty, bool kretprobe)
231231
{
232232
LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
233-
char **syms = NULL;
234-
size_t cnt = 0;
233+
struct bpf_link *link = NULL;
234+
struct ksyms *ksyms = NULL;
235235

236236
/* Some recursive functions will be skipped in
237237
* bpf_get_ksyms -> skip_entry, as they can introduce sufficient
@@ -241,16 +241,18 @@ static void attach_ksyms_all(struct bpf_program *empty, bool kretprobe)
241241
* So, don't run the kprobe-multi-all and kretprobe-multi-all on
242242
* a debug kernel.
243243
*/
244-
if (bpf_get_ksyms(&syms, &cnt, true)) {
244+
if (bpf_get_ksyms(&ksyms, true)) {
245245
fprintf(stderr, "failed to get ksyms\n");
246246
exit(1);
247247
}
248248

249-
opts.syms = (const char **) syms;
250-
opts.cnt = cnt;
249+
opts.syms = (const char **)ksyms->filtered_syms;
250+
opts.cnt = ksyms->filtered_cnt;
251251
opts.retprobe = kretprobe;
252252
/* attach empty to all the kernel functions except bpf_get_numa_node_id. */
253-
if (!bpf_program__attach_kprobe_multi_opts(empty, NULL, &opts)) {
253+
link = bpf_program__attach_kprobe_multi_opts(empty, NULL, &opts);
254+
free_kallsyms_local(ksyms);
255+
if (!link) {
254256
fprintf(stderr, "failed to attach bpf_program__attach_kprobe_multi_opts to all\n");
255257
exit(1);
256258
}

tools/testing/selftests/bpf/bpf_util.h

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <errno.h>
99
#include <syscall.h>
1010
#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
11+
#include <linux/args.h>
1112

1213
static inline unsigned int bpf_num_possible_cpus(void)
1314
{
@@ -21,25 +22,43 @@ static inline unsigned int bpf_num_possible_cpus(void)
2122
return possible_cpus;
2223
}
2324

24-
/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
25-
* is zero-terminated string no matter what (unless sz == 0, in which case
26-
* it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
27-
* in what is returned. Given this is internal helper, it's trivial to extend
28-
* this, when necessary. Use this instead of strncpy inside libbpf source code.
25+
/*
26+
* Simplified strscpy() implementation. The kernel one is in lib/string.c
2927
*/
30-
static inline void bpf_strlcpy(char *dst, const char *src, size_t sz)
28+
static inline ssize_t sized_strscpy(char *dest, const char *src, size_t count)
3129
{
32-
size_t i;
30+
long res = 0;
3331

34-
if (sz == 0)
35-
return;
32+
if (count == 0)
33+
return -E2BIG;
3634

37-
sz--;
38-
for (i = 0; i < sz && src[i]; i++)
39-
dst[i] = src[i];
40-
dst[i] = '\0';
35+
while (count > 1) {
36+
char c;
37+
38+
c = src[res];
39+
dest[res] = c;
40+
if (!c)
41+
return res;
42+
res++;
43+
count--;
44+
}
45+
46+
/* Force NUL-termination. */
47+
dest[res] = '\0';
48+
49+
/* Return E2BIG if the source didn't stop */
50+
return src[res] ? -E2BIG : res;
4151
}
4252

53+
#define __strscpy0(dst, src, ...) \
54+
sized_strscpy(dst, src, sizeof(dst))
55+
#define __strscpy1(dst, src, size) \
56+
sized_strscpy(dst, src, size)
57+
58+
#undef strscpy /* Redefine the placeholder from tools/include/linux/string.h */
59+
#define strscpy(dst, src, ...) \
60+
CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
61+
4362
#define __bpf_percpu_val_align __attribute__((__aligned__(8)))
4463

4564
#define BPF_DECLARE_PERCPU(type, name) \

tools/testing/selftests/bpf/bpftool_helpers.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
// SPDX-License-Identifier: GPL-2.0-only
2-
#include "bpftool_helpers.h"
32
#include <unistd.h>
43
#include <string.h>
54
#include <stdbool.h>
65

6+
#include "bpf_util.h"
7+
#include "bpftool_helpers.h"
8+
79
#define BPFTOOL_PATH_MAX_LEN 64
810
#define BPFTOOL_FULL_CMD_MAX_LEN 512
911

1012
#define BPFTOOL_DEFAULT_PATH "tools/sbin/bpftool"
1113

12-
static int detect_bpftool_path(char *buffer)
14+
static int detect_bpftool_path(char *buffer, size_t size)
1315
{
1416
char tmp[BPFTOOL_PATH_MAX_LEN];
17+
const char *env_path;
18+
19+
/* First, check if BPFTOOL environment variable is set */
20+
env_path = getenv("BPFTOOL");
21+
if (env_path && access(env_path, X_OK) == 0) {
22+
strscpy(buffer, env_path, size);
23+
return 0;
24+
} else if (env_path) {
25+
fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
26+
return 1;
27+
}
1528

1629
/* Check default bpftool location (will work if we are running the
1730
* default flavor of test_progs)
1831
*/
1932
snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
2033
if (access(tmp, X_OK) == 0) {
21-
strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
34+
strscpy(buffer, tmp, size);
2235
return 0;
2336
}
2437

@@ -27,11 +40,11 @@ static int detect_bpftool_path(char *buffer)
2740
*/
2841
snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
2942
if (access(tmp, X_OK) == 0) {
30-
strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
43+
strscpy(buffer, tmp, size);
3144
return 0;
3245
}
3346

34-
/* Failed to find bpftool binary */
47+
fprintf(stderr, "Failed to detect bpftool path, use BPFTOOL env var to override\n");
3548
return 1;
3649
}
3750

@@ -44,7 +57,7 @@ static int run_command(char *args, char *output_buf, size_t output_max_len)
4457
int ret;
4558

4659
/* Detect and cache bpftool binary location */
47-
if (bpftool_path[0] == 0 && detect_bpftool_path(bpftool_path))
60+
if (bpftool_path[0] == 0 && detect_bpftool_path(bpftool_path, sizeof(bpftool_path)))
4861
return 1;
4962

5063
ret = snprintf(command, BPFTOOL_FULL_CMD_MAX_LEN, "%s %s%s",

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
8686
enable[len] = 0;
8787
close(fd);
8888
} else {
89-
bpf_strlcpy(enable, controllers, sizeof(enable));
89+
strscpy(enable, controllers);
9090
}
9191

9292
snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);

0 commit comments

Comments
 (0)