Skip to content

Commit 8faaed1

Browse files
committed
uprobes/x86: Introduce sizeof_long(), cleanup adjust_ret_addr() and arch_uretprobe_hijack_return_addr()
1. Add the trivial sizeof_long() helper and change other callers of is_ia32_task() to use it. 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(). 2. As Jim pointed out "ncopied" in arch_uretprobe_hijack_return_addr() and adjust_ret_addr() should be named "nleft". And in fact only the last copy_to_user() in arch_uretprobe_hijack_return_addr() actually needs to inspect the non-zero error code. TODO: adjust_ret_addr() should die. We can always calculate the value we need to write into *regs->sp, just UPROBE_FIX_CALL should record insn->length. Signed-off-by: Oleg Nesterov <[email protected]> Reviewed-by: Jim Keniston <[email protected]>
1 parent 75f9ef0 commit 8faaed1

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

arch/x86/kernel/uprobes.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ struct uprobe_xol_ops {
408408
int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
409409
};
410410

411+
static inline int sizeof_long(void)
412+
{
413+
return is_ia32_task() ? 4 : 8;
414+
}
415+
411416
static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
412417
{
413418
pre_xol_rip_insn(auprobe, regs, &current->utask->autask);
@@ -419,21 +424,14 @@ static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
419424
*/
420425
static int adjust_ret_addr(unsigned long sp, long correction)
421426
{
422-
int rasize, ncopied;
423-
long ra = 0;
424-
425-
if (is_ia32_task())
426-
rasize = 4;
427-
else
428-
rasize = 8;
427+
int rasize = sizeof_long();
428+
long ra;
429429

430-
ncopied = copy_from_user(&ra, (void __user *)sp, rasize);
431-
if (unlikely(ncopied))
430+
if (copy_from_user(&ra, (void __user *)sp, rasize))
432431
return -EFAULT;
433432

434433
ra += correction;
435-
ncopied = copy_to_user((void __user *)sp, &ra, rasize);
436-
if (unlikely(ncopied))
434+
if (copy_to_user((void __user *)sp, &ra, rasize))
437435
return -EFAULT;
438436

439437
return 0;
@@ -450,10 +448,7 @@ static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs
450448

451449
if (auprobe->fixups & UPROBE_FIX_CALL) {
452450
if (adjust_ret_addr(regs->sp, correction)) {
453-
if (is_ia32_task())
454-
regs->sp += 4;
455-
else
456-
regs->sp += 8;
451+
regs->sp += sizeof_long();
457452
return -ERESTART;
458453
}
459454
}
@@ -714,23 +709,21 @@ bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
714709
unsigned long
715710
arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
716711
{
717-
int rasize, ncopied;
712+
int rasize = sizeof_long(), nleft;
718713
unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
719714

720-
rasize = is_ia32_task() ? 4 : 8;
721-
ncopied = copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize);
722-
if (unlikely(ncopied))
715+
if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
723716
return -1;
724717

725718
/* check whether address has been already hijacked */
726719
if (orig_ret_vaddr == trampoline_vaddr)
727720
return orig_ret_vaddr;
728721

729-
ncopied = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
730-
if (likely(!ncopied))
722+
nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
723+
if (likely(!nleft))
731724
return orig_ret_vaddr;
732725

733-
if (ncopied != rasize) {
726+
if (nleft != rasize) {
734727
pr_err("uprobe: return address clobbered: pid=%d, %%sp=%#lx, "
735728
"%%ip=%#lx\n", current->pid, regs->sp, regs->ip);
736729

0 commit comments

Comments
 (0)