Skip to content

Commit a51cead

Browse files
tohojokernel-patches-bot
authored andcommitted
selftests: Add selftest for disallowing modify_return attachment to freplace
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. Acked-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
1 parent d892830 commit a51cead

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,60 @@ static void test_func_replace_multi(void)
232232
prog_name, true, test_second_attach);
233233
}
234234

235+
static void test_fmod_ret_freplace(void)
236+
{
237+
struct bpf_object *freplace_obj = NULL, *pkt_obj, *fmod_obj = NULL;
238+
const char *freplace_name = "./freplace_get_constant.o";
239+
const char *fmod_ret_name = "./fmod_ret_freplace.o";
240+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
241+
const char *tgt_name = "./test_pkt_access.o";
242+
struct bpf_link *freplace_link = 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+
opts.attach_prog_fd = pkt_fd;
254+
255+
freplace_obj = bpf_object__open_file(freplace_name, &opts);
256+
if (CHECK(IS_ERR_OR_NULL(freplace_obj), "freplace_obj_open",
257+
"failed to open %s: %ld\n", freplace_name,
258+
PTR_ERR(freplace_obj)))
259+
goto out;
260+
261+
err = bpf_object__load(freplace_obj);
262+
if (CHECK(err, "freplace_obj_load", "err %d\n", err))
263+
goto out;
264+
265+
prog = bpf_program__next(NULL, freplace_obj);
266+
freplace_link = bpf_program__attach_trace(prog);
267+
if (CHECK(IS_ERR(freplace_link), "freplace_attach_trace", "failed to link\n"))
268+
goto out;
269+
270+
opts.attach_prog_fd = bpf_program__fd(prog);
271+
fmod_obj = bpf_object__open_file(fmod_ret_name, &opts);
272+
if (CHECK(IS_ERR_OR_NULL(fmod_obj), "fmod_obj_open",
273+
"failed to open %s: %ld\n", fmod_ret_name,
274+
PTR_ERR(fmod_obj)))
275+
goto out;
276+
277+
err = bpf_object__load(fmod_obj);
278+
if (CHECK(!err, "fmod_obj_load", "loading fmod_ret should fail\n"))
279+
goto out;
280+
281+
out:
282+
bpf_link__destroy(freplace_link);
283+
bpf_object__close(freplace_obj);
284+
bpf_object__close(fmod_obj);
285+
bpf_object__close(pkt_obj);
286+
}
287+
288+
235289
static void test_func_sockmap_update(void)
236290
{
237291
const char *prog_name[] = {
@@ -314,4 +368,6 @@ void test_fexit_bpf2bpf(void)
314368
test_func_map_prog_compatibility();
315369
if (test__start_subtest("func_replace_multi"))
316370
test_func_replace_multi();
371+
if (test__start_subtest("fmod_ret_freplace"))
372+
test_fmod_ret_freplace();
317373
}
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)