Add a new subtest to the token prog_test which attaches an fentry program to a module function. This requires libbpf to read the module BTF information using a bpf_token, which is only possible with the fixes in the previous commits. Signed-off-by: Toke Høiland-Jørgensen --- tools/testing/selftests/bpf/prog_tests/token.c | 37 +++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/priv_module.c | 15 ++++++++++ 2 files changed, 52 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c index 6499db1704bb..b16b9b4d3d45 100644 --- a/tools/testing/selftests/bpf/prog_tests/token.c +++ b/tools/testing/selftests/bpf/prog_tests/token.c @@ -26,6 +26,7 @@ #include "token_kallsyms.skel.h" #include "token_lsm.skel.h" #include "priv_freplace_prog.skel.h" +#include "priv_module.skel.h" static inline int sys_mount(const char *dev_name, const char *dir_name, const char *type, unsigned long flags, @@ -1099,6 +1100,34 @@ static int userns_obj_priv_prog_kallsyms(int mnt_fd, struct token_lsm *lsm_skel) return err; } +static int userns_obj_priv_fentry_module(int mnt_fd, struct token_lsm *lsm_skel) +{ + LIBBPF_OPTS(bpf_object_open_opts, opts); + struct priv_module *skel = NULL; + char buf[256]; + int err; + + /* use bpf_token_path to provide BPF FS path */ + snprintf(buf, sizeof(buf), "/proc/self/fd/%d", mnt_fd); + opts.bpf_token_path = buf; + skel = priv_module__open_opts(&opts); + if (!ASSERT_OK_PTR(skel, "priv_module__open_opts")) + return -EINVAL; + + err = priv_module__load(skel); + if (!ASSERT_OK(err, "priv_module__load")) + goto out; + + err = priv_module__attach(skel); + if (!ASSERT_OK(err, "priv_module__attach")) + goto out; + +out: + priv_module__destroy(skel); + return err; +} + + #define bit(n) (1ULL << (n)) static int userns_bpf_token_info(int mnt_fd, struct token_lsm *lsm_skel) @@ -1195,6 +1224,14 @@ void serial_test_token(void) }; subtest_userns(&opts, userns_obj_priv_freplace_prog_fail); } + if (test__start_subtest("obj_priv_fentry_module")) { + struct bpffs_opts opts = { + .cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD) | bit(BPF_BTF_GET_FD_BY_ID) | bit(BPF_BTF_GET_NEXT_ID), + .progs = bit(BPF_PROG_TYPE_TRACING), + .attachs = ~0ULL, + }; + subtest_userns(&opts, userns_obj_priv_fentry_module); + } if (test__start_subtest("obj_priv_btf_fail")) { struct bpffs_opts opts = { /* disallow BTF loading */ diff --git a/tools/testing/selftests/bpf/progs/priv_module.c b/tools/testing/selftests/bpf/progs/priv_module.c new file mode 100644 index 000000000000..3743ceb43c42 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/priv_module.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include +#include "../test_kmods/bpf_testmod.h" + +SEC("fentry/bpf_testmod_test_read") +int BPF_PROG(handle_fentry) +{ + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.56.1