Skip to contentSkip to conversation

uprobes: Harden uretprobe syscall trampoline check

PATCH Superseded Jiri Olsa 5 participants 5 messages Feb 9, 2025 – Feb 11, 2025

Subsystems (6)
Stable branch x86 BPF Tracing +2 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 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.

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

---
 arch/x86/kernel/uprobes.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
arch/x86/kernel/uprobes.c +9 -5
index 5a952c5ea66b..109d6641a1b3 100644
@@ -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 == -1)
+		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
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Jiri Olsa

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

---
 arch/x86/kernel/uprobes.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
#2 lore raw
In reply to Jiri Olsa
Quoted 34 lines · Jiri Olsa

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.

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

---
 arch/x86/kernel/uprobes.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
arch/x86/kernel/uprobes.c +1 -3
index 5a952c5ea66b..109d6641a1b3 100644
@@ -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);
 }

arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 372

Quoted 10 lines
arch/x86/kernel/uprobes.c +5 -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 == -1)

slight nit: mixing -1 and unsigned long looks sloppy. Maybe let's add something like

#define UPROBE_NO_TRAMPOLINE_VADDR ((unsigned long)-1)

and return that from uprobe_get_trampoline_vaddr()?

Quoted 11 lines
arch/x86/kernel/uprobes.c +3 -2
+               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));
--

2.48.1

Reviewed-by on v1 1/1
#4 lore raw
In reply to Jiri Olsa
Quoted 8 lines · Jiri Olsa

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.

Quoted 6 lines

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

Thanks for this!

Signature · 1 line
Kees Cook
#3 lore raw
In reply to Andrii Nakryiko
Quoted 49 lines · Andrii Nakryiko
Quoted 34 lines · Jiri Olsa

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.

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

---
 arch/x86/kernel/uprobes.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
arch/x86/kernel/uprobes.c +1 -3
index 5a952c5ea66b..109d6641a1b3 100644
@@ -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);
 }

arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 372

Quoted 10 lines
arch/x86/kernel/uprobes.c +5 -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 == -1)

slight nit: mixing -1 and unsigned long looks sloppy. Maybe let's add something like

Quoted 3 lines

#define UPROBE_NO_TRAMPOLINE_VADDR ((unsigned long)-1)

and return that from uprobe_get_trampoline_vaddr()?

ok, will add that

thanks,
jirka

Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/uprobes.c +3 -2
+               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));
--

2.48.1