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/freelist.h>
10 #include <linux/kallsyms.h>
11 #include <linux/llist.h>
12 #include <linux/rcupdate.h>
13 #include <linux/refcount.h>
17 typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
20 * struct rethook - The rethook management data structure.
21 * @data: The user-defined data storage.
22 * @handler: The user-defined return hook handler.
23 * @pool: The pool of struct rethook_node.
24 * @ref: The reference counter.
25 * @rcu: The rcu_head for deferred freeing.
27 * Don't embed to another data structure, because this is a self-destructive
28 * data structure when all rethook_node are freed.
33 * To avoid sparse warnings, this uses a raw function pointer with
34 * __rcu, instead of rethook_handler_t. But this must be same as
37 void (__rcu *handler) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
38 struct freelist_head pool;
44 * struct rethook_node - The rethook shadow-stack entry node.
45 * @freelist: The freelist, linked to struct rethook::pool.
46 * @rcu: The rcu_head for deferred freeing.
47 * @llist: The llist, linked to a struct task_struct::rethooks.
48 * @rethook: The pointer to the struct rethook.
49 * @ret_addr: The storage for the real return address.
50 * @frame: The storage for the frame pointer.
52 * You can embed this to your extended data structure to store any data
53 * on each entry of the shadow stack.
57 struct freelist_node freelist;
60 struct llist_node llist;
61 struct rethook *rethook;
62 unsigned long ret_addr;
66 struct rethook *rethook_alloc(void *data, rethook_handler_t handler);
67 void rethook_stop(struct rethook *rh);
68 void rethook_free(struct rethook *rh);
69 void rethook_add_node(struct rethook *rh, struct rethook_node *node);
70 struct rethook_node *rethook_try_get(struct rethook *rh);
71 void rethook_recycle(struct rethook_node *node);
72 void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount);
73 unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
74 struct llist_node **cur);
76 /* Arch dependent code must implement arch_* and trampoline code */
77 void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount);
78 void arch_rethook_trampoline(void);
81 * is_rethook_trampoline() - Check whether the address is rethook trampoline
82 * @addr: The address to be checked
84 * Return true if the @addr is the rethook trampoline address.
86 static inline bool is_rethook_trampoline(unsigned long addr)
88 return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
91 /* If the architecture needs to fixup the return address, implement it. */
92 void arch_rethook_fixup_return(struct pt_regs *regs,
93 unsigned long correct_ret_addr);
95 /* Generic trampoline handler, arch code must prepare asm stub */
96 unsigned long rethook_trampoline_handler(struct pt_regs *regs,
100 void rethook_flush_task(struct task_struct *tk);
102 #define rethook_flush_task(tsk) do { } while (0)