uprobes: Harden uretprobe syscall trampoline check
Subsystems (7)
Jiri Olsa
reviewerPATCH v3 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.
---
v3 changes:
- used ~0UL instead of -1 [Alexei]
- used UPROBE_NO_TRAMPOLINE_VADDR in uprobe_get_trampoline_vaddr [Masami]
- added unlikely [Andrii]
- I kept the review/ack tags, because I think the change is basically the same, please scream otherwise
arch/x86/kernel/uprobes.c | 14 +++++++++----- include/linux/uprobes.h | 2 ++ kernel/events/uprobes.c | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-)
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 (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));
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
@@ -2156,8 +2156,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);/* ^^^ */
Signature · 1 line
2.48.1
Quoted 9 lines · On Wed, 12 Feb 2025 23:04:33 +0100 Jiri Olsa <[email protected]> wrote:
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.
Quoted 9 lines
Cc Kees Cook <[email protected]>Cc Eyal Birger <[email protected]>Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")Acked-by Andrii Nakryiko <[email protected]>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]>
Looks good to me.
Thank you,
Quoted 76 lines
---
v3 changes:
- used ~0UL instead of -1 [Alexei]
- usedUPROBE_NO_TRAMPOLINE_VADDRinuprobe_get_trampoline_vaddr[Masami]
- added unlikely [Andrii]
- I kept the review/ack tags, because I think the change is basically the same, please scream otherwisearch/x86/kernel/uprobes.c | 14 +++++++++----- include/linux/uprobes.h | 2 ++ kernel/events/uprobes.c | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-)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 (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));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@@ -2156,8 +2156,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);/* ^^^ */ Signature · 1 line
2.48.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 377
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (is_a_real_uretprobe())
skip seccomp;
where is_a_real_uretprobe() is only true if the nr and arch match uretprobe *and* the address is right.
--Andy
(sorry for the HTML spam)
Quoted 18 lines · Andy Lutomirski
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 377
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;
Quoted 8 lines
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (
is_a_real_uretprobe())skip seccomp;where
is_a_real_uretprobe()is only true if the nr and arch match uretprobe *and* the address is right.
Why would it make sense to rely on CONFIG_SECCOMP for this check? seems this check should be done regardless of seccomp.
Or maybe I missed something in the suggestion.
Eyal.
Quoted 3 lines
--Andy
Quoted 17 lines · Andy Lutomirski
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
arch/x86/kernel/uprobes.c void *arch_uprobe_trampoline(unsigned long *psize) · line 377
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;
Quoted 5 lines
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (
is_a_real_uretprobe())skip seccomp;
IIUC you want to move the address check earlier to the seccomp path.. with the benefit that we would kill not allowed caller sooner?
jirka
Quoted 5 lines
where
is_a_real_uretprobe()is only true if the nr and arch match uretprobe *and* the address is right.--Andy
Quoted 19 lines · Jiri Olsa
Quoted 17 lines · Andy Lutomirski
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;
Quoted 8 lines
Quoted 5 lines
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (
is_a_real_uretprobe())skip seccomp;IIUC you want to move the address check earlier to the seccomp path.. with the benefit that we would kill not allowed caller sooner?
The benefit would be that seccomp users that want to do something other than killing a process (returning an error code, getting notified, etc) could retain that functionality without the new automatic hole being poked for uretprobe() in cases where uprobes aren't in use or where the calling address doesn't match the uprobe trampoline. IOW it would reduce the scope to which we're making seccomp behave unexpectedly.
Quoted 9 lines
jirka
Quoted 5 lines
where
is_a_real_uretprobe()is only true if the nr and arch match uretprobe *and* the address is right.--Andy
Quoted 29 lines · Andy Lutomirski
Quoted 19 lines · Jiri Olsa
Quoted 17 lines · Andy Lutomirski
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;Quoted 8 lines
Quoted 5 lines
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (
is_a_real_uretprobe())skip seccomp;IIUC you want to move the address check earlier to the seccomp path.. with the benefit that we would kill not allowed caller sooner?
Quoted 7 lines
The benefit would be that seccomp users that want to do something other than killing a process (returning an error code, getting notified, etc) could retain that functionality without the new automatic hole being poked for
uretprobe()in cases where uprobes aren't in use or where the calling address doesn't match the uprobe trampoline. IOW it would reduce the scope to which we're making seccomp behave unexpectedly.
Kees, any thoughts about this approach?
thanks,
jirka
Quoted 10 lines
Quoted 10 lines
jirka
Quoted 5 lines
where
is_a_real_uretprobe()is only true if the nr and arch match uretprobe *and* the address is right.--Andy
Quoted 38 lines · Jiri Olsa
Quoted 29 lines · Andy Lutomirski
Quoted 19 lines · Jiri Olsa
Quoted 17 lines · Andy Lutomirski
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;Quoted 8 lines
Quoted 5 lines
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (
is_a_real_uretprobe())skip seccomp;IIUC you want to move the address check earlier to the seccomp path.. with the benefit that we would kill not allowed caller sooner?
Quoted 7 lines
The benefit would be that seccomp users that want to do something other than killing a process (returning an error code, getting notified, etc) could retain that functionality without the new automatic hole being poked for
uretprobe()in cases where uprobes aren't in use or where the calling address doesn't match the uprobe trampoline. IOW it would reduce the scope to which we're making seccomp behave unexpectedly.
Quoted 1 line
Kees, any thoughts about this approach?
ping, any idea?
thanks,
jirka
Quoted 15 lines
thanks,
jirkaQuoted 10 lines
Quoted 10 lines
jirka
Quoted 5 lines
where
is_a_real_uretprobe()is only true if the nr and arch match uretprobe *and* the address is right.--Andy
Quoted 39 lines · Jiri Olsa
Quoted 38 lines · Jiri Olsa
Quoted 36 lines · Andy Lutomirski
Quoted 19 lines · Jiri Olsa
Quoted 17 lines · Andy Lutomirski
Quoted 8 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.
It would be a layering violation, but we could perhaps do better here:
Quoted 4 lines
arch/x86/kernel/uprobes.c +2
-1-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;Quoted 8 lines
Quoted 5 lines
Instead of SIGILL, perhaps this should do the seccomp action? So the logic in seccomp would be (sketchily, with some real mode1 mess):
if (
is_a_real_uretprobe())skip seccomp;IIUC you want to move the address check earlier to the seccomp path.. with the benefit that we would kill not allowed caller sooner?
The benefit would be that seccomp users that want to do something other than killing a process (returning an error code, getting notified, etc) could retain that functionality without the new automatic hole being poked for
uretprobe()in cases where uprobes aren't in use or where the calling address doesn't match the uprobe trampoline. IOW it would reduce the scope to which we're making seccomp behave unexpectedly.
Quoted 3 lines
Quoted 1 line
Kees, any thoughts about this approach?
ping, any idea?
So in any case I think the seccomp QoL tie-in suggested by Andy should be done in a separate patch, and I've applied the -v3 patch to tip:perf/core as-is.
( I've added Alexei's Acked-by too, which as I've read the v2 thread's
discussion was a given as long as his ~0 suggestion was implemented, which you did. )
Thanks,
Ingo
[tip: perf/core] uprobes/x86: Harden uretprobe syscall trampoline check
In reply to Jiri OlsaThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: fa6192adc32f4fdfe5b74edd5b210e12afd6ecc0 Gitweb: https://git.kernel.org/tip/fa6192adc32f4fdfe5b74edd5b210e12afd6ecc0 Author: Jiri Olsa <[email protected]> AuthorDate: Wed, 12 Feb 2025 23:04:33 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 06 Mar 2025 12:22:45 +01:00
uprobes/x86: Harden uretprobe syscall trampoline check
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:
Though the mmap minimum address restrictions will typically prevent creating mappings there, let's make sure uretprobe syscall checks for that.
--- arch/x86/kernel/uprobes.c | 14 +++++++++----- include/linux/uprobes.h | 2 ++ kernel/events/uprobes.c | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-)
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 (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));
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
@@ -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);/* ^^^ */