From: Jiri Olsa <olsajiri@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: rtm@csail.mit.edu, Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
bpf@vger.kernel.org
Subject: Re: a riscv ebpf uprobe can wedge the machine by writing pt_regs.status
Date: Mon, 16 Nov 2026 22:44:58 +0100 [thread overview]
Message-ID: <avt5WnmsPkYkeZrz@krava> (raw)
In-Reply-To: <DMWUY4M0QXMH.33XAPR76ZXKRT@gmail.com>
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;
> }
>
prev parent reply other threads:[~2026-11-16 21:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 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=avt5WnmsPkYkeZrz@krava \
--to=olsajiri@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=rtm@csail.mit.edu \
/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