From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Lorenzo Stoakes <ljs@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Federico Di Pierro <nierro92@gmail.com>
Subject: [PATCH bpf-next 6/8] selftests/bpf: Test user memory page exec
Date: Thu, 29 Oct 2026 22:25:49 +0100 [thread overview]
Message-ID: <20261029212551.56964-7-jolsa@kernel.org> (raw)
In-Reply-To: <20261029212551.56964-1-jolsa@kernel.org>
Use bpf_copy_to_user from a uprobe to write architecture-specific
instructions into user memory, then execute them on x86-64 and arm64.
Test execution from a mapping created with read and execute permissions,
and from individual pages made executable with mprotect.
Assisted-by: Codex:GPT-6
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
.../selftests/bpf/prog_tests/user_mem.c | 75 +++++++++++++++++++
tools/testing/selftests/bpf/progs/user_mem.c | 22 ++++++
2 files changed, 97 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/user_mem.c b/tools/testing/selftests/bpf/prog_tests/user_mem.c
index f301a1843706..7fa827298d24 100644
--- a/tools/testing/selftests/bpf/prog_tests/user_mem.c
+++ b/tools/testing/selftests/bpf/prog_tests/user_mem.c
@@ -11,6 +11,7 @@
enum {
USER_MEM_MMAP = 1,
USER_MEM_UNMAP,
+ USER_MEM_COPY_INSNS,
};
static noinline void user_mem_trigger(void)
@@ -92,6 +93,76 @@ static void test_mapping(struct user_mem *skel)
munmap((void *)addr, len);
}
+static bool user_mem_copy_insns(struct user_mem *skel, unsigned long addr)
+{
+ skel->bss->user_addr = addr;
+ skel->bss->action = USER_MEM_COPY_INSNS;
+ user_mem_trigger();
+
+ return ASSERT_OK(skel->bss->ret, "user_write");
+}
+
+static void test_execute_user_page(struct user_mem *skel)
+{
+ long page_size = sysconf(_SC_PAGESIZE);
+ unsigned long addr = ~0UL;
+
+#if !defined(__x86_64__) && !defined(__aarch64__)
+ test__skip();
+ return;
+#endif
+
+ if (!user_mem_mmap(skel, page_size, PROT_READ | PROT_EXEC, &addr))
+ return;
+ if (!check_user_mem_vma((void *)addr, page_size, "r-xp"))
+ goto out;
+ if (!user_mem_copy_insns(skel, addr))
+ goto out;
+
+ /* Execute user page. */
+ ASSERT_EQ(((int (*)(void))addr)(), 42, "execute_user_page");
+
+out:
+ if (addr != ~0UL)
+ munmap((void *)addr, page_size);
+}
+
+static void test_partial_exec(struct user_mem *skel)
+{
+ long page_size = sysconf(_SC_PAGESIZE);
+ unsigned long addr = ~0UL;
+
+#if !defined(__x86_64__) && !defined(__aarch64__)
+ test__skip();
+ return;
+#endif
+
+ if (!user_mem_mmap(skel, page_size * 3, PROT_READ | PROT_WRITE, &addr))
+ return;
+
+ /* Copy instructions to middle page. */
+ if (!user_mem_copy_insns(skel, addr + page_size))
+ goto out;
+
+ /* Set +x to middle page. */
+ if (!ASSERT_OK(mprotect((void *)(addr + page_size), page_size,
+ PROT_READ | PROT_EXEC), "middle_page_mprotect"))
+ goto out;
+
+ if (!check_user_mem_vma((void *)addr, page_size, "rw-p") ||
+ !check_user_mem_vma((void *)(addr + page_size), page_size, "r-xp") ||
+ !check_user_mem_vma((void *)(addr + 2 * page_size), page_size, "rw-p"))
+ goto out;
+
+ /* Execute middle page. */
+ if (!ASSERT_EQ(((int (*)(void))(addr + page_size))(), 42, "execute_middle_page"))
+ goto out;
+
+out:
+ if (addr != ~0UL)
+ munmap((void *)addr, page_size * 3);
+}
+
void test_user_mem(void)
{
struct user_mem *skel;
@@ -106,6 +177,10 @@ void test_user_mem(void)
if (test__start_subtest("mapping"))
test_mapping(skel);
+ if (test__start_subtest("execute_user_page"))
+ test_execute_user_page(skel);
+ if (test__start_subtest("partial_exec"))
+ test_partial_exec(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 a6b4f922da49..b477132e3883 100644
--- a/tools/testing/selftests/bpf/progs/user_mem.c
+++ b/tools/testing/selftests/bpf/progs/user_mem.c
@@ -10,10 +10,13 @@ char _license[] SEC("license") = "GPL";
extern unsigned long bpf_user_mmap(unsigned long len, unsigned long prot) __ksym;
extern int bpf_user_munmap(unsigned long addr, unsigned long len) __ksym;
+extern int bpf_copy_to_user(void *unsafe_ptr__ign, const void *src__srcptr,
+ u32 src__sz, u64 flags) __ksym;
enum {
USER_MEM_MMAP = 1,
USER_MEM_UNMAP,
+ USER_MEM_COPY_INSNS,
};
int monitored_pid;
@@ -24,6 +27,22 @@ unsigned long user_addr;
unsigned long map_len;
unsigned long map_prot;
+#if defined(__TARGET_ARCH_x86)
+static const unsigned char insns[] = {
+ 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
+ 0xb8, 42, 0, 0, 0, 0xc3, /* mov $42, %eax; ret */
+};
+#elif defined(__TARGET_ARCH_arm64)
+static const unsigned char insns[] = {
+ 0x5f, 0x24, 0x03, 0xd5, /* bti c */
+ 0x40, 0x05, 0x80, 0x52, /* mov w0, #42 */
+ 0xc0, 0x03, 0x5f, 0xd6, /* ret */
+};
+#else
+/* The execute tests are skipped on other architectures. */
+static const unsigned char insns[] = { 0 };
+#endif
+
SEC("uprobe.s//proc/self/exe:user_mem_trigger")
int user_mem_test(struct pt_regs *ctx)
{
@@ -37,6 +56,9 @@ int user_mem_test(struct pt_regs *ctx)
case USER_MEM_UNMAP:
ret = bpf_user_munmap(user_addr, map_len);
break;
+ case USER_MEM_COPY_INSNS:
+ ret = bpf_copy_to_user((void *)user_addr, insns, sizeof(insns), 0);
+ break;
}
return 0;
--
2.54.0
next prev parent reply other threads:[~2026-10-29 21:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 21:25 [PATCH bpf-next 0/8] bpf: Add user memory kfuncs Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 1/8] bpf: Add __srcptr kfunc argument suffix Jiri Olsa
2026-10-29 21:49 ` Andrii Nakryiko
2026-10-29 21:25 ` [PATCH bpf-next 2/8] bpf: Add user memory kfuncs Jiri Olsa
2026-10-29 22:00 ` Andrii Nakryiko
2026-10-29 22:12 ` bot+bpf-ci
2026-10-29 21:25 ` [PATCH bpf-next 3/8] selftests/bpf: Add get_procmap_info helper Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 4/8] selftests/bpf: Test __srcptr kfunc arguments Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 5/8] selftests/bpf: Test user memory mmap kfuncs Jiri Olsa
2026-10-29 21:59 ` bot+bpf-ci
2026-10-29 21:25 ` Jiri Olsa [this message]
2026-10-29 21:25 ` [PATCH bpf-next 7/8] selftests/bpf: Test bpf_copy_to_user kfunc Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 8/8] selftests/bpf: Test user memory kfunc errors Jiri Olsa
2026-10-29 21:59 ` bot+bpf-ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029212551.56964-7-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=memxor@gmail.com \
--cc=nierro92@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.