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