From: Mykyta Yatsenko Introducing selftests for validating file-backed dynptr works as expected. * validate implementation supports dynptr slice and read operations * validate destructors should be paired with initializers * validate sleepable progs can page in. Signed-off-by: Mykyta Yatsenko --- .../selftests/bpf/prog_tests/file_reader.c | 114 +++++++++++++ .../testing/selftests/bpf/progs/file_reader.c | 157 ++++++++++++++++++ 2 files changed, 271 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/file_reader.c create mode 100644 tools/testing/selftests/bpf/progs/file_reader.c diff --git a/tools/testing/selftests/bpf/prog_tests/file_reader.c b/tools/testing/selftests/bpf/prog_tests/file_reader.c new file mode 100644 index 000000000000..f4f52dfcb2a5 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/file_reader.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include "file_reader.skel.h" +#include +#include + +const char *user_ptr = "hello world"; +char file_contents[256000]; + +void *get_executable_base_addr(void) +{ + Dl_info info; + + if (!dladdr((void *)&get_executable_base_addr, &info)) { + fprintf(stderr, "dladdr failed\n"); + return NULL; + } + + return info.dli_fbase; +} + +static int initialize_file_contents(void) +{ + int fd, page_sz = sysconf(_SC_PAGESIZE); + ssize_t n = 0, cur, off; + void *addr; + + fd = open("/proc/self/exe", O_RDONLY); + if (!ASSERT_GT(fd, 0, "Open /proc/self/exe\n")) + return 1; + + do { + cur = read(fd, file_contents + n, sizeof(file_contents) - n); + if (!ASSERT_GT(cur, 0, "read success")) + break; + n += cur; + } while (n < sizeof(file_contents)); + + close(fd); + + if (!ASSERT_EQ(n, sizeof(file_contents), "Read /proc/self/exe\n")) + return 1; + + addr = get_executable_base_addr(); + if (!ASSERT_NEQ(addr, NULL, "get executable address")) + return 1; + + /* page-align base file address */ + addr = (void *)((unsigned long)addr & ~(page_sz - 1)); + + for (off = 0; off < sizeof(file_contents); off += page_sz) { + if (!ASSERT_OK(madvise(addr + off, page_sz, MADV_PAGEOUT), + "madvise pageout")) + return errno; + } + + return 0; +} + +static void run_test(const char *prog_name) +{ + struct file_reader *skel; + struct bpf_program *prog; + int err; + char data[256]; + LIBBPF_OPTS(bpf_test_run_opts, opts, .data_in = &data, .repeat = 1, + .data_size_in = sizeof(data)); + + err = initialize_file_contents(); + if (!ASSERT_OK(err, "initialize file contents")) + return; + + skel = file_reader__open(); + if (!ASSERT_OK_PTR(skel, "file_reader__open")) + return; + + bpf_object__for_each_program(prog, skel->obj) { + if (strcmp(bpf_program__name(prog), prog_name) == 0) + bpf_program__set_autoload(prog, true); + else + bpf_program__set_autoload(prog, false); + } + + skel->bss->user_buf = file_contents; + skel->rodata->user_buf_sz = sizeof(file_contents); + skel->bss->pid = getpid(); + skel->bss->user_ptr = (char *)user_ptr; + + err = file_reader__load(skel); + if (!ASSERT_OK(err, "file_reader__load")) + goto cleanup; + + err = file_reader__attach(skel); + if (!ASSERT_OK(err, "file_reader__attach")) + goto cleanup; + + getpid(); + + ASSERT_EQ(skel->bss->err, 0, "err"); +cleanup: + file_reader__destroy(skel); +} + +void test_file_reader(void) +{ + if (test__start_subtest("on_getpid_expect_fault")) + run_test("on_getpid_expect_fault"); + + if (test__start_subtest("on_getpid_validate_file_read")) + run_test("on_getpid_validate_file_read"); +} diff --git a/tools/testing/selftests/bpf/progs/file_reader.c b/tools/testing/selftests/bpf/progs/file_reader.c new file mode 100644 index 000000000000..fce0b40367fe --- /dev/null +++ b/tools/testing/selftests/bpf/progs/file_reader.c @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include +#include "bpf_misc.h" +#include "errno.h" + +char _license[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, struct elem); +} arrmap SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 10000000); +} ringbuf SEC(".maps"); + +struct elem { + struct file *file; + struct bpf_task_work tw; +}; + +int pid = 0; +int err; +char *user_buf; +const char *user_ptr; +volatile const __u32 user_buf_sz; + +static int validate_file_read(struct task_struct *task, struct vm_area_struct *vma, void *data); +static int task_work_callback(struct bpf_map *map, void *key, void *value); +static int dynptr_file_read_fault(struct task_struct *task, struct vm_area_struct *vma, void *data); + +SEC("raw_tp/sys_enter") +int on_getpid_expect_fault(void *c) +{ + struct task_struct *task = bpf_get_current_task_btf(); + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 1; + + /* Verify that in non-sleepable context read faults */ + bpf_find_vma(task, (unsigned long)user_ptr, dynptr_file_read_fault, NULL, 0); + return 0; +} + +/* Tries to read user_buf_sz bytes from file dynptr, returns read error */ +static int dynptr_file_read_fault(struct task_struct *task, struct vm_area_struct *vma, void *data) +{ + struct bpf_dynptr dynptr; + struct file *file = vma->vm_file; + char *rbuf = NULL; + int local_err = 1; + + if (!file) { + err = 1; + return 0; + } + + if (bpf_dynptr_from_file(file, 0, &dynptr)) + goto out; + + rbuf = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0); + if (!rbuf) + goto out; + + local_err = bpf_dynptr_read(rbuf, user_buf_sz, &dynptr, 0, 0); + local_err = local_err == -EFAULT ? 0 : 1; /* Expect page fault */ +out: + if (rbuf) + bpf_ringbuf_discard(rbuf, 0); + bpf_dynptr_file_discard(&dynptr); + if (local_err) + err = local_err; + return 0; +} + +SEC("raw_tp/sys_enter") +int on_getpid_validate_file_read(void *c) +{ + struct task_struct *task = bpf_get_current_task_btf(); + struct elem *work; + int key = 0; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 1; + + work = bpf_map_lookup_elem(&arrmap, &key); + if (!work) { + err = 1; + return 0; + } + bpf_task_work_schedule_signal(task, &work->tw, &arrmap, task_work_callback, NULL); + return 0; +} + +/* Called in a sleepable context, read 256K bytes, cross check with user space read data */ +static int task_work_callback(struct bpf_map *map, void *key, void *value) +{ + struct task_struct *task = bpf_get_current_task_btf(); + + bpf_find_vma(task, (unsigned long)user_ptr, validate_file_read, NULL, 0); + return 0; +} + +static int validate_file_read(struct task_struct *task, struct vm_area_struct *vma, void *data) +{ + struct bpf_dynptr dynptr; + int local_err = 1, i; + char *rbuf1 = NULL, *rbuf2 = NULL; + struct file *file = vma->vm_file; + + if (!file) { + err = 1; + return 1; + } + + if (bpf_dynptr_from_file(file, 0, &dynptr)) + goto cleanup_file; + + rbuf1 = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0); + if (!rbuf1) + goto cleanup_file; + + rbuf2 = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0); + if (!rbuf2) + goto cleanup_all; + + if (bpf_dynptr_read(rbuf1, user_buf_sz, &dynptr, 0, 0)) + goto cleanup_all; + + bpf_copy_from_user(rbuf2, user_buf_sz, user_buf); + /* Verify file contents read from BPF is the same as the one read from userspace */ + bpf_for(i, 0, user_buf_sz) + { + if (i >= 256000 || rbuf1[i] != rbuf2[i]) + goto cleanup_all; + } + local_err = 0; + +cleanup_all: + if (rbuf1) + bpf_ringbuf_discard(rbuf1, 0); + if (rbuf2) + bpf_ringbuf_discard(rbuf2, 0); +cleanup_file: + bpf_dynptr_file_discard(&dynptr); + if (local_err) + err = local_err; + return 0; +} -- 2.51.0