xref: /openbmc/linux/include/linux/rethook.h (revision 29b9ebc8)
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 
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>
14 
15 struct rethook_node;
16 
17 typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
18 
19 /**
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.
26  *
27  * Don't embed to another data structure, because this is a self-destructive
28  * data structure when all rethook_node are freed.
29  */
30 struct rethook {
31 	void			*data;
32 	/*
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
35 	 * rethook_handler_t.
36 	 */
37 	void (__rcu *handler) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
38 	struct freelist_head	pool;
39 	refcount_t		ref;
40 	struct rcu_head		rcu;
41 };
42 
43 /**
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.
51  *
52  * You can embed this to your extended data structure to store any data
53  * on each entry of the shadow stack.
54  */
55 struct rethook_node {
56 	union {
57 		struct freelist_node freelist;
58 		struct rcu_head      rcu;
59 	};
60 	struct llist_node	llist;
61 	struct rethook		*rethook;
62 	unsigned long		ret_addr;
63 	unsigned long		frame;
64 };
65 
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);
75 
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);
79 
80 /**
81  * is_rethook_trampoline() - Check whether the address is rethook trampoline
82  * @addr: The address to be checked
83  *
84  * Return true if the @addr is the rethook trampoline address.
85  */
is_rethook_trampoline(unsigned long addr)86 static inline bool is_rethook_trampoline(unsigned long addr)
87 {
88 	return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
89 }
90 
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);
94 
95 /* Generic trampoline handler, arch code must prepare asm stub */
96 unsigned long rethook_trampoline_handler(struct pt_regs *regs,
97 					 unsigned long frame);
98 
99 #ifdef CONFIG_RETHOOK
100 void rethook_flush_task(struct task_struct *tk);
101 #else
102 #define rethook_flush_task(tsk)	do { } while (0)
103 #endif
104 
105 #endif
106 
107