]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
uprobes/x86: Harden uretprobe syscall trampoline check
authorJiri Olsa <jolsa@kernel.org>
Wed, 12 Feb 2025 22:04:33 +0000 (23:04 +0100)
committerIngo Molnar <mingo@kernel.org>
Thu, 6 Mar 2025 11:22:45 +0000 (12:22 +0100)
Jann reported a 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:

   https://lore.kernel.org/bpf/202502081235.5A6F352985@keescook/T/#m9d416df341b8fbc11737dacbcd29f0054413cbbf

Though the mmap minimum address restrictions will typically prevent
creating mappings there, let's make sure uretprobe syscall checks
for that.

Fixes: ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
Reported-by: Jann Horn <jannh@google.com>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20250212220433.3624297-1-jolsa@kernel.org
arch/x86/kernel/uprobes.c
include/linux/uprobes.h
kernel/events/uprobes.c

index 5a952c5ea66bc616f8ebff4536f40021df12057e..9194695662b26f793a77957f4838598f0869c395 100644 (file)
@@ -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 (unlikely(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 (unlikely(regs->ip != trampoline_check_ip(tramp)))
                goto sigill;
 
        err = copy_from_user(r11_cx_ax, (void __user *)regs->sp, sizeof(r11_cx_ax));
index a40efdda9052bb2fc7063809a432f28f4814e1fe..2e46b69ff0a6f044ddf7aaa0fe45b92af20d359c 100644 (file)
@@ -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
index 921ad91621362ee6ae501622c3c0c115b4eee35c..70c84b9d7be3b6221ea4b5de4419cda00d500b27 100644 (file)
@@ -2169,8 +2169,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); /* ^^^ */