Skip to content

Commit e52b304

Browse files
teknoraverNobody
authored and
Nobody
committed
selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat()
bpf_core_types_are_compat() was limited to 2 recursion levels, which are enough to parse a function prototype. Add a test which checks the existence of a function prototype, so to test the bpf_core_types_are_compat() code path. The test for the recursion limit being hit is done in a separate object, because the kernel failure makes the whole load to fail. Sample run log with extra prints: [ 5689.913751] bpf_core_apply_relo_insn:1200 cands->len: 2 [ 5689.913902] bpf_core_types_are_compat:6896: ret: 1 [ 5689.913994] bpf_core_types_are_compat:6896: ret: 0 [ 5689.914025] bpf_core_apply_relo_insn:1200 cands->len: 2 [ 5689.914141] bpf_core_types_are_compat:6896: ret: 0 [ 5689.914246] bpf_core_types_are_compat:6896: ret: 0 test_core_kern_lskel:PASS:open_and_load 0 nsec test_core_kern_lskel:PASS:attach(core_relo_proto) 0 nsec test_core_kern_lskel:PASS:bpf_core_type_exists 0 nsec test_core_kern_lskel:PASS:!bpf_core_type_exists 0 nsec #41 core_kern_lskel:OK [ 5689.915267] bpf_core_apply_relo_insn:1200 cands->len: 2 [ 5689.915399] bpf_core_types_are_compat:6896: ret: 0 [ 5689.915504] bpf_core_types_are_compat:6896: ret: -22 test_core_kern_overflow_lskel:PASS:open_and_load 0 nsec #42 core_kern_overflow_lskel:OK Summary: 2/0 PASSED, 0 SKIPPED, 0 FAILED Successfully unloaded bpf_testmod.ko. Signed-off-by: Matteo Croce <[email protected]>
1 parent 3455997 commit e52b304

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
330330

331331
LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
332332
test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \
333-
map_ptr_kern.c core_kern.c
333+
map_ptr_kern.c core_kern.c core_kern_overflow.c
334334
# Generate both light skeleton and libbpf skeleton for these
335335
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c
336336
SKEL_BLACKLIST += $$(LSKELS)

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#define CREATE_TRACE_POINTS
1414
#include "bpf_testmod-events.h"
1515

16+
typedef int (*func_proto_typedef___match)(long);
17+
typedef int (*func_proto_typedef___overflow)(func_proto_typedef___match);
18+
func_proto_typedef___match funcp = NULL;
19+
func_proto_typedef___overflow funcp_of = NULL;
20+
1621
DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
1722

1823
noinline void

tools/testing/selftests/bpf/prog_tests/core_kern.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@
77
void test_core_kern_lskel(void)
88
{
99
struct core_kern_lskel *skel;
10+
int link_fd;
1011

1112
skel = core_kern_lskel__open_and_load();
12-
ASSERT_OK_PTR(skel, "open_and_load");
13+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
14+
return;
15+
16+
link_fd = core_kern_lskel__core_relo_proto__attach(skel);
17+
if (!ASSERT_GT(link_fd, 0, "attach(core_relo_proto)"))
18+
goto cleanup;
19+
20+
/* trigger tracepoints */
21+
usleep(1);
22+
ASSERT_TRUE(skel->bss->proto_out[0], "bpf_core_type_exists");
23+
ASSERT_FALSE(skel->bss->proto_out[1], "!bpf_core_type_exists");
24+
25+
cleanup:
1326
core_kern_lskel__destroy(skel);
1427
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "test_progs.h"
4+
#include "core_kern_overflow.lskel.h"
5+
6+
void test_core_kern_overflow_lskel(void)
7+
{
8+
struct core_kern_overflow_lskel *skel;
9+
10+
skel = core_kern_overflow_lskel__open_and_load();
11+
if (!ASSERT_NULL(skel, "open_and_load"))
12+
core_kern_overflow_lskel__destroy(skel);
13+
}

tools/testing/selftests/bpf/progs/core_kern.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,18 @@ int balancer_ingress(struct __sk_buff *ctx)
101101
return 0;
102102
}
103103

104+
typedef int (*func_proto_typedef___match)(long);
105+
typedef void (*func_proto_typedef___doesnt_match)(char*);
106+
107+
int proto_out[2];
108+
109+
SEC("raw_tracepoint/sys_enter")
110+
int core_relo_proto(void *ctx)
111+
{
112+
proto_out[0] = bpf_core_type_exists(func_proto_typedef___match);
113+
proto_out[1] = bpf_core_type_exists(func_proto_typedef___doesnt_match);
114+
115+
return 0;
116+
}
117+
104118
char LICENSE[] SEC("license") = "GPL";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include <bpf/bpf_core_read.h>
7+
8+
typedef int (*func_proto_typedef___match)(long);
9+
typedef int (*func_proto_typedef___overflow)(func_proto_typedef___match);
10+
11+
int proto_out;
12+
13+
SEC("raw_tracepoint/sys_enter")
14+
int core_relo_proto(void *ctx)
15+
{
16+
proto_out = bpf_core_type_exists(func_proto_typedef___overflow);
17+
18+
return 0;
19+
}
20+
21+
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)