uprobe: uretprobe speed up
Subsystems (11)
Jiri Olsa
reviewerv2 cover uprobe: uretprobe speed up
hi,
as part of the effort on speeding up the uprobes [0] coming with return uprobe optimization by using syscall instead of the trap on the uretprobe trampoline.
The speed up depends on instruction type that uprobe is installed and depends on specific HW type, please check patch 1 for details.
I added extra selftests to check on registers values before and after uretprobe to make sure the syscall saves all the needed regs and that uprobe consumer can still change registers.
Patch 1 is based on:
https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
probes/for-next
and patches 2,3 are based on bpf-next/master and can be taken separately through bpf-next tree without patch 1.
v2 changes:
- reposted to proper tracing mailing list [Masami]
- renamed selftest trigger functions [Andrii]
- kept bpf_testmod register call pattern [Andrii]
- added acks [Andrii/Oleg]
thanks,
jirka
[0] https://lore.kernel.org/bpf/ZeCXHKJ--iYYbmLj@krava/
---
Jiri Olsa (3):
uprobe: Add uretprobe syscall to speed up return probe selftests/bpf: Add uretprobe syscall test for regs integrity selftests/bpf: Add uretprobe syscall test for regs changes
arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/kernel/uprobes.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/syscalls.h | 2 ++ include/linux/uprobes.h | 2 ++ include/uapi/asm-generic/unistd.h | 5 +++- kernel/events/uprobes.c | 18 +++++++++--- kernel/sys_ni.c | 2 ++ tools/include/linux/compiler.h | 4 +++ tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/uprobe_syscall.c | 15 ++++++++++ 11 files changed, 478 insertions(+), 6 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c
Jiri Olsa
reviewerPATCH v2 1/3 uprobe: Add uretprobe syscall to speed up return probe
Adding uretprobe syscall instead of trap to speed up return probe.
At the moment the uretprobe setup/path is:
- install entry uprobe
- when the uprobe is hit, it overwrites probed function's return address on stack with address of the trampoline that contains breakpoint instruction
- the breakpoint trap code handles the uretprobe consumers execution and jumps back to original return address
This patch replaces the above trampoline's breakpoint instruction with new ureprobe syscall call. This syscall does exactly the same job as the trap with some more extra work:
- syscall trampoline must save original value for rax/r11/rcx registers on stack - rax is set to syscall number and r11/rcx are changed and used by syscall instruction
- the syscall code reads the original values of those registers and restore those values in task's pt_regs area
Even with the extra registers handling code the having uretprobes handled by syscalls shows speed improvement.
On Intel (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz)
current:
base : 15.888 ± 0.033M/s
uprobe-nop : 3.016 ± 0.000M/s
uprobe-push : 2.832 ± 0.005M/s
uprobe-ret : 1.104 ± 0.000M/s
uretprobe-nop : 1.487 ± 0.000M/s
uretprobe-push : 1.456 ± 0.000M/s
uretprobe-ret : 0.816 ± 0.001M/swith the fix:
base : 15.116 ± 0.045M/s
uprobe-nop : 3.001 ± 0.045M/s
uprobe-push : 2.831 ± 0.004M/s
uprobe-ret : 1.102 ± 0.001M/s
uretprobe-nop : 1.969 ± 0.001M/s < 32% speedup
uretprobe-push : 1.905 ± 0.004M/s < 30% speedup
uretprobe-ret : 0.933 ± 0.002M/s < 14% speedupOn Amd (AMD Ryzen 7 5700U)
current:
base : 5.105 ± 0.003M/s
uprobe-nop : 1.552 ± 0.002M/s
uprobe-push : 1.408 ± 0.003M/s
uprobe-ret : 0.827 ± 0.001M/s
uretprobe-nop : 0.779 ± 0.001M/s
uretprobe-push : 0.750 ± 0.001M/s
uretprobe-ret : 0.539 ± 0.001M/swith the fix:
base : 5.119 ± 0.002M/s
uprobe-nop : 1.523 ± 0.003M/s
uprobe-push : 1.384 ± 0.003M/s
uprobe-ret : 0.826 ± 0.002M/s
uretprobe-nop : 0.866 ± 0.002M/s < 11% speedup
uretprobe-push : 0.826 ± 0.002M/s < 10% speedup
uretprobe-ret : 0.581 ± 0.001M/s < 7% speedup--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/kernel/uprobes.c | 83 ++++++++++++++++++++++++++ include/linux/syscalls.h | 2 + include/linux/uprobes.h | 2 + include/uapi/asm-generic/unistd.h | 5 +- kernel/events/uprobes.c | 18 ++++-- kernel/sys_ni.c | 2 + 7 files changed, 108 insertions(+), 5 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -383,6 +383,7 @@ 459 common lsm_get_self_attr sys_lsm_get_self_attr 460 common lsm_set_self_attr sys_lsm_set_self_attr 461 common lsm_list_modules sys_lsm_list_modules +462 64 uretprobe sys_uretprobe # # Due to a historical design error, certain syscalls are numbered differently
arch/x86/kernel/uprobes.c +83 -0
95 lines not on this page · raw message
include/linux/syscalls.h +2 -0
@@ -972,6 +972,8 @@ asmlinkage long sys_lsm_list_modules(u64 *ids, size_t *size, u32 flags);/* x86 */ asmlinkage long sys_ioperm (unsigned long from,unsigned long num,int on); +asmlinkage long sys_uretprobe (void ); +/* pciconfig: alpha, arm, arm64, ia64, sparc */ asmlinkage long sys_pciconfig_read(unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len,
include/linux/uprobes.h +2 -0
@@ -138,6 +138,8 @@ extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check cextern bool arch_uprobe_ignore (struct arch_uprobe *aup,struct pt_regs *regs);extern void arch_uprobe_copy_ixol (struct page *page,unsigned long vaddr,void *src,unsigned long len); +extern void uprobe_handle_trampoline (struct pt_regs *regs); +extern void *arch_uprobe_trampoline (unsigned long *psize);#else /* !CONFIG_UPROBES */ struct uprobes_state { };
include/uapi/asm-generic/unistd.h +4 -1
@@ -842,8 +842,11 @@ __SYSCALL(__NR_lsm_set_self_attr, sys_lsm_set_self_attr)#define __NR_lsm_list_modules 461 __SYSCALL(__NR_lsm_list_modules, sys_lsm_list_modules) +#define __NR_uretprobe 462 +__SYSCALL(__NR_uretprobe, sys_uretprobe) +#undef __NR_syscalls -#define __NR_syscalls 462 +#define __NR_syscalls 463/* * 32 bit systems traditionally used different
kernel/events/uprobes.c +14 -4
@@ -1474,11 +1474,20 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)return ret; } +void *__weak arch_uprobe_trampoline (unsigned long *psize) +{ +static uprobe_opcode_t insn =UPROBE_SWBP_INSN ; + + *psize =UPROBE_SWBP_INSN_SIZE ; +return &insn; +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm; -uprobe_opcode_t insn =UPROBE_SWBP_INSN ; +unsigned long insns_size;struct xol_area *area; +void *insns; area =kmalloc (sizeof (*area),GFP_KERNEL );if (unlikely (!area))
@@ -1502,7 +1511,8 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)/* Reserve the 1st slot for get_trampoline_vaddr() */ set_bit (0 , area->bitmap);atomic_set (&area->slot_count,1 ); -arch_uprobe_copy_ixol (area->pages[0 ],0 , &insn,UPROBE_SWBP_INSN_SIZE ); + insns =arch_uprobe_trampoline (&insns_size); +arch_uprobe_copy_ixol (area->pages[0 ],0 , insns, insns_size);if (!xol_add_vma (mm, area))return area;
@@ -2123,7 +2133,7 @@ static struct return_instance *find_next_ret_chain(struct return_instance *ri)return ri; } -static void handle_trampoline (struct pt_regs *regs) +void uprobe_handle_trampoline (struct pt_regs *regs) {struct uprobe_task *utask;struct return_instance *ri, *next;
@@ -2188,7 +2198,7 @@ static void handle_swbp(struct pt_regs *regs) bp_vaddr =uprobe_get_swbp_addr (regs);if (bp_vaddr ==get_trampoline_vaddr ()) -return handle_trampoline (regs); +return uprobe_handle_trampoline (regs); uprobe =find_active_uprobe (bp_vaddr, &is_swbp);if (!uprobe) {
kernel/sys_ni.c +2 -0
@@ -391,3 +391,5 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq); + +COND_SYSCALL (uretprobe);
Signature · 1 line
2.44.0
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
Also, we should run syzkaller on this syscall. And if uretprobe is set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
Thank you,
Quoted 308 lines
At the moment the uretprobe setup/path is:
- install entry uprobe
- when the uprobe is hit, it overwrites probed function's return address on stack with address of the trampoline that contains breakpoint instruction
- the breakpoint trap code handles the uretprobe consumers execution and jumps back to original return address
This patch replaces the above trampoline's breakpoint instruction with new ureprobe syscall call. This syscall does exactly the same job as the trap with some more extra work:
- syscall trampoline must save original value for rax/r11/rcx registers on stack - rax is set to syscall number and r11/rcx are changed and used by syscall instruction
- the syscall code reads the original values of those registers and restore those values in task's
pt_regsareaEven with the extra registers handling code the having uretprobes handled by syscalls shows speed improvement.
On Intel (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz)
current:
base : 15.888 ± 0.033M/s uprobe-nop : 3.016 ± 0.000M/s uprobe-push : 2.832 ± 0.005M/s uprobe-ret : 1.104 ± 0.000M/s uretprobe-nop : 1.487 ± 0.000M/s uretprobe-push : 1.456 ± 0.000M/s uretprobe-ret : 0.816 ± 0.001M/swith the fix:
base : 15.116 ± 0.045M/s uprobe-nop : 3.001 ± 0.045M/s uprobe-push : 2.831 ± 0.004M/s uprobe-ret : 1.102 ± 0.001M/s uretprobe-nop : 1.969 ± 0.001M/s < 32% speedup uretprobe-push : 1.905 ± 0.004M/s < 30% speedup uretprobe-ret : 0.933 ± 0.002M/s < 14% speedupOn Amd (AMD Ryzen 7 5700U)
current:
base : 5.105 ± 0.003M/s uprobe-nop : 1.552 ± 0.002M/s uprobe-push : 1.408 ± 0.003M/s uprobe-ret : 0.827 ± 0.001M/s uretprobe-nop : 0.779 ± 0.001M/s uretprobe-push : 0.750 ± 0.001M/s uretprobe-ret : 0.539 ± 0.001M/swith the fix:
base : 5.119 ± 0.002M/s uprobe-nop : 1.523 ± 0.003M/s uprobe-push : 1.384 ± 0.003M/s uprobe-ret : 0.826 ± 0.002M/s uretprobe-nop : 0.866 ± 0.002M/s < 11% speedup uretprobe-push : 0.826 ± 0.002M/s < 10% speedup uretprobe-ret : 0.581 ± 0.001M/s < 7% speedupReviewed-by Oleg Nesterov <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Signed-off-by Oleg Nesterov <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/kernel/uprobes.c | 83 ++++++++++++++++++++++++++ include/linux/syscalls.h | 2 + include/linux/uprobes.h | 2 + include/uapi/asm-generic/unistd.h | 5 +- kernel/events/uprobes.c | 18 ++++-- kernel/sys_ni.c | 2 + 7 files changed, 108 insertions(+), 5 deletions(-)arch/x86/entry/syscalls/syscall_64.tbl +1
-0@@ -383,6 +383,7 @@ 459 common lsm_get_self_attr sys_lsm_get_self_attr 460 common lsm_set_self_attr sys_lsm_set_self_attr 461 common lsm_list_modules sys_lsm_list_modules +462 64 uretprobe sys_uretprobe # # Due to a historical design error, certain syscalls are numbered differentlyarch/x86/kernel/uprobes.c +83
-0@@ -12,6 +12,7 @@#include <linux/ptrace.h> #include <linux/uprobes.h> #include <linux/uaccess.h> +#include <linux/syscalls.h> #include <linux/kdebug.h> #include <asm/processor.h> @@ -308,6 +309,88 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool }#ifdef CONFIG_X86_64 + +asm ( +".pushsection .rodata\n" +".global uretprobe_syscall_entry\n" +"uretprobe_syscall_entry:\n" +"pushq %rax\n" +"pushq %rcx\n" +"pushq %r11\n" + "movq $" __stringify(__NR_uretprobe) ", %rax\n" +"syscall\n" +"popq %r11\n" +"popq %rcx\n" + +/* The uretprobe syscall replaces stored %rax value with final +* return address, so we don't restore %rax in here and just +* call ret. +*/ +"retq\n" +".global uretprobe_syscall_end\n" +"uretprobe_syscall_end:\n" +".popsection\n" +); + +extern u8 uretprobe_syscall_entry[]; +extern u8 uretprobe_syscall_end[]; + +void *arch_uprobe_trampoline (unsigned long *psize) +{ + *psize = uretprobe_syscall_end - uretprobe_syscall_entry; +return uretprobe_syscall_entry; +} + +SYSCALL_DEFINE0 (uretprobe) +{ +struct pt_regs *regs =task_pt_regs (current); +unsigned long err, ip, sp, r11_cx_ax[3 ]; + + err =copy_from_user (r11_cx_ax, (void __user *)regs->sp,sizeof (r11_cx_ax)); +WARN_ON_ONCE (err); + +/* expose the "right" values of r11/cx/ax/sp to uprobe_consumer/s */ + regs->r11 = r11_cx_ax[0 ]; + regs->cx = r11_cx_ax[1 ]; + regs->ax = r11_cx_ax[2 ]; + regs->sp +=sizeof (r11_cx_ax); + regs->orig_ax =-1 ; + + ip = regs->ip; + sp = regs->sp; + +uprobe_handle_trampoline (regs); + +/* +* uprobe_consumer has changed sp, we can do nothing, +* just return via iret +*/ +if (regs->sp != sp) +return regs->ax; + regs->sp -=sizeof (r11_cx_ax); + +/* for the case uprobe_consumer has changed r11/cx */ + r11_cx_ax[0 ] = regs->r11; + r11_cx_ax[1 ] = regs->cx; + +/* +* ax register is passed through as return value, so we can use +* its space on stack for ip value and jump to it through the +* trampoline's ret instruction +*/ + r11_cx_ax[2 ] = regs->ip; + regs->ip = ip; + + err =copy_to_user ((void __user *)regs->sp, r11_cx_ax,sizeof (r11_cx_ax)); +WARN_ON_ONCE (err); + +/* ensure sysret, see do_syscall_64() */ + regs->r11 = regs->flags; + regs->cx = regs->ip; + +return regs->ax; +} +/* * If arch_uprobe->insn doesn't use rip-relative addressing, return * immediately. Otherwise, rewrite the instruction so that it accesses include/linux/syscalls.h +2
-0@@ -972,6 +972,8 @@ asmlinkage long sys_lsm_list_modules(u64 *ids, size_t *size, u32 flags);/* x86 */ asmlinkage long sys_ioperm (unsigned long from,unsigned long num,int on); +asmlinkage long sys_uretprobe (void ); +/* pciconfig: alpha, arm, arm64, ia64, sparc */ asmlinkage long sys_pciconfig_read(unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len,include/linux/uprobes.h +2
-0@@ -138,6 +138,8 @@ extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check cextern bool arch_uprobe_ignore (struct arch_uprobe *aup,struct pt_regs *regs);extern void arch_uprobe_copy_ixol (struct page *page,unsigned long vaddr,void *src,unsigned long len); +extern void uprobe_handle_trampoline (struct pt_regs *regs); +extern void *arch_uprobe_trampoline (unsigned long *psize);#else /* !CONFIG_UPROBES */ struct uprobes_state { };include/uapi/asm-generic/unistd.h +4
-1@@ -842,8 +842,11 @@ __SYSCALL(__NR_lsm_set_self_attr, sys_lsm_set_self_attr)#define __NR_lsm_list_modules 461 __SYSCALL(__NR_lsm_list_modules, sys_lsm_list_modules) +#define __NR_uretprobe 462 +__SYSCALL(__NR_uretprobe, sys_uretprobe) +#undef __NR_syscalls -#define __NR_syscalls 462 +#define __NR_syscalls 463/* * 32 bit systems traditionally used different kernel/events/uprobes.c +14
-4@@ -1474,11 +1474,20 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)return ret; } +void *__weak arch_uprobe_trampoline (unsigned long *psize) +{ +static uprobe_opcode_t insn =UPROBE_SWBP_INSN ; + + *psize =UPROBE_SWBP_INSN_SIZE ; +return &insn; +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm; -uprobe_opcode_t insn =UPROBE_SWBP_INSN ; +unsigned long insns_size;struct xol_area *area; +void *insns; area =kmalloc (sizeof (*area),GFP_KERNEL );if (unlikely (!area))@@ -1502,7 +1511,8 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)/* Reserve the 1st slot for get_trampoline_vaddr() */ set_bit (0 , area->bitmap);atomic_set (&area->slot_count,1 ); -arch_uprobe_copy_ixol (area->pages[0 ],0 , &insn,UPROBE_SWBP_INSN_SIZE ); + insns =arch_uprobe_trampoline (&insns_size); +arch_uprobe_copy_ixol (area->pages[0 ],0 , insns, insns_size);if (!xol_add_vma (mm, area))return area;@@ -2123,7 +2133,7 @@ static struct return_instance *find_next_ret_chain(struct return_instance *ri)return ri; } -static void handle_trampoline (struct pt_regs *regs) +void uprobe_handle_trampoline (struct pt_regs *regs) {struct uprobe_task *utask;struct return_instance *ri, *next;@@ -2188,7 +2198,7 @@ static void handle_swbp(struct pt_regs *regs) bp_vaddr =uprobe_get_swbp_addr (regs);if (bp_vaddr ==get_trampoline_vaddr ()) -return handle_trampoline (regs); +return uprobe_handle_trampoline (regs); uprobe =find_active_uprobe (bp_vaddr, &is_swbp);if (!uprobe) {kernel/sys_ni.c +2
-0@@ -391,3 +391,5 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq); + +COND_SYSCALL (uretprobe);Signature · 1 line
2.44.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 9 lines · Masami Hiramatsu
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Quoted 2 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
thanks,
jirka
Quoted 315 lines
Thank you,
Quoted 308 lines
At the moment the uretprobe setup/path is:
- install entry uprobe
- when the uprobe is hit, it overwrites probed function's return address on stack with address of the trampoline that contains breakpoint instruction
- the breakpoint trap code handles the uretprobe consumers execution and jumps back to original return address
This patch replaces the above trampoline's breakpoint instruction with new ureprobe syscall call. This syscall does exactly the same job as the trap with some more extra work:
- syscall trampoline must save original value for rax/r11/rcx registers on stack - rax is set to syscall number and r11/rcx are changed and used by syscall instruction
- the syscall code reads the original values of those registers and restore those values in task's
pt_regsareaEven with the extra registers handling code the having uretprobes handled by syscalls shows speed improvement.
On Intel (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz)
current:
base : 15.888 ± 0.033M/s uprobe-nop : 3.016 ± 0.000M/s uprobe-push : 2.832 ± 0.005M/s uprobe-ret : 1.104 ± 0.000M/s uretprobe-nop : 1.487 ± 0.000M/s uretprobe-push : 1.456 ± 0.000M/s uretprobe-ret : 0.816 ± 0.001M/swith the fix:
base : 15.116 ± 0.045M/s uprobe-nop : 3.001 ± 0.045M/s uprobe-push : 2.831 ± 0.004M/s uprobe-ret : 1.102 ± 0.001M/s uretprobe-nop : 1.969 ± 0.001M/s < 32% speedup uretprobe-push : 1.905 ± 0.004M/s < 30% speedup uretprobe-ret : 0.933 ± 0.002M/s < 14% speedupOn Amd (AMD Ryzen 7 5700U)
current:
base : 5.105 ± 0.003M/s uprobe-nop : 1.552 ± 0.002M/s uprobe-push : 1.408 ± 0.003M/s uprobe-ret : 0.827 ± 0.001M/s uretprobe-nop : 0.779 ± 0.001M/s uretprobe-push : 0.750 ± 0.001M/s uretprobe-ret : 0.539 ± 0.001M/swith the fix:
base : 5.119 ± 0.002M/s uprobe-nop : 1.523 ± 0.003M/s uprobe-push : 1.384 ± 0.003M/s uprobe-ret : 0.826 ± 0.002M/s uretprobe-nop : 0.866 ± 0.002M/s < 11% speedup uretprobe-push : 0.826 ± 0.002M/s < 10% speedup uretprobe-ret : 0.581 ± 0.001M/s < 7% speedupReviewed-by Oleg Nesterov <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Signed-off-by Oleg Nesterov <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/kernel/uprobes.c | 83 ++++++++++++++++++++++++++ include/linux/syscalls.h | 2 + include/linux/uprobes.h | 2 + include/uapi/asm-generic/unistd.h | 5 +- kernel/events/uprobes.c | 18 ++++-- kernel/sys_ni.c | 2 + 7 files changed, 108 insertions(+), 5 deletions(-)arch/x86/entry/syscalls/syscall_64.tbl +1
-0@@ -383,6 +383,7 @@ 459 common lsm_get_self_attr sys_lsm_get_self_attr 460 common lsm_set_self_attr sys_lsm_set_self_attr 461 common lsm_list_modules sys_lsm_list_modules +462 64 uretprobe sys_uretprobe # # Due to a historical design error, certain syscalls are numbered differentlyarch/x86/kernel/uprobes.c +83
-0@@ -12,6 +12,7 @@#include <linux/ptrace.h> #include <linux/uprobes.h> #include <linux/uaccess.h> +#include <linux/syscalls.h> #include <linux/kdebug.h> #include <asm/processor.h> @@ -308,6 +309,88 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool }#ifdef CONFIG_X86_64 + +asm ( +".pushsection .rodata\n" +".global uretprobe_syscall_entry\n" +"uretprobe_syscall_entry:\n" +"pushq %rax\n" +"pushq %rcx\n" +"pushq %r11\n" + "movq $" __stringify(__NR_uretprobe) ", %rax\n" +"syscall\n" +"popq %r11\n" +"popq %rcx\n" + +/* The uretprobe syscall replaces stored %rax value with final +* return address, so we don't restore %rax in here and just +* call ret. +*/ +"retq\n" +".global uretprobe_syscall_end\n" +"uretprobe_syscall_end:\n" +".popsection\n" +); + +extern u8 uretprobe_syscall_entry[]; +extern u8 uretprobe_syscall_end[]; + +void *arch_uprobe_trampoline (unsigned long *psize) +{ + *psize = uretprobe_syscall_end - uretprobe_syscall_entry; +return uretprobe_syscall_entry; +} + +SYSCALL_DEFINE0 (uretprobe) +{ +struct pt_regs *regs =task_pt_regs (current); +unsigned long err, ip, sp, r11_cx_ax[3 ]; + + err =copy_from_user (r11_cx_ax, (void __user *)regs->sp,sizeof (r11_cx_ax)); +WARN_ON_ONCE (err); + +/* expose the "right" values of r11/cx/ax/sp to uprobe_consumer/s */ + regs->r11 = r11_cx_ax[0 ]; + regs->cx = r11_cx_ax[1 ]; + regs->ax = r11_cx_ax[2 ]; + regs->sp +=sizeof (r11_cx_ax); + regs->orig_ax =-1 ; + + ip = regs->ip; + sp = regs->sp; + +uprobe_handle_trampoline (regs); + +/* +* uprobe_consumer has changed sp, we can do nothing, +* just return via iret +*/ +if (regs->sp != sp) +return regs->ax; + regs->sp -=sizeof (r11_cx_ax); + +/* for the case uprobe_consumer has changed r11/cx */ + r11_cx_ax[0 ] = regs->r11; + r11_cx_ax[1 ] = regs->cx; + +/* +* ax register is passed through as return value, so we can use +* its space on stack for ip value and jump to it through the +* trampoline's ret instruction +*/ + r11_cx_ax[2 ] = regs->ip; + regs->ip = ip; + + err =copy_to_user ((void __user *)regs->sp, r11_cx_ax,sizeof (r11_cx_ax)); +WARN_ON_ONCE (err); + +/* ensure sysret, see do_syscall_64() */ + regs->r11 = regs->flags; + regs->cx = regs->ip; + +return regs->ax; +} +/* * If arch_uprobe->insn doesn't use rip-relative addressing, return * immediately. Otherwise, rewrite the instruction so that it accesses include/linux/syscalls.h +2
-0@@ -972,6 +972,8 @@ asmlinkage long sys_lsm_list_modules(u64 *ids, size_t *size, u32 flags);/* x86 */ asmlinkage long sys_ioperm (unsigned long from,unsigned long num,int on); +asmlinkage long sys_uretprobe (void ); +/* pciconfig: alpha, arm, arm64, ia64, sparc */ asmlinkage long sys_pciconfig_read(unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len,include/linux/uprobes.h +2
-0@@ -138,6 +138,8 @@ extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check cextern bool arch_uprobe_ignore (struct arch_uprobe *aup,struct pt_regs *regs);extern void arch_uprobe_copy_ixol (struct page *page,unsigned long vaddr,void *src,unsigned long len); +extern void uprobe_handle_trampoline (struct pt_regs *regs); +extern void *arch_uprobe_trampoline (unsigned long *psize);#else /* !CONFIG_UPROBES */ struct uprobes_state { };include/uapi/asm-generic/unistd.h +4
-1@@ -842,8 +842,11 @@ __SYSCALL(__NR_lsm_set_self_attr, sys_lsm_set_self_attr)#define __NR_lsm_list_modules 461 __SYSCALL(__NR_lsm_list_modules, sys_lsm_list_modules) +#define __NR_uretprobe 462 +__SYSCALL(__NR_uretprobe, sys_uretprobe) +#undef __NR_syscalls -#define __NR_syscalls 462 +#define __NR_syscalls 463/* * 32 bit systems traditionally used different kernel/events/uprobes.c +14
-4@@ -1474,11 +1474,20 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)return ret; } +void *__weak arch_uprobe_trampoline (unsigned long *psize) +{ +static uprobe_opcode_t insn =UPROBE_SWBP_INSN ; + + *psize =UPROBE_SWBP_INSN_SIZE ; +return &insn; +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm; -uprobe_opcode_t insn =UPROBE_SWBP_INSN ; +unsigned long insns_size;struct xol_area *area; +void *insns; area =kmalloc (sizeof (*area),GFP_KERNEL );if (unlikely (!area))@@ -1502,7 +1511,8 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)/* Reserve the 1st slot for get_trampoline_vaddr() */ set_bit (0 , area->bitmap);atomic_set (&area->slot_count,1 ); -arch_uprobe_copy_ixol (area->pages[0 ],0 , &insn,UPROBE_SWBP_INSN_SIZE ); + insns =arch_uprobe_trampoline (&insns_size); +arch_uprobe_copy_ixol (area->pages[0 ],0 , insns, insns_size);if (!xol_add_vma (mm, area))return area;@@ -2123,7 +2133,7 @@ static struct return_instance *find_next_ret_chain(struct return_instance *ri)return ri; } -static void handle_trampoline (struct pt_regs *regs) +void uprobe_handle_trampoline (struct pt_regs *regs) {struct uprobe_task *utask;struct return_instance *ri, *next;@@ -2188,7 +2198,7 @@ static void handle_swbp(struct pt_regs *regs) bp_vaddr =uprobe_get_swbp_addr (regs);if (bp_vaddr ==get_trampoline_vaddr ()) -return handle_trampoline (regs); +return uprobe_handle_trampoline (regs); uprobe =find_active_uprobe (bp_vaddr, &is_swbp);if (!uprobe) {kernel/sys_ni.c +2
-0@@ -391,3 +391,5 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq); + +COND_SYSCALL (uretprobe);Signature · 1 line
2.44.0Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Oleg Nesterov
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
I leave this to you and Masami, but...
Quoted 6 lines · Jiri Olsa
Quoted 3 lines · Masami Hiramatsu
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
Quoted 7 lines
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Agreed.
Quoted 3 lines
Quoted 1 line
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
I don't understand this concern...
Quoted 5 lines
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task,
Yes,
Quoted 1 line
or it will trigger uretprobe if there's one pending
... and corrupt the caller. So what?
Quoted 3 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Yes, we can... well, ignoring the race with mremap() from another thread.
But why should we care?
Userspace should not call sys_uretprobe(). Likewise, it should not call sys_restart_syscall(). Likewise, it should not jump to xol_area.
Of course, userspace (especially syzkaller) _can_ do this. So what?
I think the only thing we need to ensure is that the "malicious" task which calls sys_uretprobe() can only harm itself, nothing more.
No?
Oleg.
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Jiri OlsaQuoted 11 lines · On Wed, 3 Apr 2024 11:47:41 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
Quoted 7 lines
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Hmm, I think a similar syscall is ptrace? prctl may also be a candidate.
Quoted 11 lines
Quoted 3 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
Quoted 4 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Why not limit? :) The uprobe_handle_trampoline() expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)
Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.
Thank you,
Quoted 320 lines
thanks,
jirkaQuoted 315 lines
Thank you,
Quoted 308 lines
At the moment the uretprobe setup/path is:
- install entry uprobe
- when the uprobe is hit, it overwrites probed function's return address on stack with address of the trampoline that contains breakpoint instruction
- the breakpoint trap code handles the uretprobe consumers execution and jumps back to original return address
This patch replaces the above trampoline's breakpoint instruction with new ureprobe syscall call. This syscall does exactly the same job as the trap with some more extra work:
- syscall trampoline must save original value for rax/r11/rcx registers on stack - rax is set to syscall number and r11/rcx are changed and used by syscall instruction
- the syscall code reads the original values of those registers and restore those values in task's
pt_regsareaEven with the extra registers handling code the having uretprobes handled by syscalls shows speed improvement.
On Intel (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz)
current:
base : 15.888 ± 0.033M/s uprobe-nop : 3.016 ± 0.000M/s uprobe-push : 2.832 ± 0.005M/s uprobe-ret : 1.104 ± 0.000M/s uretprobe-nop : 1.487 ± 0.000M/s uretprobe-push : 1.456 ± 0.000M/s uretprobe-ret : 0.816 ± 0.001M/swith the fix:
base : 15.116 ± 0.045M/s uprobe-nop : 3.001 ± 0.045M/s uprobe-push : 2.831 ± 0.004M/s uprobe-ret : 1.102 ± 0.001M/s uretprobe-nop : 1.969 ± 0.001M/s < 32% speedup uretprobe-push : 1.905 ± 0.004M/s < 30% speedup uretprobe-ret : 0.933 ± 0.002M/s < 14% speedupOn Amd (AMD Ryzen 7 5700U)
current:
base : 5.105 ± 0.003M/s uprobe-nop : 1.552 ± 0.002M/s uprobe-push : 1.408 ± 0.003M/s uprobe-ret : 0.827 ± 0.001M/s uretprobe-nop : 0.779 ± 0.001M/s uretprobe-push : 0.750 ± 0.001M/s uretprobe-ret : 0.539 ± 0.001M/swith the fix:
base : 5.119 ± 0.002M/s uprobe-nop : 1.523 ± 0.003M/s uprobe-push : 1.384 ± 0.003M/s uprobe-ret : 0.826 ± 0.002M/s uretprobe-nop : 0.866 ± 0.002M/s < 11% speedup uretprobe-push : 0.826 ± 0.002M/s < 10% speedup uretprobe-ret : 0.581 ± 0.001M/s < 7% speedupReviewed-by Oleg Nesterov <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Signed-off-by Oleg Nesterov <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/kernel/uprobes.c | 83 ++++++++++++++++++++++++++ include/linux/syscalls.h | 2 + include/linux/uprobes.h | 2 + include/uapi/asm-generic/unistd.h | 5 +- kernel/events/uprobes.c | 18 ++++-- kernel/sys_ni.c | 2 + 7 files changed, 108 insertions(+), 5 deletions(-)arch/x86/entry/syscalls/syscall_64.tbl +1
-0@@ -383,6 +383,7 @@ 459 common lsm_get_self_attr sys_lsm_get_self_attr 460 common lsm_set_self_attr sys_lsm_set_self_attr 461 common lsm_list_modules sys_lsm_list_modules +462 64 uretprobe sys_uretprobe # # Due to a historical design error, certain syscalls are numbered differentlyarch/x86/kernel/uprobes.c +83
-0@@ -12,6 +12,7 @@#include <linux/ptrace.h> #include <linux/uprobes.h> #include <linux/uaccess.h> +#include <linux/syscalls.h> #include <linux/kdebug.h> #include <asm/processor.h> @@ -308,6 +309,88 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool }#ifdef CONFIG_X86_64 + +asm ( +".pushsection .rodata\n" +".global uretprobe_syscall_entry\n" +"uretprobe_syscall_entry:\n" +"pushq %rax\n" +"pushq %rcx\n" +"pushq %r11\n" + "movq $" __stringify(__NR_uretprobe) ", %rax\n" +"syscall\n" +"popq %r11\n" +"popq %rcx\n" + +/* The uretprobe syscall replaces stored %rax value with final +* return address, so we don't restore %rax in here and just +* call ret. +*/ +"retq\n" +".global uretprobe_syscall_end\n" +"uretprobe_syscall_end:\n" +".popsection\n" +); + +extern u8 uretprobe_syscall_entry[]; +extern u8 uretprobe_syscall_end[]; + +void *arch_uprobe_trampoline (unsigned long *psize) +{ + *psize = uretprobe_syscall_end - uretprobe_syscall_entry; +return uretprobe_syscall_entry; +} + +SYSCALL_DEFINE0 (uretprobe) +{ +struct pt_regs *regs =task_pt_regs (current); +unsigned long err, ip, sp, r11_cx_ax[3 ]; + + err =copy_from_user (r11_cx_ax, (void __user *)regs->sp,sizeof (r11_cx_ax)); +WARN_ON_ONCE (err); + +/* expose the "right" values of r11/cx/ax/sp to uprobe_consumer/s */ + regs->r11 = r11_cx_ax[0 ]; + regs->cx = r11_cx_ax[1 ]; + regs->ax = r11_cx_ax[2 ]; + regs->sp +=sizeof (r11_cx_ax); + regs->orig_ax =-1 ; + + ip = regs->ip; + sp = regs->sp; + +uprobe_handle_trampoline (regs); + +/* +* uprobe_consumer has changed sp, we can do nothing, +* just return via iret +*/ +if (regs->sp != sp) +return regs->ax; + regs->sp -=sizeof (r11_cx_ax); + +/* for the case uprobe_consumer has changed r11/cx */ + r11_cx_ax[0 ] = regs->r11; + r11_cx_ax[1 ] = regs->cx; + +/* +* ax register is passed through as return value, so we can use +* its space on stack for ip value and jump to it through the +* trampoline's ret instruction +*/ + r11_cx_ax[2 ] = regs->ip; + regs->ip = ip; + + err =copy_to_user ((void __user *)regs->sp, r11_cx_ax,sizeof (r11_cx_ax)); +WARN_ON_ONCE (err); + +/* ensure sysret, see do_syscall_64() */ + regs->r11 = regs->flags; + regs->cx = regs->ip; + +return regs->ax; +} +/* * If arch_uprobe->insn doesn't use rip-relative addressing, return * immediately. Otherwise, rewrite the instruction so that it accesses include/linux/syscalls.h +2
-0@@ -972,6 +972,8 @@ asmlinkage long sys_lsm_list_modules(u64 *ids, size_t *size, u32 flags);/* x86 */ asmlinkage long sys_ioperm (unsigned long from,unsigned long num,int on); +asmlinkage long sys_uretprobe (void ); +/* pciconfig: alpha, arm, arm64, ia64, sparc */ asmlinkage long sys_pciconfig_read(unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len,include/linux/uprobes.h +2
-0@@ -138,6 +138,8 @@ extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check cextern bool arch_uprobe_ignore (struct arch_uprobe *aup,struct pt_regs *regs);extern void arch_uprobe_copy_ixol (struct page *page,unsigned long vaddr,void *src,unsigned long len); +extern void uprobe_handle_trampoline (struct pt_regs *regs); +extern void *arch_uprobe_trampoline (unsigned long *psize);#else /* !CONFIG_UPROBES */ struct uprobes_state { };include/uapi/asm-generic/unistd.h +4
-1@@ -842,8 +842,11 @@ __SYSCALL(__NR_lsm_set_self_attr, sys_lsm_set_self_attr)#define __NR_lsm_list_modules 461 __SYSCALL(__NR_lsm_list_modules, sys_lsm_list_modules) +#define __NR_uretprobe 462 +__SYSCALL(__NR_uretprobe, sys_uretprobe) +#undef __NR_syscalls -#define __NR_syscalls 462 +#define __NR_syscalls 463/* * 32 bit systems traditionally used different kernel/events/uprobes.c +14
-4@@ -1474,11 +1474,20 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)return ret; } +void *__weak arch_uprobe_trampoline (unsigned long *psize) +{ +static uprobe_opcode_t insn =UPROBE_SWBP_INSN ; + + *psize =UPROBE_SWBP_INSN_SIZE ; +return &insn; +} +static struct xol_area *__create_xol_area (unsigned long vaddr) {struct mm_struct *mm = current->mm; -uprobe_opcode_t insn =UPROBE_SWBP_INSN ; +unsigned long insns_size;struct xol_area *area; +void *insns; area =kmalloc (sizeof (*area),GFP_KERNEL );if (unlikely (!area))@@ -1502,7 +1511,8 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)/* Reserve the 1st slot for get_trampoline_vaddr() */ set_bit (0 , area->bitmap);atomic_set (&area->slot_count,1 ); -arch_uprobe_copy_ixol (area->pages[0 ],0 , &insn,UPROBE_SWBP_INSN_SIZE ); + insns =arch_uprobe_trampoline (&insns_size); +arch_uprobe_copy_ixol (area->pages[0 ],0 , insns, insns_size);if (!xol_add_vma (mm, area))return area;@@ -2123,7 +2133,7 @@ static struct return_instance *find_next_ret_chain(struct return_instance *ri)return ri; } -static void handle_trampoline (struct pt_regs *regs) +void uprobe_handle_trampoline (struct pt_regs *regs) {struct uprobe_task *utask;struct return_instance *ri, *next;@@ -2188,7 +2198,7 @@ static void handle_swbp(struct pt_regs *regs) bp_vaddr =uprobe_get_swbp_addr (regs);if (bp_vaddr ==get_trampoline_vaddr ()) -return handle_trampoline (regs); +return uprobe_handle_trampoline (regs); uprobe =find_active_uprobe (bp_vaddr, &is_swbp);if (!uprobe) {kernel/sys_ni.c +2
-0@@ -391,3 +391,5 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq); + +COND_SYSCALL (uretprobe);Signature · 1 line
2.44.0Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Oleg Nesterov
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Again, I leave this to you and Jiri, but
Quoted 10 lines · Masami Hiramatsu
On Wed, 3 Apr 2024 11:47:41 +0200
Quoted 5 lines
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
...
Quoted 3 lines
Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
We have sys_munmap(). should it check if the caller is going to unmap the code region which contains regs->ip and do nothing?
I don't think it should. Userspace should blame itself, SIGSEGV is not "too aggressive" in this case.
Quoted 1 line
I concern that new system calls introduce vulnerabilities.
Yes, we need to ensure that sys_uretprobe() can only damage the malicious caller and nothing else.
Oleg.
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Masami Hiramatsu (Google)Quoted 19 lines · Masami Hiramatsu
Quoted 15 lines · On Wed, 3 Apr 2024 11:47:41 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
Quoted 5 lines
Quoted 3 lines
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Hmm, I think a similar syscall is ptrace? prctl may also be a candidate.
I think both ptrace and prctl are for completely different use cases and it would be an abuse of existing API to reuse them for uretprobe tracing. Also, keep in mind, that any extra argument that has to be passed into this syscall means that we need to complicate and slow generated assembly code that is injected into user process (to save/restore registers) and also kernel-side (again, to deal with all the extra registers that would be stored/restored on stack).
Given syscalls are not some kind of scarce resources, what's the downside to have a dedicated and simple syscall?
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
This is the way it is today with uretprobes even through interrupt. E.g., it could happen that user process is using fibers and is replacing stack pointer without kernel realizing this, which will trigger some defensive checks in uretprobe handling code and kernel will send SIGILL because it can't support such cases. This is happening today already, and it works fine in practice (except for applications that manually change stack pointer, too bad, you can't trace them with uretprobes, unfortunately).
So I think it's absolutely adequate to have this behavior if the user process is *intentionally* abusing this API.
Quoted 5 lines
Quoted 4 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Quoted 9 lines
Why not limit? :) The
uprobe_handle_trampoline()expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.
As Oleg and Jiri mentioned, this syscall can't harm kernel or other processes, only the process that is abusing the API. So any extra checks that would slow down this approach is an unnecessary overhead and complication that will never be useful in practice.
Also note that sys_uretprobe is a kind of internal and unstable API and it is explicitly called out that its contract can change at any time and user space shouldn't rely on it. It's purely for the kernel's own usage.
So let's please keep it fast and simple.
Quoted 9 lines
Thank you,
Quoted 6 lines
thanks,
jirkaQuoted 1 line
[...]
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 34 lines · On Wed, 3 Apr 2024 09:58:12 -0700 Andrii Nakryiko <[email protected]>…
Quoted 19 lines · Masami Hiramatsu
Quoted 15 lines · On Wed, 3 Apr 2024 11:47:41 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
Quoted 5 lines
Quoted 3 lines
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Hmm, I think a similar syscall is ptrace? prctl may also be a candidate.
I think both ptrace and prctl are for completely different use cases and it would be an abuse of existing API to reuse them for uretprobe tracing. Also, keep in mind, that any extra argument that has to be passed into this syscall means that we need to complicate and slow generated assembly code that is injected into user process (to save/restore registers) and also kernel-side (again, to deal with all the extra registers that would be stored/restored on stack).
Quoted 2 lines
Given syscalls are not some kind of scarce resources, what's the downside to have a dedicated and simple syscall?
Syscalls are explicitly exposed to user space, thus, even if it is used ONLY for a very specific situation, it is an official kernel interface, and need to care about the compatibility. (If it causes SIGILL unless a specific use case, I don't know there is a "compatibility".) And the number of syscalls are limited resource.
I'm actually not sure how much we need to care of it, but adding a new syscall is worth to be discussed carefully because all of them are user-space compatibility.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
This is the way it is today with uretprobes even through interrupt.
I doubt that the interrupt (exception) and syscall should be handled differently. Especially, this exception is injected by uprobes but syscall will be caused by itself. But syscall can be called from user program (of couse this works as sys_kill(self, SIGILL)).
Quoted 7 lines
E.g., it could happen that user process is using fibers and is replacing stack pointer without kernel realizing this, which will trigger some defensive checks in uretprobe handling code and kernel will send SIGILL because it can't support such cases. This is happening today already, and it works fine in practice (except for applications that manually change stack pointer, too bad, you can't trace them with uretprobes, unfortunately).
OK, we at least need to document it.
Quoted 3 lines
So I think it's absolutely adequate to have this behavior if the user process is *intentionally* abusing this API.
Of course user expected that it is abusing. So at least we need to add a document that this syscall number is reserved to uprobes and user program must not use it.
Quoted 16 lines
Quoted 6 lines
Quoted 5 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Quoted 9 lines
Why not limit? :) The
uprobe_handle_trampoline()expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.
Quoted 4 lines
As Oleg and Jiri mentioned, this syscall can't harm kernel or other processes, only the process that is abusing the API. So any extra checks that would slow down this approach is an unnecessary overhead and complication that will never be useful in practice.
I think at least it should check the caller address to ensure the address is in the trampoline.
But anyway, uprobes itself can break the target process, so no one might care if this system call breaks the process now.
Quoted 5 lines
Also note that
sys_uretprobeis a kind of internal and unstable API and it is explicitly called out that its contract can change at any time and user space shouldn't rely on it. It's purely for the kernel's own usage.
Is that OK to use a syscall as "internal" and "unstable" API?
Quoted 8 lines
So let's please keep it fast and simple.
Quoted 4 lines
Thank you,
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
thanks,
jirkaQuoted 1 line
[...]
([OT] If we can add syscall so casually, I would like to add sys_traceevent for recording user space events :-) .)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 38 lines · Masami Hiramatsu
Quoted 34 lines · On Wed, 3 Apr 2024 09:58:12 -0700 Andrii Nakryiko <[email protected]>…
Quoted 19 lines · Masami Hiramatsu
Quoted 15 lines · On Wed, 3 Apr 2024 11:47:41 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
Quoted 5 lines
Quoted 3 lines
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Hmm, I think a similar syscall is ptrace? prctl may also be a candidate.
I think both ptrace and prctl are for completely different use cases and it would be an abuse of existing API to reuse them for uretprobe tracing. Also, keep in mind, that any extra argument that has to be passed into this syscall means that we need to complicate and slow generated assembly code that is injected into user process (to save/restore registers) and also kernel-side (again, to deal with all the extra registers that would be stored/restored on stack).
Quoted 7 lines
Quoted 2 lines
Given syscalls are not some kind of scarce resources, what's the downside to have a dedicated and simple syscall?
Syscalls are explicitly exposed to user space, thus, even if it is used ONLY for a very specific situation, it is an official kernel interface, and need to care about the compatibility. (If it causes SIGILL unless a specific use case, I don't know there is a "compatibility".)
Check rt_sigreturn syscall (manpage at [0], for example).
sigreturn() exists only to allow the implementation of signal handlers. It should never be called directly. (Indeed, a simple sigreturn() wrapper in the GNU C library simply returns -1, with errno set to ENOSYS.) Details of the arguments (if any) passed to sigreturn() vary depending on the architecture. (On some architectures, such as x86-64, sigreturn() takes no arguments, since all of the information that it requires is available in the stack frame that was previously created by the kernel on the user-space stack.)
This is a very similar use case. Also, check its source code in arch/x86/kernel/signal_64.c. It sends SIGSEGV to the calling process on any sign of something not being right. It's exactly the same with sys_uretprobe.
[0] https://man7.org/linux/man-pages/man2/sigreturn.2.html
Quoted 1 line
And the number of syscalls are limited resource.
We have almost 500 of them, it didn't seems like adding 1-2 for good reasons would be a problem. Can you please point to where the limits on syscalls as a resource are described? I'm curious to learn.
Quoted 4 lines
I'm actually not sure how much we need to care of it, but adding a new syscall is worth to be discussed carefully because all of them are user-space compatibility.
Absolutely, it's a good discussion to have.
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 10 lines
Quoted 2 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
Quoted 6 lines
Quoted 1 line
This is the way it is today with uretprobes even through interrupt.
I doubt that the interrupt (exception) and syscall should be handled differently. Especially, this exception is injected by uprobes but syscall will be caused by itself. But syscall can be called from user program (of couse this works as
sys_kill(self, SIGILL)).
Yep, I'd keep the behavior the same between uretprobes implemented through int3 and sys_uretprobe.
Quoted 10 lines
Quoted 8 lines
E.g., it could happen that user process is using fibers and is replacing stack pointer without kernel realizing this, which will trigger some defensive checks in uretprobe handling code and kernel will send SIGILL because it can't support such cases. This is happening today already, and it works fine in practice (except for applications that manually change stack pointer, too bad, you can't trace them with uretprobes, unfortunately).
OK, we at least need to document it.
+1, yep
Quoted 9 lines
Quoted 4 lines
So I think it's absolutely adequate to have this behavior if the user process is *intentionally* abusing this API.
Of course user expected that it is abusing. So at least we need to add a document that this syscall number is reserved to uprobes and user program must not use it.
Totally agree about documenting this.
Quoted 21 lines
Quoted 16 lines
Quoted 6 lines
Quoted 5 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Quoted 9 lines
Why not limit? :) The
uprobe_handle_trampoline()expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.Quoted 4 lines
As Oleg and Jiri mentioned, this syscall can't harm kernel or other processes, only the process that is abusing the API. So any extra checks that would slow down this approach is an unnecessary overhead and complication that will never be useful in practice.
Quoted 4 lines
I think at least it should check the caller address to ensure the address is in the trampoline.
But anyway, uprobes itself can break the target process, so no one might care if this system call breaks the process now.
If we already have an expected range of addresses, then I think it's fine to do a quick unlikely() check. I'd be more concerned if we need to do another lookup or search to just validate this. I'm sure Jiri will figure it out.
Quoted 8 lines
Quoted 6 lines
Also note that
sys_uretprobeis a kind of internal and unstable API and it is explicitly called out that its contract can change at any time and user space shouldn't rely on it. It's purely for the kernel's own usage.Is that OK to use a syscall as "internal" and "unstable" API?
See above about rt_sigreturn. It seems like yes, for some highly specialized syscalls it is the case already.
Quoted 15 lines
Quoted 15 lines
So let's please keep it fast and simple.
Quoted 9 lines
Thank you,
Quoted 6 lines
thanks,
jirkaQuoted 1 line
Quoted 5 lines
Quoted 1 line
[...]
([OT] If we can add syscall so casually, I would like to add
sys_traceeventfor recording user space events :-) .)
Have you proposed this upstream? :) I have no clue and no opinion about it...
Quoted 3 lines
--
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
On Wed, Apr 03, 2024 at 07:00:07PM -0700, Andrii Nakryiko wrote:
SNIP
Quoted 49 lines
Check
rt_sigreturnsyscall (manpage at [0], for example).
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. (Indeed, a simplesigreturn()wrapper in the GNU C library simply returns -1, with errno set to ENOSYS.) Details of the arguments (if any) passed tosigreturn()vary depending on the architecture. (On some architectures, such as x86-64,sigreturn()takes no arguments, since all of the information that it requires is available in the stack frame that was previously created by the kernel on the user-space stack.)This is a very similar use case. Also, check its source code in
arch/x86/kernel/signal_64.c. It sends SIGSEGV to the calling process on any sign of something not being right. It's exactly the same withsys_uretprobe.[0] https://man7.org/linux/man-pages/man2/sigreturn.2.html
Quoted 1 line
And the number of syscalls are limited resource.
We have almost 500 of them, it didn't seems like adding 1-2 for good reasons would be a problem. Can you please point to where the limits on syscalls as a resource are described? I'm curious to learn.
Quoted 4 lines
I'm actually not sure how much we need to care of it, but adding a new syscall is worth to be discussed carefully because all of them are user-space compatibility.
Absolutely, it's a good discussion to have.
Quoted 17 lines
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
This is the way it is today with uretprobes even through interrupt.
Quoted 7 lines
Quoted 4 lines
I doubt that the interrupt (exception) and syscall should be handled differently. Especially, this exception is injected by uprobes but syscall will be caused by itself. But syscall can be called from user program (of couse this works as
sys_kill(self, SIGILL)).Yep, I'd keep the behavior the same between uretprobes implemented through int3 and
sys_uretprobe.
+1
Quoted 19 lines
Quoted 11 lines
Quoted 9 lines
E.g., it could happen that user process is using fibers and is replacing stack pointer without kernel realizing this, which will trigger some defensive checks in uretprobe handling code and kernel will send SIGILL because it can't support such cases. This is happening today already, and it works fine in practice (except for applications that manually change stack pointer, too bad, you can't trace them with uretprobes, unfortunately).
OK, we at least need to document it.
+1, yep
Quoted 5 lines
Quoted 4 lines
So I think it's absolutely adequate to have this behavior if the user process is *intentionally* abusing this API.
Quoted 6 lines
Quoted 4 lines
Of course user expected that it is abusing. So at least we need to add a document that this syscall number is reserved to uprobes and user program must not use it.
Totally agree about documenting this.
ok there's map page on sigreturn.. do you think we should add man page for uretprobe or you can think of some other place to document it?
Quoted 27 lines
Quoted 22 lines
Quoted 17 lines
Quoted 7 lines
Quoted 6 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Quoted 9 lines
Why not limit? :) The
uprobe_handle_trampoline()expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.Quoted 4 lines
As Oleg and Jiri mentioned, this syscall can't harm kernel or other processes, only the process that is abusing the API. So any extra checks that would slow down this approach is an unnecessary overhead and complication that will never be useful in practice.
Quoted 4 lines
I think at least it should check the caller address to ensure the address is in the trampoline.
But anyway, uprobes itself can break the target process, so no one might care if this system call breaks the process now.
Quoted 4 lines
If we already have an expected range of addresses, then I think it's fine to do a quick
unlikely()check. I'd be more concerned if we need to do another lookup or search to just validate this. I'm sure Jiri will figure it out.
Oleg mentioned the trampoline address check could race with another thread's mremap call, however trap is using that check as well, it still seems like good idea to do it also in the uretprobe syscall
jirka
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 87 lines · On Thu, 4 Apr 2024 13:58:43 +0200 Jiri Olsa <[email protected]> wrote:
On Wed, Apr 03, 2024 at 07:00:07PM -0700, Andrii Nakryiko wrote:
SNIP
Quoted 49 lines
Check
rt_sigreturnsyscall (manpage at [0], for example).
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. (Indeed, a simplesigreturn()wrapper in the GNU C library simply returns -1, with errno set to ENOSYS.) Details of the arguments (if any) passed tosigreturn()vary depending on the architecture. (On some architectures, such as x86-64,sigreturn()takes no arguments, since all of the information that it requires is available in the stack frame that was previously created by the kernel on the user-space stack.)This is a very similar use case. Also, check its source code in
arch/x86/kernel/signal_64.c. It sends SIGSEGV to the calling process on any sign of something not being right. It's exactly the same withsys_uretprobe.[0] https://man7.org/linux/man-pages/man2/sigreturn.2.html
Quoted 1 line
And the number of syscalls are limited resource.
We have almost 500 of them, it didn't seems like adding 1-2 for good reasons would be a problem. Can you please point to where the limits on syscalls as a resource are described? I'm curious to learn.
Quoted 4 lines
I'm actually not sure how much we need to care of it, but adding a new syscall is worth to be discussed carefully because all of them are user-space compatibility.
Absolutely, it's a good discussion to have.
Quoted 17 lines
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
This is the way it is today with uretprobes even through interrupt.
Quoted 7 lines
Quoted 4 lines
I doubt that the interrupt (exception) and syscall should be handled differently. Especially, this exception is injected by uprobes but syscall will be caused by itself. But syscall can be called from user program (of couse this works as
sys_kill(self, SIGILL)).Yep, I'd keep the behavior the same between uretprobes implemented through int3 and
sys_uretprobe.+1
Quoted 24 lines
Quoted 11 lines
Quoted 9 lines
E.g., it could happen that user process is using fibers and is replacing stack pointer without kernel realizing this, which will trigger some defensive checks in uretprobe handling code and kernel will send SIGILL because it can't support such cases. This is happening today already, and it works fine in practice (except for applications that manually change stack pointer, too bad, you can't trace them with uretprobes, unfortunately).
OK, we at least need to document it.
+1, yep
Quoted 9 lines
Quoted 4 lines
So I think it's absolutely adequate to have this behavior if the user process is *intentionally* abusing this API.
Of course user expected that it is abusing. So at least we need to add a document that this syscall number is reserved to uprobes and user program must not use it.
Quoted 4 lines
Quoted 1 line
Totally agree about documenting this.
ok there's map page on sigreturn.. do you think we should add man page for uretprobe or you can think of some other place to document it?
I think it is better to have a man-page. Anyway, to discuss and explain this syscall, the man-page is a good format to describe it.
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 18 lines
Quoted 8 lines
Quoted 7 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Quoted 9 lines
Why not limit? :) The
uprobe_handle_trampoline()expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.Quoted 4 lines
As Oleg and Jiri mentioned, this syscall can't harm kernel or other processes, only the process that is abusing the API. So any extra checks that would slow down this approach is an unnecessary overhead and complication that will never be useful in practice.
I think at least it should check the caller address to ensure the address is in the trampoline.
But anyway, uprobes itself can break the target process, so no one might care if this system call breaks the process now.
Quoted 8 lines
Quoted 4 lines
If we already have an expected range of addresses, then I think it's fine to do a quick
unlikely()check. I'd be more concerned if we need to do another lookup or search to just validate this. I'm sure Jiri will figure it out.Oleg mentioned the trampoline address check could race with another thread's mremap call, however trap is using that check as well, it still seems like good idea to do it also in the uretprobe syscall
Yeah, and also, can we add a stack pointer check if the trampoline is shared with other probe points?
Thank you,
Quoted 2 lines
jirka
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Andrii NakryikoQuoted 59 lines · On Wed, 3 Apr 2024 19:00:07 -0700 Andrii Nakryiko <[email protected]>…
Quoted 38 lines · Masami Hiramatsu
Quoted 34 lines · On Wed, 3 Apr 2024 09:58:12 -0700 Andrii Nakryiko <[email protected]>…
Quoted 19 lines · Masami Hiramatsu
Quoted 15 lines · On Wed, 3 Apr 2024 11:47:41 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu
Hi Jiri,
Quoted 1 line · On Tue, 2 Apr 2024 11:33:00 +0200 Jiri Olsa <[email protected]> wrote:
Adding uretprobe syscall instead of trap to speed up return probe.
This is interesting approach. But I doubt we need to add additional syscall just for this purpose. Can't we use another syscall or ioctl?
so the plan is to optimize entry uprobe in a similar way and given the syscall is not a scarce resource I wanted to add another syscall for that one as well
Quoted 5 lines
Quoted 3 lines
tbh I'm not sure sure which syscall or ioctl to reuse for this, it's possible to do that, the trampoline will just have to save one or more additional registers, but adding new syscall seems cleaner to me
Hmm, I think a similar syscall is ptrace? prctl may also be a candidate.
I think both ptrace and prctl are for completely different use cases and it would be an abuse of existing API to reuse them for uretprobe tracing. Also, keep in mind, that any extra argument that has to be passed into this syscall means that we need to complicate and slow generated assembly code that is injected into user process (to save/restore registers) and also kernel-side (again, to deal with all the extra registers that would be stored/restored on stack).
Quoted 7 lines
Quoted 2 lines
Given syscalls are not some kind of scarce resources, what's the downside to have a dedicated and simple syscall?
Syscalls are explicitly exposed to user space, thus, even if it is used ONLY for a very specific situation, it is an official kernel interface, and need to care about the compatibility. (If it causes SIGILL unless a specific use case, I don't know there is a "compatibility".)
Check
rt_sigreturnsyscall (manpage at [0], for example).
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. (Indeed, a simplesigreturn()wrapper in the GNU C library simply returns -1, with errno set to ENOSYS.) Details of the arguments (if any) passed tosigreturn()vary depending on the architecture. (On some architectures, such as x86-64,sigreturn()takes no arguments, since all of the information that it requires is available in the stack frame that was previously created by the kernel on the user-space stack.)
Quoted 6 lines
This is a very similar use case. Also, check its source code in
arch/x86/kernel/signal_64.c. It sends SIGSEGV to the calling process on any sign of something not being right. It's exactly the same withsys_uretprobe.
Thanks for a good example.
Hm, in the case of rt_sigreturn, it has no other way to do it so it needs to use syscall. OTOH, sys_uretprobe is only for performance optimization, and the performance may depend on the architecture.
Quoted 5 lines
Quoted 1 line
And the number of syscalls are limited resource.
We have almost 500 of them, it didn't seems like adding 1-2 for good reasons would be a problem. Can you please point to where the limits on syscalls as a resource are described? I'm curious to learn.
Syscall table is compiled as a fixed array, so if we increase the number, we need more tables. Of course this just increase 1 entry and at least for x86 we already allocated bigger table, so it is OK. But I'm just afraid if we can add more syscalls without any clear rules, we may fill the tables with more specific syscalls.
Ah, we also should follow this document.
https://docs.kernel.org/process/adding-syscalls.html
Let me Cc [email protected].
Quoted 6 lines
Quoted 4 lines
I'm actually not sure how much we need to care of it, but adding a new syscall is worth to be discussed carefully because all of them are user-space compatibility.
Absolutely, it's a good discussion to have.
Thanks, if this is discussed enough and agreed from other maintainers, I can safely pick this on my tree.
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Also, we should run syzkaller on this syscall. And if uretprobe is
right, I'll check on syzkaller
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
set in the user function, what happen if the user function directly calls this syscall? (maybe it consumes shadow stack?)
the process should receive SIGILL if there's no pending uretprobe for the current task, or it will trigger uretprobe if there's one pending
No, that is too aggressive and not safe. Since the syscall is exposed to user program, it should return appropriate error code instead of SIGILL.
This is the way it is today with uretprobes even through interrupt.
Quoted 7 lines
Quoted 4 lines
I doubt that the interrupt (exception) and syscall should be handled differently. Especially, this exception is injected by uprobes but syscall will be caused by itself. But syscall can be called from user program (of couse this works as
sys_kill(self, SIGILL)).Yep, I'd keep the behavior the same between uretprobes implemented through int3 and
sys_uretprobe.
OK, so this syscall is something like coding int3 without debugger.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
E.g., it could happen that user process is using fibers and is replacing stack pointer without kernel realizing this, which will trigger some defensive checks in uretprobe handling code and kernel will send SIGILL because it can't support such cases. This is happening today already, and it works fine in practice (except for applications that manually change stack pointer, too bad, you can't trace them with uretprobes, unfortunately).
Quoted 3 lines
Quoted 1 line
OK, we at least need to document it.
+1, yep
Can we make this syscall and uprobe behavior clearer? As you said, if the application use sigreturn or longjump, it may skip returns and shadow stack entries are left in the kernel. In such cases, can uretprobe detect it properly, or just crash the process (or process runs wrongly)?
Quoted 39 lines
Quoted 10 lines
Quoted 5 lines
So I think it's absolutely adequate to have this behavior if the user process is *intentionally* abusing this API.
Of course user expected that it is abusing. So at least we need to add a document that this syscall number is reserved to uprobes and user program must not use it.
Totally agree about documenting this.
Quoted 21 lines
Quoted 16 lines
Quoted 6 lines
Quoted 5 lines
but we could limit the syscall to be executed just from the trampoline, that should prevent all the user space use cases, I'll do that in next version and add more tests for that
Quoted 9 lines
Why not limit? :) The
uprobe_handle_trampoline()expects it is called only from the trampoline, so it is natural to check the caller address. (and uprobe should know where is the trampoline)Since the syscall is always exposed to the user program, it should
- Do nothing and return an error unless it is properly called.
- check the prerequisites for operation strictly.
I concern that new system calls introduce vulnerabilities.Quoted 4 lines
As Oleg and Jiri mentioned, this syscall can't harm kernel or other processes, only the process that is abusing the API. So any extra checks that would slow down this approach is an unnecessary overhead and complication that will never be useful in practice.
Quoted 4 lines
I think at least it should check the caller address to ensure the address is in the trampoline.
But anyway, uprobes itself can break the target process, so no one might care if this system call breaks the process now.
Quoted 4 lines
If we already have an expected range of addresses, then I think it's fine to do a quick
unlikely()check. I'd be more concerned if we need to do another lookup or search to just validate this. I'm sure Jiri will figure it out.
Good.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Also note that
sys_uretprobeis a kind of internal and unstable API and it is explicitly called out that its contract can change at any time and user space shouldn't rely on it. It's purely for the kernel's own usage.
Quoted 4 lines
Quoted 1 line
Is that OK to use a syscall as "internal" and "unstable" API?
See above about
rt_sigreturn. It seems like yes, for some highly specialized syscalls it is the case already.
OK, but as I said it is just for performance optimization, that is a bit different from rt_sigreturn case.
Thank you,
Quoted 26 lines
Quoted 15 lines
Quoted 15 lines
So let's please keep it fast and simple.
Quoted 9 lines
Thank you,
Quoted 6 lines
thanks,
jirkaQuoted 1 line
Quoted 5 lines
Quoted 1 line
[...]
([OT] If we can add syscall so casually, I would like to add
sys_traceeventfor recording user space events :-) .)Have you proposed this upstream? :) I have no clue and no opinion about it...
Quoted 3 lines
--
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Oleg Nesterov
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 5 lines · Masami Hiramatsu
Can we make this syscall and uprobe behavior clearer? As you said, if the application use sigreturn or longjump, it may skip returns and shadow stack entries are left in the kernel. In such cases, can uretprobe detect it properly, or just crash the process (or process runs wrongly)?
Please see the comment in handle_trampoline(), it tries to detect this case. This patch should not make any difference.
Oleg.
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 9 lines · On Thu, 4 Apr 2024 18:11:09 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 5 lines · Masami Hiramatsu
Can we make this syscall and uprobe behavior clearer? As you said, if the application use sigreturn or longjump, it may skip returns and shadow stack entries are left in the kernel. In such cases, can uretprobe detect it properly, or just crash the process (or process runs wrongly)?
Please see the comment in
handle_trampoline(), it tries to detect this case. This patch should not make any difference.
I think you mean this loop will skip and discard the stacked return_instance to find the valid one.
----
do {
/*
* We should throw out the frames invalidated by longjmp().
* If this chain is valid, then the next one should be alive
* or NULL; the latter case means that nobody but ri->func
* could hit this trampoline on return. TODO: sigaltstack().
*/
next = find_next_ret_chain(ri);
valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
instruction_pointer_set(regs, ri->orig_ret_vaddr);
do {
if (valid)
handle_uretprobe_chain(ri, regs);
ri = free_ret_instance(ri);
utask->depth--;
} while (ri != next);
} while (!valid);----
I think this expects setjmp/longjmp as below
foo() { <- retprobe1
setjmp()
bar() { <- retprobe2
longjmp()
}
} <- return to trampolineIn this case, we need to skip retprobe2's instance.
My concern is, if we can not find appropriate return instance, what happen? e.g.
foo() { <-- retprobe1
bar() { # sp is decremented
sys_uretprobe() <-- ??
}
}It seems sys_uretprobe() will handle retprobe1 at that point instead of SIGILL.
Can we avoid this with below strict check?
if (ri->stack != regs->sp + expected_offset)
goto sigill;
expected_offset should be 16 (push * 3 - ret) on x64 if we ri->stack is the regs->sp right after call.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 50 lines · Masami Hiramatsu
Quoted 9 lines · On Thu, 4 Apr 2024 18:11:09 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 5 lines · Masami Hiramatsu
Can we make this syscall and uprobe behavior clearer? As you said, if the application use sigreturn or longjump, it may skip returns and shadow stack entries are left in the kernel. In such cases, can uretprobe detect it properly, or just crash the process (or process runs wrongly)?
Please see the comment in
handle_trampoline(), it tries to detect this case. This patch should not make any difference.I think you mean this loop will skip and discard the stacked
return_instanceto find the valid one.----
do { /* * We should throw out the frames invalidated by longjmp(). * If this chain is valid, then the next one should be alive * or NULL; the latter case means that nobody but ri->func * could hit this trampoline on return. TODO: sigaltstack(). */ next = find_next_ret_chain(ri); valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs); instruction_pointer_set(regs, ri->orig_ret_vaddr); do { if (valid) handle_uretprobe_chain(ri, regs); ri = free_ret_instance(ri); utask->depth--; } while (ri != next); } while (!valid);----
I think this expects setjmp/longjmp as below
foo(){ <- retprobe1setjmp() bar() { <- retprobe2 longjmp() } } <- return to trampolineIn this case, we need to skip retprobe2's instance.
My concern is, if we can not find appropriate return instance, what happen? e.g.
Quoted 8 lines
foo(){ <-- retprobe1
bar(){ # sp is decrementedsys_uretprobe() <-- ?? } }It seems
sys_uretprobe()will handle retprobe1 at that point instead of SIGILL.
yes, and I think it's fine, you get the consumer called in wrong place, but it's your fault and kernel won't crash
this can be fixed by checking the syscall is called from the trampoline and prevent handle_trampoline call if it's not
Quoted 5 lines
Can we avoid this with below strict check?
if (
ri->stack!=regs->sp+expected_offset)goto sigill;
hm the current uprobe 'alive' check makes sure the return_instance is above or at the same stack address, not sure we can match it exactly, need to think about that more
Quoted 3 lines
expected_offsetshould be 16 (push * 3 - ret) on x64 if weri->stackis theregs->spright after call.
the syscall trampoline already updates the regs->sp before calling handle_trampoline
regs->sp += sizeof(r11_cx_ax);
jirka
Oleg Nesterov
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 5 lines · Jiri Olsa
Quoted 3 lines · Masami Hiramatsu
I think this expects setjmp/longjmp as below
Quoted 8 lines
Quoted 8 lines
foo(){ <- retprobe1setjmp() bar() { <- retprobe2 longjmp() } } <- return to trampolineIn this case, we need to skip retprobe2's instance.
Yes,
Quoted 9 lines
Quoted 9 lines
My concern is, if we can not find appropriate return instance, what happen? e.g.
foo(){ <-- retprobe1
bar(){ # sp is decrementedsys_uretprobe() <-- ?? } }
Quoted 5 lines
Quoted 2 lines
It seems
sys_uretprobe()will handle retprobe1 at that point instead of SIGILL.yes, and I think it's fine, you get the consumer called in wrong place, but it's your fault and kernel won't crash
Agreed.
With or without this patch userpace can also do
foo() { <-- retprobe1
bar() {
jump to xol_area
}
}handle_trampoline() will handle retprobe1.
Quoted 2 lines
this can be fixed by checking the syscall is called from the trampoline and prevent
handle_trampolinecall if it's not
Yes, but I still do not think this makes a lot of sense. But I won't argue.
And what should sys_uretprobe() do if it is not called from the trampoline? I'd prefer force_sig(SIGILL) to punish the abuser ;) OK, OK, EINVAL.
I agree very much with Andrii,
sigreturn() exists only to allow the implementation of signal handlers. It should never be called directly. Details of the arguments (if any) passed to sigreturn() vary depending on the architecture.
this is how sys_uretprobe() should be treated/documented.
sigreturn() can be "improved" too. Say, it could validate sigcontext->ip and return -EINVAL if this addr is not valid. But why?
Oleg.
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 36 lines · On Fri, 5 Apr 2024 13:02:30 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 5 lines · Jiri Olsa
Quoted 3 lines · Masami Hiramatsu
I think this expects setjmp/longjmp as below
Quoted 8 lines
Quoted 8 lines
foo(){ <- retprobe1setjmp() bar() { <- retprobe2 longjmp() } } <- return to trampolineIn this case, we need to skip retprobe2's instance.
Yes,
Quoted 9 lines
Quoted 9 lines
My concern is, if we can not find appropriate return instance, what happen? e.g.
foo(){ <-- retprobe1
bar(){ # sp is decrementedsys_uretprobe() <-- ?? } }Quoted 5 lines
Quoted 2 lines
It seems
sys_uretprobe()will handle retprobe1 at that point instead of SIGILL.yes, and I think it's fine, you get the consumer called in wrong place, but it's your fault and kernel won't crash
Agreed.
With or without this patch userpace can also do
Quoted 7 lines
foo() { <-- retprobe1 bar() { jump to xol_area } }
handle_trampoline()will handle retprobe1.
This is OK because the execution path has been changed to trampoline, but the above will continue running bar() after sys_uretprobe().
Quoted 9 lines
Quoted 3 lines
this can be fixed by checking the syscall is called from the trampoline and prevent
handle_trampolinecall if it's notYes, but I still do not think this makes a lot of sense. But I won't argue.
And what should
sys_uretprobe()do if it is not called from the trampoline? I'd preferforce_sig(SIGILL)to punish the abuser ;) OK, OK, EINVAL.
Quoted 7 lines
I agree very much with Andrii,
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. Details of the arguments (if any) passed tosigreturn()vary depending on the architecture.this is how
sys_uretprobe()should be treated/documented.
OK.
Quoted 3 lines
sigreturn()can be "improved" too. Say, it could validatesigcontext->ipand return-EINVALif this addr is not valid. But why?
Because sigreturn() never returns, but sys_uretprobe() will return. If it changes the regs->ip and directly returns to the original return address, I think it is natural to send SIGILL.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Oleg Nesterov
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 12 lines · Masami Hiramatsu
Quoted 8 lines · On Fri, 5 Apr 2024 13:02:30 +0200 Oleg Nesterov <[email protected]> wrote:
With or without this patch userpace can also do
foo() { <-- retprobe1 bar() { jump to xol_area } }
Quoted 3 lines
Quoted 1 line
handle_trampoline()will handle retprobe1.This is OK because the execution path has been changed to trampoline,
Agreed, in this case the misuse is more clear. But please see below.
Quoted 1 line
but the above will continue running
bar()aftersys_uretprobe().
... and most probably crash
Quoted 4 lines
Quoted 2 lines
sigreturn()can be "improved" too. Say, it could validatesigcontext->ipand return-EINVALif this addr is not valid. But why?Because
sigreturn()never returns, butsys_uretprobe()will return.
You mean, sys_uretprobe() returns to the next insn after syscall.
Almost certainly yes, but this is not necessarily true. If one of consumers changes regs->sp sys_uretprobe() "returns" to another location, just like sys_rt_sigreturn().
That said.
Masami, it is not that I am trying to prove that you are "wrong" ;) No.
I see your points even if I am biased, I understand that my objections are not 100% "fair".
I am just trying to explain why, rightly or not, I care much less about the abuse of sys_uretprobe().
Thanks!
Oleg.
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 15 lines · On Sat, 6 Apr 2024 19:55:59 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 14 lines · Masami Hiramatsu
Quoted 9 lines · On Fri, 5 Apr 2024 13:02:30 +0200 Oleg Nesterov <[email protected]> wrote:
With or without this patch userpace can also do
foo() { <-- retprobe1 bar() { jump to xol_area } }
handle_trampoline()will handle retprobe1.
Quoted 7 lines
Quoted 1 line
This is OK because the execution path has been changed to trampoline,
Agreed, in this case the misuse is more clear. But please see below.
Quoted 1 line
but the above will continue running
bar()aftersys_uretprobe()... and most probably crash
Yes, unless it returns with longjmp(). (but this is rare case and maybe malicious program.)
Quoted 14 lines
Quoted 5 lines
Quoted 3 lines
sigreturn()can be "improved" too. Say, it could validatesigcontext->ipand return-EINVALif this addr is not valid. But why?Because
sigreturn()never returns, butsys_uretprobe()will return.You mean,
sys_uretprobe()returns to the next insn after syscall.Almost certainly yes, but this is not necessarily true. If one of consumers changes
regs->spsys_uretprobe()"returns" to another location, just likesys_rt_sigreturn().That said.
Quoted 7 lines
Masami, it is not that I am trying to prove that you are "wrong" ;) No.
I see your points even if I am biased, I understand that my objections are not 100% "fair".
I am just trying to explain why, rightly or not, I care much less about the abuse of
sys_uretprobe().
I would like to clear that the abuse of this syscall will not possible to harm the normal programs, and even if it is used by malicious code (e.g. injected by stack overflow) it doesn't cause a problem. At least thsese points are cleared, and documented. it is easier to push it as new Linux API.
Thank you,
Quoted 6 lines
Thanks!
Oleg.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Oleg NesterovQuoted 44 lines · Oleg Nesterov
Quoted 5 lines · Jiri Olsa
Quoted 3 lines · Masami Hiramatsu
I think this expects setjmp/longjmp as below
Quoted 8 lines
Quoted 8 lines
foo(){ <- retprobe1setjmp() bar() { <- retprobe2 longjmp() } } <- return to trampolineIn this case, we need to skip retprobe2's instance.
Yes,
Quoted 9 lines
Quoted 9 lines
My concern is, if we can not find appropriate return instance, what happen? e.g.
foo(){ <-- retprobe1
bar(){ # sp is decrementedsys_uretprobe() <-- ?? } }Quoted 5 lines
Quoted 2 lines
It seems
sys_uretprobe()will handle retprobe1 at that point instead of SIGILL.yes, and I think it's fine, you get the consumer called in wrong place, but it's your fault and kernel won't crash
Agreed.
With or without this patch userpace can also do
foo() { <-- retprobe1 bar() { jump to xol_area } }
handle_trampoline()will handle retprobe1.
Quoted 7 lines
Quoted 2 lines
this can be fixed by checking the syscall is called from the trampoline and prevent
handle_trampolinecall if it's notYes, but I still do not think this makes a lot of sense. But I won't argue.
And what should
sys_uretprobe()do if it is not called from the trampoline? I'd preferforce_sig(SIGILL)to punish the abuser ;) OK, OK, EINVAL.
so the similar behaviour with int3 ends up with immediate SIGTRAP and not invoking pending uretprobe consumers, like:
- setup uretprobe for foo
- foo() {
executes int 3 -> sends SIGTRAP
}because the int3 handler checks if it got executed from the uretprobe's trampoline.. if not it treats that int3 as regular trap
while for uretprobe syscall we have at the moment following behaviour:
- setup uretprobe for foo
- foo() {
uretprobe_syscall -> executes foo's uretprobe consumers
} - at some point we get to the 'ret' instruction that jump into uretprobe trampoline and the uretprobe_syscall won't find pending uretprobe and will send SIGILL
so I think we should mimic int3 behaviour and:
- setup uretprobe for foo
- foo() {
uretprobe_syscall -> check if we got executed from uretprobe's trampoline and send SIGILL if that's not the case
I think it's better to have the offending process killed right away, rather than having more undefined behaviour, waiting for final 'ret' instruction that jumps to uretprobe trampoline and causes SIGILL
Quoted 8 lines
I agree very much with Andrii,
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. Details of the arguments (if any) passed tosigreturn()vary depending on the architecture.this is how
sys_uretprobe()should be treated/documented.
yes, will include man page patch in new version
jirka
Quoted 6 lines
sigreturn()can be "improved" too. Say, it could validatesigcontext->ipand return-EINVALif this addr is not valid. But why?Oleg.
Oleg Nesterov
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 9 lines · Jiri Olsa
Quoted 3 lines · Oleg Nesterov
And what should
sys_uretprobe()do if it is not called from the trampoline? I'd preferforce_sig(SIGILL)to punish the abuser ;) OK, OK, EINVAL.so the similar behaviour with int3 ends up with immediate SIGTRAP and not invoking pending uretprobe consumers, like:
Quoted 7 lines
- setup uretprobe for foo
- foo() { executes int 3 -> sends SIGTRAP }because the int3 handler checks if it got executed from the uretprobe's trampoline.
... or the task has uprobe at this address
Quoted 1 line
if not it treats that int3 as regular trap
Yes this mimics the "default" behaviour without uprobes/uretprobes
Quoted 6 lines
so I think we should mimic int3 behaviour and:
- setup uretprobe for foo
-foo(){
uretprobe_syscall-> check if we got executed from uretprobe's trampoline and send SIGILL if that's not the case
Agreed,
Quoted 3 lines
I think it's better to have the offending process killed right away, rather than having more undefined behaviour, waiting for final 'ret' instruction that jumps to uretprobe trampoline and causes SIGILL
Agreed. In fact I think it should be also killed if copy_to/from_user() fails by the same reason.
Oleg.
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 31 lines · Oleg Nesterov
Quoted 9 lines · Jiri Olsa
Quoted 3 lines · Oleg Nesterov
And what should
sys_uretprobe()do if it is not called from the trampoline? I'd preferforce_sig(SIGILL)to punish the abuser ;) OK, OK, EINVAL.so the similar behaviour with int3 ends up with immediate SIGTRAP and not invoking pending uretprobe consumers, like:
Quoted 7 lines
- setup uretprobe for foo
- foo() { executes int 3 -> sends SIGTRAP }because the int3 handler checks if it got executed from the uretprobe's trampoline.
... or the task has uprobe at this address
Quoted 1 line
if not it treats that int3 as regular trap
Yes this mimics the "default" behaviour without uprobes/uretprobes
Quoted 6 lines
so I think we should mimic int3 behaviour and:
- setup uretprobe for foo
-foo(){
uretprobe_syscall-> check if we got executed from uretprobe's trampoline and send SIGILL if that's not the case
Quoted 8 lines
Agreed,
Quoted 3 lines
I think it's better to have the offending process killed right away, rather than having more undefined behaviour, waiting for final 'ret' instruction that jumps to uretprobe trampoline and causes SIGILL
Agreed. In fact I think it should be also killed if copy_to/from_user() fails by the same reason.
+1 makes sense
jirka
Quoted 3 lines
Oleg.
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Jiri OlsaQuoted 56 lines · On Mon, 8 Apr 2024 18:02:13 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 44 lines · Oleg Nesterov
Quoted 5 lines · Jiri Olsa
Quoted 3 lines · Masami Hiramatsu
I think this expects setjmp/longjmp as below
Quoted 8 lines
Quoted 8 lines
foo(){ <- retprobe1setjmp() bar() { <- retprobe2 longjmp() } } <- return to trampolineIn this case, we need to skip retprobe2's instance.
Yes,
Quoted 9 lines
Quoted 9 lines
My concern is, if we can not find appropriate return instance, what happen? e.g.
foo(){ <-- retprobe1
bar(){ # sp is decrementedsys_uretprobe() <-- ?? } }Quoted 5 lines
Quoted 2 lines
It seems
sys_uretprobe()will handle retprobe1 at that point instead of SIGILL.yes, and I think it's fine, you get the consumer called in wrong place, but it's your fault and kernel won't crash
Agreed.
With or without this patch userpace can also do
foo() { <-- retprobe1 bar() { jump to xol_area } }
handle_trampoline()will handle retprobe1.Quoted 7 lines
Quoted 2 lines
this can be fixed by checking the syscall is called from the trampoline and prevent
handle_trampolinecall if it's notYes, but I still do not think this makes a lot of sense. But I won't argue.
And what should
sys_uretprobe()do if it is not called from the trampoline? I'd preferforce_sig(SIGILL)to punish the abuser ;) OK, OK, EINVAL.so the similar behaviour with int3 ends up with immediate SIGTRAP and not invoking pending uretprobe consumers, like:
Quoted 7 lines
- setup uretprobe for foo
- foo() { executes int 3 -> sends SIGTRAP }because the int3 handler checks if it got executed from the uretprobe's trampoline.. if not it treats that int3 as regular trap
Yeah, that is consistent behavior. Sounds good to me.
Quoted 12 lines
while for uretprobe syscall we have at the moment following behaviour:
- setup uretprobe for foo
- foo() { uretprobe_syscall -> executes foo's uretprobe consumers }- at some point we get to the 'ret' instruction that jump into uretprobe trampoline and the
uretprobe_syscallwon't find pending uretprobe and will send SIGILL
Quoted 6 lines
so I think we should mimic int3 behaviour and:
- setup uretprobe for foo
-foo(){
uretprobe_syscall-> check if we got executed from uretprobe's trampoline and send SIGILL if that's not the case
OK, this looks good to me.
Quoted 8 lines
I think it's better to have the offending process killed right away, rather than having more undefined behaviour, waiting for final 'ret' instruction that jumps to uretprobe trampoline and causes SIGILL
Quoted 3 lines
I agree very much with Andrii,
Quoted 7 lines
Quoted 5 lines
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. Details of the arguments (if any) passed tosigreturn()vary depending on the architecture.this is how
sys_uretprobe()should be treated/documented.yes, will include man page patch in new version
And please follow Documentation/process/adding-syscalls.rst in new version, then we can avoid repeating the same discussion :-)
Thank you!
Quoted 9 lines
jirka
Quoted 6 lines
sigreturn()can be "improved" too. Say, it could validatesigcontext->ipand return-EINVALif this addr is not valid. But why?Oleg.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
On Tue, Apr 09, 2024 at 09:34:39AM +0900, Masami Hiramatsu wrote:
SNIP
Quoted 55 lines
Quoted 12 lines
Quoted 8 lines
Quoted 3 lines
this can be fixed by checking the syscall is called from the trampoline and prevent
handle_trampolinecall if it's notYes, but I still do not think this makes a lot of sense. But I won't argue.
And what should
sys_uretprobe()do if it is not called from the trampoline? I'd preferforce_sig(SIGILL)to punish the abuser ;) OK, OK, EINVAL.so the similar behaviour with int3 ends up with immediate SIGTRAP and not invoking pending uretprobe consumers, like:
Quoted 7 lines
- setup uretprobe for foo
- foo() { executes int 3 -> sends SIGTRAP }because the int3 handler checks if it got executed from the uretprobe's trampoline.. if not it treats that int3 as regular trap
Yeah, that is consistent behavior. Sounds good to me.
Quoted 12 lines
while for uretprobe syscall we have at the moment following behaviour:
- setup uretprobe for foo
- foo() { uretprobe_syscall -> executes foo's uretprobe consumers }- at some point we get to the 'ret' instruction that jump into uretprobe trampoline and the
uretprobe_syscallwon't find pending uretprobe and will send SIGILLQuoted 6 lines
so I think we should mimic int3 behaviour and:
- setup uretprobe for foo
-foo(){
uretprobe_syscall-> check if we got executed from uretprobe's trampoline and send SIGILL if that's not the caseOK, this looks good to me.
Quoted 12 lines
I think it's better to have the offending process killed right away, rather than having more undefined behaviour, waiting for final 'ret' instruction that jumps to uretprobe trampoline and causes SIGILL
Quoted 7 lines
I agree very much with Andrii,
sigreturn()exists only to allow the implementation of signal handlers. It should never be called directly. Details of the arguments (if any) passed tosigreturn()vary depending on the architecture.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
this is how
sys_uretprobe()should be treated/documented.yes, will include man page patch in new version
And please follow
Documentation/process/adding-syscalls.rstin new version, then we can avoid repeating the same discussion :-)
yep, will do
thanks,
jirka
Masami Hiramatsu (Google)
maintainerRe: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Jiri OlsaQuoted 11 lines · On Fri, 5 Apr 2024 10:56:15 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 5 lines
Can we avoid this with below strict check?
if (
ri->stack!=regs->sp+expected_offset)goto sigill;hm the current uprobe 'alive' check makes sure the
return_instanceis above or at the same stack address, not sure we can match it exactly, need to think about that moreQuoted 1 line
Quoted 7 lines
Quoted 2 lines
expected_offsetshould be 16 (push * 3 - ret) on x64 if weri->stackis theregs->spright after call.the syscall trampoline already updates the
regs->spbefore callinghandle_trampolineregs->sp += sizeof(r11_cx_ax);
Yes, that is "push * 3" part. And "- ret" is that the stack entry is consumed by the "ret", which is stored by call.
1: |--------| <- sp at function entry == ri->stack 0: |ret-addr| <- call pushed it
0: |ret-addr| <- sp at return trampoline
3: |r11 | <- regs->sp at syscall 2: |rcx | 1: |rax | <- ri->stack 0: |ret-addr|
(Note: The lower the line, the larger the address.)
Thus, we can check the stack address by (regs->sp + 16 == ri->stack).
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
In reply to Jiri OlsaOn Tue, Apr 02, 2024 at 11:33:00AM +0200, Jiri Olsa wrote:
SNIP
Quoted 26 lines
#include <
linux/kdebug.h>
#include <asm/processor.h>arch/x86/kernel/uprobes.c +20
-0@@ -308,6 +309,88 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool }#ifdef CONFIG_X86_64 + +asm ( +".pushsection .rodata\n" +".global uretprobe_syscall_entry\n" +"uretprobe_syscall_entry:\n" +"pushq %rax\n" +"pushq %rcx\n" +"pushq %r11\n" + "movq $" __stringify(__NR_uretprobe) ", %rax\n" +"syscall\n" +"popq %r11\n" +"popq %rcx\n" + +/* The uretprobe syscall replaces stored %rax value with final +* return address, so we don't restore %rax in here and just +* call ret. +*/ +"retq\n" +".global uretprobe_syscall_end\n" +"uretprobe_syscall_end:\n"
arch/x86/kernel/uprobes.c static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool · line 341
Quoted 10 lines
arch/x86/kernel/uprobes.c +10
-0+".popsection\n" +); + +extern u8 uretprobe_syscall_entry[]; +extern u8 uretprobe_syscall_end[]; + +void *arch_uprobe_trampoline (unsigned long *psize) +{ + *psize = uretprobe_syscall_end - uretprobe_syscall_entry; +return uretprobe_syscall_entry;
fyi I realized this screws 32-bit programs, we either need to add compat trampoline, or keep the standard breakpoint for them:
+ struct pt_regs *regs = task_pt_regs(current);
+ static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
+
+ if (user_64bit_mode(regs)) {
+ *psize = uretprobe_syscall_end - uretprobe_syscall_entry;
+ return uretprobe_syscall_entry;
+ }
+
+ *psize = UPROBE_SWBP_INSN_SIZE;
+ return &insn;
not sure it's worth the effort to add the trampoline, I'll check
jirka
Re: [PATCHv2 1/3] uprobe: Add uretprobe syscall to speed up return probe
Quoted 57 lines · Jiri Olsa
On Tue, Apr 02, 2024 at 11:33:00AM +0200, Jiri Olsa wrote:
SNIP
Quoted 26 lines
#include <
linux/kdebug.h>
#include <asm/processor.h>arch/x86/kernel/uprobes.c +20
-0@@ -308,6 +309,88 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool }#ifdef CONFIG_X86_64 + +asm ( +".pushsection .rodata\n" +".global uretprobe_syscall_entry\n" +"uretprobe_syscall_entry:\n" +"pushq %rax\n" +"pushq %rcx\n" +"pushq %r11\n" + "movq $" __stringify(__NR_uretprobe) ", %rax\n" +"syscall\n" +"popq %r11\n" +"popq %rcx\n" + +/* The uretprobe syscall replaces stored %rax value with final +* return address, so we don't restore %rax in here and just +* call ret. +*/ +"retq\n" +".global uretprobe_syscall_end\n" +"uretprobe_syscall_end:\n" arch/x86/kernel/uprobes.c static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool · line 341
Quoted 10 lines
arch/x86/kernel/uprobes.c +10
-0+".popsection\n" +); + +extern u8 uretprobe_syscall_entry[]; +extern u8 uretprobe_syscall_end[]; + +void *arch_uprobe_trampoline (unsigned long *psize) +{ + *psize = uretprobe_syscall_end - uretprobe_syscall_entry; +return uretprobe_syscall_entry;fyi I realized this screws 32-bit programs, we either need to add compat trampoline, or keep the standard breakpoint for them:
arch/x86/kernel/uprobes.c +10
-0+struct pt_regs *regs =task_pt_regs (current); +static uprobe_opcode_t insn =UPROBE_SWBP_INSN ; + +if (user_64bit_mode (regs)) { + *psize = uretprobe_syscall_end - uretprobe_syscall_entry; +return uretprobe_syscall_entry; + } + + *psize =UPROBE_SWBP_INSN_SIZE ; +return &insn;
Quoted 2 lines
not sure it's worth the effort to add the trampoline, I'll check
32-bit arch isn't a high-performance target anyways, so I'd probably not bother and prioritize simplicity and long term maintenance.
Quoted 2 lines
jirka
Jiri Olsa
reviewerPATCH v2 2/3 selftests/bpf: Add uretprobe test for regs integrity
In reply to Jiri OlsaAdd uretprobe test that compares register values before and after the uretprobe is hit. It also compares the register values seen from attached bpf program.
--- tools/include/linux/compiler.h | 4 + .../selftests/bpf/prog_tests/uprobe_syscall.c | 163 ++++++++++++++++++ .../selftests/bpf/progs/uprobe_syscall.c | 15 ++ 3 files changed, 182 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c
tools/include/linux/compiler.h +4 -0
@@ -62,6 +62,10 @@#define __nocf_check __attribute__((nocf_check))#endif +#ifndef __naked +#define __naked __attribute__((__naked__)) +#endif +/* Are two types/vars the same type (ignoring qualifiers)? */ #ifndef __same_type# define __same_type (a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +163 -0
163 lines not on this page · raw message
tools/testing/selftests/bpf/progs/uprobe_syscall.c new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <string.h> + +struct pt_regs regs; + +char _license[] SEC("license") = "GPL"; + +SEC("uretprobe//proc/self/exe:uretprobe_regs_trigger") +int uretprobe (struct pt_regs *ctx) +{ +__builtin_memcpy (®s, ctx,sizeof (regs)); +return 0 ; +}
Signature · 1 line
2.44.0
Jiri Olsa
reviewerPATCH v2 3/3 selftests/bpf: Add uretprobe test for regs changes
In reply to Jiri OlsaAdding test that creates uprobe consumer on uretprobe which changes some of the registers. Making sure the changed registers are propagated to the user space from the ureprobe trampoline on x86_64.
To be able to do this, adding support to bpf_testmod to create uprobe via new attribute file:
/sys/kernel/bpf_testmod_uprobe
This file is expecting file offset and creates related uprobe on current process exe file and removes existing uprobe if offset is 0. The can be only single uprobe at any time.
The uprobe has specific consumer that changes registers used in ureprobe trampoline and which are later checked in the test.
--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 122 +++++++++++++++++- .../selftests/bpf/prog_tests/uprobe_syscall.c | 67 ++++++++++ 2 files changed, 188 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +121 -1
@@ -10,6 +10,7 @@#include <linux/percpu-defs.h> #include <linux/sysfs.h> #include <linux/tracepoint.h> +#include <linux/namei.h> #include "bpf_testmod.h" #include "bpf_testmod_kfunc.h"
@@ -343,6 +344,118 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { .write = bpf_testmod_test_write, }; +/* bpf_testmod_uprobe sysfs attribute is so far enabled for x86_64 only, +* please see test_uretprobe_regs_change test */ +#ifdef __x86_64__ + +static int +uprobe_ret_handler (struct uprobe_consumer *self,unsigned long func, +struct pt_regs *regs) + +{ + regs->ax =0x12345678deadbeef ; + regs->cx =0x87654321feebdaed ; + regs->r11 = (u64) -1 ; +return true; +} + +struct testmod_uprobe { +struct path path; +loff_t offset; +struct uprobe_consumer consumer; +}; + +static DEFINE_MUTEX (testmod_uprobe_mutex); + +static struct testmod_uprobe uprobe = { + .consumer.ret_handler = uprobe_ret_handler, +}; + +static int testmod_register_uprobe (loff_t offset) +{ +int err = -EBUSY ; + +if (uprobe.offset) +return -EBUSY ; + +mutex_lock (&testmod_uprobe_mutex); + +if (uprobe.offset) + goto out; + + err =kern_path ("/proc/self/exe" ,LOOKUP_FOLLOW , &uprobe.path); +if (err) + goto out; + + err =uprobe_register_refctr (d_real_inode (uprobe.path.dentry), + offset,0 , &uprobe.consumer); +if (err) +path_put (&uprobe.path); +else + uprobe.offset = offset; + +out: +mutex_unlock (&testmod_uprobe_mutex); +return err; +} + +static void testmod_unregister_uprobe (void ) +{ +mutex_lock (&testmod_uprobe_mutex); + +if (uprobe.offset) { +uprobe_unregister (d_real_inode (uprobe.path.dentry), + uprobe.offset, &uprobe.consumer); + uprobe.offset =0 ; + } + +mutex_unlock (&testmod_uprobe_mutex); +} + +static ssize_t +bpf_testmod_uprobe_write (struct file *file,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buf,loff_t off,size_t len) +{ +unsigned long offset; +int err; + +if (kstrtoul (buf,0 , &offset)) +return -EINVAL ; + +if (offset) + err =testmod_register_uprobe (offset); +else +testmod_unregister_uprobe (); + +return err ?:strlen (buf); +} + +static struct bin_attribute bin_attr_bpf_testmod_uprobe_file__ro_after_init = { + .attr = { .name ="bpf_testmod_uprobe" , .mode =0666 , }, + .write = bpf_testmod_uprobe_write, +}; + +static int register_bpf_testmod_uprobe (void ) +{ +return sysfs_create_bin_file (kernel_kobj, &bin_attr_bpf_testmod_uprobe_file); +} + +static void unregister_bpf_testmod_uprobe (void ) +{ +testmod_unregister_uprobe (); +sysfs_remove_bin_file (kernel_kobj, &bin_attr_bpf_testmod_uprobe_file); +} + +#else +static int register_bpf_testmod_uprobe (void ) +{ +return 0 ; +} + +static void unregister_bpf_testmod_uprobe (void ) { } +#endif + BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
@@ -650,7 +763,13 @@ static int bpf_testmod_init(void)return ret;if (bpf_fentry_test1 (0 ) <0 )return -EINVAL ; -return sysfs_create_bin_file (kernel_kobj, &bin_attr_bpf_testmod_file); + ret =sysfs_create_bin_file (kernel_kobj, &bin_attr_bpf_testmod_file); +if (ret <0 ) +return ret; + ret =register_bpf_testmod_uprobe (); +if (ret <0 ) +return ret; +return 0 ; }static void bpf_testmod_exit (void )
@@ -664,6 +783,7 @@ static void bpf_testmod_exit(void)msleep (20 );sysfs_remove_bin_file (kernel_kobj, &bin_attr_bpf_testmod_file); +unregister_bpf_testmod_uprobe (); }module_init (bpf_testmod_init);
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +67 -0
82 lines not on this page · raw message
Signature · 1 line
2.44.0