~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/kernel/rethook.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * x86 implementation of rethook. Mostly copied from arch/x86/kernel/kprobes/core.c.
  4  */
  5 #include <linux/bug.h>
  6 #include <linux/rethook.h>
  7 #include <linux/kprobes.h>
  8 #include <linux/objtool.h>
  9 
 10 #include "kprobes/common.h"
 11 
 12 __visible void arch_rethook_trampoline_callback(struct pt_regs *regs);
 13 
 14 #ifndef ANNOTATE_NOENDBR
 15 #define ANNOTATE_NOENDBR
 16 #endif
 17 
 18 /*
 19  * When a target function returns, this code saves registers and calls
 20  * arch_rethook_trampoline_callback(), which calls the rethook handler.
 21  */
 22 asm(
 23         ".text\n"
 24         ".global arch_rethook_trampoline\n"
 25         ".type arch_rethook_trampoline, @function\n"
 26         "arch_rethook_trampoline:\n"
 27 #ifdef CONFIG_X86_64
 28         ANNOTATE_NOENDBR        /* This is only jumped from ret instruction */
 29         /* Push a fake return address to tell the unwinder it's a rethook. */
 30         "       pushq $arch_rethook_trampoline\n"
 31         UNWIND_HINT_FUNC
 32         "       pushq $" __stringify(__KERNEL_DS) "\n"
 33         /* Save the 'sp - 16', this will be fixed later. */
 34         "       pushq %rsp\n"
 35         "       pushfq\n"
 36         SAVE_REGS_STRING
 37         "       movq %rsp, %rdi\n"
 38         "       call arch_rethook_trampoline_callback\n"
 39         RESTORE_REGS_STRING
 40         /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
 41         "       addq $16, %rsp\n"
 42         "       popfq\n"
 43 #else
 44         /* Push a fake return address to tell the unwinder it's a rethook. */
 45         "       pushl $arch_rethook_trampoline\n"
 46         UNWIND_HINT_FUNC
 47         "       pushl %ss\n"
 48         /* Save the 'sp - 8', this will be fixed later. */
 49         "       pushl %esp\n"
 50         "       pushfl\n"
 51         SAVE_REGS_STRING
 52         "       movl %esp, %eax\n"
 53         "       call arch_rethook_trampoline_callback\n"
 54         RESTORE_REGS_STRING
 55         /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
 56         "       addl $8, %esp\n"
 57         "       popfl\n"
 58 #endif
 59         ASM_RET
 60         ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n"
 61 );
 62 NOKPROBE_SYMBOL(arch_rethook_trampoline);
 63 
 64 /*
 65  * Called from arch_rethook_trampoline
 66  */
 67 __used __visible void arch_rethook_trampoline_callback(struct pt_regs *regs)
 68 {
 69         unsigned long *frame_pointer;
 70 
 71         /* fixup registers */
 72         regs->cs = __KERNEL_CS;
 73 #ifdef CONFIG_X86_32
 74         regs->gs = 0;
 75 #endif
 76         regs->ip = (unsigned long)&arch_rethook_trampoline;
 77         regs->orig_ax = ~0UL;
 78         regs->sp += 2*sizeof(long);
 79         frame_pointer = (long *)(regs + 1);
 80 
 81         /*
 82          * The return address at 'frame_pointer' is recovered by the
 83          * arch_rethook_fixup_return() which called from this
 84          * rethook_trampoline_handler().
 85          */
 86         rethook_trampoline_handler(regs, (unsigned long)frame_pointer);
 87 
 88         /*
 89          * Copy FLAGS to 'pt_regs::ss' so that arch_rethook_trapmoline()
 90          * can do RET right after POPF.
 91          */
 92         *(unsigned long *)&regs->ss = regs->flags;
 93 }
 94 NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
 95 
 96 /*
 97  * arch_rethook_trampoline() skips updating frame pointer. The frame pointer
 98  * saved in arch_rethook_trampoline_callback() points to the real caller
 99  * function's frame pointer. Thus the arch_rethook_trampoline() doesn't have
100  * a standard stack frame with CONFIG_FRAME_POINTER=y.
101  * Let's mark it non-standard function. Anyway, FP unwinder can correctly
102  * unwind without the hint.
103  */
104 STACK_FRAME_NON_STANDARD_FP(arch_rethook_trampoline);
105 
106 /* This is called from rethook_trampoline_handler(). */
107 void arch_rethook_fixup_return(struct pt_regs *regs,
108                                unsigned long correct_ret_addr)
109 {
110         unsigned long *frame_pointer = (void *)(regs + 1);
111 
112         /* Replace fake return address with real one. */
113         *frame_pointer = correct_ret_addr;
114 }
115 NOKPROBE_SYMBOL(arch_rethook_fixup_return);
116 
117 void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
118 {
119         unsigned long *stack = (unsigned long *)regs->sp;
120 
121         rh->ret_addr = stack[0];
122         rh->frame = regs->sp;
123 
124         /* Replace the return addr with trampoline addr */
125         stack[0] = (unsigned long) arch_rethook_trampoline;
126 }
127 NOKPROBE_SYMBOL(arch_rethook_prepare);
128 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php