Skip to content

Commit 0b0122f

Browse files
Masami HiramatsuIngo Molnar
authored andcommitted
x86: kprobes bugfix
Kprobes for x86-64 may cause a kernel crash if it inserted on "iret" instruction. "call absolute" is invalid on x86-64, so we don't need treat it. - Change the processing order as same as x86-32. - Add "iret"(0xcf) case. - Remove next_rip local variable. Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 29b6cd7 commit 0b0122f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

arch/x86/kernel/kprobes_64.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ static void __kprobes resume_execution(struct kprobe *p,
485485
struct pt_regs *regs, struct kprobe_ctlblk *kcb)
486486
{
487487
unsigned long *tos = (unsigned long *)regs->rsp;
488-
unsigned long next_rip = 0;
489488
unsigned long copy_rip = (unsigned long)p->ainsn.insn;
490489
unsigned long orig_rip = (unsigned long)p->addr;
491490
kprobe_opcode_t *insn = p->ainsn.insn;
@@ -494,46 +493,42 @@ static void __kprobes resume_execution(struct kprobe *p,
494493
if (*insn >= 0x40 && *insn <= 0x4f)
495494
insn++;
496495

496+
regs->eflags &= ~TF_MASK;
497497
switch (*insn) {
498-
case 0x9c: /* pushfl */
498+
case 0x9c: /* pushfl */
499499
*tos &= ~(TF_MASK | IF_MASK);
500500
*tos |= kcb->kprobe_old_rflags;
501501
break;
502-
case 0xc3: /* ret/lret */
503-
case 0xcb:
504-
case 0xc2:
502+
case 0xc2: /* iret/ret/lret */
503+
case 0xc3:
505504
case 0xca:
506-
regs->eflags &= ~TF_MASK;
507-
/* rip is already adjusted, no more changes required*/
508-
return;
509-
case 0xe8: /* call relative - Fix return addr */
505+
case 0xcb:
506+
case 0xcf:
507+
case 0xea: /* jmp absolute -- ip is correct */
508+
/* ip is already adjusted, no more changes required */
509+
goto no_change;
510+
case 0xe8: /* call relative - Fix return addr */
510511
*tos = orig_rip + (*tos - copy_rip);
511512
break;
512513
case 0xff:
513514
if ((insn[1] & 0x30) == 0x10) {
514515
/* call absolute, indirect */
515-
/* Fix return addr; rip is correct. */
516-
next_rip = regs->rip;
516+
/* Fix return addr; ip is correct. */
517517
*tos = orig_rip + (*tos - copy_rip);
518+
goto no_change;
518519
} else if (((insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
519520
((insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
520-
/* rip is correct. */
521-
next_rip = regs->rip;
521+
/* ip is correct. */
522+
goto no_change;
522523
}
523-
break;
524-
case 0xea: /* jmp absolute -- rip is correct */
525-
next_rip = regs->rip;
526-
break;
527524
default:
528525
break;
529526
}
530527

531-
regs->eflags &= ~TF_MASK;
532-
if (next_rip) {
533-
regs->rip = next_rip;
534-
} else {
535-
regs->rip = orig_rip + (regs->rip - copy_rip);
536-
}
528+
regs->rip = orig_rip + (regs->rip - copy_rip);
529+
no_change:
530+
531+
return;
537532
}
538533

539534
int __kprobes post_kprobe_handler(struct pt_regs *regs)

0 commit comments

Comments
 (0)