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.
32 * To avoid sparse warnings, this uses a raw function pointer with
33 * __rcu, instead of rethook_handler_t. But this must be same as
36 void (__rcu
*handler
) (struct rethook_node
*, void *, unsigned long, struct pt_regs
*);
37 struct objpool_head pool
;
42 * struct rethook_node - The rethook shadow-stack entry node.
43 * @rcu: The rcu_head for deferred freeing.
44 * @llist: The llist, linked to a struct task_struct::rethooks.
45 * @rethook: The pointer to the struct rethook.
46 * @ret_addr: The storage for the real return address.
47 * @frame: The storage for the frame pointer.
49 * You can embed this to your extended data structure to store any data
50 * on each entry of the shadow stack.
54 struct llist_node llist
;
55 struct rethook
*rethook
;
56 unsigned long ret_addr
;
60 struct rethook
*rethook_alloc(void *data
, rethook_handler_t handler
, int size
, int num
);
61 void rethook_stop(struct rethook
*rh
);
62 void rethook_free(struct rethook
*rh
);
63 struct rethook_node
*rethook_try_get(struct rethook
*rh
);
64 void rethook_recycle(struct rethook_node
*node
);
65 void rethook_hook(struct rethook_node
*node
, struct pt_regs
*regs
, bool mcount
);
66 unsigned long rethook_find_ret_addr(struct task_struct
*tsk
, unsigned long frame
,
67 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);
74 * is_rethook_trampoline() - Check whether the address is rethook trampoline
75 * @addr: The address to be checked
77 * Return true if the @addr is the rethook trampoline address.
79 static inline bool is_rethook_trampoline(unsigned long addr
)
81 return addr
== (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline
);
84 /* If the architecture needs to fixup the return address, implement it. */
85 void arch_rethook_fixup_return(struct pt_regs
*regs
,
86 unsigned long correct_ret_addr
);
88 /* Generic trampoline handler, arch code must prepare asm stub */
89 unsigned long rethook_trampoline_handler(struct pt_regs
*regs
,
93 void rethook_flush_task(struct task_struct
*tk
);
95 #define rethook_flush_task(tsk) do { } while (0)