Skip to contentSkip to conversation

bpf: Limit writable registers in uprobe context

PATCH Applied Jiri Olsa 2 participants 4 messages Nov 18 – Nov 20

Subsystems (12)
Stable branch x86 Kselftest BPF +8 more subsystems
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover bpf: Limit writable registers in uprobe context

Hi,
a uprobe BPF program can currently write fields in struct pt_regs that are loaded directly by the architecture’s return-to-user path. Some of these fields are restricted by ptrace, but the BPF verifier does not apply the same restrictions.

This series limits uprobe context writes to registers that ptrace can set to arbitrary values. It adds architecture-specific writable-register checks for arm64, riscv, and x86-64; other architectures default to allowing no writes. Reads are unchanged.

The second patch adds verifier tests for writable and read-only registers on x86-64, arm64, and riscv.

Patch 1 fixes the issue reported by rtm and is marked for stable consideration. Patch 2 adds the regression tests.

thanks,
jirka

---
Jiri Olsa (2):
bpf: Restrict uprobe ctx writes to the registers ptrace can set selftests/bpf: Test which ctx registers a uprobe prog can write

 arch/arm64/kernel/probes/uprobes.c                            |   6 ++
 arch/riscv/kernel/probes/uprobes.c                            |   6 ++
 arch/x86/kernel/uprobes.c                                     |  15 ++++
 include/linux/uprobes.h                                       |   5 ++
 kernel/events/uprobes.c                                       |   9 +++
 kernel/trace/bpf_trace.c                                      |  10 ++-
 tools/testing/selftests/bpf/prog_tests/verifier.c             |   2 +
 tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c | 196 +++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 248 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
author reviewer
#1 lore raw

PATCH 1/2 bpf: Restrict uprobe ctx writes to the registers ptrace can set

Since commit 7384893d970e ("bpf: Allow uprobe program to change context registers") a uprobe program can write any field of struct pt_regs. Some of those fields are not plain user registers. The exit to user space loads them into the CPU:

  arch    field   exit path                        ptrace
  ------  ------  -------------------------------  ------------------
  riscv   status  csrw CSR_STATUS, a0              not in the regset
  arm64   pstate  msr spsr_el1, x22                valid_user_regs()
  x86-64  cs, ss  (cs & 3) == 0: iretq to ring 0   set_segment_reg()
  x86-64  flags   iretq loads IF and IOPL          set_flags()

On riscv a program that sets sstatus.SPP in pt_regs.status makes ret_from_exception return to user code in supervisor mode. On x86-64 a program that writes a kernel selector to cs makes iretq return to ring 0.
ptrace guards these fields, but the bpf verifier does not.

Add arch_uprobe_regs_writable(). Each arch lists the registers that ptrace can set to any value, and kprobe_prog_is_valid_access() rejects writes to any other offset when the program is loaded:

  arm64:   x0 to x30, sp, pc
  riscv:   epc, ra to t6
  x86-64:  r15 to di, ip, sp

The default is no writes, so arches without a list (and 32-bit x86) lose ctx writes for uprobe programs. Reads are not affected.

flags on x86-64 and pstate on arm64 become read-only, including the condition bits that ptrace would let a debugger change. orig_ax is read-only too.

Kprobe programs that write ctx were already rejected when they were attached. Those that write one of these fields now fail to load.

Fixes 7384893d970e ("bpf: Allow uprobe program to change context registers")
Reported-by [email protected]
---
 arch/arm64/kernel/probes/uprobes.c |  6 ++++++
 arch/riscv/kernel/probes/uprobes.c |  6 ++++++
 arch/x86/kernel/uprobes.c          | 15 +++++++++++++++
 include/linux/uprobes.h            |  5 +++++
 kernel/events/uprobes.c            |  9 +++++++++
 kernel/trace/bpf_trace.c           | 10 +++++++++-
 6 files changed, 50 insertions(+), 1 deletion(-)
arch/arm64/kernel/probes/uprobes.c +6 -0
index 2a9b0bc083a3..1e6ac4785a53 100644
@@ -152,6 +152,12 @@ bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
 		return regs->sp < ret->stack;
 }
 
+bool arch_uprobe_regs_writable(int off, int size)
+{
+	/* From x0 to x30, sp, pc. */
+	return off + size <= offsetof(struct pt_regs, pstate);
+}
+
 unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
 				  struct pt_regs *regs)
arch/riscv/kernel/probes/uprobes.c +6 -0
index eb177d0ce8ab..dbb153463197 100644
@@ -126,6 +126,12 @@ bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
 		return regs->sp < ret->stack;
 }
 
+bool arch_uprobe_regs_writable(int off, int size)
+{
+	/* epc, from ra to t6. */
+	return off + size <= offsetof(struct pt_regs, status);
+}
+
 unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
 				  struct pt_regs *regs)
arch/x86/kernel/uprobes.c +15 -0
index 0f60c0d076b6..fc07bcd0f462 100644
@@ -1888,6 +1888,21 @@ bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
 		return regs->sp <= ret->stack;
 }
 
+bool arch_uprobe_regs_writable(int off, int size)
+{
+	int end = off + size;
+
+	/* The 32-bit pt_regs has segments in front of orig_ax. */
+	if (!IS_ENABLED(CONFIG_X86_64))
+		return false;
+	/* From r15 to di. Not orig_ax, cs, flags, ss. */
+	if (end <= offsetof(struct pt_regs, orig_ax))
+		return true;
+	if (off >= offsetof(struct pt_regs, ip) && end <= offsetofend(struct pt_regs, ip))
+		return true;
+	return off >= offsetof(struct pt_regs, sp) && end <= offsetofend(struct pt_regs, sp);
+}
+
 /*
  * Heuristic-based check if uprobe is installed at the function entry.
  *
include/linux/uprobes.h +5 -0
index d34dbc0fbbfe..43b250bff162 100644
@@ -239,6 +239,7 @@ extern void uprobe_copy_from_page(struct page *page, unsigned long vaddr, void *
 extern void handle_syscall_uprobe(struct pt_regs *regs, unsigned long bp_vaddr);
 extern void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr);
 extern unsigned long arch_uprobe_get_xol_area(void);
+extern bool arch_uprobe_regs_writable(int off, int size);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
@@ -300,5 +301,9 @@ static inline void uprobe_copy_process(struct task_struct *t, u64 flags)
 static inline void uprobe_clear_state(struct mm_struct *mm)
 {
 }
+static inline bool arch_uprobe_regs_writable(int off, int size)
+{
+	return false;
+}
 #endif /* !CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/events/uprobes.c +9 -0
index caf444762a27..c47b1ee266d5 100644
@@ -2703,6 +2703,15 @@ void __weak arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vadd
 {
 }
 
+/*
+ * Can a bpf prog write these bytes of pt_regs? An arch opts in and lists the
+ * registers that ptrace can set to any value.
+ */
+bool __weak arch_uprobe_regs_writable(int off, int size)
+{
+	return false;
+}
+
 /*
  * Run handler and ask thread to singlestep.
  * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
kernel/trace/bpf_trace.c +9 -1
index 2aec56d7f3d3..636c9849e07f 100644
@@ -1397,8 +1397,16 @@ static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type
 	if (off + size > sizeof(struct pt_regs))
 		return false;
 
-	if (type == BPF_WRITE)
+	if (type == BPF_WRITE) {
+		/*
+		 * pt_regs also holds what the kernel restores on the way to
+		 * user space: cs and flags on x86, pstate on arm64, status on
+		 * riscv. A prog can write the registers of the task only.
+		 */
+		if (!arch_uprobe_regs_writable(off, size))
+			return false;
 		prog->aux->kprobe_write_ctx = true;
+	}
 
 	return true;
 }
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH 2/2 selftests/bpf: Test which ctx registers a uprobe prog can write

In reply to Jiri Olsa

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.

---
 .../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
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 6a494c381266..5a31f04677e0 100644
@@ -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); }
tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw
In reply to Jiri Olsa
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 6 lines · On Wed, 18 Nov 2026 09:44:48 +0100 you wrote:

Hi,
a uprobe BPF program can currently write fields in struct pt_regs that are loaded directly by the architecture’s return-to-user path. Some of these fields are restricted by ptrace, but the BPF verifier does not apply the same restrictions.

Quoted 6 lines

This series limits uprobe context writes to registers that ptrace can set to arbitrary values. It adds architecture-specific writable-register checks for arm64, riscv, and x86-64; other architectures default to allowing no writes. Reads are unchanged.

[...]

Here is the summary with links:
- [bpf,1/2] bpf: Restrict uprobe ctx writes to the registers ptrace can set

    https://git.kernel.org/bpf/bpf/c/ca44731a7009
  - [bpf,2/2] selftests/bpf: Test which ctx registers a uprobe prog can write
    https://git.kernel.org/bpf/bpf/c/c21b0f82ca96

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html