selftests/bpf: add file dynptr tests

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 <yatsenko@meta.com>
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251026203853.135105-11-mykyta.yatsenko5@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Mykyta Yatsenko
2025-10-26 20:38:53 +00:00
committed by Alexei Starovoitov
parent 2c52e8943a
commit 784cdf9315
3 changed files with 310 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <network_helpers.h>
#include "file_reader.skel.h"
#include "file_reader_fail.skel.h"
#include <dlfcn.h>
#include <sys/mman.h>
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_OK_FD(fd, "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, fd;
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) {
bpf_program__set_autoload(prog, strcmp(bpf_program__name(prog), prog_name) == 0);
}
memcpy(skel->bss->user_buf, file_contents, sizeof(file_contents));
skel->bss->pid = getpid();
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;
fd = open("/proc/self/exe", O_RDONLY);
if (fd >= 0)
close(fd);
ASSERT_EQ(skel->bss->err, 0, "err");
ASSERT_EQ(skel->bss->run_success, 1, "run_success");
cleanup:
file_reader__destroy(skel);
}
void test_file_reader(void)
{
if (test__start_subtest("on_open_expect_fault"))
run_test("on_open_expect_fault");
if (test__start_subtest("on_open_validate_file_read"))
run_test("on_open_validate_file_read");
if (test__start_subtest("negative"))
RUN_TESTS(file_reader_fail);
}

View File

@@ -0,0 +1,145 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <vmlinux.h>
#include <string.h>
#include <stdbool.h>
#include <bpf/bpf_tracing.h>
#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 elem {
struct file *file;
struct bpf_task_work tw;
};
char user_buf[256000];
char tmp_buf[256000];
int pid = 0;
int err, run_success = 0;
static int validate_file_read(struct file *file);
static int task_work_callback(struct bpf_map *map, void *key, void *value);
SEC("lsm/file_open")
int on_open_expect_fault(void *c)
{
struct bpf_dynptr dynptr;
struct file *file;
int local_err = 1;
__u32 user_buf_sz = sizeof(user_buf);
if (bpf_get_current_pid_tgid() >> 32 != pid)
return 0;
file = bpf_get_task_exe_file(bpf_get_current_task_btf());
if (!file)
return 0;
if (bpf_dynptr_from_file(file, 0, &dynptr))
goto out;
local_err = bpf_dynptr_read(tmp_buf, user_buf_sz, &dynptr, 0, 0);
if (local_err == -EFAULT) { /* Expect page fault */
local_err = 0;
run_success = 1;
}
out:
bpf_dynptr_file_discard(&dynptr);
if (local_err)
err = local_err;
bpf_put_file(file);
return 0;
}
SEC("lsm/file_open")
int on_open_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 0;
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();
struct file *file = bpf_get_task_exe_file(task);
if (!file)
return 0;
err = validate_file_read(file);
if (!err)
run_success = 1;
bpf_put_file(file);
return 0;
}
static int verify_dynptr_read(struct bpf_dynptr *ptr, u32 off, char *user_buf, u32 len)
{
int i;
if (bpf_dynptr_read(tmp_buf, len, ptr, off, 0))
return 1;
/* Verify file contents read from BPF is the same as the one read from userspace */
bpf_for(i, 0, len)
{
if (tmp_buf[i] != user_buf[i])
return 1;
}
return 0;
}
static int validate_file_read(struct file *file)
{
struct bpf_dynptr dynptr;
int loc_err = 1, off;
__u32 user_buf_sz = sizeof(user_buf);
if (bpf_dynptr_from_file(file, 0, &dynptr))
goto cleanup;
loc_err = verify_dynptr_read(&dynptr, 0, user_buf, user_buf_sz);
off = 1;
loc_err = loc_err ?: verify_dynptr_read(&dynptr, off, user_buf + off, user_buf_sz - off);
off = user_buf_sz - 1;
loc_err = loc_err ?: verify_dynptr_read(&dynptr, off, user_buf + off, user_buf_sz - off);
/* Read file with random offset and length */
off = 4097;
loc_err = loc_err ?: verify_dynptr_read(&dynptr, off, user_buf + off, 100);
/* Adjust dynptr, verify read */
loc_err = loc_err ?: bpf_dynptr_adjust(&dynptr, off, off + 1);
loc_err = loc_err ?: verify_dynptr_read(&dynptr, 0, user_buf + off, 1);
/* Can't read more than 1 byte */
loc_err = loc_err ?: verify_dynptr_read(&dynptr, 0, user_buf + off, 2) == 0;
/* Can't read with far offset */
loc_err = loc_err ?: verify_dynptr_read(&dynptr, 1, user_buf + off, 1) == 0;
cleanup:
bpf_dynptr_file_discard(&dynptr);
return loc_err;
}

View File

@@ -0,0 +1,52 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <vmlinux.h>
#include <string.h>
#include <stdbool.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
char _license[] SEC("license") = "GPL";
int err;
void *user_ptr;
SEC("lsm/file_open")
__failure
__msg("Unreleased reference id=")
int on_nanosleep_unreleased_ref(void *ctx)
{
struct task_struct *task = bpf_get_current_task_btf();
struct file *file = bpf_get_task_exe_file(task);
struct bpf_dynptr dynptr;
if (!file)
return 0;
err = bpf_dynptr_from_file(file, 0, &dynptr);
return err ? 1 : 0;
}
SEC("xdp")
__failure
__msg("Expected a dynptr of type file as arg #0")
int xdp_wrong_dynptr_type(struct xdp_md *xdp)
{
struct bpf_dynptr dynptr;
bpf_dynptr_from_xdp(xdp, 0, &dynptr);
bpf_dynptr_file_discard(&dynptr);
return 0;
}
SEC("xdp")
__failure
__msg("Expected an initialized dynptr as arg #0")
int xdp_no_dynptr_type(struct xdp_md *xdp)
{
struct bpf_dynptr dynptr;
bpf_dynptr_file_discard(&dynptr);
return 0;
}