xref: /linux/include/linux/kprobes.h (revision 0be3ff0c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_KPROBES_H
3 #define _LINUX_KPROBES_H
4 /*
5  *  Kernel Probes (KProbes)
6  *
7  * Copyright (C) IBM Corporation, 2002, 2004
8  *
9  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
10  *		Probes initial implementation ( includes suggestions from
11  *		Rusty Russell).
12  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
13  *		interface to access function arguments.
14  * 2005-May	Hien Nguyen <hien@us.ibm.com> and Jim Keniston
15  *		<jkenisto@us.ibm.com>  and Prasanna S Panchamukhi
16  *		<prasanna@in.ibm.com> added function-return probes.
17  */
18 #include <linux/compiler.h>
19 #include <linux/linkage.h>
20 #include <linux/list.h>
21 #include <linux/notifier.h>
22 #include <linux/smp.h>
23 #include <linux/bug.h>
24 #include <linux/percpu.h>
25 #include <linux/spinlock.h>
26 #include <linux/rcupdate.h>
27 #include <linux/mutex.h>
28 #include <linux/ftrace.h>
29 #include <linux/refcount.h>
30 #include <linux/freelist.h>
31 #include <linux/rethook.h>
32 #include <asm/kprobes.h>
33 
34 #ifdef CONFIG_KPROBES
35 
36 /* kprobe_status settings */
37 #define KPROBE_HIT_ACTIVE	0x00000001
38 #define KPROBE_HIT_SS		0x00000002
39 #define KPROBE_REENTER		0x00000004
40 #define KPROBE_HIT_SSDONE	0x00000008
41 
42 #else /* !CONFIG_KPROBES */
43 #include <asm-generic/kprobes.h>
44 typedef int kprobe_opcode_t;
45 struct arch_specific_insn {
46 	int dummy;
47 };
48 #endif /* CONFIG_KPROBES */
49 
50 struct kprobe;
51 struct pt_regs;
52 struct kretprobe;
53 struct kretprobe_instance;
54 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
55 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
56 				       unsigned long flags);
57 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
58 				    struct pt_regs *);
59 
60 struct kprobe {
61 	struct hlist_node hlist;
62 
63 	/* list of kprobes for multi-handler support */
64 	struct list_head list;
65 
66 	/*count the number of times this probe was temporarily disarmed */
67 	unsigned long nmissed;
68 
69 	/* location of the probe point */
70 	kprobe_opcode_t *addr;
71 
72 	/* Allow user to indicate symbol name of the probe point */
73 	const char *symbol_name;
74 
75 	/* Offset into the symbol */
76 	unsigned int offset;
77 
78 	/* Called before addr is executed. */
79 	kprobe_pre_handler_t pre_handler;
80 
81 	/* Called after addr is executed, unless... */
82 	kprobe_post_handler_t post_handler;
83 
84 	/* Saved opcode (which has been replaced with breakpoint) */
85 	kprobe_opcode_t opcode;
86 
87 	/* copy of the original instruction */
88 	struct arch_specific_insn ainsn;
89 
90 	/*
91 	 * Indicates various status flags.
92 	 * Protected by kprobe_mutex after this kprobe is registered.
93 	 */
94 	u32 flags;
95 };
96 
97 /* Kprobe status flags */
98 #define KPROBE_FLAG_GONE	1 /* breakpoint has already gone */
99 #define KPROBE_FLAG_DISABLED	2 /* probe is temporarily disabled */
100 #define KPROBE_FLAG_OPTIMIZED	4 /*
101 				   * probe is really optimized.
102 				   * NOTE:
103 				   * this flag is only for optimized_kprobe.
104 				   */
105 #define KPROBE_FLAG_FTRACE	8 /* probe is using ftrace */
106 
107 /* Has this kprobe gone ? */
108 static inline bool kprobe_gone(struct kprobe *p)
109 {
110 	return p->flags & KPROBE_FLAG_GONE;
111 }
112 
113 /* Is this kprobe disabled ? */
114 static inline bool kprobe_disabled(struct kprobe *p)
115 {
116 	return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
117 }
118 
119 /* Is this kprobe really running optimized path ? */
120 static inline bool kprobe_optimized(struct kprobe *p)
121 {
122 	return p->flags & KPROBE_FLAG_OPTIMIZED;
123 }
124 
125 /* Is this kprobe uses ftrace ? */
126 static inline bool kprobe_ftrace(struct kprobe *p)
127 {
128 	return p->flags & KPROBE_FLAG_FTRACE;
129 }
130 
131 /*
132  * Function-return probe -
133  * Note:
134  * User needs to provide a handler function, and initialize maxactive.
135  * maxactive - The maximum number of instances of the probed function that
136  * can be active concurrently.
137  * nmissed - tracks the number of times the probed function's return was
138  * ignored, due to maxactive being too low.
139  *
140  */
141 struct kretprobe_holder {
142 	struct kretprobe	*rp;
143 	refcount_t		ref;
144 };
145 
146 struct kretprobe {
147 	struct kprobe kp;
148 	kretprobe_handler_t handler;
149 	kretprobe_handler_t entry_handler;
150 	int maxactive;
151 	int nmissed;
152 	size_t data_size;
153 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
154 	struct rethook *rh;
155 #else
156 	struct freelist_head freelist;
157 	struct kretprobe_holder *rph;
158 #endif
159 };
160 
161 #define KRETPROBE_MAX_DATA_SIZE	4096
162 
163 struct kretprobe_instance {
164 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
165 	struct rethook_node node;
166 #else
167 	union {
168 		struct freelist_node freelist;
169 		struct rcu_head rcu;
170 	};
171 	struct llist_node llist;
172 	struct kretprobe_holder *rph;
173 	kprobe_opcode_t *ret_addr;
174 	void *fp;
175 #endif
176 	char data[];
177 };
178 
179 struct kretprobe_blackpoint {
180 	const char *name;
181 	void *addr;
182 };
183 
184 struct kprobe_blacklist_entry {
185 	struct list_head list;
186 	unsigned long start_addr;
187 	unsigned long end_addr;
188 };
189 
190 #ifdef CONFIG_KPROBES
191 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
192 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
193 
194 extern void kprobe_busy_begin(void);
195 extern void kprobe_busy_end(void);
196 
197 #ifdef CONFIG_KRETPROBES
198 /* Check whether @p is used for implementing a trampoline. */
199 extern int arch_trampoline_kprobe(struct kprobe *p);
200 
201 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
202 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
203 {
204 	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
205 		"Kretprobe is accessed from instance under preemptive context");
206 
207 	return (struct kretprobe *)READ_ONCE(ri->node.rethook->data);
208 }
209 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
210 {
211 	return ri->node.ret_addr;
212 }
213 #else
214 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
215 				   struct pt_regs *regs);
216 void arch_kretprobe_fixup_return(struct pt_regs *regs,
217 				 kprobe_opcode_t *correct_ret_addr);
218 
219 void __kretprobe_trampoline(void);
220 /*
221  * Since some architecture uses structured function pointer,
222  * use dereference_function_descriptor() to get real function address.
223  */
224 static nokprobe_inline void *kretprobe_trampoline_addr(void)
225 {
226 	return dereference_kernel_function_descriptor(__kretprobe_trampoline);
227 }
228 
229 /* If the trampoline handler called from a kprobe, use this version */
230 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
231 					     void *frame_pointer);
232 
233 static nokprobe_inline
234 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
235 					   void *frame_pointer)
236 {
237 	unsigned long ret;
238 	/*
239 	 * Set a dummy kprobe for avoiding kretprobe recursion.
240 	 * Since kretprobe never runs in kprobe handler, no kprobe must
241 	 * be running at this point.
242 	 */
243 	kprobe_busy_begin();
244 	ret = __kretprobe_trampoline_handler(regs, frame_pointer);
245 	kprobe_busy_end();
246 
247 	return ret;
248 }
249 
250 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
251 {
252 	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
253 		"Kretprobe is accessed from instance under preemptive context");
254 
255 	return READ_ONCE(ri->rph->rp);
256 }
257 
258 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
259 {
260 	return (unsigned long)ri->ret_addr;
261 }
262 #endif /* CONFIG_KRETPROBE_ON_RETHOOK */
263 
264 #else /* !CONFIG_KRETPROBES */
265 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
266 					struct pt_regs *regs)
267 {
268 }
269 static inline int arch_trampoline_kprobe(struct kprobe *p)
270 {
271 	return 0;
272 }
273 #endif /* CONFIG_KRETPROBES */
274 
275 /* Markers of '_kprobe_blacklist' section */
276 extern unsigned long __start_kprobe_blacklist[];
277 extern unsigned long __stop_kprobe_blacklist[];
278 
279 extern struct kretprobe_blackpoint kretprobe_blacklist[];
280 
281 #ifdef CONFIG_KPROBES_SANITY_TEST
282 extern int init_test_probes(void);
283 #else /* !CONFIG_KPROBES_SANITY_TEST */
284 static inline int init_test_probes(void)
285 {
286 	return 0;
287 }
288 #endif /* CONFIG_KPROBES_SANITY_TEST */
289 
290 extern int arch_prepare_kprobe(struct kprobe *p);
291 extern void arch_arm_kprobe(struct kprobe *p);
292 extern void arch_disarm_kprobe(struct kprobe *p);
293 extern int arch_init_kprobes(void);
294 extern void kprobes_inc_nmissed_count(struct kprobe *p);
295 extern bool arch_within_kprobe_blacklist(unsigned long addr);
296 extern int arch_populate_kprobe_blacklist(void);
297 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
298 
299 extern bool within_kprobe_blacklist(unsigned long addr);
300 extern int kprobe_add_ksym_blacklist(unsigned long entry);
301 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
302 
303 struct kprobe_insn_cache {
304 	struct mutex mutex;
305 	void *(*alloc)(void);	/* allocate insn page */
306 	void (*free)(void *);	/* free insn page */
307 	const char *sym;	/* symbol for insn pages */
308 	struct list_head pages; /* list of kprobe_insn_page */
309 	size_t insn_size;	/* size of instruction slot */
310 	int nr_garbage;
311 };
312 
313 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
314 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
315 extern void __free_insn_slot(struct kprobe_insn_cache *c,
316 			     kprobe_opcode_t *slot, int dirty);
317 /* sleep-less address checking routine  */
318 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
319 				unsigned long addr);
320 
321 #define DEFINE_INSN_CACHE_OPS(__name)					\
322 extern struct kprobe_insn_cache kprobe_##__name##_slots;		\
323 									\
324 static inline kprobe_opcode_t *get_##__name##_slot(void)		\
325 {									\
326 	return __get_insn_slot(&kprobe_##__name##_slots);		\
327 }									\
328 									\
329 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
330 {									\
331 	__free_insn_slot(&kprobe_##__name##_slots, slot, dirty);	\
332 }									\
333 									\
334 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
335 {									\
336 	return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);	\
337 }
338 #define KPROBE_INSN_PAGE_SYM		"kprobe_insn_page"
339 #define KPROBE_OPTINSN_PAGE_SYM		"kprobe_optinsn_page"
340 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
341 			     unsigned long *value, char *type, char *sym);
342 #else /* !__ARCH_WANT_KPROBES_INSN_SLOT */
343 #define DEFINE_INSN_CACHE_OPS(__name)					\
344 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
345 {									\
346 	return 0;							\
347 }
348 #endif
349 
350 DEFINE_INSN_CACHE_OPS(insn);
351 
352 #ifdef CONFIG_OPTPROBES
353 /*
354  * Internal structure for direct jump optimized probe
355  */
356 struct optimized_kprobe {
357 	struct kprobe kp;
358 	struct list_head list;	/* list for optimizing queue */
359 	struct arch_optimized_insn optinsn;
360 };
361 
362 /* Architecture dependent functions for direct jump optimization */
363 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
364 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
365 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
366 					 struct kprobe *orig);
367 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
368 extern void arch_optimize_kprobes(struct list_head *oplist);
369 extern void arch_unoptimize_kprobes(struct list_head *oplist,
370 				    struct list_head *done_list);
371 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
372 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
373 					kprobe_opcode_t *addr);
374 
375 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
376 
377 DEFINE_INSN_CACHE_OPS(optinsn);
378 
379 extern void wait_for_kprobe_optimizer(void);
380 #else /* !CONFIG_OPTPROBES */
381 static inline void wait_for_kprobe_optimizer(void) { }
382 #endif /* CONFIG_OPTPROBES */
383 
384 #ifdef CONFIG_KPROBES_ON_FTRACE
385 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
386 				  struct ftrace_ops *ops, struct ftrace_regs *fregs);
387 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
388 #else
389 static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
390 {
391 	return -EINVAL;
392 }
393 #endif /* CONFIG_KPROBES_ON_FTRACE */
394 
395 /* Get the kprobe at this addr (if any) - called with preemption disabled */
396 struct kprobe *get_kprobe(void *addr);
397 
398 /* kprobe_running() will just return the current_kprobe on this CPU */
399 static inline struct kprobe *kprobe_running(void)
400 {
401 	return __this_cpu_read(current_kprobe);
402 }
403 
404 static inline void reset_current_kprobe(void)
405 {
406 	__this_cpu_write(current_kprobe, NULL);
407 }
408 
409 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
410 {
411 	return this_cpu_ptr(&kprobe_ctlblk);
412 }
413 
414 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
415 kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry);
416 
417 int register_kprobe(struct kprobe *p);
418 void unregister_kprobe(struct kprobe *p);
419 int register_kprobes(struct kprobe **kps, int num);
420 void unregister_kprobes(struct kprobe **kps, int num);
421 
422 int register_kretprobe(struct kretprobe *rp);
423 void unregister_kretprobe(struct kretprobe *rp);
424 int register_kretprobes(struct kretprobe **rps, int num);
425 void unregister_kretprobes(struct kretprobe **rps, int num);
426 
427 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
428 #define kprobe_flush_task(tk)	do {} while (0)
429 #else
430 void kprobe_flush_task(struct task_struct *tk);
431 #endif
432 
433 void kprobe_free_init_mem(void);
434 
435 int disable_kprobe(struct kprobe *kp);
436 int enable_kprobe(struct kprobe *kp);
437 
438 void dump_kprobe(struct kprobe *kp);
439 
440 void *alloc_insn_page(void);
441 
442 void *alloc_optinsn_page(void);
443 void free_optinsn_page(void *page);
444 
445 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
446 		       char *sym);
447 
448 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
449 			    char *type, char *sym);
450 #else /* !CONFIG_KPROBES: */
451 
452 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
453 {
454 	return 0;
455 }
456 static inline struct kprobe *get_kprobe(void *addr)
457 {
458 	return NULL;
459 }
460 static inline struct kprobe *kprobe_running(void)
461 {
462 	return NULL;
463 }
464 #define kprobe_busy_begin()	do {} while (0)
465 #define kprobe_busy_end()	do {} while (0)
466 
467 static inline int register_kprobe(struct kprobe *p)
468 {
469 	return -EOPNOTSUPP;
470 }
471 static inline int register_kprobes(struct kprobe **kps, int num)
472 {
473 	return -EOPNOTSUPP;
474 }
475 static inline void unregister_kprobe(struct kprobe *p)
476 {
477 }
478 static inline void unregister_kprobes(struct kprobe **kps, int num)
479 {
480 }
481 static inline int register_kretprobe(struct kretprobe *rp)
482 {
483 	return -EOPNOTSUPP;
484 }
485 static inline int register_kretprobes(struct kretprobe **rps, int num)
486 {
487 	return -EOPNOTSUPP;
488 }
489 static inline void unregister_kretprobe(struct kretprobe *rp)
490 {
491 }
492 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
493 {
494 }
495 static inline void kprobe_flush_task(struct task_struct *tk)
496 {
497 }
498 static inline void kprobe_free_init_mem(void)
499 {
500 }
501 static inline int disable_kprobe(struct kprobe *kp)
502 {
503 	return -EOPNOTSUPP;
504 }
505 static inline int enable_kprobe(struct kprobe *kp)
506 {
507 	return -EOPNOTSUPP;
508 }
509 
510 static inline bool within_kprobe_blacklist(unsigned long addr)
511 {
512 	return true;
513 }
514 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
515 				     char *type, char *sym)
516 {
517 	return -ERANGE;
518 }
519 #endif /* CONFIG_KPROBES */
520 
521 static inline int disable_kretprobe(struct kretprobe *rp)
522 {
523 	return disable_kprobe(&rp->kp);
524 }
525 static inline int enable_kretprobe(struct kretprobe *rp)
526 {
527 	return enable_kprobe(&rp->kp);
528 }
529 
530 #ifndef CONFIG_KPROBES
531 static inline bool is_kprobe_insn_slot(unsigned long addr)
532 {
533 	return false;
534 }
535 #endif /* !CONFIG_KPROBES */
536 
537 #ifndef CONFIG_OPTPROBES
538 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
539 {
540 	return false;
541 }
542 #endif /* !CONFIG_OPTPROBES */
543 
544 #ifdef CONFIG_KRETPROBES
545 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
546 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
547 {
548 	return is_rethook_trampoline(addr);
549 }
550 
551 static nokprobe_inline
552 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
553 				      struct llist_node **cur)
554 {
555 	return rethook_find_ret_addr(tsk, (unsigned long)fp, cur);
556 }
557 #else
558 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
559 {
560 	return (void *)addr == kretprobe_trampoline_addr();
561 }
562 
563 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
564 				      struct llist_node **cur);
565 #endif
566 #else
567 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
568 {
569 	return false;
570 }
571 
572 static nokprobe_inline
573 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
574 				      struct llist_node **cur)
575 {
576 	return 0;
577 }
578 #endif
579 
580 /* Returns true if kprobes handled the fault */
581 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
582 					      unsigned int trap)
583 {
584 	if (!IS_ENABLED(CONFIG_KPROBES))
585 		return false;
586 	if (user_mode(regs))
587 		return false;
588 	/*
589 	 * To be potentially processing a kprobe fault and to be allowed
590 	 * to call kprobe_running(), we have to be non-preemptible.
591 	 */
592 	if (preemptible())
593 		return false;
594 	if (!kprobe_running())
595 		return false;
596 	return kprobe_fault_handler(regs, trap);
597 }
598 
599 #endif /* _LINUX_KPROBES_H */
600