rethook.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Return hooking with list-based shadow stack.
  4. */
  5. #ifndef _LINUX_RETHOOK_H
  6. #define _LINUX_RETHOOK_H
  7. #include <linux/compiler.h>
  8. #include <linux/freelist.h>
  9. #include <linux/kallsyms.h>
  10. #include <linux/llist.h>
  11. #include <linux/rcupdate.h>
  12. #include <linux/refcount.h>
  13. struct rethook_node;
  14. typedef void (*rethook_handler_t) (struct rethook_node *, void *, struct pt_regs *);
  15. /**
  16. * struct rethook - The rethook management data structure.
  17. * @data: The user-defined data storage.
  18. * @handler: The user-defined return hook handler.
  19. * @pool: The pool of struct rethook_node.
  20. * @ref: The reference counter.
  21. * @rcu: The rcu_head for deferred freeing.
  22. *
  23. * Don't embed to another data structure, because this is a self-destructive
  24. * data structure when all rethook_node are freed.
  25. */
  26. struct rethook {
  27. void *data;
  28. /*
  29. * To avoid sparse warnings, this uses a raw function pointer with
  30. * __rcu, instead of rethook_handler_t. But this must be same as
  31. * rethook_handler_t.
  32. */
  33. void (__rcu *handler) (struct rethook_node *, void *, struct pt_regs *);
  34. struct freelist_head pool;
  35. refcount_t ref;
  36. struct rcu_head rcu;
  37. };
  38. /**
  39. * struct rethook_node - The rethook shadow-stack entry node.
  40. * @freelist: The freelist, linked to struct rethook::pool.
  41. * @rcu: The rcu_head for deferred freeing.
  42. * @llist: The llist, linked to a struct task_struct::rethooks.
  43. * @rethook: The pointer to the struct rethook.
  44. * @ret_addr: The storage for the real return address.
  45. * @frame: The storage for the frame pointer.
  46. *
  47. * You can embed this to your extended data structure to store any data
  48. * on each entry of the shadow stack.
  49. */
  50. struct rethook_node {
  51. union {
  52. struct freelist_node freelist;
  53. struct rcu_head rcu;
  54. };
  55. struct llist_node llist;
  56. struct rethook *rethook;
  57. unsigned long ret_addr;
  58. unsigned long frame;
  59. };
  60. struct rethook *rethook_alloc(void *data, rethook_handler_t handler);
  61. void rethook_stop(struct rethook *rh);
  62. void rethook_free(struct rethook *rh);
  63. void rethook_add_node(struct rethook *rh, struct rethook_node *node);
  64. struct rethook_node *rethook_try_get(struct rethook *rh);
  65. void rethook_recycle(struct rethook_node *node);
  66. void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount);
  67. unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
  68. struct llist_node **cur);
  69. /* Arch dependent code must implement arch_* and trampoline code */
  70. void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount);
  71. void arch_rethook_trampoline(void);
  72. /**
  73. * is_rethook_trampoline() - Check whether the address is rethook trampoline
  74. * @addr: The address to be checked
  75. *
  76. * Return true if the @addr is the rethook trampoline address.
  77. */
  78. static inline bool is_rethook_trampoline(unsigned long addr)
  79. {
  80. return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
  81. }
  82. /* If the architecture needs to fixup the return address, implement it. */
  83. void arch_rethook_fixup_return(struct pt_regs *regs,
  84. unsigned long correct_ret_addr);
  85. /* Generic trampoline handler, arch code must prepare asm stub */
  86. unsigned long rethook_trampoline_handler(struct pt_regs *regs,
  87. unsigned long frame);
  88. #ifdef CONFIG_RETHOOK
  89. void rethook_flush_task(struct task_struct *tk);
  90. #else
  91. #define rethook_flush_task(tsk) do { } while (0)
  92. #endif
  93. #endif