xref: /linux/include/linux/kprobes.h (revision 07f0148aafe8c95a3a76cd59e9e75b4d78d1d31d)
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 #define KPROBE_FLAG_ON_FUNC_ENTRY	16 /* probe is on the function entry */
107 
108 /* Has this kprobe gone ? */
109 static inline bool kprobe_gone(struct kprobe *p)
110 {
111 	return p->flags & KPROBE_FLAG_GONE;
112 }
113 
114 /* Is this kprobe disabled ? */
115 static inline bool kprobe_disabled(struct kprobe *p)
116 {
117 	return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
118 }
119 
120 /* Is this kprobe really running optimized path ? */
121 static inline bool kprobe_optimized(struct kprobe *p)
122 {
123 	return p->flags & KPROBE_FLAG_OPTIMIZED;
124 }
125 
126 /* Is this kprobe uses ftrace ? */
127 static inline bool kprobe_ftrace(struct kprobe *p)
128 {
129 	return p->flags & KPROBE_FLAG_FTRACE;
130 }
131 
132 /*
133  * Function-return probe -
134  * Note:
135  * User needs to provide a handler function, and initialize maxactive.
136  * maxactive - The maximum number of instances of the probed function that
137  * can be active concurrently.
138  * nmissed - tracks the number of times the probed function's return was
139  * ignored, due to maxactive being too low.
140  *
141  */
142 struct kretprobe_holder {
143 	struct kretprobe	*rp;
144 	refcount_t		ref;
145 };
146 
147 struct kretprobe {
148 	struct kprobe kp;
149 	kretprobe_handler_t handler;
150 	kretprobe_handler_t entry_handler;
151 	int maxactive;
152 	int nmissed;
153 	size_t data_size;
154 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
155 	struct rethook *rh;
156 #else
157 	struct freelist_head freelist;
158 	struct kretprobe_holder *rph;
159 #endif
160 };
161 
162 #define KRETPROBE_MAX_DATA_SIZE	4096
163 
164 struct kretprobe_instance {
165 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
166 	struct rethook_node node;
167 #else
168 	union {
169 		struct freelist_node freelist;
170 		struct rcu_head rcu;
171 	};
172 	struct llist_node llist;
173 	struct kretprobe_holder *rph;
174 	kprobe_opcode_t *ret_addr;
175 	void *fp;
176 #endif
177 	char data[];
178 };
179 
180 struct kretprobe_blackpoint {
181 	const char *name;
182 	void *addr;
183 };
184 
185 struct kprobe_blacklist_entry {
186 	struct list_head list;
187 	unsigned long start_addr;
188 	unsigned long end_addr;
189 };
190 
191 #ifdef CONFIG_KPROBES
192 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
193 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
194 
195 extern void kprobe_busy_begin(void);
196 extern void kprobe_busy_end(void);
197 
198 #ifdef CONFIG_KRETPROBES
199 /* Check whether @p is used for implementing a trampoline. */
200 extern int arch_trampoline_kprobe(struct kprobe *p);
201 
202 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
203 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
204 {
205 	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
206 		"Kretprobe is accessed from instance under preemptive context");
207 
208 	return (struct kretprobe *)READ_ONCE(ri->node.rethook->data);
209 }
210 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
211 {
212 	return ri->node.ret_addr;
213 }
214 #else
215 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
216 				   struct pt_regs *regs);
217 void arch_kretprobe_fixup_return(struct pt_regs *regs,
218 				 kprobe_opcode_t *correct_ret_addr);
219 
220 void __kretprobe_trampoline(void);
221 /*
222  * Since some architecture uses structured function pointer,
223  * use dereference_function_descriptor() to get real function address.
224  */
225 static nokprobe_inline void *kretprobe_trampoline_addr(void)
226 {
227 	return dereference_kernel_function_descriptor(__kretprobe_trampoline);
228 }
229 
230 /* If the trampoline handler called from a kprobe, use this version */
231 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
232 					     void *frame_pointer);
233 
234 static nokprobe_inline
235 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
236 					   void *frame_pointer)
237 {
238 	unsigned long ret;
239 	/*
240 	 * Set a dummy kprobe for avoiding kretprobe recursion.
241 	 * Since kretprobe never runs in kprobe handler, no kprobe must
242 	 * be running at this point.
243 	 */
244 	kprobe_busy_begin();
245 	ret = __kretprobe_trampoline_handler(regs, frame_pointer);
246 	kprobe_busy_end();
247 
248 	return ret;
249 }
250 
251 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
252 {
253 	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
254 		"Kretprobe is accessed from instance under preemptive context");
255 
256 	return READ_ONCE(ri->rph->rp);
257 }
258 
259 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
260 {
261 	return (unsigned long)ri->ret_addr;
262 }
263 #endif /* CONFIG_KRETPROBE_ON_RETHOOK */
264 
265 #else /* !CONFIG_KRETPROBES */
266 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
267 					struct pt_regs *regs)
268 {
269 }
270 static inline int arch_trampoline_kprobe(struct kprobe *p)
271 {
272 	return 0;
273 }
274 #endif /* CONFIG_KRETPROBES */
275 
276 /* Markers of '_kprobe_blacklist' section */
277 extern unsigned long __start_kprobe_blacklist[];
278 extern unsigned long __stop_kprobe_blacklist[];
279 
280 extern struct kretprobe_blackpoint kretprobe_blacklist[];
281 
282 #ifdef CONFIG_KPROBES_SANITY_TEST
283 extern int init_test_probes(void);
284 #else /* !CONFIG_KPROBES_SANITY_TEST */
285 static inline int init_test_probes(void)
286 {
287 	return 0;
288 }
289 #endif /* CONFIG_KPROBES_SANITY_TEST */
290 
291 extern int arch_prepare_kprobe(struct kprobe *p);
292 extern void arch_arm_kprobe(struct kprobe *p);
293 extern void arch_disarm_kprobe(struct kprobe *p);
294 extern int arch_init_kprobes(void);
295 extern void kprobes_inc_nmissed_count(struct kprobe *p);
296 extern bool arch_within_kprobe_blacklist(unsigned long addr);
297 extern int arch_populate_kprobe_blacklist(void);
298 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
299 
300 extern bool within_kprobe_blacklist(unsigned long addr);
301 extern int kprobe_add_ksym_blacklist(unsigned long entry);
302 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
303 
304 struct kprobe_insn_cache {
305 	struct mutex mutex;
306 	void *(*alloc)(void);	/* allocate insn page */
307 	void (*free)(void *);	/* free insn page */
308 	const char *sym;	/* symbol for insn pages */
309 	struct list_head pages; /* list of kprobe_insn_page */
310 	size_t insn_size;	/* size of instruction slot */
311 	int nr_garbage;
312 };
313 
314 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
315 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
316 extern void __free_insn_slot(struct kprobe_insn_cache *c,
317 			     kprobe_opcode_t *slot, int dirty);
318 /* sleep-less address checking routine  */
319 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
320 				unsigned long addr);
321 
322 #define DEFINE_INSN_CACHE_OPS(__name)					\
323 extern struct kprobe_insn_cache kprobe_##__name##_slots;		\
324 									\
325 static inline kprobe_opcode_t *get_##__name##_slot(void)		\
326 {									\
327 	return __get_insn_slot(&kprobe_##__name##_slots);		\
328 }									\
329 									\
330 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
331 {									\
332 	__free_insn_slot(&kprobe_##__name##_slots, slot, dirty);	\
333 }									\
334 									\
335 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
336 {									\
337 	return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);	\
338 }
339 #define KPROBE_INSN_PAGE_SYM		"kprobe_insn_page"
340 #define KPROBE_OPTINSN_PAGE_SYM		"kprobe_optinsn_page"
341 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
342 			     unsigned long *value, char *type, char *sym);
343 #else /* !__ARCH_WANT_KPROBES_INSN_SLOT */
344 #define DEFINE_INSN_CACHE_OPS(__name)					\
345 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
346 {									\
347 	return 0;							\
348 }
349 #endif
350 
351 DEFINE_INSN_CACHE_OPS(insn);
352 
353 #ifdef CONFIG_OPTPROBES
354 /*
355  * Internal structure for direct jump optimized probe
356  */
357 struct optimized_kprobe {
358 	struct kprobe kp;
359 	struct list_head list;	/* list for optimizing queue */
360 	struct arch_optimized_insn optinsn;
361 };
362 
363 /* Architecture dependent functions for direct jump optimization */
364 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
365 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
366 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
367 					 struct kprobe *orig);
368 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
369 extern void arch_optimize_kprobes(struct list_head *oplist);
370 extern void arch_unoptimize_kprobes(struct list_head *oplist,
371 				    struct list_head *done_list);
372 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
373 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
374 					kprobe_opcode_t *addr);
375 
376 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
377 
378 DEFINE_INSN_CACHE_OPS(optinsn);
379 
380 extern void wait_for_kprobe_optimizer(void);
381 #else /* !CONFIG_OPTPROBES */
382 static inline void wait_for_kprobe_optimizer(void) { }
383 #endif /* CONFIG_OPTPROBES */
384 
385 #ifdef CONFIG_KPROBES_ON_FTRACE
386 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
387 				  struct ftrace_ops *ops, struct ftrace_regs *fregs);
388 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
389 #else
390 static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
391 {
392 	return -EINVAL;
393 }
394 #endif /* CONFIG_KPROBES_ON_FTRACE */
395 
396 /* Get the kprobe at this addr (if any) - called with preemption disabled */
397 struct kprobe *get_kprobe(void *addr);
398 
399 /* kprobe_running() will just return the current_kprobe on this CPU */
400 static inline struct kprobe *kprobe_running(void)
401 {
402 	return __this_cpu_read(current_kprobe);
403 }
404 
405 static inline void reset_current_kprobe(void)
406 {
407 	__this_cpu_write(current_kprobe, NULL);
408 }
409 
410 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
411 {
412 	return this_cpu_ptr(&kprobe_ctlblk);
413 }
414 
415 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
416 kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry);
417 
418 int register_kprobe(struct kprobe *p);
419 void unregister_kprobe(struct kprobe *p);
420 int register_kprobes(struct kprobe **kps, int num);
421 void unregister_kprobes(struct kprobe **kps, int num);
422 
423 int register_kretprobe(struct kretprobe *rp);
424 void unregister_kretprobe(struct kretprobe *rp);
425 int register_kretprobes(struct kretprobe **rps, int num);
426 void unregister_kretprobes(struct kretprobe **rps, int num);
427 
428 #if defined(CONFIG_KRETPROBE_ON_RETHOOK) || !defined(CONFIG_KRETPROBES)
429 #define kprobe_flush_task(tk)	do {} while (0)
430 #else
431 void kprobe_flush_task(struct task_struct *tk);
432 #endif
433 
434 void kprobe_free_init_mem(void);
435 
436 int disable_kprobe(struct kprobe *kp);
437 int enable_kprobe(struct kprobe *kp);
438 
439 void dump_kprobe(struct kprobe *kp);
440 
441 void *alloc_insn_page(void);
442 
443 void *alloc_optinsn_page(void);
444 void free_optinsn_page(void *page);
445 
446 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
447 		       char *sym);
448 
449 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
450 			    char *type, char *sym);
451 #else /* !CONFIG_KPROBES: */
452 
453 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
454 {
455 	return 0;
456 }
457 static inline struct kprobe *get_kprobe(void *addr)
458 {
459 	return NULL;
460 }
461 static inline struct kprobe *kprobe_running(void)
462 {
463 	return NULL;
464 }
465 #define kprobe_busy_begin()	do {} while (0)
466 #define kprobe_busy_end()	do {} while (0)
467 
468 static inline int register_kprobe(struct kprobe *p)
469 {
470 	return -EOPNOTSUPP;
471 }
472 static inline int register_kprobes(struct kprobe **kps, int num)
473 {
474 	return -EOPNOTSUPP;
475 }
476 static inline void unregister_kprobe(struct kprobe *p)
477 {
478 }
479 static inline void unregister_kprobes(struct kprobe **kps, int num)
480 {
481 }
482 static inline int register_kretprobe(struct kretprobe *rp)
483 {
484 	return -EOPNOTSUPP;
485 }
486 static inline int register_kretprobes(struct kretprobe **rps, int num)
487 {
488 	return -EOPNOTSUPP;
489 }
490 static inline void unregister_kretprobe(struct kretprobe *rp)
491 {
492 }
493 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
494 {
495 }
496 static inline void kprobe_flush_task(struct task_struct *tk)
497 {
498 }
499 static inline void kprobe_free_init_mem(void)
500 {
501 }
502 static inline int disable_kprobe(struct kprobe *kp)
503 {
504 	return -EOPNOTSUPP;
505 }
506 static inline int enable_kprobe(struct kprobe *kp)
507 {
508 	return -EOPNOTSUPP;
509 }
510 
511 static inline bool within_kprobe_blacklist(unsigned long addr)
512 {
513 	return true;
514 }
515 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
516 				     char *type, char *sym)
517 {
518 	return -ERANGE;
519 }
520 #endif /* CONFIG_KPROBES */
521 
522 static inline int disable_kretprobe(struct kretprobe *rp)
523 {
524 	return disable_kprobe(&rp->kp);
525 }
526 static inline int enable_kretprobe(struct kretprobe *rp)
527 {
528 	return enable_kprobe(&rp->kp);
529 }
530 
531 #ifndef CONFIG_KPROBES
532 static inline bool is_kprobe_insn_slot(unsigned long addr)
533 {
534 	return false;
535 }
536 #endif /* !CONFIG_KPROBES */
537 
538 #ifndef CONFIG_OPTPROBES
539 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
540 {
541 	return false;
542 }
543 #endif /* !CONFIG_OPTPROBES */
544 
545 #ifdef CONFIG_KRETPROBES
546 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
547 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
548 {
549 	return is_rethook_trampoline(addr);
550 }
551 
552 static nokprobe_inline
553 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
554 				      struct llist_node **cur)
555 {
556 	return rethook_find_ret_addr(tsk, (unsigned long)fp, cur);
557 }
558 #else
559 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
560 {
561 	return (void *)addr == kretprobe_trampoline_addr();
562 }
563 
564 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
565 				      struct llist_node **cur);
566 #endif
567 #else
568 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
569 {
570 	return false;
571 }
572 
573 static nokprobe_inline
574 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
575 				      struct llist_node **cur)
576 {
577 	return 0;
578 }
579 #endif
580 
581 /* Returns true if kprobes handled the fault */
582 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
583 					      unsigned int trap)
584 {
585 	if (!IS_ENABLED(CONFIG_KPROBES))
586 		return false;
587 	if (user_mode(regs))
588 		return false;
589 	/*
590 	 * To be potentially processing a kprobe fault and to be allowed
591 	 * to call kprobe_running(), we have to be non-preemptible.
592 	 */
593 	if (preemptible())
594 		return false;
595 	if (!kprobe_running())
596 		return false;
597 	return kprobe_fault_handler(regs, trap);
598 }
599 
600 #endif /* _LINUX_KPROBES_H */
601