From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380938; b=ia8YGmqZDlUWDubYgIBMzYbxFeuleQUATg7iybM1dudgqDmiiEE0oDcju8pfYDJRfxtG IMkjS7DSvkGfv5a2TBD5+ahwHlcJjDhA6r9kHQaratM3ntvuwMjSRCUCHaokS/3WEwK+l UD1aBC7ocveIIy2TS1tuWVoww04ZvhagiB0BeEtXtGJfPZQIajOXRDndqXDM8eFwhRxCh VzDaa6B7j2J7c3o9k7eipHgeNVTVsfYUGZeje/HebIwOvXpejb19x0Kg3yJCY5lD8H50r OcG/7IMiu/5ui39Q8W716Hv0sBmrR0s8CqVy8coKXO0zARA4rG1EZi0Wi6SNo6T+DyQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380938; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=x9bNbdAVoV1IEDeJaJlx0M3G43Q4QiZkUbEgHMH/qo8=; b=hqhuZjOJJCNza2MEeIcvX1EB6UF6bG/Dm7/TGXJ70KG2Gi2Olr4mdLTv92U/MCRmR+u9 0GEml7m0wqE/Ir8Dl/0t9Pbt7wu/XRfIXFDuJyRI6aB6VDJi7qUg58K41zh8qN0Tvg5OX a5Z2v0lJz17qmveLMYk+I1OcNI3Y4k5Zkq1PBIAAVpUY+5YOrLuJ0EQaEBJ8UUaBBC5Ms PZttXPiJP3USdyki9KHPi0flLYQ/SxvQJTSQ1mzkYHMONka9DQucwSKF/pQvEHgiW0uL2 PArHYL+mWUOQ8vvW4TWEBbVjhMwnFzAoqmQKIBD0O+V0uA6bU15bkxHiWHn7pjO6YBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=FuoVzIxi; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8738606A322; Fri, 4 Dec 2026 10:42:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=FuoVzIxi Received: by smtp.kernel.org (Postfix) with ESMTPSA id 223DA1F000C2; Fri, 4 Dec 2026 10:42:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380938; bh=x9bNbdAVoV1IEDeJaJlx0M3G43Q4QiZkUbEgHMH/qo8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FuoVzIxiYq1vLuXEbrZPUDfg4JLPmAsI6/VaoQZoVZA/pnXAfQE7jvn9DLYAaAsPo ZWktLWeZfZvCKH5PmgFJJSo0B3M6S4Q/jzDM87BHCfbMG5m8wbkTxoN0sWqcDL4MaA JoBDTj/tJxUfPjAn/I8azX99tb+TVDEK54dDGTAI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, rtm@csail.mit.edu, Alexei Starovoitov , Jiri Olsa Subject: [PATCH 7.3 448/742] bpf: Restrict uprobe ctx writes to the registers ptrace can set Date: Fri, 4 Dec 2026 19:05:12 +0900 Message-ID: <20261204100647.051770422@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jiri Olsa commit ca44731a7009da1dbcdf1979438d1985d93331d1 upstream. 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 Signed-off-by: Jiri Olsa Signed-off-by: Alexei Starovoitov 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/ Signed-off-by: Greg Kroah-Hartman --- 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(-) --- a/arch/arm64/kernel/probes/uprobes.c +++ b/arch/arm64/kernel/probes/uprobes.c @@ -152,6 +152,12 @@ bool arch_uretprobe_is_alive(struct retu 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) --- a/arch/riscv/kernel/probes/uprobes.c +++ b/arch/riscv/kernel/probes/uprobes.c @@ -126,6 +126,12 @@ bool arch_uretprobe_is_alive(struct retu 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) --- a/arch/x86/kernel/uprobes.c +++ b/arch/x86/kernel/uprobes.c @@ -1888,6 +1888,21 @@ bool arch_uretprobe_is_alive(struct retu 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. * --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -239,6 +239,7 @@ extern void uprobe_copy_from_page(struct 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(s 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 */ --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -2704,6 +2704,15 @@ void __weak arch_uprobe_optimize(struct } /* + * 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. */ --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1390,8 +1390,16 @@ static bool kprobe_prog_is_valid_access( 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; }