Test that bpf_copy_to_user can fault in a paged-out page and write to a read-only mapping. Also verify that a partial copy crossing into an unmapped page returns -EFAULT while leaving the copied byte in place. Assisted-by: Codex:GPT-6 Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/user_mem.c | 106 ++++++++++++++++++ tools/testing/selftests/bpf/progs/user_mem.c | 17 +++ 2 files changed, 123 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/user_mem.c b/tools/testing/selftests/bpf/prog_tests/user_mem.c index 7fa827298d24..c87620e39abb 100644 --- a/tools/testing/selftests/bpf/prog_tests/user_mem.c +++ b/tools/testing/selftests/bpf/prog_tests/user_mem.c @@ -12,8 +12,11 @@ enum { USER_MEM_MMAP = 1, USER_MEM_UNMAP, USER_MEM_COPY_INSNS, + USER_MEM_COPY, }; +static const char data[] = "fault-enabled BPF write"; + static noinline void user_mem_trigger(void) { asm volatile ("" ::: "memory"); @@ -102,6 +105,18 @@ static bool user_mem_copy_insns(struct user_mem *skel, unsigned long addr) return ASSERT_OK(skel->bss->ret, "user_write"); } +static int user_mem_copy(struct user_mem *skel, unsigned long addr, + u32 size, u64 flags) +{ + skel->bss->user_addr = addr; + skel->bss->copy_size = size; + skel->bss->copy_flags = flags; + skel->bss->action = USER_MEM_COPY; + user_mem_trigger(); + + return skel->bss->ret; +} + static void test_execute_user_page(struct user_mem *skel) { long page_size = sysconf(_SC_PAGESIZE); @@ -163,6 +178,91 @@ static void test_partial_exec(struct user_mem *skel) munmap((void *)addr, page_size * 3); } +static void test_faultable_write(struct user_mem *skel) +{ + long page_size = sysconf(_SC_PAGESIZE); + unsigned long addr = ~0UL; + unsigned char vec; + int i; + + if (!user_mem_mmap(skel, page_size, PROT_READ | PROT_WRITE, &addr)) + return; + + /* Make sure there is a private resident page to page out. */ + memset((void *)addr, 0, page_size); + + /* Retry pageout until mincore confirms that the page is no longer resident. */ + for (i = 0; i < 500; i++) { + if (madvise((void *)addr, page_size, MADV_PAGEOUT)) { + test__skip(); + goto out; + } + if (!ASSERT_OK(mincore((void *)addr, page_size, &vec), "mincore")) + goto out; + if (!(vec & 1)) + break; + usleep(100); + } + if (i == 500) { + test__skip(); + goto out; + } + + /* ..and do the copy to it. */ + if (!ASSERT_OK(user_mem_copy(skel, addr, sizeof(data), 0), "user_write_data")) + goto out; + + ASSERT_EQ(memcmp((void *)addr, data, sizeof(data)), 0, "written_data"); + +out: + if (addr != ~0UL) + munmap((void *)addr, page_size); +} + +static void test_copy_to_readonly_mapping(struct user_mem *skel) +{ + long page_size = sysconf(_SC_PAGESIZE); + unsigned long addr = ~0UL; + + if (!user_mem_mmap(skel, page_size, PROT_READ, &addr)) + return; + + /* Assert we can copy to read-only mapping. */ + if (!ASSERT_OK(user_mem_copy(skel, addr, sizeof(data), 0), "readonly_copy")) + goto out; + + ASSERT_EQ(memcmp((void *)addr, data, sizeof(data)), 0, "readonly_copy_data"); + +out: + if (addr != ~0UL) + munmap((void *)addr, page_size); +} + +static void test_partial_copy_fault(struct user_mem *skel) +{ + long page_size = sysconf(_SC_PAGESIZE); + bool second_page_unmapped = false; + unsigned long addr = ~0UL; + + if (!user_mem_mmap(skel, page_size * 2, PROT_READ | PROT_WRITE, &addr)) + return; + + if (!user_mem_munmap(skel, addr + page_size, page_size)) + goto out; + + second_page_unmapped = true; + + ASSERT_EQ(user_mem_copy(skel, addr + page_size - 1, sizeof(data), 0), + -EFAULT, "partial_copy"); + + /* The byte before the fault stays written. */ + ASSERT_EQ(((char *)addr)[page_size - 1], 'f', "partial_copy_data"); + +out: + if (addr != ~0UL) + munmap((void *)addr, second_page_unmapped ? page_size : page_size * 2); +} + void test_user_mem(void) { struct user_mem *skel; @@ -181,6 +281,12 @@ void test_user_mem(void) test_execute_user_page(skel); if (test__start_subtest("partial_exec")) test_partial_exec(skel); + if (test__start_subtest("faultable_write")) + test_faultable_write(skel); + if (test__start_subtest("copy_to_readonly_mapping")) + test_copy_to_readonly_mapping(skel); + if (test__start_subtest("partial_copy_fault")) + test_partial_copy_fault(skel); out: user_mem__destroy(skel); diff --git a/tools/testing/selftests/bpf/progs/user_mem.c b/tools/testing/selftests/bpf/progs/user_mem.c index b477132e3883..6ebfdc54edad 100644 --- a/tools/testing/selftests/bpf/progs/user_mem.c +++ b/tools/testing/selftests/bpf/progs/user_mem.c @@ -17,6 +17,7 @@ enum { USER_MEM_MMAP = 1, USER_MEM_UNMAP, USER_MEM_COPY_INSNS, + USER_MEM_COPY, }; int monitored_pid; @@ -27,6 +28,9 @@ unsigned long user_addr; unsigned long map_len; unsigned long map_prot; +u64 copy_flags; +u32 copy_size; + #if defined(__TARGET_ARCH_x86) static const unsigned char insns[] = { 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */ @@ -43,9 +47,13 @@ static const unsigned char insns[] = { static const unsigned char insns[] = { 0 }; #endif +static const char data[] = "fault-enabled BPF write"; + SEC("uprobe.s//proc/self/exe:user_mem_trigger") int user_mem_test(struct pt_regs *ctx) { + u32 size; + if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid) return 0; @@ -59,6 +67,15 @@ int user_mem_test(struct pt_regs *ctx) case USER_MEM_COPY_INSNS: ret = bpf_copy_to_user((void *)user_addr, insns, sizeof(insns), 0); break; + case USER_MEM_COPY: + /* Bound the kfunc length using a local copy of copy_size. */ + size = copy_size; + if (size > sizeof(data)) { + ret = -22; /* -EINVAL */ + break; + } + ret = bpf_copy_to_user((void *)user_addr, data, size, copy_flags); + break; } return 0; -- 2.54.0