Skip to content

Commit 07d48f9

Browse files
tohojokernel-patches-bot
authored andcommitted
From: Toke Høiland-Jørgensen <[email protected]>
This adds a selftest that ensures that modify_return tracing programs cannot be attached to freplace programs. The security_ prefix is added to the freplace program because that would otherwise let it pass the check for modify_return. Signed-off-by: Toke Høiland-Jørgensen <[email protected]> --- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 68 ++++++++++++++++++++ .../selftests/bpf/progs/fmod_ret_freplace.c | 14 ++++ .../selftests/bpf/progs/freplace_get_constant.c | 2 - 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/fmod_ret_freplace.c
1 parent 8443a62 commit 07d48f9

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,72 @@ static void test_func_replace_multi(void)
233233
prog_name, true, test_second_attach);
234234
}
235235

236+
static void test_fmod_ret_freplace(void)
237+
{
238+
const char *tgt_name = "./test_pkt_access.o";
239+
const char *freplace_name = "./freplace_get_constant.o";
240+
const char *fmod_ret_name = "./fmod_ret_freplace.o";
241+
struct bpf_link *freplace_link = NULL, *fmod_link = NULL;
242+
struct bpf_object *freplace_obj = NULL, *pkt_obj, *fmod_obj = NULL;
243+
struct bpf_program *prog;
244+
__u32 duration = 0;
245+
int err, pkt_fd;
246+
247+
err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC,
248+
&pkt_obj, &pkt_fd);
249+
/* the target prog should load fine */
250+
if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
251+
tgt_name, err, errno))
252+
return;
253+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
254+
.attach_prog_fd = pkt_fd,
255+
);
256+
257+
freplace_obj = bpf_object__open_file(freplace_name, &opts);
258+
if (CHECK(IS_ERR_OR_NULL(freplace_obj), "freplace_obj_open",
259+
"failed to open %s: %ld\n", freplace_name,
260+
PTR_ERR(freplace_obj)))
261+
goto out;
262+
263+
err = bpf_object__load(freplace_obj);
264+
if (CHECK(err, "freplace_obj_load", "err %d\n", err))
265+
goto out;
266+
267+
prog = bpf_program__next(NULL, freplace_obj);
268+
freplace_link = bpf_program__attach_trace(prog);
269+
if (CHECK(IS_ERR(freplace_link), "freplace_attach_trace", "failed to link\n"))
270+
goto out;
271+
272+
opts.attach_prog_fd = bpf_program__fd(prog);
273+
fmod_obj = bpf_object__open_file(fmod_ret_name, &opts);
274+
if (CHECK(IS_ERR_OR_NULL(fmod_obj), "fmod_obj_open",
275+
"failed to open %s: %ld\n", fmod_ret_name,
276+
PTR_ERR(fmod_obj)))
277+
goto out;
278+
279+
err = bpf_object__load(fmod_obj);
280+
if (CHECK(err, "fmod_obj_load", "err %d\n", err))
281+
goto out;
282+
283+
prog = bpf_program__next(NULL, fmod_obj);
284+
fmod_link = bpf_program__attach_trace(prog);
285+
if (CHECK(!IS_ERR(fmod_link), "fmod_attach_trace",
286+
"linking fmod_ret to freplace should fail\n"))
287+
goto out;
288+
289+
out:
290+
if (!IS_ERR_OR_NULL(freplace_link))
291+
bpf_link__destroy(freplace_link);
292+
if (!IS_ERR_OR_NULL(fmod_link))
293+
bpf_link__destroy(fmod_link);
294+
if (!IS_ERR_OR_NULL(freplace_obj))
295+
bpf_object__close(freplace_obj);
296+
if (!IS_ERR_OR_NULL(fmod_obj))
297+
bpf_object__close(fmod_obj);
298+
bpf_object__close(pkt_obj);
299+
}
300+
301+
236302
static void test_func_sockmap_update(void)
237303
{
238304
const char *prog_name[] = {
@@ -315,4 +381,6 @@ void test_fexit_bpf2bpf(void)
315381
test_func_map_prog_compatibility();
316382
if (test__start_subtest("func_replace_multi"))
317383
test_func_replace_multi();
384+
if (test__start_subtest("fmod_ret_freplace"))
385+
test_fmod_ret_freplace();
318386
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
volatile __u64 test_fmod_ret = 0;
7+
SEC("fmod_ret/security_new_get_constant")
8+
int BPF_PROG(fmod_ret_test, long val, int ret)
9+
{
10+
test_fmod_ret = 1;
11+
return 120;
12+
}
13+
14+
char _license[] SEC("license") = "GPL";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
volatile __u64 test_get_constant = 0;
77
SEC("freplace/get_constant")
8-
int new_get_constant(long val)
8+
int security_new_get_constant(long val)
99
{
1010
if (val != 123)
1111
return 0;

0 commit comments

Comments
 (0)