Check that a BPF program that uses cgroup storage cannot be added to a program array map. Signed-off-by: Amery Hung --- .../selftests/bpf/prog_tests/tailcalls.c | 25 ++++++++++++ .../bpf/progs/tailcall_cgrp_storage.c | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c index 0ab36503c3b2..e4a5287f10b1 100644 --- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c +++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c @@ -8,6 +8,7 @@ #include "tailcall_freplace.skel.h" #include "tc_bpf2bpf.skel.h" #include "tailcall_fail.skel.h" +#include "tailcall_cgrp_storage.skel.h" /* test_tailcall_1 checks basic functionality by patching multiple locations * in a single program for a single tail call slot with nop->jmp, jmp->nop @@ -1648,6 +1649,28 @@ static void test_tailcall_bpf2bpf_freplace(void) tc_bpf2bpf__destroy(tc_skel); } +/* + * test_tail_call_cgrp_storage makes sure that callee programs cannot + * use cgroup storage + */ +static void test_tailcall_cgrp_storage(void) +{ + int err, prog_fd, prog_array_fd, key = 0; + struct tailcall_cgrp_storage *skel; + + skel = tailcall_cgrp_storage__open_and_load(); + if (!ASSERT_OK_PTR(skel, "tailcall_cgrp_storage__open_and_load")) + return; + + prog_fd = bpf_program__fd(skel->progs.callee_prog); + prog_array_fd = bpf_map__fd(skel->maps.prog_array); + + err = bpf_map_update_elem(prog_array_fd, &key, &prog_fd, BPF_ANY); + ASSERT_ERR(err, "bpf_map_update_elem"); + + tailcall_cgrp_storage__destroy(skel); +} + static void test_tailcall_failure() { RUN_TESTS(tailcall_fail); @@ -1705,6 +1728,8 @@ void test_tailcalls(void) test_tailcall_freplace(); if (test__start_subtest("tailcall_bpf2bpf_freplace")) test_tailcall_bpf2bpf_freplace(); + if (test__start_subtest("tailcall_cgrp_storage")) + test_tailcall_cgrp_storage(); if (test__start_subtest("tailcall_failure")) test_tailcall_failure(); } diff --git a/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c b/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c new file mode 100644 index 000000000000..e4f277d2c4fe --- /dev/null +++ b/tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); + __type(key, struct bpf_cgroup_storage_key); + __type(value, __u64); +} storage_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} prog_array SEC(".maps"); + +SEC("cgroup_skb/egress") +int caller_prog(struct __sk_buff *skb) +{ + bpf_tail_call(skb, &prog_array, 0); + + return 1; +} + +SEC("cgroup_skb/egress") +int callee_prog(struct __sk_buff *skb) +{ + __u64 *storage; + + storage = bpf_get_local_storage(&storage_map, 0); + if (storage) + *storage = 1; + + return 1; +} + +char _license[] SEC("license") = "GPL"; -- 2.47.3