Skip to content

Commit

Permalink
x86/kprobes: Fixup return address in generic trampoline handler
Browse files Browse the repository at this point in the history
In x86, the fake return address on the stack saved by
__kretprobe_trampoline() will be replaced with the real return
address after returning from trampoline_handler(). Before fixing
the return address, the real return address can be found in the
'current->kretprobe_instances'.

However, since there is a window between updating the
'current->kretprobe_instances' and fixing the address on the stack,
if an interrupt happens at that timing and the interrupt handler
does stacktrace, it may fail to unwind because it can not get
the correct return address from 'current->kretprobe_instances'.

This will eliminate that window by fixing the return address
right before updating 'current->kretprobe_instances'.

Link: https://lkml.kernel.org/r/163163057094.489837.9044470370440745866.stgit@devnote2

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Tested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
  • Loading branch information
Masami Hiramatsu authored and Steven Rostedt (VMware) committed Oct 1, 2021
1 parent 7da8949 commit bf094cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
18 changes: 16 additions & 2 deletions arch/x86/kernel/kprobes/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,16 @@ NOKPROBE_SYMBOL(__kretprobe_trampoline);
*/
STACK_FRAME_NON_STANDARD_FP(__kretprobe_trampoline);

/* This is called from kretprobe_trampoline_handler(). */
void arch_kretprobe_fixup_return(struct pt_regs *regs,
kprobe_opcode_t *correct_ret_addr)
{
unsigned long *frame_pointer = &regs->sp + 1;

/* Replace fake return address with real one. */
*frame_pointer = (unsigned long)correct_ret_addr;
}

/*
* Called from __kretprobe_trampoline
*/
Expand All @@ -1082,8 +1092,12 @@ __used __visible void trampoline_handler(struct pt_regs *regs)
regs->sp += sizeof(long);
frame_pointer = &regs->sp + 1;

/* Replace fake return address with real one. */
*frame_pointer = kretprobe_trampoline_handler(regs, frame_pointer);
/*
* The return address at 'frame_pointer' is recovered by the
* arch_kretprobe_fixup_return() which called from the
* kretprobe_trampoline_handler().
*/
kretprobe_trampoline_handler(regs, frame_pointer);

/*
* Copy FLAGS to 'pt_regs::sp' so that __kretprobe_trapmoline()
Expand Down
3 changes: 3 additions & 0 deletions include/linux/kprobes.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
struct pt_regs *regs);
extern int arch_trampoline_kprobe(struct kprobe *p);

void arch_kretprobe_fixup_return(struct pt_regs *regs,
kprobe_opcode_t *correct_ret_addr);

void __kretprobe_trampoline(void);
/*
* Since some architecture uses structured function pointer,
Expand Down
11 changes: 11 additions & 0 deletions kernel/kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,15 @@ unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
}
NOKPROBE_SYMBOL(kretprobe_find_ret_addr);

void __weak arch_kretprobe_fixup_return(struct pt_regs *regs,
kprobe_opcode_t *correct_ret_addr)
{
/*
* Do nothing by default. Please fill this to update the fake return
* address on the stack with the correct one on each arch if possible.
*/
}

unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
void *frame_pointer)
{
Expand Down Expand Up @@ -1967,6 +1976,8 @@ unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
first = first->next;
}

arch_kretprobe_fixup_return(regs, correct_ret_addr);

/* Unlink all nodes for this frame. */
first = current->kretprobe_instances.first;
current->kretprobe_instances.first = node->next;
Expand Down

0 comments on commit bf094cf

Please sign in to comment.