From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Oleg Nesterov <oleg@redhat.com>
Cc: bpf@vger.kernel.org, x86@kernel.org,
linux-riscv@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
linux-trace-kernel@vger.kernel.org,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Will Deacon <will@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Peter Zijlstra <peterz@infradead.org>,
rtm@csail.mit.edu
Subject: [PATCH bpf 2/2] selftests/bpf: Test which ctx registers a uprobe prog can write
Date: Wed, 18 Nov 2026 09:44:50 +0100 [thread overview]
Message-ID: <20261118084451.12590-3-jolsa@kernel.org> (raw)
In-Reply-To: <20261118084451.12590-1-jolsa@kernel.org>
A uprobe program can write only the pt_regs fields that ptrace can set
to any value. Add verifier tests for x86-64, arm64 and riscv:
- writes to the general registers, the stack pointer and the
instruction pointer load
- writes to cs, ss, flags and orig_ax on x86-64, to pstate and
orig_x0 on arm64, and to status, badaddr and cause on riscv fail
with "invalid bpf_context access"
- a 4-byte write to the low half of a read-only field fails too
- reading a read-only field still works
- on x86-64, a kprobe program that writes ctx->flags fails to load
Other arches get one test that just loads.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
.../selftests/bpf/prog_tests/verifier.c | 2 +
.../bpf/progs/verifier_uprobe_write_ctx.c | 196 ++++++++++++++++++
2 files changed, 198 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 6a494c381266..5a31f04677e0 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -131,6 +131,7 @@
#include "verifier_unpriv.skel.h"
#include "verifier_unwind.skel.h"
#include "verifier_unpriv_perf.skel.h"
+#include "verifier_uprobe_write_ctx.skel.h"
#include "verifier_value_adj_spill.skel.h"
#include "verifier_value.skel.h"
#include "verifier_value_illegal_alu.skel.h"
@@ -321,6 +322,7 @@ void test_verifier_uninit(void) { RUN(verifier_uninit); }
void test_verifier_unpriv(void) { RUN(verifier_unpriv); }
void test_verifier_unwind(void) { RUN(verifier_unwind); }
void test_verifier_unpriv_perf(void) { RUN(verifier_unpriv_perf); }
+void test_verifier_uprobe_write_ctx(void) { RUN(verifier_uprobe_write_ctx); }
void test_verifier_value_adj_spill(void) { RUN(verifier_value_adj_spill); }
void test_verifier_value(void) { RUN(verifier_value); }
void test_verifier_value_illegal_alu(void) { RUN(verifier_value_illegal_alu); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c b/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
new file mode 100644
index 000000000000..f5ed453b1798
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+/*
+ * A uprobe prog can write only the registers that ptrace can set to any
+ * value. The fields the kernel restores on the way to user space (segments,
+ * flags, privilege state) are read-only.
+ */
+
+#define WRITE_CTX(name, field, sz) \
+__naked void name(void) \
+{ \
+ asm volatile (" \
+ r2 = 0; \
+ *(" #sz " *)(r1 + %[off]) = r2; \
+ r0 = 0; \
+ exit; \
+" : \
+ : __imm_const(off, offsetof(struct pt_regs, field)) \
+ : __clobber_all); \
+}
+
+#define READ_CTX(name, field) \
+__naked void name(void) \
+{ \
+ asm volatile (" \
+ r0 = *(u64 *)(r1 + %[off]); \
+ r0 = 0; \
+ exit; \
+" : \
+ : __imm_const(off, offsetof(struct pt_regs, field)) \
+ : __clobber_all); \
+}
+
+#if defined(__TARGET_ARCH_x86)
+
+SEC("uprobe")
+__description("uprobe: write ctx->ax")
+__success
+WRITE_CTX(write_ax, ax, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->r15")
+__success
+WRITE_CTX(write_r15, r15, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->di")
+__success
+WRITE_CTX(write_di, di, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->ip")
+__success
+WRITE_CTX(write_ip, ip, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->sp")
+__success
+WRITE_CTX(write_sp, sp, u64)
+
+SEC("uprobe")
+__description("uprobe: write lower half of ctx->ax")
+__success
+WRITE_CTX(write_ax_u32, ax, u32)
+
+SEC("uprobe")
+__description("uprobe: write ctx->orig_ax")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_orig_ax, orig_ax, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->cs")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_cs, cs, u64)
+
+SEC("uprobe")
+__description("uprobe: write lower half of ctx->cs")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_cs_u32, cs, u32)
+
+SEC("uprobe")
+__description("uprobe: write ctx->flags")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_flags, flags, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->ss")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_ss, ss, u64)
+
+SEC("uprobe")
+__description("uprobe: read ctx->flags")
+__success
+READ_CTX(read_flags, flags)
+
+SEC("kprobe")
+__description("kprobe: write ctx->flags")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(kprobe_write_flags, flags, u64)
+
+#elif defined(__TARGET_ARCH_arm64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->regs[0]")
+__success
+WRITE_CTX(write_x0, regs[0], u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->regs[30]")
+__success
+WRITE_CTX(write_x30, regs[30], u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->sp")
+__success
+WRITE_CTX(write_sp, sp, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->pc")
+__success
+WRITE_CTX(write_pc, pc, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->pstate")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_pstate, pstate, u64)
+
+SEC("uprobe")
+__description("uprobe: write lower half of ctx->pstate")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_pstate_u32, pstate, u32)
+
+SEC("uprobe")
+__description("uprobe: write ctx->orig_x0")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_orig_x0, orig_x0, u64)
+
+SEC("uprobe")
+__description("uprobe: read ctx->pstate")
+__success
+READ_CTX(read_pstate, pstate)
+
+#elif defined(__TARGET_ARCH_riscv)
+
+SEC("uprobe")
+__description("uprobe: write ctx->epc")
+__success
+WRITE_CTX(write_epc, epc, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->a0")
+__success
+WRITE_CTX(write_a0, a0, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->t6")
+__success
+WRITE_CTX(write_t6, t6, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->status")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_status, status, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->badaddr")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_badaddr, badaddr, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->cause")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_cause, cause, u64)
+
+SEC("uprobe")
+__description("uprobe: read ctx->status")
+__success
+READ_CTX(read_status, status)
+
+#else
+
+SEC("uprobe")
+__description("uprobe: ctx writes not checked on this arch")
+__success
+__naked void unsupported(void)
+{
+ asm volatile ("r0 = 0; exit;" ::: __clobber_all);
+}
+
+#endif
+
+char _license[] SEC("license") = "GPL";
--
2.43.0
prev parent reply other threads:[~2026-11-18 8:45 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 8:44 [PATCH bpf 0/2] bpf: Limit writable registers in uprobe context Jiri Olsa
2026-11-18 8:44 ` [PATCH bpf 1/2] bpf: Restrict uprobe ctx writes to the registers ptrace can set Jiri Olsa
2026-11-18 8:44 ` Jiri Olsa [this message]
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=20261118084451.12590-3-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=linux-arm-kernel@lists.infradead.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mhiramat@kernel.org \
--cc=oleg@redhat.com \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=rtm@csail.mit.edu \
--cc=song@kernel.org \
--cc=will@kernel.org \
--cc=x86@kernel.org \
--cc=yonghong.song@linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox