summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@kernel.org>2026-11-18 09:44:49 +0100
committerAlexei Starovoitov <ast@kernel.org>2026-11-20 02:30:59 +0000
commitca44731a7009da1dbcdf1979438d1985d93331d1 (patch)
tree8175eea5d797f1e3aa63e0a804c3de11061b81ab /include
parent7543d8d314b222452258b30e5c7a40c6f4cbfcda (diff)
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: rtm@csail.mit.edu Suggested-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261118084451.12590-2-jolsa@kernel.org Closes: https://lore.kernel.org/bpf/30318.1794850523@30-11-166.wireless.csail.mit.edu/
Diffstat (limited to 'include')
-rw-r--r--include/linux/uprobes.h5
1 files changed, 5 insertions, 0 deletions
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index d34dbc0fbbfe..43b250bff162 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 */