Skip to content

Commit 3d08b6f

Browse files
KP SinghAlexei Starovoitov
authored andcommitted
bpf: Add selftests for BPF_MODIFY_RETURN
Test for two scenarios: * When the fmod_ret program returns 0, the original function should be called along with fentry and fexit programs. * When the fmod_ret program returns a non-zero value, the original function should not be called, no side effect should be observed and fentry and fexit programs should be called. The result from the kernel function call and whether a side-effect is observed is returned via the retval attr of the BPF_PROG_TEST_RUN (bpf) syscall. Signed-off-by: KP Singh <kpsingh@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200304191853.1529-8-kpsingh@chromium.org
1 parent da00d2f commit 3d08b6f

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎net/bpf/test_run.c‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <net/bpf_sk_storage.h>
1111
#include <net/sock.h>
1212
#include <net/tcp.h>
13+
#include <linux/error-injection.h>
1314

1415
#define CREATE_TRACE_POINTS
1516
#include <trace/events/bpf_test_run.h>
@@ -143,6 +144,14 @@ int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
143144
return a + (long)b + c + d + (long)e + f;
144145
}
145146

147+
int noinline bpf_modify_return_test(int a, int *b)
148+
{
149+
*b += 1;
150+
return a + *b;
151+
}
152+
153+
ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
154+
146155
static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
147156
u32 headroom, u32 tailroom)
148157
{
@@ -168,7 +177,9 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog,
168177
const union bpf_attr *kattr,
169178
union bpf_attr __user *uattr)
170179
{
171-
int err = -EFAULT;
180+
u16 side_effect = 0, ret = 0;
181+
int b = 2, err = -EFAULT;
182+
u32 retval = 0;
172183

173184
switch (prog->expected_attach_type) {
174185
case BPF_TRACE_FENTRY:
@@ -181,10 +192,19 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog,
181192
bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111)
182193
goto out;
183194
break;
195+
case BPF_MODIFY_RETURN:
196+
ret = bpf_modify_return_test(1, &b);
197+
if (b != 2)
198+
side_effect = 1;
199+
break;
184200
default:
185201
goto out;
186202
}
187203

204+
retval = ((u32)side_effect << 16) | ret;
205+
if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
206+
goto out;
207+
188208
err = 0;
189209
out:
190210
trace_bpf_test_finish(&err);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright 2020 Google LLC.
5+
*/
6+
7+
#include <test_progs.h>
8+
#include "modify_return.skel.h"
9+
10+
#define LOWER(x) ((x) & 0xffff)
11+
#define UPPER(x) ((x) >> 16)
12+
13+
14+
static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
15+
{
16+
struct modify_return *skel = NULL;
17+
int err, prog_fd;
18+
__u32 duration = 0, retval;
19+
__u16 side_effect;
20+
__s16 ret;
21+
22+
skel = modify_return__open_and_load();
23+
if (CHECK(!skel, "skel_load", "modify_return skeleton failed\n"))
24+
goto cleanup;
25+
26+
err = modify_return__attach(skel);
27+
if (CHECK(err, "modify_return", "attach failed: %d\n", err))
28+
goto cleanup;
29+
30+
skel->bss->input_retval = input_retval;
31+
prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
32+
err = bpf_prog_test_run(prog_fd, 1, NULL, 0, NULL, 0,
33+
&retval, &duration);
34+
35+
CHECK(err, "test_run", "err %d errno %d\n", err, errno);
36+
37+
side_effect = UPPER(retval);
38+
ret = LOWER(retval);
39+
40+
CHECK(ret != want_ret, "test_run",
41+
"unexpected ret: %d, expected: %d\n", ret, want_ret);
42+
CHECK(side_effect != want_side_effect, "modify_return",
43+
"unexpected side_effect: %d\n", side_effect);
44+
45+
CHECK(skel->bss->fentry_result != 1, "modify_return",
46+
"fentry failed\n");
47+
CHECK(skel->bss->fexit_result != 1, "modify_return",
48+
"fexit failed\n");
49+
CHECK(skel->bss->fmod_ret_result != 1, "modify_return",
50+
"fmod_ret failed\n");
51+
52+
cleanup:
53+
modify_return__destroy(skel);
54+
}
55+
56+
void test_modify_return(void)
57+
{
58+
run_test(0 /* input_retval */,
59+
1 /* want_side_effect */,
60+
4 /* want_ret */);
61+
run_test(-EINVAL /* input_retval */,
62+
0 /* want_side_effect */,
63+
-EINVAL /* want_ret */);
64+
}
65+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright 2020 Google LLC.
5+
*/
6+
7+
#include <linux/bpf.h>
8+
#include <bpf/bpf_helpers.h>
9+
#include <bpf/bpf_tracing.h>
10+
11+
char _license[] SEC("license") = "GPL";
12+
13+
static int sequence = 0;
14+
__s32 input_retval = 0;
15+
16+
__u64 fentry_result = 0;
17+
SEC("fentry/bpf_modify_return_test")
18+
int BPF_PROG(fentry_test, int a, __u64 b)
19+
{
20+
sequence++;
21+
fentry_result = (sequence == 1);
22+
return 0;
23+
}
24+
25+
__u64 fmod_ret_result = 0;
26+
SEC("fmod_ret/bpf_modify_return_test")
27+
int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
28+
{
29+
sequence++;
30+
/* This is the first fmod_ret program, the ret passed should be 0 */
31+
fmod_ret_result = (sequence == 2 && ret == 0);
32+
return input_retval;
33+
}
34+
35+
__u64 fexit_result = 0;
36+
SEC("fexit/bpf_modify_return_test")
37+
int BPF_PROG(fexit_test, int a, __u64 b, int ret)
38+
{
39+
sequence++;
40+
/* If the input_reval is non-zero a successful modification should have
41+
* occurred.
42+
*/
43+
if (input_retval)
44+
fexit_result = (sequence == 3 && ret == input_retval);
45+
else
46+
fexit_result = (sequence == 3 && ret == 4);
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)