x86/uprobes: Keep shadow stack in sync for emulated CALLs
PATCH 1/2 x86/uprobes: Keep shadow stack in sync for emulated CALLs
Uprobe CALL emulation updates the normal user stack, but not the CET user shadow stack. The subsequent RET then sees a stale shadow stack entry and raises #CP.
Update the relative CALL emulation and XOL CALL fixup paths to keep the shadow stack in sync.
--- arch/x86/kernel/uprobes.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kernel/uprobes.c +9 -1
@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr; regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen)) +if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ; }/* popf; tell the caller to not touch TF */
@@ -1338,6 +1342,10 @@ static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)*/ if (emulate_push_stack (regs, new_ip))return false; +if (shstk_push (new_ip) == -EFAULT ) { + regs->sp +=sizeof_long (regs); +return false; + } }else if (!check_jmp_cond (auprobe, regs)) { offs =0 ; }
Signature · 1 line
2.43.0
PATCH 2/2 selftests/x86: Add shadow stack uprobe CALL test
Add coverage for entry uprobes installed on CALL instructions while user shadow stack is enabled. The test puts an entry uprobe on a helper whose first instruction is a relative CALL, then verifies that the call/return sequence completes without SIGSEGV.
This catches regressions where x86 uprobe CALL emulation updates the regular user stack but leaves the CET shadow stack stale.
--- tools/testing/selftests/x86/test_shadow_stack.c | 86 +++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
tools/testing/selftests/x86/test_shadow_stack.c +86 -0
@@ -873,6 +873,86 @@ static int test_uretprobe(void)return err; } +/* Keep the CALL first so the function address is exactly the probed CALL. */ +extern void uprobe_call_trigger (void ); +asm (".pushsection .text\n" +".global uprobe_call_target\n" +".type uprobe_call_target, @function\n" +"uprobe_call_target:\n" +" ret\n" +".size uprobe_call_target, .-uprobe_call_target\n" + +".global uprobe_call_trigger\n" +".type uprobe_call_trigger, @function\n" +"uprobe_call_trigger:\n" +" call uprobe_call_target\n" +" ret\n" +".size uprobe_call_trigger, .-uprobe_call_trigger\n" +".popsection\n" +); + +/* If CALL emulation misses the shadow stack update, this exits via SIGSEGV. */ +static int test_uprobe_call (void ) +{ +const size_t attr_sz =sizeof (struct perf_event_attr ); +const char *file ="/proc/self/exe" ; +int fd =-1 , type, err =1 ; +struct perf_event_attr attr; +struct sigaction sa = {}; +ssize_t offset; + + type =determine_uprobe_perf_type (); +if (type <0 ) { +if (type == -ENOENT ) +printf ("[SKIP]\tUprobe on CALL test, uprobes are not available\n" ); +return 0 ; + } + + offset =get_uprobe_offset (uprobe_call_trigger); +if (offset <0 ) +return 1 ; + + sa.sa_sigaction = segv_gp_handler; + sa.sa_flags =SA_SIGINFO ; +if (sigaction (SIGSEGV , &sa,NULL )) +return 1 ; + +/* Setup entry uprobe through perf event interface. */ +memset (&attr,0 , attr_sz); + attr.size = attr_sz; + attr.type = type; + attr.config =0 ; + attr.config1 = (__u64 )(unsigned long )file; + attr.config2 = offset; + + fd =syscall (__NR_perf_event_open, &attr,0 /* pid */ ,-1 /* cpu */ , +-1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ); +if (fd <0 ) + goto out; + +if (sigsetjmp (jmp_buffer,1 )) + goto out; + +if (ARCH_PRCTL (ARCH_SHSTK_ENABLE ,ARCH_SHSTK_SHSTK )) + goto out; + +/* +* This either segfaults and goes through sigsetjmp above +* or succeeds and we're good. +*/ +uprobe_call_trigger (); + +printf ("[OK]\tUprobe on CALL test\n" ); + err =0 ; + +out: +ARCH_PRCTL (ARCH_SHSTK_DISABLE ,ARCH_SHSTK_SHSTK ); +signal (SIGSEGV ,SIG_DFL ); +if (fd >=0 ) +close (fd); +return err; +} +void segv_handler_ptrace (int signum,siginfo_t *si,void *uc) {/* The SSP adjustment caused a segfault. */
@@ -1071,6 +1151,12 @@ int main(int argc, char *argv[]) goto out; } +if (test_uprobe_call ()) { + ret =1 ; +printf ("[FAIL]\tuprobe on CALL test\n" ); + goto out; + } +return ret; out:
Signature · 1 line
2.43.0
Peter Zijlstra
maintainerQuoted 8 lines · David Windsor
Uprobe CALL emulation updates the normal user stack, but not the CET user shadow stack. The subsequent RET then sees a stale shadow stack entry and raises #CP.
Update the relative CALL emulation and XOL CALL fixup paths to keep the shadow stack in sync.
Fixes 488af8ea7131 ("x86/shstk: Wire in shadow stack interface")
I can confirm this patch fixes the included test case, so yay for that.
However, should this not be:
?
Quoted 36 lines
Signed-off-by David Windsor <[email protected]>--- arch/x86/kernel/uprobes.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)arch/x86/kernel/uprobes.c +9
-1@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr; regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen)) +if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ; }/* popf; tell the caller to not touch TF */ @@ -1338,6 +1342,10 @@ static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)*/ if (emulate_push_stack (regs, new_ip))return false; +if (shstk_push (new_ip) == -EFAULT ) { + regs->sp +=sizeof_long (regs); +return false; + } }else if (!check_jmp_cond (auprobe, regs)) { offs =0 ; }Signature · 1 line
2.43.0
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · David Windsor
Uprobe CALL emulation updates the normal user stack, but not the CET user shadow stack. The subsequent RET then sees a stale shadow stack entry and raises #CP.
Update the relative CALL emulation and XOL CALL fixup paths to keep the shadow stack in sync.
Fixes 488af8ea7131 ("x86/shstk: Wire in shadow stack interface")
Quoted 8 lines
I can confirm this patch fixes the included test case, so yay for that.
However, should this not be:
Fixes 1713b63a07a2 ("x86/shstk: Make return uprobe work with shadow stack")?
Hmm, this commit appears to only be concerned with the uretprobe case?
Oleg Nesterov
maintainerQuoted 5 lines · David Windsor
arch/x86/kernel/uprobes.c +0
-0@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr;
arch/x86/kernel/uprobes.c static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs · line 1255
Quoted 10 lines
arch/x86/kernel/uprobes.c +5
-1regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen)) +if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ;
Well, if shstk_update_last_frame() fails after emulate_push_stack(), we should probably return another error, so that the caller handle_singlestep() will kill this task?
Oleg.
Peter Zijlstra
maintainerQuoted 12 lines · Oleg Nesterov
Quoted 11 lines · David Windsor
arch/x86/kernel/uprobes.c +2
-1@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr; regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen))
Quoted 8 lines
arch/x86/kernel/uprobes.c static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs · line 1255
Quoted 4 lines
arch/x86/kernel/uprobes.c +3
-0+if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ;Well, if
shstk_update_last_frame()fails afteremulate_push_stack(), we should probably return another error, so that the callerhandle_singlestep()will kill this task?
Makes sense, the other user has a force_sig(SIGSEGV) on failure.
Oleg Nesterov
maintainerQuoted 23 lines · Peter Zijlstra
Quoted 12 lines · Oleg Nesterov
Quoted 11 lines · David Windsor
arch/x86/kernel/uprobes.c +2
-1@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr; regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen))Quoted 8 lines
arch/x86/kernel/uprobes.c static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs · line 1255
Quoted 4 lines
arch/x86/kernel/uprobes.c +3
-0+if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ;Well, if
shstk_update_last_frame()fails afteremulate_push_stack(), we should probably return another error, so that the callerhandle_singlestep()will kill this task?
Quoted 1 line
Makes sense, the other user has a
force_sig(SIGSEGV)on failure.
Offtopic question... both shstk_update_last_frame() and shstk_push() are only used by arch/x86/kernel/uprobes.c. But they are not symmetric in that shstk_update_last_frame() returns 0 if !features_enabled(ARCH_SHSTK_SHSTK), while shstk_push() returns -ENOTSUPP in this case.
That is why the users can't just do "if (shstk_push(xxx)) ...". This is really minor, but perhaps it makes sense to change shstk_push() to return 0 in this case too? I don't think -ENOTSUPP is actually useful...
Oleg.
Quoted 27 lines · Oleg Nesterov
Quoted 23 lines · Peter Zijlstra
Quoted 12 lines · Oleg Nesterov
Quoted 11 lines · David Windsor
arch/x86/kernel/uprobes.c +2
-1@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr; regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen))Quoted 8 lines
arch/x86/kernel/uprobes.c static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs · line 1255
Quoted 4 lines
arch/x86/kernel/uprobes.c +3
-0+if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ;Well, if
shstk_update_last_frame()fails afteremulate_push_stack(), we should probably return another error, so that the callerhandle_singlestep()will kill this task?Quoted 1 line
Makes sense, the other user has a
force_sig(SIGSEGV)on failure.
Quoted 9 lines
Offtopic question... both
shstk_update_last_frame()andshstk_push()are only used byarch/x86/kernel/uprobes.c. But they are not symmetric in thatshstk_update_last_frame()returns 0 if !features_enabled(ARCH_SHSTK_SHSTK), whileshstk_push()returns-ENOTSUPPin this case.That is why the users can't just do "if (
shstk_push(xxx)) ...". This is really minor, but perhaps it makes sense to changeshstk_push()to return 0 in this case too? I don't think-ENOTSUPPis actually useful...
Agreed, will send a follow-up patch changing shstk_push() to return 0 rather than -ENOTSUPP.
I'll send v2 shortly with the additional call to force_sig(SIGSEGV) to balance out the callers.
Quoted 2 lines
Oleg.
Quoted 39 lines · David Windsor
Quoted 27 lines · Oleg Nesterov
Quoted 23 lines · Peter Zijlstra
Quoted 12 lines · Oleg Nesterov
Quoted 11 lines · David Windsor
arch/x86/kernel/uprobes.c +2
-1@@ -1246,8 +1246,12 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regslong correction = utask->vaddr - utask->xol_vaddr; regs->ip += correction; }else if (auprobe->defparam.fixups &UPROBE_FIX_CALL ) { +unsigned long retaddr = utask->vaddr + auprobe->defparam.ilen; + regs->sp +=sizeof_long (regs);/* Pop incorrect return address */ -if (emulate_push_stack (regs, utask->vaddr + auprobe->defparam.ilen))Quoted 8 lines
arch/x86/kernel/uprobes.c static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs · line 1255
Quoted 4 lines
arch/x86/kernel/uprobes.c +3
-0+if (emulate_push_stack (regs, retaddr)) +return -ERESTART ; +if (shstk_update_last_frame (retaddr))return -ERESTART ;Well, if
shstk_update_last_frame()fails afteremulate_push_stack(), we should probably return another error, so that the callerhandle_singlestep()will kill this task?Quoted 1 line
Makes sense, the other user has a
force_sig(SIGSEGV)on failure.Quoted 9 lines
Offtopic question... both
shstk_update_last_frame()andshstk_push()are only used byarch/x86/kernel/uprobes.c. But they are not symmetric in thatshstk_update_last_frame()returns 0 if !features_enabled(ARCH_SHSTK_SHSTK), whileshstk_push()returns-ENOTSUPPin this case.That is why the users can't just do "if (
shstk_push(xxx)) ...". This is really minor, but perhaps it makes sense to changeshstk_push()to return 0 in this case too? I don't think-ENOTSUPPis actually useful...
Quoted 6 lines
Agreed, will send a follow-up patch changing
shstk_push()to return 0 rather than-ENOTSUPP.I'll send v2 shortly with the additional call to
force_sig(SIGSEGV)to balance out the callers.
Actually, we don't need to force_sig() ourselves in this case, the handle_singlestep() path will take care of that. We just need to return something that's not -ERESTART. You may have meant that in your reply, but just noting it for the record for when I send v2.
Quoted 2 lines
Quoted 2 lines
Oleg.