This patch adds test cases for bpf_fget_task() kfunc. test_bpf_fget_task is used to test obtaining struct file based on the file descriptor in the current process. bpf_fget_task_null_task and bpf_fget_task_untrusted_task are used to test the failure cases of passing NULL or untrusted pointer as argument. Signed-off-by: Juntong Deng <juntong.deng@xxxxxxxxxxx> --- .../testing/selftests/bpf/bpf_experimental.h | 8 +++ .../selftests/bpf/prog_tests/fs_kfuncs.c | 46 ++++++++++++++ .../selftests/bpf/progs/fs_kfuncs_failure.c | 33 ++++++++++ .../selftests/bpf/progs/test_fget_task.c | 63 +++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c create mode 100644 tools/testing/selftests/bpf/progs/test_fget_task.c diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index ce1520c56b55..e0c9e7d9ba0a 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -221,6 +221,14 @@ extern void bpf_put_file(struct file *file) __ksym; */ extern int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) __ksym; +/* Description + * Get a pointer to the struct file corresponding to the task file descriptor + * Note that this function acquires a reference to struct file. + * Returns + * The corresponding struct file pointer if found, otherwise returns NULL + */ +extern struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) __ksym; + /* This macro must be used to mark the exception callback corresponding to the * main program. For example: * diff --git a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c index 5a0b51157451..89f5e09672b3 100644 --- a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +++ b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c @@ -9,6 +9,8 @@ #include <test_progs.h> #include "test_get_xattr.skel.h" #include "test_fsverity.skel.h" +#include "test_fget_task.skel.h" +#include "fs_kfuncs_failure.skel.h" static const char testfile[] = "/tmp/test_progs_fs_kfuncs"; @@ -139,6 +141,45 @@ static void test_fsverity(void) remove(testfile); } +static void test_fget_task(void) +{ + int pipefd[2], prog_fd, err; + struct test_fget_task *skel; + struct bpf_program *prog; + + skel = test_fget_task__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + if (!ASSERT_OK(skel->bss->err, "pre_test_err")) + goto cleanup_skel; + + prog = bpf_object__find_program_by_name(skel->obj, "test_bpf_fget_task"); + if (!ASSERT_OK_PTR(prog, "find_program_by_name")) + goto cleanup_skel; + + prog_fd = bpf_program__fd(prog); + if (!ASSERT_GT(prog_fd, -1, "bpf_program__fd")) + goto cleanup_skel; + + if (pipe(pipefd) < 0) + goto cleanup_skel; + + skel->bss->test_fd1 = pipefd[0]; + skel->bss->test_fd2 = pipefd[1]; + + err = bpf_prog_test_run_opts(prog_fd, NULL); + if (!ASSERT_OK(err, "prog_test_run")) + goto cleanup_pipe; + + ASSERT_OK(skel->bss->err, "run_bpf_fget_task_test_failure"); +cleanup_pipe: + close(pipefd[0]); + close(pipefd[1]); +cleanup_skel: + test_fget_task__destroy(skel); +} + void test_fs_kfuncs(void) { if (test__start_subtest("xattr")) @@ -146,4 +187,9 @@ void test_fs_kfuncs(void) if (test__start_subtest("fsverity")) test_fsverity(); + + if (test__start_subtest("fget_task")) + test_fget_task(); + + RUN_TESTS(fs_kfuncs_failure); } diff --git a/tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c b/tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c new file mode 100644 index 000000000000..57aa6d2787ac --- /dev/null +++ b/tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" +#include "bpf_experimental.h" + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_fget_task_null_task(void *ctx) +{ + struct task_struct *task = NULL; + + bpf_fget_task(task, 1); + + return 0; +} + +SEC("syscall") +__failure __msg("R1 must be referenced or trusted") +int bpf_fget_task_untrusted_task(void *ctx) +{ + struct task_struct *task; + + task = bpf_get_current_task_btf()->parent; + + bpf_fget_task(task, 1); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/test_fget_task.c b/tools/testing/selftests/bpf/progs/test_fget_task.c new file mode 100644 index 000000000000..fee5d5e1244a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_fget_task.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" +#include "bpf_experimental.h" +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +int err, test_fd1, test_fd2; + +extern const void pipefifo_fops __ksym; + +SEC("syscall") +int test_bpf_fget_task(void *ctx) +{ + struct task_struct *task; + struct file *file; + + task = bpf_get_current_task_btf(); + if (task == NULL) { + err = 1; + return 0; + } + + file = bpf_fget_task(task, test_fd1); + if (file == NULL) { + err = 2; + return 0; + } + + if (file->f_op != &pipefifo_fops) { + err = 3; + bpf_put_file(file); + return 0; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, test_fd2); + if (file == NULL) { + err = 4; + return 0; + } + + if (file->f_op != &pipefifo_fops) { + err = 5; + bpf_put_file(file); + return 0; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 9999); + if (file != NULL) { + err = 6; + bpf_put_file(file); + } + + return 0; +} -- 2.39.5