aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Mayr <me@sam.st>2019-07-28 11:26:17 -0400
committerThomas Gleixner <tglx@linutronix.de>2019-08-26 09:55:09 -0400
commit9212ec7d8357ea630031e89d0d399c761421c83b (patch)
tree0f646f21199e0bef2ee3d48caa71b65cd4df98cf
parent3e5bedc2c258341702ddffbd7688c5e6eb01eafa (diff)
uprobes/x86: Fix detection of 32-bit user mode
32-bit processes running on a 64-bit kernel are not always detected correctly, causing the process to crash when uretprobes are installed. The reason for the crash is that in_ia32_syscall() is used to determine the process's mode, which only works correctly when called from a syscall. In the case of uretprobes, however, the function is called from a exception and always returns 'false' on a 64-bit kernel. In consequence this leads to corruption of the process's return address. Fix this by using user_64bit_mode() instead of in_ia32_syscall(), which is correct in any situation. [ tglx: Add a comment and the following historical info ] This should have been detected by the rename which happened in commit abfb9498ee13 ("x86/entry: Rename is_{ia32,x32}_task() to in_{ia32,x32}_syscall()") which states in the changelog: The is_ia32_task()/is_x32_task() function names are a big misnomer: they suggests that the compat-ness of a system call is a task property, which is not true, the compatness of a system call purely depends on how it was invoked through the system call layer. ..... and then it went and blindly renamed every call site. Sadly enough this was already mentioned here: 8faaed1b9f50 ("uprobes/x86: Introduce sizeof_long(), cleanup adjust_ret_addr() and arch_uretprobe_hijack_return_addr()") where the changelog says: TODO: is_ia32_task() is not what we actually want, TS_COMPAT does not necessarily mean 32bit. Fortunately syscall-like insns can't be probed so it actually works, but it would be better to rename and use is_ia32_frame(). and goes all the way back to: 0326f5a94dde ("uprobes/core: Handle breakpoint and singlestep exceptions") Oh well. 7+ years until someone actually tried a uretprobe on a 32bit process on a 64bit kernel.... Fixes: 0326f5a94dde ("uprobes/core: Handle breakpoint and singlestep exceptions") Signed-off-by: Sebastian Mayr <me@sam.st> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Dmitry Safonov <dsafonov@virtuozzo.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20190728152617.7308-1-me@sam.st
-rw-r--r--arch/x86/kernel/uprobes.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index d8359ebeea70..8cd745ef8c7b 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -508,9 +508,12 @@ struct uprobe_xol_ops {
508 void (*abort)(struct arch_uprobe *, struct pt_regs *); 508 void (*abort)(struct arch_uprobe *, struct pt_regs *);
509}; 509};
510 510
511static inline int sizeof_long(void) 511static inline int sizeof_long(struct pt_regs *regs)
512{ 512{
513 return in_ia32_syscall() ? 4 : 8; 513 /*
514 * Check registers for mode as in_xxx_syscall() does not apply here.
515 */
516 return user_64bit_mode(regs) ? 8 : 4;
514} 517}
515 518
516static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs) 519static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
@@ -521,9 +524,9 @@ static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
521 524
522static int emulate_push_stack(struct pt_regs *regs, unsigned long val) 525static int emulate_push_stack(struct pt_regs *regs, unsigned long val)
523{ 526{
524 unsigned long new_sp = regs->sp - sizeof_long(); 527 unsigned long new_sp = regs->sp - sizeof_long(regs);
525 528
526 if (copy_to_user((void __user *)new_sp, &val, sizeof_long())) 529 if (copy_to_user((void __user *)new_sp, &val, sizeof_long(regs)))
527 return -EFAULT; 530 return -EFAULT;
528 531
529 regs->sp = new_sp; 532 regs->sp = new_sp;
@@ -556,7 +559,7 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs
556 long correction = utask->vaddr - utask->xol_vaddr; 559 long correction = utask->vaddr - utask->xol_vaddr;
557 regs->ip += correction; 560 regs->ip += correction;
558 } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) { 561 } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
559 regs->sp += sizeof_long(); /* Pop incorrect return address */ 562 regs->sp += sizeof_long(regs); /* Pop incorrect return address */
560 if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen)) 563 if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen))
561 return -ERESTART; 564 return -ERESTART;
562 } 565 }
@@ -675,7 +678,7 @@ static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
675 * "call" insn was executed out-of-line. Just restore ->sp and restart. 678 * "call" insn was executed out-of-line. Just restore ->sp and restart.
676 * We could also restore ->ip and try to call branch_emulate_op() again. 679 * We could also restore ->ip and try to call branch_emulate_op() again.
677 */ 680 */
678 regs->sp += sizeof_long(); 681 regs->sp += sizeof_long(regs);
679 return -ERESTART; 682 return -ERESTART;
680} 683}
681 684
@@ -1056,7 +1059,7 @@ bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
1056unsigned long 1059unsigned long
1057arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs) 1060arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
1058{ 1061{
1059 int rasize = sizeof_long(), nleft; 1062 int rasize = sizeof_long(regs), nleft;
1060 unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */ 1063 unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
1061 1064
1062 if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize)) 1065 if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))