uprobes: Harden uretprobe syscall trampoline check
Subsystems (7)
Jiri Olsa
reviewerPATCH v2 1/1 uprobes: Harden uretprobe syscall trampoline check
Jann reported [1] possible issue when trampoline_check_ip returns address near the bottom of the address space that is allowed to call into the syscall if uretprobes are not set up.
Though the mmap minimum address restrictions will typically prevent creating mappings there, let's make sure uretprobe syscall checks for that.
---
v2 changes:
- adding UPROBE_NO_TRAMPOLINE_VADDR macro (Andrii)
- rebased on top of perf/core
arch/x86/kernel/uprobes.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
arch/x86/kernel/uprobes.c +11 -5
@@ -357,19 +357,25 @@ void *arch_uprobe_trampoline(unsigned long *psize)return &insn; } -static unsigned long trampoline_check_ip (void ) +static unsigned long trampoline_check_ip (unsigned long tramp) { -unsigned long tramp =uprobe_get_trampoline_vaddr (); -return tramp + (uretprobe_syscall_check - uretprobe_trampoline_entry); } +#define UPROBE_NO_TRAMPOLINE_VADDR ((unsigned long)-1) +SYSCALL_DEFINE0 (uretprobe) {struct pt_regs *regs =task_pt_regs (current); -unsigned long err, ip, sp, r11_cx_ax[3 ]; +unsigned long err, ip, sp, r11_cx_ax[3 ], tramp; + +/* If there's no trampoline, we are called from wrong place. */ + tramp =uprobe_get_trampoline_vaddr (); +if (tramp ==UPROBE_NO_TRAMPOLINE_VADDR ) + goto sigill; -if (regs->ip !=trampoline_check_ip ()) +/* Make sure the ip matches the only allowed sys_uretprobe caller. */ +if (regs->ip !=trampoline_check_ip (tramp)) goto sigill; err =copy_from_user (r11_cx_ax, (void __user *)regs->sp,sizeof (r11_cx_ax));
Signature · 1 line
2.48.1
Quoted 50 lines · Jiri Olsa
Jann reported [1] possible issue when
trampoline_check_ipreturns address near the bottom of the address space that is allowed to call into the syscall if uretprobes are not set up.Though the mmap minimum address restrictions will typically prevent creating mappings there, let's make sure uretprobe syscall checks for that.
Cc Kees Cook <[email protected]>Cc Eyal Birger <[email protected]>Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")Reported-by Jann Horn <[email protected]>Reviewed-by Oleg Nesterov <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
v2 changes:
- addingUPROBE_NO_TRAMPOLINE_VADDRmacro (Andrii)
- rebased on top of perf/corearch/x86/kernel/uprobes.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)arch/x86/kernel/uprobes.c +5
-4@@ -357,19 +357,25 @@ void *arch_uprobe_trampoline(unsigned long *psize)return &insn; } -static unsigned long trampoline_check_ip (void ) +static unsigned long trampoline_check_ip (unsigned long tramp) { -unsigned long tramp =uprobe_get_trampoline_vaddr (); -return tramp + (uretprobe_syscall_check - uretprobe_trampoline_entry); } +#define UPROBE_NO_TRAMPOLINE_VADDR ((unsigned long)-1) +SYSCALL_DEFINE0 (uretprobe) {struct pt_regs *regs =task_pt_regs (current); -unsigned long err, ip, sp, r11_cx_ax[3 ]; +unsigned long err, ip, sp, r11_cx_ax[3 ], tramp; +
arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 380
Quoted 10 lines
arch/x86/kernel/uprobes.c +6
-1+/* If there's no trampoline, we are called from wrong place. */ + tramp =uprobe_get_trampoline_vaddr (); +if (tramp ==UPROBE_NO_TRAMPOLINE_VADDR ) + goto sigill; -if (regs->ip !=trampoline_check_ip ()) +/* Make sure the ip matches the only allowed sys_uretprobe caller. */ +if (regs->ip !=trampoline_check_ip (tramp)) goto sigill;
LGTM. I don't know if that would make any difference, but I'd sprinkle unlikely() around these two conditions to make sure they don't interfere with instruction flow much
Quoted 4 lines
arch/x86/kernel/uprobes.c +0
-1err =copy_from_user (r11_cx_ax, (void __user *)regs->sp,sizeof (r11_cx_ax)); --2.48.1
Quoted 44 lines · Andrii Nakryiko
Quoted 42 lines · Jiri Olsa
Jann reported [1] possible issue when
trampoline_check_ipreturns address near the bottom of the address space that is allowed to call into the syscall if uretprobes are not set up.Though the mmap minimum address restrictions will typically prevent creating mappings there, let's make sure uretprobe syscall checks for that.
Cc Kees Cook <[email protected]>Cc Eyal Birger <[email protected]>Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")Reported-by Jann Horn <[email protected]>Reviewed-by Oleg Nesterov <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
v2 changes:
- addingUPROBE_NO_TRAMPOLINE_VADDRmacro (Andrii)
- rebased on top of perf/corearch/x86/kernel/uprobes.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)arch/x86/kernel/uprobes.c +1
-3@@ -357,19 +357,25 @@ void *arch_uprobe_trampoline(unsigned long *psize)return &insn; } -static unsigned long trampoline_check_ip (void ) +static unsigned long trampoline_check_ip (unsigned long tramp) { -unsigned long tramp =uprobe_get_trampoline_vaddr (); -return tramp + (uretprobe_syscall_check - uretprobe_trampoline_entry); }
Quoted 1 line
arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 365
Quoted 1 line
arch/x86/kernel/uprobes.c +1
-0+#define UPROBE_NO_TRAMPOLINE_VADDR ((unsigned long)-1)
If you respin anyway maybe use ~0UL instead?
In the above and inuprobe_get_trampoline_vaddr(),
since
unsigned long trampoline_vaddr = -1;
looks odd too.
Oleg Nesterov
maintainerQuoted 9 lines · Alexei Starovoitov
Quoted 2 lines
Quoted 2 lines
+#define
UPROBE_NO_TRAMPOLINE_VADDR((unsigned long)-1)If you respin anyway maybe use ~0UL instead?
In the above and inuprobe_get_trampoline_vaddr(),
sinceunsigned long
trampoline_vaddr= -1;
... or -1ul in both cases.
I agree, UPROBE_NO_TRAMPOLINE_VADDR has a single user, looks a bit strange...
Oleg.
Masami Hiramatsu (Google)
maintainerQuoted 9 lines · On Tue, 11 Feb 2025 17:59:41 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 8 lines · Alexei Starovoitov
Quoted 2 lines
Quoted 2 lines
+#define
UPROBE_NO_TRAMPOLINE_VADDR((unsigned long)-1)If you respin anyway maybe use ~0UL instead?
In the above and inuprobe_get_trampoline_vaddr(),
since
Quoted 6 lines
Quoted 1 line
unsigned long
trampoline_vaddr= -1;... or -1ul in both cases.
I agree,
UPROBE_NO_TRAMPOLINE_VADDRhas a single user, looks a bit strange...
I think both this function and uprobe_get_trampoline_vaddr() should use the same macro as a token.
(and ~0UL is a bit more comfortable for me too :) )
----
unsigned long uprobe_get_trampoline_vaddr(void)
{
struct xol_area *area;
unsigned long trampoline_vaddr = -1;----
Thank you,
Quoted 4 lines
Oleg.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 23 lines · Masami Hiramatsu
Quoted 9 lines · On Tue, 11 Feb 2025 17:59:41 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 8 lines · Alexei Starovoitov
Quoted 2 lines
Quoted 2 lines
+#define
UPROBE_NO_TRAMPOLINE_VADDR((unsigned long)-1)If you respin anyway maybe use ~0UL instead?
In the above and inuprobe_get_trampoline_vaddr(),
sinceQuoted 6 lines
Quoted 1 line
unsigned long
trampoline_vaddr= -1;... or -1ul in both cases.
I agree,
UPROBE_NO_TRAMPOLINE_VADDRhas a single user, looks a bit strange...I think both this function and
uprobe_get_trampoline_vaddr()should use the same macro as a token.
(and ~0UL is a bit more comfortable for me too :) )
Quoted 6 lines
----
unsigned longuprobe_get_trampoline_vaddr(void){ struct xol_area *area; unsigned long trampoline_vaddr = -1;----
sounds good, I'll send new version with change below if there are no objections
thanks,
jirka
---
arch/x86/kernel/uprobes.c +9 -5
@@ -357,19 +357,23 @@ void *arch_uprobe_trampoline(unsigned long *psize)return &insn; } -static unsigned long trampoline_check_ip (void ) +static unsigned long trampoline_check_ip (unsigned long tramp) { -unsigned long tramp =uprobe_get_trampoline_vaddr (); -return tramp + (uretprobe_syscall_check - uretprobe_trampoline_entry); }SYSCALL_DEFINE0 (uretprobe) {struct pt_regs *regs =task_pt_regs (current); -unsigned long err, ip, sp, r11_cx_ax[3 ]; +unsigned long err, ip, sp, r11_cx_ax[3 ], tramp; + +/* If there's no trampoline, we are called from wrong place. */ + tramp =uprobe_get_trampoline_vaddr (); +if (tramp ==UPROBE_NO_TRAMPOLINE_VADDR ) + goto sigill; -if (regs->ip !=trampoline_check_ip ()) +/* Make sure the ip matches the only allowed sys_uretprobe caller. */ +if (regs->ip !=trampoline_check_ip (tramp)) goto sigill; err =copy_from_user (r11_cx_ax, (void __user *)regs->sp,sizeof (r11_cx_ax));
include/linux/uprobes.h +2 -0
@@ -39,6 +39,8 @@ struct page;#define MAX_URETPROBE_DEPTH 64 +#define UPROBE_NO_TRAMPOLINE_VADDR (~0UL) +struct uprobe_consumer {/* * handler() can return UPROBE_HANDLER_REMOVE to signal the need to
kernel/events/uprobes.c +1 -1
@@ -2160,8 +2160,8 @@ void uprobe_copy_process(struct task_struct *t, unsigned long flags)*/ unsigned long uprobe_get_trampoline_vaddr (void ) { +unsigned long trampoline_vaddr =UPROBE_NO_TRAMPOLINE_VADDR ;struct xol_area *area; -unsigned long trampoline_vaddr =-1 ;/* Pairs with xol_add_vma() smp_store_release() */ area =READ_ONCE (current->mm->uprobes_state.xol_area);/* ^^^ */