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/objpool.h>
10#include <linux/kallsyms.h>
11#include <linux/llist.h>
12#include <linux/rcupdate.h>
13
14struct rethook_node;
15
16typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
17
18/**
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.
25 *
26 * Don't embed to another data structure, because this is a self-destructive
27 * data structure when all rethook_node are freed.
28 */
29struct rethook {
30	void			*data;
31	/*
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
34	 * rethook_handler_t.
35	 */
36	void (__rcu *handler) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
37	struct objpool_head	pool;
38	struct rcu_head		rcu;
39};
40
41/**
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.
48 *
49 * You can embed this to your extended data structure to store any data
50 * on each entry of the shadow stack.
51 */
52struct rethook_node {
53	struct rcu_head		rcu;
54	struct llist_node	llist;
55	struct rethook		*rethook;
56	unsigned long		ret_addr;
57	unsigned long		frame;
58};
59
60struct rethook *rethook_alloc(void *data, rethook_handler_t handler, int size, int num);
61void rethook_stop(struct rethook *rh);
62void rethook_free(struct rethook *rh);
63struct rethook_node *rethook_try_get(struct rethook *rh);
64void rethook_recycle(struct rethook_node *node);
65void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount);
66unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
67				    struct llist_node **cur);
68
69/* Arch dependent code must implement arch_* and trampoline code */
70void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount);
71void arch_rethook_trampoline(void);
72
73/**
74 * is_rethook_trampoline() - Check whether the address is rethook trampoline
75 * @addr: The address to be checked
76 *
77 * Return true if the @addr is the rethook trampoline address.
78 */
79static inline bool is_rethook_trampoline(unsigned long addr)
80{
81	return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
82}
83
84/* If the architecture needs to fixup the return address, implement it. */
85void arch_rethook_fixup_return(struct pt_regs *regs,
86			       unsigned long correct_ret_addr);
87
88/* Generic trampoline handler, arch code must prepare asm stub */
89unsigned long rethook_trampoline_handler(struct pt_regs *regs,
90					 unsigned long frame);
91
92#ifdef CONFIG_RETHOOK
93void rethook_flush_task(struct task_struct *tk);
94#else
95#define rethook_flush_task(tsk)	do { } while (0)
96#endif
97
98#endif
99