kprobes/ftrace: Recover original IP if pre_handler doesn't change it
authorMasami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Thu, 9 Oct 2014 13:01:06 +0000 (13:01 +0000)
committerSteven Rostedt <rostedt@goodmis.org>
Thu, 20 Nov 2014 16:42:48 +0000 (11:42 -0500)
Recover original IP register if the pre_handler doesn't change it.
Since current kprobes doesn't expect that another ftrace handler
may change regs->ip, it sets kprobe.addr + MCOUNT_INSN_SIZE to
regs->ip and returns to ftrace.
This seems wrong behavior since kprobes can recover regs->ip
and safely pass it to another handler.

This adds code which recovers original regs->ip passed from
ftrace right before returning to ftrace, so that another ftrace
user can change regs->ip.

Link: http://lkml.kernel.org/r/20141009130106.4698.26362.stgit@kbuild-f20.novalocal
Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
arch/x86/kernel/kprobes/ftrace.c

index 717b02a22e67638e8511c5d2ae299afa201739d8..5f8f0b3cc6740bc63a925c5f54e168baa8c39add 100644 (file)
@@ -27,7 +27,7 @@
 
 static nokprobe_inline
 int __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
-                     struct kprobe_ctlblk *kcb)
+                     struct kprobe_ctlblk *kcb, unsigned long orig_ip)
 {
        /*
         * Emulate singlestep (and also recover regs->ip)
@@ -39,6 +39,8 @@ int __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
                p->post_handler(p, regs, 0);
        }
        __this_cpu_write(current_kprobe, NULL);
+       if (orig_ip)
+               regs->ip = orig_ip;
        return 1;
 }
 
@@ -46,7 +48,7 @@ int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
                    struct kprobe_ctlblk *kcb)
 {
        if (kprobe_ftrace(p))
-               return __skip_singlestep(p, regs, kcb);
+               return __skip_singlestep(p, regs, kcb, 0);
        else
                return 0;
 }
@@ -71,13 +73,14 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
        if (kprobe_running()) {
                kprobes_inc_nmissed_count(p);
        } else {
+               unsigned long orig_ip = regs->ip;
                /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
                regs->ip = ip + sizeof(kprobe_opcode_t);
 
                __this_cpu_write(current_kprobe, p);
                kcb->kprobe_status = KPROBE_HIT_ACTIVE;
                if (!p->pre_handler || !p->pre_handler(p, regs))
-                       __skip_singlestep(p, regs, kcb);
+                       __skip_singlestep(p, regs, kcb, orig_ip);
                /*
                 * If pre_handler returns !0, it sets regs->ip and
                 * resets current kprobe.