aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/kprobes
diff options
context:
space:
mode:
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>2015-01-12 12:12:03 -0500
committerSteven Rostedt <rostedt@goodmis.org>2015-01-15 09:39:18 -0500
commit237d28db036e411f22c03cfd5b0f6dc2aa9bf3bc (patch)
treea773352c4b5b3e3209893832a881ad8550334295 /arch/x86/kernel/kprobes
parent7485058eea40783ac142a60c3e799fc66ce72583 (diff)
ftrace/jprobes/x86: Fix conflict between jprobes and function graph tracing
If the function graph tracer traces a jprobe callback, the system will crash. This can easily be demonstrated by compiling the jprobe sample module that is in the kernel tree, loading it and running the function graph tracer. # modprobe jprobe_example.ko # echo function_graph > /sys/kernel/debug/tracing/current_tracer # ls The first two commands end up in a nice crash after the first fork. (do_fork has a jprobe attached to it, so "ls" just triggers that fork) The problem is caused by the jprobe_return() that all jprobe callbacks must end with. The way jprobes works is that the function a jprobe is attached to has a breakpoint placed at the start of it (or it uses ftrace if fentry is supported). The breakpoint handler (or ftrace callback) will copy the stack frame and change the ip address to return to the jprobe handler instead of the function. The jprobe handler must end with jprobe_return() which swaps the stack and does an int3 (breakpoint). This breakpoint handler will then put back the saved stack frame, simulate the instruction at the beginning of the function it added a breakpoint to, and then continue on. For function tracing to work, it hijakes the return address from the stack frame, and replaces it with a hook function that will trace the end of the call. This hook function will restore the return address of the function call. If the function tracer traces the jprobe handler, the hook function for that handler will not be called, and its saved return address will be used for the next function. This will result in a kernel crash. To solve this, pause function tracing before the jprobe handler is called and unpause it before it returns back to the function it probed. Some other updates: Used a variable "saved_sp" to hold kcb->jprobe_saved_sp. This makes the code look a bit cleaner and easier to understand (various tries to fix this bug required this change). Note, if fentry is being used, jprobes will change the ip address before the function graph tracer runs and it will not be able to trace the function that the jprobe is probing. Link: http://lkml.kernel.org/r/20150114154329.552437962@goodmis.org Cc: stable@vger.kernel.org # 2.6.30+ Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Diffstat (limited to 'arch/x86/kernel/kprobes')
-rw-r--r--arch/x86/kernel/kprobes/core.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index f7e3cd50ece0..98f654d466e5 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -1020,6 +1020,15 @@ int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1020 regs->flags &= ~X86_EFLAGS_IF; 1020 regs->flags &= ~X86_EFLAGS_IF;
1021 trace_hardirqs_off(); 1021 trace_hardirqs_off();
1022 regs->ip = (unsigned long)(jp->entry); 1022 regs->ip = (unsigned long)(jp->entry);
1023
1024 /*
1025 * jprobes use jprobe_return() which skips the normal return
1026 * path of the function, and this messes up the accounting of the
1027 * function graph tracer to get messed up.
1028 *
1029 * Pause function graph tracing while performing the jprobe function.
1030 */
1031 pause_graph_tracing();
1023 return 1; 1032 return 1;
1024} 1033}
1025NOKPROBE_SYMBOL(setjmp_pre_handler); 1034NOKPROBE_SYMBOL(setjmp_pre_handler);
@@ -1048,24 +1057,25 @@ int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1048 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 1057 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1049 u8 *addr = (u8 *) (regs->ip - 1); 1058 u8 *addr = (u8 *) (regs->ip - 1);
1050 struct jprobe *jp = container_of(p, struct jprobe, kp); 1059 struct jprobe *jp = container_of(p, struct jprobe, kp);
1060 void *saved_sp = kcb->jprobe_saved_sp;
1051 1061
1052 if ((addr > (u8 *) jprobe_return) && 1062 if ((addr > (u8 *) jprobe_return) &&
1053 (addr < (u8 *) jprobe_return_end)) { 1063 (addr < (u8 *) jprobe_return_end)) {
1054 if (stack_addr(regs) != kcb->jprobe_saved_sp) { 1064 if (stack_addr(regs) != saved_sp) {
1055 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; 1065 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
1056 printk(KERN_ERR 1066 printk(KERN_ERR
1057 "current sp %p does not match saved sp %p\n", 1067 "current sp %p does not match saved sp %p\n",
1058 stack_addr(regs), kcb->jprobe_saved_sp); 1068 stack_addr(regs), saved_sp);
1059 printk(KERN_ERR "Saved registers for jprobe %p\n", jp); 1069 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1060 show_regs(saved_regs); 1070 show_regs(saved_regs);
1061 printk(KERN_ERR "Current registers\n"); 1071 printk(KERN_ERR "Current registers\n");
1062 show_regs(regs); 1072 show_regs(regs);
1063 BUG(); 1073 BUG();
1064 } 1074 }
1075 /* It's OK to start function graph tracing again */
1076 unpause_graph_tracing();
1065 *regs = kcb->jprobe_saved_regs; 1077 *regs = kcb->jprobe_saved_regs;
1066 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp), 1078 memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
1067 kcb->jprobes_stack,
1068 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
1069 preempt_enable_no_resched(); 1079 preempt_enable_no_resched();
1070 return 1; 1080 return 1;
1071 } 1081 }