Skip to content

Commit 10bccd5

Browse files
TropicaoKernel Patches Daemon
authored andcommitted
selftests/bpf: ensure that functions passing structs on stack can not be hooked
When attaching ebpf programs to functions through fentry/fexit, the generated trampolines can not really make sure about the arguments exact location on the stack if those are structures: those structures can be altered with attributes such as packed or aligned(x), but this information is not encoded in BTF. Update tracing_struct_many_args test to check that programs can not be attached on those specific functions. Not all architectures can use the same number of registers to pass arguments, so define a testing function that makes all currently supported architectures start passing arguments on stack (-> more than 8 args) Signed-off-by: Alexis Lothoré (eBPF Foundation) <[email protected]>
1 parent 7c387bf commit 10bccd5

File tree

3 files changed

+6
-144
lines changed

3 files changed

+6
-144
lines changed

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,9 @@ static void test_struct_many_args(void)
7070
return;
7171

7272
err = tracing_struct_many_args__attach(skel);
73-
if (!ASSERT_OK(err, "tracing_struct_many_args__attach"))
73+
if (!ASSERT_EQ(err, -ENOTSUPP, "tracing_struct_many_args__attach"))
7474
goto destroy_skel;
7575

76-
ASSERT_OK(trigger_module_test_read(256), "trigger_read");
77-
78-
ASSERT_EQ(skel->bss->t7_a, 16, "t7:a");
79-
ASSERT_EQ(skel->bss->t7_b, 17, "t7:b");
80-
ASSERT_EQ(skel->bss->t7_c, 18, "t7:c");
81-
ASSERT_EQ(skel->bss->t7_d, 19, "t7:d");
82-
ASSERT_EQ(skel->bss->t7_e, 20, "t7:e");
83-
ASSERT_EQ(skel->bss->t7_f_a, 21, "t7:f.a");
84-
ASSERT_EQ(skel->bss->t7_f_b, 22, "t7:f.b");
85-
ASSERT_EQ(skel->bss->t7_ret, 133, "t7 ret");
86-
87-
ASSERT_EQ(skel->bss->t8_a, 16, "t8:a");
88-
ASSERT_EQ(skel->bss->t8_b, 17, "t8:b");
89-
ASSERT_EQ(skel->bss->t8_c, 18, "t8:c");
90-
ASSERT_EQ(skel->bss->t8_d, 19, "t8:d");
91-
ASSERT_EQ(skel->bss->t8_e, 20, "t8:e");
92-
ASSERT_EQ(skel->bss->t8_f_a, 21, "t8:f.a");
93-
ASSERT_EQ(skel->bss->t8_f_b, 22, "t8:f.b");
94-
ASSERT_EQ(skel->bss->t8_g, 23, "t8:g");
95-
ASSERT_EQ(skel->bss->t8_ret, 156, "t8 ret");
96-
97-
ASSERT_EQ(skel->bss->t9_a, 16, "t9:a");
98-
ASSERT_EQ(skel->bss->t9_b, 17, "t9:b");
99-
ASSERT_EQ(skel->bss->t9_c, 18, "t9:c");
100-
ASSERT_EQ(skel->bss->t9_d, 19, "t9:d");
101-
ASSERT_EQ(skel->bss->t9_e, 20, "t9:e");
102-
ASSERT_EQ(skel->bss->t9_f, 21, "t9:f");
103-
ASSERT_EQ(skel->bss->t9_g, 22, "t9:f");
104-
ASSERT_EQ(skel->bss->t9_h_a, 23, "t9:h.a");
105-
ASSERT_EQ(skel->bss->t9_h_b, 24, "t9:h.b");
106-
ASSERT_EQ(skel->bss->t9_h_c, 25, "t9:h.c");
107-
ASSERT_EQ(skel->bss->t9_h_d, 26, "t9:h.d");
108-
ASSERT_EQ(skel->bss->t9_i, 27, "t9:i");
109-
ASSERT_EQ(skel->bss->t9_ret, 258, "t9 ret");
110-
11176
destroy_skel:
11277
tracing_struct_many_args__destroy(skel);
11378
}

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

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,88 +8,18 @@ struct bpf_testmod_struct_arg_4 {
88
int b;
99
};
1010

11-
struct bpf_testmod_struct_arg_5 {
12-
char a;
13-
short b;
14-
int c;
15-
long d;
16-
};
17-
18-
long t7_a, t7_b, t7_c, t7_d, t7_e, t7_f_a, t7_f_b, t7_ret;
19-
long t8_a, t8_b, t8_c, t8_d, t8_e, t8_f_a, t8_f_b, t8_g, t8_ret;
20-
long t9_a, t9_b, t9_c, t9_d, t9_e, t9_f, t9_g, t9_h_a, t9_h_b, t9_h_c, t9_h_d, t9_i, t9_ret;
2111

2212
SEC("fentry/bpf_testmod_test_struct_arg_7")
2313
int BPF_PROG2(test_struct_many_args_1, __u64, a, void *, b, short, c, int, d,
2414
void *, e, struct bpf_testmod_struct_arg_4, f)
2515
{
26-
t7_a = a;
27-
t7_b = (long)b;
28-
t7_c = c;
29-
t7_d = d;
30-
t7_e = (long)e;
31-
t7_f_a = f.a;
32-
t7_f_b = f.b;
3316
return 0;
3417
}
3518

3619
SEC("fexit/bpf_testmod_test_struct_arg_7")
3720
int BPF_PROG2(test_struct_many_args_2, __u64, a, void *, b, short, c, int, d,
3821
void *, e, struct bpf_testmod_struct_arg_4, f, int, ret)
3922
{
40-
t7_ret = ret;
41-
return 0;
42-
}
43-
44-
SEC("fentry/bpf_testmod_test_struct_arg_8")
45-
int BPF_PROG2(test_struct_many_args_3, __u64, a, void *, b, short, c, int, d,
46-
void *, e, struct bpf_testmod_struct_arg_4, f, int, g)
47-
{
48-
t8_a = a;
49-
t8_b = (long)b;
50-
t8_c = c;
51-
t8_d = d;
52-
t8_e = (long)e;
53-
t8_f_a = f.a;
54-
t8_f_b = f.b;
55-
t8_g = g;
56-
return 0;
57-
}
58-
59-
SEC("fexit/bpf_testmod_test_struct_arg_8")
60-
int BPF_PROG2(test_struct_many_args_4, __u64, a, void *, b, short, c, int, d,
61-
void *, e, struct bpf_testmod_struct_arg_4, f, int, g,
62-
int, ret)
63-
{
64-
t8_ret = ret;
6523
return 0;
6624
}
67-
68-
SEC("fentry/bpf_testmod_test_struct_arg_9")
69-
int BPF_PROG2(test_struct_many_args_5, __u64, a, void *, b, short, c, int, d, void *, e,
70-
char, f, short, g, struct bpf_testmod_struct_arg_5, h, long, i)
71-
{
72-
t9_a = a;
73-
t9_b = (long)b;
74-
t9_c = c;
75-
t9_d = d;
76-
t9_e = (long)e;
77-
t9_f = f;
78-
t9_g = g;
79-
t9_h_a = h.a;
80-
t9_h_b = h.b;
81-
t9_h_c = h.c;
82-
t9_h_d = h.d;
83-
t9_i = i;
84-
return 0;
85-
}
86-
87-
SEC("fexit/bpf_testmod_test_struct_arg_9")
88-
int BPF_PROG2(test_struct_many_args_6, __u64, a, void *, b, short, c, int, d, void *, e,
89-
char, f, short, g, struct bpf_testmod_struct_arg_5, h, long, i, int, ret)
90-
{
91-
t9_ret = ret;
92-
return 0;
93-
}
94-
9525
char _license[] SEC("license") = "GPL";

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

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ struct bpf_testmod_struct_arg_4 {
5555
int b;
5656
};
5757

58-
struct bpf_testmod_struct_arg_5 {
59-
char a;
60-
short b;
61-
int c;
62-
long d;
63-
};
64-
6558
__bpf_hook_start();
6659

6760
noinline int
@@ -101,30 +94,10 @@ bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) {
10194
return bpf_testmod_test_struct_arg_result;
10295
}
10396

104-
noinline int
105-
bpf_testmod_test_struct_arg_7(u64 a, void *b, short c, int d, void *e,
106-
struct bpf_testmod_struct_arg_4 f)
107-
{
108-
bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
109-
(long)e + f.a + f.b;
110-
return bpf_testmod_test_struct_arg_result;
111-
}
112-
113-
noinline int
114-
bpf_testmod_test_struct_arg_8(u64 a, void *b, short c, int d, void *e,
115-
struct bpf_testmod_struct_arg_4 f, int g)
97+
noinline int bpf_testmod_test_struct_arg_7(u64 a, void *b, short c, int d,
98+
void *e, u64 f, u64 g, u64 h,
99+
struct bpf_testmod_struct_arg_4 i)
116100
{
117-
bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
118-
(long)e + f.a + f.b + g;
119-
return bpf_testmod_test_struct_arg_result;
120-
}
121-
122-
noinline int
123-
bpf_testmod_test_struct_arg_9(u64 a, void *b, short c, int d, void *e, char f,
124-
short g, struct bpf_testmod_struct_arg_5 h, long i)
125-
{
126-
bpf_testmod_test_struct_arg_result = a + (long)b + c + d + (long)e +
127-
f + g + h.a + h.b + h.c + h.d + i;
128101
return bpf_testmod_test_struct_arg_result;
129102
}
130103

@@ -397,7 +370,6 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
397370
struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
398371
struct bpf_testmod_struct_arg_3 *struct_arg3;
399372
struct bpf_testmod_struct_arg_4 struct_arg4 = {21, 22};
400-
struct bpf_testmod_struct_arg_5 struct_arg5 = {23, 24, 25, 26};
401373
int i = 1;
402374

403375
while (bpf_testmod_return_ptr(i))
@@ -408,13 +380,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
408380
(void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
409381
(void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
410382
(void)bpf_testmod_test_struct_arg_5();
411-
(void)bpf_testmod_test_struct_arg_7(16, (void *)17, 18, 19,
412-
(void *)20, struct_arg4);
413-
(void)bpf_testmod_test_struct_arg_8(16, (void *)17, 18, 19,
414-
(void *)20, struct_arg4, 23);
415-
(void)bpf_testmod_test_struct_arg_9(16, (void *)17, 18, 19, (void *)20,
416-
21, 22, struct_arg5, 27);
417-
383+
(void)bpf_testmod_test_struct_arg_7(16, (void *)17, 18, 19, (void *)20,
384+
21, 22, 23, struct_arg4);
418385
(void)bpf_testmod_test_arg_ptr_to_struct(&struct_arg1_2);
419386

420387
(void)trace_bpf_testmod_test_raw_tp_null_tp(NULL);

0 commit comments

Comments
 (0)