Add a subtest in syscall to test bpf_mkdir(), bpf_rmdir(), bpf_unlink() and object pinning in syscall prog. Signed-off-by: Hao Luo <haoluo@xxxxxxxxxx> --- .../selftests/bpf/prog_tests/syscall.c | 67 +++++++++++++++++- .../testing/selftests/bpf/progs/syscall_fs.c | 69 +++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/syscall_fs.c diff --git a/tools/testing/selftests/bpf/prog_tests/syscall.c b/tools/testing/selftests/bpf/prog_tests/syscall.c index f4d40001155a..782b5fe73096 100644 --- a/tools/testing/selftests/bpf/prog_tests/syscall.c +++ b/tools/testing/selftests/bpf/prog_tests/syscall.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2021 Facebook */ +#include <sys/stat.h> #include <test_progs.h> #include "syscall.skel.h" +#include "syscall_fs.skel.h" struct args { __u64 log_buf; @@ -12,7 +14,7 @@ struct args { int btf_fd; }; -void test_syscall(void) +static void test_syscall_basic(void) { static char verifier_log[8192]; struct args ctx = { @@ -53,3 +55,66 @@ void test_syscall(void) if (ctx.btf_fd > 0) close(ctx.btf_fd); } + +static void test_syscall_fs(void) +{ + char tmpl[] = "/sys/fs/bpf/syscall_XXXXXX"; + struct stat statbuf = {}; + static char verifier_log[8192]; + struct args ctx = { + .log_buf = (uintptr_t) verifier_log, + .log_size = sizeof(verifier_log), + .prog_fd = 0, + }; + LIBBPF_OPTS(bpf_test_run_opts, tattr, + .ctx_in = &ctx, + .ctx_size_in = sizeof(ctx), + ); + struct syscall_fs *skel = NULL; + int err, mkdir_fd, rmdir_fd; + char *root, *dir, *path; + + /* prepares test directories */ + system("mount -t bpf bpffs /sys/fs/bpf"); + root = mkdtemp(tmpl); + chmod(root, 0755); + + /* loads prog */ + skel = syscall_fs__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + dir = skel->bss->dirname; + snprintf(dir, sizeof(skel->bss->dirname), "%s/test", root); + path = skel->bss->pathname; + snprintf(path, sizeof(skel->bss->pathname), "%s/prog", dir); + + /* tests mkdir */ + mkdir_fd = bpf_program__fd(skel->progs.mkdir_prog); + err = bpf_prog_test_run_opts(mkdir_fd, &tattr); + ASSERT_EQ(err, 0, "mkdir_err"); + ASSERT_EQ(tattr.retval, 0, "mkdir_retval"); + ASSERT_OK(stat(dir, &statbuf), "mkdir_success"); + ASSERT_OK(stat(path, &statbuf), "pin_success"); + + /* tests rmdir */ + rmdir_fd = bpf_program__fd(skel->progs.rmdir_prog); + err = bpf_prog_test_run_opts(rmdir_fd, &tattr); + ASSERT_EQ(err, 0, "rmdir_err"); + ASSERT_EQ(tattr.retval, 0, "rmdir_retval"); + ASSERT_ERR(stat(path, &statbuf), "unlink_success"); + ASSERT_ERR(stat(dir, &statbuf), "rmdir_success"); + +cleanup: + syscall_fs__destroy(skel); + if (ctx.prog_fd > 0) + close(ctx.prog_fd); + rmdir(root); +} + +void test_syscall(void) { + if (test__start_subtest("basic")) + test_syscall_basic(); + if (test__start_subtest("filesystem")) + test_syscall_fs(); +} diff --git a/tools/testing/selftests/bpf/progs/syscall_fs.c b/tools/testing/selftests/bpf/progs/syscall_fs.c new file mode 100644 index 000000000000..9418d1364c09 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/syscall_fs.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <../../../tools/include/linux/filter.h> + +char _license[] SEC("license") = "GPL"; + +struct args { + __u64 log_buf; + __u32 log_size; + int max_entries; + int map_fd; + int prog_fd; + int btf_fd; +}; + +char dirname[64]; +char pathname[64]; + +SEC("syscall") +int mkdir_prog(struct args *ctx) +{ + static char license[] = "GPL"; + static struct bpf_insn insns[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + static union bpf_attr load_attr = { + .prog_type = BPF_PROG_TYPE_XDP, + .insn_cnt = sizeof(insns) / sizeof(insns[0]), + }; + static union bpf_attr pin_attr = { + .file_flags = 0, + }; + int ret; + + ret = bpf_mkdir(dirname, sizeof(dirname), 0644); + if (ret) + return ret; + + load_attr.license = (long) license; + load_attr.insns = (long) insns; + load_attr.log_buf = ctx->log_buf; + load_attr.log_size = ctx->log_size; + load_attr.log_level = 1; + ret = bpf_sys_bpf(BPF_PROG_LOAD, &load_attr, sizeof(load_attr)); + if (ret < 0) + return ret; + else if (ret == 0) + return -1; + ctx->prog_fd = ret; + + pin_attr.pathname = (__u64)pathname; + pin_attr.bpf_fd = ret; + return bpf_sys_bpf(BPF_OBJ_PIN, &pin_attr, sizeof(pin_attr)); +} + +SEC("syscall") +int rmdir_prog(struct args *ctx) +{ + int ret; + + ret = bpf_unlink(pathname, sizeof(pathname)); + if (ret) + return ret; + + return bpf_rmdir(dirname, sizeof(dirname)); +} -- 2.35.1.574.g5d30c73bfb-goog