rethook.c 3.7 KB

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