Skip to content

Commit 737480a

Browse files
KUMANO SyuheiIngo Molnar
authored andcommitted
kprobes/x86: Fix the return address of multiple kretprobes
Fix the return address of subsequent kretprobes when multiple kretprobes are set on the same function. For example: # cd /sys/kernel/debug/tracing # echo "r:event1 sys_symlink" > kprobe_events # echo "r:event2 sys_symlink" >> kprobe_events # echo 1 > events/kprobes/enable # ln -s /tmp/foo /tmp/bar (without this patch) # cat trace ln-897 [000] 20404.133727: event1: (kretprobe_trampoline+0x0/0x4c <- sys_symlink) ln-897 [000] 20404.133747: event2: (system_call_fastpath+0x16/0x1b <- sys_symlink) (with this patch) # cat trace ln-740 [000] 13799.491076: event1: (system_call_fastpath+0x16/0x1b <- sys_symlink) ln-740 [000] 13799.491096: event2: (system_call_fastpath+0x16/0x1b <- sys_symlink) Signed-off-by: KUMANO Syuhei <kumano.prog@gmail.com> Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> LKML-Reference: <1281853084.3254.11.camel@camp10-laptop> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent f203688 commit 737480a

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

‎arch/x86/kernel/kprobes.c‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
709709
struct hlist_node *node, *tmp;
710710
unsigned long flags, orig_ret_address = 0;
711711
unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
712+
kprobe_opcode_t *correct_ret_addr = NULL;
712713

713714
INIT_HLIST_HEAD(&empty_rp);
714715
kretprobe_hash_lock(current, &head, &flags);
@@ -740,14 +741,34 @@ static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
740741
/* another task is sharing our hash bucket */
741742
continue;
742743

744+
orig_ret_address = (unsigned long)ri->ret_addr;
745+
746+
if (orig_ret_address != trampoline_address)
747+
/*
748+
* This is the real return address. Any other
749+
* instances associated with this task are for
750+
* other calls deeper on the call stack
751+
*/
752+
break;
753+
}
754+
755+
kretprobe_assert(ri, orig_ret_address, trampoline_address);
756+
757+
correct_ret_addr = ri->ret_addr;
758+
hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
759+
if (ri->task != current)
760+
/* another task is sharing our hash bucket */
761+
continue;
762+
763+
orig_ret_address = (unsigned long)ri->ret_addr;
743764
if (ri->rp && ri->rp->handler) {
744765
__get_cpu_var(current_kprobe) = &ri->rp->kp;
745766
get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
767+
ri->ret_addr = correct_ret_addr;
746768
ri->rp->handler(ri, regs);
747769
__get_cpu_var(current_kprobe) = NULL;
748770
}
749771

750-
orig_ret_address = (unsigned long)ri->ret_addr;
751772
recycle_rp_inst(ri, &empty_rp);
752773

753774
if (orig_ret_address != trampoline_address)
@@ -759,8 +780,6 @@ static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
759780
break;
760781
}
761782

762-
kretprobe_assert(ri, orig_ret_address, trampoline_address);
763-
764783
kretprobe_hash_unlock(current, &flags);
765784

766785
hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {

0 commit comments

Comments
 (0)