1 // SPDX-License-Identifier: GPL-2.0 2 3 #define pr_fmt(fmt) "rethook: " fmt 4 5 #include <linux/bug.h> 6 #include <linux/kallsyms.h> 7 #include <linux/kprobes.h> 8 #include <linux/preempt.h> 9 #include <linux/rethook.h> 10 #include <linux/slab.h> 11 #include <linux/sort.h> 12 #include <linux/smp.h> 13 14 /* Return hook list (shadow stack by list) */ 15 16 /* 17 * This function is called from delayed_put_task_struct() when a task is 18 * dead and cleaned up to recycle any kretprobe instances associated with 19 * this task. These left over instances represent probed functions that 20 * have been called but will never return. 21 */ 22 void rethook_flush_task(struct task_struct *tk) 23 { 24 struct rethook_node *rhn; 25 struct llist_node *node; 26 27 node = __llist_del_all(&tk->rethooks); 28 while (node) { 29 rhn = container_of(node, struct rethook_node, llist); 30 node = node->next; 31 preempt_disable(); 32 rethook_recycle(rhn); 33 preempt_enable(); 34 } 35 } 36 37 static void rethook_free_rcu(struct rcu_head *head) 38 { 39 struct rethook *rh = container_of(head, struct rethook, rcu); 40 objpool_fini(&rh->pool); 41 } 42 43 /** 44 * rethook_stop() - Stop using a rethook. 45 * @rh: the struct rethook to stop. 46 * 47 * Stop using a rethook to prepare for freeing it. If you want to wait for 48 * all running rethook handler before calling rethook_free(), you need to 49 * call this first and wait RCU, and call rethook_free(). 50 */ 51 void rethook_stop(struct rethook *rh) 52 { 53 WRITE_ONCE(rh->handler, NULL); 54 } 55 56 /** 57 * rethook_free() - Free struct rethook. 58 * @rh: the struct rethook to be freed. 59 * 60 * Free the rethook. Before calling this function, user must ensure the 61 * @rh::data is cleaned if needed (or, the handler can access it after 62 * calling this function.) This function will set the @rh to be freed 63 * after all rethook_node are freed (not soon). And the caller must 64 * not touch @rh after calling this. 65 */ 66 void rethook_free(struct rethook *rh) 67 { 68 WRITE_ONCE(rh->handler, NULL); 69 70 call_rcu(&rh->rcu, rethook_free_rcu); 71 } 72 73 static int rethook_init_node(void *nod, void *context) 74 { 75 struct rethook_node *node = nod; 76 77 node->rethook = context; 78 return 0; 79 } 80 81 static int rethook_fini_pool(struct objpool_head *head, void *context) 82 { 83 kfree(context); 84 return 0; 85 } 86 87 /** 88 * rethook_alloc() - Allocate struct rethook. 89 * @data: a data to pass the @handler when hooking the return. 90 * @handler: the return hook callback function, must NOT be NULL 91 * @size: node size: rethook node and additional data 92 * @num: number of rethook nodes to be preallocated 93 * 94 * Allocate and initialize a new rethook with @data and @handler. 95 * Return pointer of new rethook, or error codes for failures. 96 * 97 * Note that @handler == NULL means this rethook is going to be freed. 98 */ 99 struct rethook *rethook_alloc(void *data, rethook_handler_t handler, 100 int size, int num) 101 { 102 struct rethook *rh; 103 104 if (!handler || num <= 0 || size < sizeof(struct rethook_node)) 105 return ERR_PTR(-EINVAL); 106 107 rh = kzalloc(sizeof(struct rethook), GFP_KERNEL); 108 if (!rh) 109 return ERR_PTR(-ENOMEM); 110 111 rh->data = data; 112 rh->handler = handler; 113 114 /* initialize the objpool for rethook nodes */ 115 if (objpool_init(&rh->pool, num, size, GFP_KERNEL, rh, 116 rethook_init_node, rethook_fini_pool)) { 117 kfree(rh); 118 return ERR_PTR(-ENOMEM); 119 } 120 return rh; 121 } 122 123 static void free_rethook_node_rcu(struct rcu_head *head) 124 { 125 struct rethook_node *node = container_of(head, struct rethook_node, rcu); 126 struct rethook *rh = node->rethook; 127 128 objpool_drop(node, &rh->pool); 129 } 130 131 /** 132 * rethook_recycle() - return the node to rethook. 133 * @node: The struct rethook_node to be returned. 134 * 135 * Return back the @node to @node::rethook. If the @node::rethook is already 136 * marked as freed, this will free the @node. 137 */ 138 void rethook_recycle(struct rethook_node *node) 139 { 140 lockdep_assert_preemption_disabled(); 141 142 if (likely(READ_ONCE(node->rethook->handler))) 143 objpool_push(node, &node->rethook->pool); 144 else 145 call_rcu(&node->rcu, free_rethook_node_rcu); 146 } 147 NOKPROBE_SYMBOL(rethook_recycle); 148 149 /** 150 * rethook_try_get() - get an unused rethook node. 151 * @rh: The struct rethook which pools the nodes. 152 * 153 * Get an unused rethook node from @rh. If the node pool is empty, this 154 * will return NULL. Caller must disable preemption. 155 */ 156 struct rethook_node *rethook_try_get(struct rethook *rh) 157 { 158 rethook_handler_t handler = READ_ONCE(rh->handler); 159 160 lockdep_assert_preemption_disabled(); 161 162 /* Check whether @rh is going to be freed. */ 163 if (unlikely(!handler)) 164 return NULL; 165 166 /* 167 * This expects the caller will set up a rethook on a function entry. 168 * When the function returns, the rethook will eventually be reclaimed 169 * or released in the rethook_recycle() with call_rcu(). 170 * This means the caller must be run in the RCU-availabe context. 171 */ 172 if (unlikely(!rcu_is_watching())) 173 return NULL; 174 175 return (struct rethook_node *)objpool_pop(&rh->pool); 176 } 177 NOKPROBE_SYMBOL(rethook_try_get); 178 179 /** 180 * rethook_hook() - Hook the current function return. 181 * @node: The struct rethook node to hook the function return. 182 * @regs: The struct pt_regs for the function entry. 183 * @mcount: True if this is called from mcount(ftrace) context. 184 * 185 * Hook the current running function return. This must be called when the 186 * function entry (or at least @regs must be the registers of the function 187 * entry.) @mcount is used for identifying the context. If this is called 188 * from ftrace (mcount) callback, @mcount must be set true. If this is called 189 * from the real function entry (e.g. kprobes) @mcount must be set false. 190 * This is because the way to hook the function return depends on the context. 191 */ 192 void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount) 193 { 194 arch_rethook_prepare(node, regs, mcount); 195 __llist_add(&node->llist, ¤t->rethooks); 196 } 197 NOKPROBE_SYMBOL(rethook_hook); 198 199 /* This assumes the 'tsk' is the current task or is not running. */ 200 static unsigned long __rethook_find_ret_addr(struct task_struct *tsk, 201 struct llist_node **cur) 202 { 203 struct rethook_node *rh = NULL; 204 struct llist_node *node = *cur; 205 206 if (!node) 207 node = tsk->rethooks.first; 208 else 209 node = node->next; 210 211 while (node) { 212 rh = container_of(node, struct rethook_node, llist); 213 if (rh->ret_addr != (unsigned long)arch_rethook_trampoline) { 214 *cur = node; 215 return rh->ret_addr; 216 } 217 node = node->next; 218 } 219 return 0; 220 } 221 NOKPROBE_SYMBOL(__rethook_find_ret_addr); 222 223 /** 224 * rethook_find_ret_addr -- Find correct return address modified by rethook 225 * @tsk: Target task 226 * @frame: A frame pointer 227 * @cur: a storage of the loop cursor llist_node pointer for next call 228 * 229 * Find the correct return address modified by a rethook on @tsk in unsigned 230 * long type. 231 * The @tsk must be 'current' or a task which is not running. @frame is a hint 232 * to get the currect return address - which is compared with the 233 * rethook::frame field. The @cur is a loop cursor for searching the 234 * kretprobe return addresses on the @tsk. The '*@cur' should be NULL at the 235 * first call, but '@cur' itself must NOT NULL. 236 * 237 * Returns found address value or zero if not found. 238 */ 239 unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame, 240 struct llist_node **cur) 241 { 242 struct rethook_node *rhn = NULL; 243 unsigned long ret; 244 245 if (WARN_ON_ONCE(!cur)) 246 return 0; 247 248 if (WARN_ON_ONCE(tsk != current && task_is_running(tsk))) 249 return 0; 250 251 do { 252 ret = __rethook_find_ret_addr(tsk, cur); 253 if (!ret) 254 break; 255 rhn = container_of(*cur, struct rethook_node, llist); 256 } while (rhn->frame != frame); 257 258 return ret; 259 } 260 NOKPROBE_SYMBOL(rethook_find_ret_addr); 261 262 void __weak arch_rethook_fixup_return(struct pt_regs *regs, 263 unsigned long correct_ret_addr) 264 { 265 /* 266 * Do nothing by default. If the architecture which uses a 267 * frame pointer to record real return address on the stack, 268 * it should fill this function to fixup the return address 269 * so that stacktrace works from the rethook handler. 270 */ 271 } 272 273 /* This function will be called from each arch-defined trampoline. */ 274 unsigned long rethook_trampoline_handler(struct pt_regs *regs, 275 unsigned long frame) 276 { 277 struct llist_node *first, *node = NULL; 278 unsigned long correct_ret_addr; 279 rethook_handler_t handler; 280 struct rethook_node *rhn; 281 282 correct_ret_addr = __rethook_find_ret_addr(current, &node); 283 if (!correct_ret_addr) { 284 pr_err("rethook: Return address not found! Maybe there is a bug in the kernel\n"); 285 BUG_ON(1); 286 } 287 288 instruction_pointer_set(regs, correct_ret_addr); 289 290 /* 291 * These loops must be protected from rethook_free_rcu() because those 292 * are accessing 'rhn->rethook'. 293 */ 294 preempt_disable_notrace(); 295 296 /* 297 * Run the handler on the shadow stack. Do not unlink the list here because 298 * stackdump inside the handlers needs to decode it. 299 */ 300 first = current->rethooks.first; 301 while (first) { 302 rhn = container_of(first, struct rethook_node, llist); 303 if (WARN_ON_ONCE(rhn->frame != frame)) 304 break; 305 handler = READ_ONCE(rhn->rethook->handler); 306 if (handler) 307 handler(rhn, rhn->rethook->data, 308 correct_ret_addr, regs); 309 310 if (first == node) 311 break; 312 first = first->next; 313 } 314 315 /* Fixup registers for returning to correct address. */ 316 arch_rethook_fixup_return(regs, correct_ret_addr); 317 318 /* Unlink used shadow stack */ 319 first = current->rethooks.first; 320 current->rethooks.first = node->next; 321 node->next = NULL; 322 323 while (first) { 324 rhn = container_of(first, struct rethook_node, llist); 325 first = first->next; 326 rethook_recycle(rhn); 327 } 328 preempt_enable_notrace(); 329 330 return correct_ret_addr; 331 } 332 NOKPROBE_SYMBOL(rethook_trampoline_handler); 333