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 rethook_handler_t handler;
32 struct objpool_head pool;
33 struct rcu_head rcu;
34};
35
36/**
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.
43 *
44 * You can embed this to your extended data structure to store any data
45 * on each entry of the shadow stack.
46 */
47struct rethook_node {
48 struct rcu_head rcu;
49 struct llist_node llist;
50 struct rethook *rethook;
51 unsigned long ret_addr;
52 unsigned long frame;
53};
54
55struct rethook *rethook_alloc(void *data, rethook_handler_t handler, int size, int num);
56void rethook_stop(struct rethook *rh);
57void rethook_free(struct rethook *rh);
58struct rethook_node *rethook_try_get(struct rethook *rh);
59void rethook_recycle(struct rethook_node *node);
60void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount);
61unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
62 struct llist_node **cur);
63
64/* Arch dependent code must implement arch_* and trampoline code */
65void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount);
66void arch_rethook_trampoline(void);
67
68/**
69 * is_rethook_trampoline() - Check whether the address is rethook trampoline
70 * @addr: The address to be checked
71 *
72 * Return true if the @addr is the rethook trampoline address.
73 */
74static inline bool is_rethook_trampoline(unsigned long addr)
75{
76 return addr == (unsigned long)dereference_symbol_descriptor(ptr: arch_rethook_trampoline);
77}
78
79/* If the architecture needs to fixup the return address, implement it. */
80void arch_rethook_fixup_return(struct pt_regs *regs,
81 unsigned long correct_ret_addr);
82
83/* Generic trampoline handler, arch code must prepare asm stub */
84unsigned long rethook_trampoline_handler(struct pt_regs *regs,
85 unsigned long frame);
86
87#ifdef CONFIG_RETHOOK
88void rethook_flush_task(struct task_struct *tk);
89#else
90#define rethook_flush_task(tsk) do { } while (0)
91#endif
92
93#endif
94

source code of linux/include/linux/rethook.h