1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Return hooking with list-based shadow stack.
5 #ifndef _LINUX_RETHOOK_H
6 #define _LINUX_RETHOOK_H
8 #include <linux/compiler.h>
9 #include <linux/objpool.h>
10 #include <linux/kallsyms.h>
11 #include <linux/llist.h>
12 #include <linux/rcupdate.h>
16 typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
19 * struct rethook - The rethook management data structure.
20 * @data: The user-defined data storage.
21 * @handler: The user-defined return hook handler.
22 * @pool: The pool of struct rethook_node.
23 * @ref: The reference counter.
24 * @rcu: The rcu_head for deferred freeing.
26 * Don't embed to another data structure, because this is a self-destructive
27 * data structure when all rethook_node are freed.
31 rethook_handler_t handler;
32 struct objpool_head pool;
37 * struct rethook_node - The rethook shadow-stack entry node.
38 * @rcu: The rcu_head for deferred freeing.
39 * @llist: The llist, linked to a struct task_struct::rethooks.
40 * @rethook: The pointer to the struct rethook.
41 * @ret_addr: The storage for the real return address.
42 * @frame: The storage for the frame pointer.
44 * You can embed this to your extended data structure to store any data
45 * on each entry of the shadow stack.
49 struct llist_node llist;
50 struct rethook *rethook;
51 unsigned long ret_addr;
55 struct rethook *rethook_alloc(void *data, rethook_handler_t handler, int size, int num);
56 void rethook_stop(struct rethook *rh);
57 void rethook_free(struct rethook *rh);
58 struct rethook_node *rethook_try_get(struct rethook *rh);
59 void rethook_recycle(struct rethook_node *node);
60 void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount);
61 unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
62 struct llist_node **cur);
64 /* Arch dependent code must implement arch_* and trampoline code */
65 void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount);
66 void arch_rethook_trampoline(void);
69 * is_rethook_trampoline() - Check whether the address is rethook trampoline
70 * @addr: The address to be checked
72 * Return true if the @addr is the rethook trampoline address.
74 static inline bool is_rethook_trampoline(unsigned long addr)
76 return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
79 /* If the architecture needs to fixup the return address, implement it. */
80 void arch_rethook_fixup_return(struct pt_regs *regs,
81 unsigned long correct_ret_addr);
83 /* Generic trampoline handler, arch code must prepare asm stub */
84 unsigned long rethook_trampoline_handler(struct pt_regs *regs,
88 void rethook_flush_task(struct task_struct *tk);
90 #define rethook_flush_task(tsk) do { } while (0)