Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * x86 implementation of rethook. Mostly copied from arch/x86/kernel/kprobes/core.c.
0004  */
0005 #include <linux/bug.h>
0006 #include <linux/rethook.h>
0007 #include <linux/kprobes.h>
0008 #include <linux/objtool.h>
0009 
0010 #include "kprobes/common.h"
0011 
0012 __visible void arch_rethook_trampoline_callback(struct pt_regs *regs);
0013 
0014 #ifndef ANNOTATE_NOENDBR
0015 #define ANNOTATE_NOENDBR
0016 #endif
0017 
0018 /*
0019  * When a target function returns, this code saves registers and calls
0020  * arch_rethook_trampoline_callback(), which calls the rethook handler.
0021  */
0022 asm(
0023     ".text\n"
0024     ".global arch_rethook_trampoline\n"
0025     ".type arch_rethook_trampoline, @function\n"
0026     "arch_rethook_trampoline:\n"
0027 #ifdef CONFIG_X86_64
0028     ANNOTATE_NOENDBR    /* This is only jumped from ret instruction */
0029     /* Push a fake return address to tell the unwinder it's a rethook. */
0030     "   pushq $arch_rethook_trampoline\n"
0031     UNWIND_HINT_FUNC
0032     "       pushq $" __stringify(__KERNEL_DS) "\n"
0033     /* Save the 'sp - 16', this will be fixed later. */
0034     "   pushq %rsp\n"
0035     "   pushfq\n"
0036     SAVE_REGS_STRING
0037     "   movq %rsp, %rdi\n"
0038     "   call arch_rethook_trampoline_callback\n"
0039     RESTORE_REGS_STRING
0040     /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
0041     "   addq $16, %rsp\n"
0042     "   popfq\n"
0043 #else
0044     /* Push a fake return address to tell the unwinder it's a rethook. */
0045     "   pushl $arch_rethook_trampoline\n"
0046     UNWIND_HINT_FUNC
0047     "   pushl %ss\n"
0048     /* Save the 'sp - 8', this will be fixed later. */
0049     "   pushl %esp\n"
0050     "   pushfl\n"
0051     SAVE_REGS_STRING
0052     "   movl %esp, %eax\n"
0053     "   call arch_rethook_trampoline_callback\n"
0054     RESTORE_REGS_STRING
0055     /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
0056     "   addl $8, %esp\n"
0057     "   popfl\n"
0058 #endif
0059     ASM_RET
0060     ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n"
0061 );
0062 NOKPROBE_SYMBOL(arch_rethook_trampoline);
0063 
0064 /*
0065  * Called from arch_rethook_trampoline
0066  */
0067 __used __visible void arch_rethook_trampoline_callback(struct pt_regs *regs)
0068 {
0069     unsigned long *frame_pointer;
0070 
0071     /* fixup registers */
0072     regs->cs = __KERNEL_CS;
0073 #ifdef CONFIG_X86_32
0074     regs->gs = 0;
0075 #endif
0076     regs->ip = (unsigned long)&arch_rethook_trampoline;
0077     regs->orig_ax = ~0UL;
0078     regs->sp += 2*sizeof(long);
0079     frame_pointer = (long *)(regs + 1);
0080 
0081     /*
0082      * The return address at 'frame_pointer' is recovered by the
0083      * arch_rethook_fixup_return() which called from this
0084      * rethook_trampoline_handler().
0085      */
0086     rethook_trampoline_handler(regs, (unsigned long)frame_pointer);
0087 
0088     /*
0089      * Copy FLAGS to 'pt_regs::ss' so that arch_rethook_trapmoline()
0090      * can do RET right after POPF.
0091      */
0092     *(unsigned long *)&regs->ss = regs->flags;
0093 }
0094 NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
0095 
0096 /*
0097  * arch_rethook_trampoline() skips updating frame pointer. The frame pointer
0098  * saved in arch_rethook_trampoline_callback() points to the real caller
0099  * function's frame pointer. Thus the arch_rethook_trampoline() doesn't have
0100  * a standard stack frame with CONFIG_FRAME_POINTER=y.
0101  * Let's mark it non-standard function. Anyway, FP unwinder can correctly
0102  * unwind without the hint.
0103  */
0104 STACK_FRAME_NON_STANDARD_FP(arch_rethook_trampoline);
0105 
0106 /* This is called from rethook_trampoline_handler(). */
0107 void arch_rethook_fixup_return(struct pt_regs *regs,
0108                    unsigned long correct_ret_addr)
0109 {
0110     unsigned long *frame_pointer = (void *)(regs + 1);
0111 
0112     /* Replace fake return address with real one. */
0113     *frame_pointer = correct_ret_addr;
0114 }
0115 NOKPROBE_SYMBOL(arch_rethook_fixup_return);
0116 
0117 void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
0118 {
0119     unsigned long *stack = (unsigned long *)regs->sp;
0120 
0121     rh->ret_addr = stack[0];
0122     rh->frame = regs->sp;
0123 
0124     /* Replace the return addr with trampoline addr */
0125     stack[0] = (unsigned long) arch_rethook_trampoline;
0126 }
0127 NOKPROBE_SYMBOL(arch_rethook_prepare);