* a riscv ebpf uprobe can wedge the machine by writing pt_regs.status
@ 2026-11-16 17:35 rtm
2026-11-16 18:37 ` Andrii Nakryiko
2026-11-16 18:47 ` Alexei Starovoitov
0 siblings, 2 replies; 4+ messages in thread
From: rtm @ 2026-11-16 17:35 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, John Fastabend; +Cc: bpf
On riscv, kprobe_prog_is_valid_access() allows a "uprobe" ebpf program
to write ctx at offset 256, which is pt_regs.status. When returning to
user space, ret_from_exception copies whatever value the ebpf program
provided to the CPU's sstatus. One way this can cause trouble is if
the ebpf program sets sstatus.SPP to 1, indicating that the trap
should return to user space in supervisor mode rather than user mode.
An ebpf program can do this if it starts with
7a 01 00 01 00 01 00 00
i.e. {code = 0x7a, dst_reg = 0x1, src_reg = 0x0, off = 0x100, imm = 0x100}
Robert Morris
rtm@mit.edu
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: a riscv ebpf uprobe can wedge the machine by writing pt_regs.status
2026-11-16 17:35 a riscv ebpf uprobe can wedge the machine by writing pt_regs.status rtm
@ 2026-11-16 18:37 ` Andrii Nakryiko
2026-11-16 18:47 ` Alexei Starovoitov
1 sibling, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2026-11-16 18:37 UTC (permalink / raw)
To: rtm, Jiri Olsa; +Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend, bpf
On Mon, Nov 16, 2026 at 9:35 AM <rtm@csail.mit.edu> wrote:
>
> On riscv, kprobe_prog_is_valid_access() allows a "uprobe" ebpf program
> to write ctx at offset 256, which is pt_regs.status. When returning to
> user space, ret_from_exception copies whatever value the ebpf program
> provided to the CPU's sstatus. One way this can cause trouble is if
> the ebpf program sets sstatus.SPP to 1, indicating that the trap
> should return to user space in supervisor mode rather than user mode.
>
We do allow BPF program to overwrite pt_regs and cause user space
program to jump to a different location (or even crash if we modify
registers incorrectly), assuming CAP_PERFMON + CAP_BPF. But yeah, we
shouldn't let it just wedge the host..
cc'ing Jiri, should we add some arch-specific range checks for uprobes
and limit it to general-purpose registers and IP?..
> An ebpf program can do this if it starts with
>
> 7a 01 00 01 00 01 00 00
>
> i.e. {code = 0x7a, dst_reg = 0x1, src_reg = 0x0, off = 0x100, imm = 0x100}
>
> Robert Morris
> rtm@mit.edu
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: a riscv ebpf uprobe can wedge the machine by writing pt_regs.status
2026-11-16 17:35 a riscv ebpf uprobe can wedge the machine by writing pt_regs.status rtm
2026-11-16 18:37 ` Andrii Nakryiko
@ 2026-11-16 18:47 ` Alexei Starovoitov
2026-11-16 21:44 ` Jiri Olsa
1 sibling, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2026-11-16 18:47 UTC (permalink / raw)
To: rtm, Daniel Borkmann, John Fastabend; +Cc: bpf
On Mon, Nov 16, 2026 at 09:35 AM rtm@csail.mit.edu <rtm@csail.mit.edu> wrote:
> On riscv, kprobe_prog_is_valid_access() allows a "uprobe" ebpf program
> to write ctx at offset 256, which is pt_regs.status. When returning to
> user space, ret_from_exception copies whatever value the ebpf program
> provided to the CPU's sstatus. One way this can cause trouble is if
> the ebpf program sets sstatus.SPP to 1, indicating that the trap
> should return to user space in supervisor mode rather than user mode.
Yes. It's a real bug, and it's not only riscv.
The exit path trusts some of the fields. ptrace guards the same fields:
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()
A uprobe prog should write only the registers that ptrace sets to any
value. The verifier can reject the other offsets at load time.
Each arch lists its registers. An arch without a list gets no writes.
flags and pstate become read-only, the condition bits too.
Jiri,
pls commandeer the following:
diff --git a/arch/arm64/kernel/probes/uprobes.c b/arch/arm64/kernel/probes/uprobes.c
index 2a9b0bc..1e6ac47 100644
--- a/arch/arm64/kernel/probes/uprobes.c
+++ b/arch/arm64/kernel/probes/uprobes.c
@@ -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)
diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
index eb177d0..dbb1534 100644
--- a/arch/riscv/kernel/probes/uprobes.c
+++ b/arch/riscv/kernel/probes/uprobes.c
@@ -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)
diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index 0f60c0d..fc07bcd 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -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.
*
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index d34dbc0..43b250b 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -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 */
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index caf4447..c47b1ee 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -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.
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index b4ff9d4..4f1be6c 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1398,8 +1398,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;
}
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: a riscv ebpf uprobe can wedge the machine by writing pt_regs.status
2026-11-16 18:47 ` Alexei Starovoitov
@ 2026-11-16 21:44 ` Jiri Olsa
0 siblings, 0 replies; 4+ messages in thread
From: Jiri Olsa @ 2026-11-16 21:44 UTC (permalink / raw)
To: Alexei Starovoitov; +Cc: rtm, Daniel Borkmann, John Fastabend, bpf
On Mon, Nov 16, 2026 at 06:47:41PM +0000, Alexei Starovoitov wrote:
> On Mon, Nov 16, 2026 at 09:35 AM rtm@csail.mit.edu <rtm@csail.mit.edu> wrote:
> > On riscv, kprobe_prog_is_valid_access() allows a "uprobe" ebpf program
> > to write ctx at offset 256, which is pt_regs.status. When returning to
> > user space, ret_from_exception copies whatever value the ebpf program
> > provided to the CPU's sstatus. One way this can cause trouble is if
> > the ebpf program sets sstatus.SPP to 1, indicating that the trap
> > should return to user space in supervisor mode rather than user mode.
>
> Yes. It's a real bug, and it's not only riscv.
> The exit path trusts some of the fields. ptrace guards the same fields:
>
> 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()
yes, we need to block it
>
> A uprobe prog should write only the registers that ptrace sets to any
> value. The verifier can reject the other offsets at load time.
> Each arch lists its registers. An arch without a list gets no writes.
> flags and pstate become read-only, the condition bits too.
I was wondering about the condition bits, but it'd be cumbersome to
block specific values and we can always make jump anyway
will send the change
thanks,
jirka
>
> Jiri,
> pls commandeer the following:
>
> diff --git a/arch/arm64/kernel/probes/uprobes.c b/arch/arm64/kernel/probes/uprobes.c
> index 2a9b0bc..1e6ac47 100644
> --- a/arch/arm64/kernel/probes/uprobes.c
> +++ b/arch/arm64/kernel/probes/uprobes.c
> @@ -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)
> diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
> index eb177d0..dbb1534 100644
> --- a/arch/riscv/kernel/probes/uprobes.c
> +++ b/arch/riscv/kernel/probes/uprobes.c
> @@ -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)
> diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
> index 0f60c0d..fc07bcd 100644
> --- a/arch/x86/kernel/uprobes.c
> +++ b/arch/x86/kernel/uprobes.c
> @@ -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.
> *
> diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
> index d34dbc0..43b250b 100644
> --- a/include/linux/uprobes.h
> +++ b/include/linux/uprobes.h
> @@ -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 */
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index caf4447..c47b1ee 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -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.
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index b4ff9d4..4f1be6c 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1398,8 +1398,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;
> }
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-16 21:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 17:35 a riscv ebpf uprobe can wedge the machine by writing pt_regs.status rtm
2026-11-16 18:37 ` Andrii Nakryiko
2026-11-16 18:47 ` Alexei Starovoitov
2026-11-16 21:44 ` Jiri Olsa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox