• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *  include/linux/kprobes.h
7  *
8  * Copyright (C) IBM Corporation, 2002, 2004
9  *
10  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
11  *		Probes initial implementation ( includes suggestions from
12  *		Rusty Russell).
13  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
14  *		interface to access function arguments.
15  * 2005-May	Hien Nguyen <hien@us.ibm.com> and Jim Keniston
16  *		<jkenisto@us.ibm.com>  and Prasanna S Panchamukhi
17  *		<prasanna@in.ibm.com> added function-return probes.
18  */
19 #include <linux/compiler.h>
20 #include <linux/linkage.h>
21 #include <linux/list.h>
22 #include <linux/notifier.h>
23 #include <linux/smp.h>
24 #include <linux/bug.h>
25 #include <linux/percpu.h>
26 #include <linux/spinlock.h>
27 #include <linux/rcupdate.h>
28 #include <linux/mutex.h>
29 #include <linux/ftrace.h>
30 #include <linux/refcount.h>
31 #include <linux/freelist.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 ? */
kprobe_gone(struct kprobe * p)108 static inline int kprobe_gone(struct kprobe *p)
109 {
110 	return p->flags & KPROBE_FLAG_GONE;
111 }
112 
113 /* Is this kprobe disabled ? */
kprobe_disabled(struct kprobe * p)114 static inline int 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 ? */
kprobe_optimized(struct kprobe * p)120 static inline int kprobe_optimized(struct kprobe *p)
121 {
122 	return p->flags & KPROBE_FLAG_OPTIMIZED;
123 }
124 
125 /* Is this kprobe uses ftrace ? */
kprobe_ftrace(struct kprobe * p)126 static inline int 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 __rcu *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 	struct freelist_head freelist;
154 	struct kretprobe_holder *rph;
155 };
156 
157 #define KRETPROBE_MAX_DATA_SIZE	4096
158 
159 struct kretprobe_instance {
160 	union {
161 		struct freelist_node freelist;
162 		struct rcu_head rcu;
163 	};
164 	struct llist_node llist;
165 	struct kretprobe_holder *rph;
166 	kprobe_opcode_t *ret_addr;
167 	void *fp;
168 	char data[];
169 };
170 
171 struct kretprobe_blackpoint {
172 	const char *name;
173 	void *addr;
174 };
175 
176 struct kprobe_blacklist_entry {
177 	struct list_head list;
178 	unsigned long start_addr;
179 	unsigned long end_addr;
180 };
181 
182 #ifdef CONFIG_KPROBES
183 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
184 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
185 
186 /*
187  * For #ifdef avoidance:
188  */
kprobes_built_in(void)189 static inline int kprobes_built_in(void)
190 {
191 	return 1;
192 }
193 
194 extern void kprobe_busy_begin(void);
195 extern void kprobe_busy_end(void);
196 
197 #ifdef CONFIG_KRETPROBES
198 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
199 				   struct pt_regs *regs);
200 extern int arch_trampoline_kprobe(struct kprobe *p);
201 
202 /* If the trampoline handler called from a kprobe, use this version */
203 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
204 				void *trampoline_address,
205 				void *frame_pointer);
206 
207 static nokprobe_inline
kretprobe_trampoline_handler(struct pt_regs * regs,void * trampoline_address,void * frame_pointer)208 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
209 				void *trampoline_address,
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, trampoline_address, frame_pointer);
220 	kprobe_busy_end();
221 
222 	return ret;
223 }
224 
get_kretprobe(struct kretprobe_instance * ri)225 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
226 {
227 	return rcu_dereference_check(ri->rph->rp, rcu_read_lock_any_held());
228 }
229 
230 #else /* CONFIG_KRETPROBES */
arch_prepare_kretprobe(struct kretprobe * rp,struct pt_regs * regs)231 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
232 					struct pt_regs *regs)
233 {
234 }
arch_trampoline_kprobe(struct kprobe * p)235 static inline int arch_trampoline_kprobe(struct kprobe *p)
236 {
237 	return 0;
238 }
239 #endif /* CONFIG_KRETPROBES */
240 
241 extern struct kretprobe_blackpoint kretprobe_blacklist[];
242 
243 #ifdef CONFIG_KPROBES_SANITY_TEST
244 extern int init_test_probes(void);
245 #else
init_test_probes(void)246 static inline int init_test_probes(void)
247 {
248 	return 0;
249 }
250 #endif /* CONFIG_KPROBES_SANITY_TEST */
251 
252 extern int arch_prepare_kprobe(struct kprobe *p);
253 extern void arch_arm_kprobe(struct kprobe *p);
254 extern void arch_disarm_kprobe(struct kprobe *p);
255 extern int arch_init_kprobes(void);
256 extern void kprobes_inc_nmissed_count(struct kprobe *p);
257 extern bool arch_within_kprobe_blacklist(unsigned long addr);
258 extern int arch_populate_kprobe_blacklist(void);
259 extern bool arch_kprobe_on_func_entry(unsigned long offset);
260 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
261 
262 extern bool within_kprobe_blacklist(unsigned long addr);
263 extern int kprobe_add_ksym_blacklist(unsigned long entry);
264 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
265 
266 struct kprobe_insn_cache {
267 	struct mutex mutex;
268 	void *(*alloc)(void);	/* allocate insn page */
269 	void (*free)(void *);	/* free insn page */
270 	const char *sym;	/* symbol for insn pages */
271 	struct list_head pages; /* list of kprobe_insn_page */
272 	size_t insn_size;	/* size of instruction slot */
273 	int nr_garbage;
274 };
275 
276 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
277 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
278 extern void __free_insn_slot(struct kprobe_insn_cache *c,
279 			     kprobe_opcode_t *slot, int dirty);
280 /* sleep-less address checking routine  */
281 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
282 				unsigned long addr);
283 
284 #define DEFINE_INSN_CACHE_OPS(__name)					\
285 extern struct kprobe_insn_cache kprobe_##__name##_slots;		\
286 									\
287 static inline kprobe_opcode_t *get_##__name##_slot(void)		\
288 {									\
289 	return __get_insn_slot(&kprobe_##__name##_slots);		\
290 }									\
291 									\
292 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
293 {									\
294 	__free_insn_slot(&kprobe_##__name##_slots, slot, dirty);	\
295 }									\
296 									\
297 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
298 {									\
299 	return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);	\
300 }
301 #define KPROBE_INSN_PAGE_SYM		"kprobe_insn_page"
302 #define KPROBE_OPTINSN_PAGE_SYM		"kprobe_optinsn_page"
303 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
304 			     unsigned long *value, char *type, char *sym);
305 #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
306 #define DEFINE_INSN_CACHE_OPS(__name)					\
307 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
308 {									\
309 	return 0;							\
310 }
311 #endif
312 
313 DEFINE_INSN_CACHE_OPS(insn);
314 
315 #ifdef CONFIG_OPTPROBES
316 /*
317  * Internal structure for direct jump optimized probe
318  */
319 struct optimized_kprobe {
320 	struct kprobe kp;
321 	struct list_head list;	/* list for optimizing queue */
322 	struct arch_optimized_insn optinsn;
323 };
324 
325 /* Architecture dependent functions for direct jump optimization */
326 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
327 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
328 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
329 					 struct kprobe *orig);
330 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
331 extern void arch_optimize_kprobes(struct list_head *oplist);
332 extern void arch_unoptimize_kprobes(struct list_head *oplist,
333 				    struct list_head *done_list);
334 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
335 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
336 					unsigned long addr);
337 
338 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
339 
340 DEFINE_INSN_CACHE_OPS(optinsn);
341 
342 #ifdef CONFIG_SYSCTL
343 extern int sysctl_kprobes_optimization;
344 extern int proc_kprobes_optimization_handler(struct ctl_table *table,
345 					     int write, void *buffer,
346 					     size_t *length, loff_t *ppos);
347 #endif
348 extern void wait_for_kprobe_optimizer(void);
349 bool optprobe_queued_unopt(struct optimized_kprobe *op);
350 bool kprobe_disarmed(struct kprobe *p);
351 #else
wait_for_kprobe_optimizer(void)352 static inline void wait_for_kprobe_optimizer(void) { }
353 #endif /* CONFIG_OPTPROBES */
354 #ifdef CONFIG_KPROBES_ON_FTRACE
355 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
356 				  struct ftrace_ops *ops, struct ftrace_regs *fregs);
357 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
358 #endif
359 
360 int arch_check_ftrace_location(struct kprobe *p);
361 
362 /* Get the kprobe at this addr (if any) - called with preemption disabled */
363 struct kprobe *get_kprobe(void *addr);
364 
365 /* kprobe_running() will just return the current_kprobe on this CPU */
kprobe_running(void)366 static inline struct kprobe *kprobe_running(void)
367 {
368 	return (__this_cpu_read(current_kprobe));
369 }
370 
reset_current_kprobe(void)371 static inline void reset_current_kprobe(void)
372 {
373 	__this_cpu_write(current_kprobe, NULL);
374 }
375 
get_kprobe_ctlblk(void)376 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
377 {
378 	return this_cpu_ptr(&kprobe_ctlblk);
379 }
380 
381 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
382 int register_kprobe(struct kprobe *p);
383 void unregister_kprobe(struct kprobe *p);
384 int register_kprobes(struct kprobe **kps, int num);
385 void unregister_kprobes(struct kprobe **kps, int num);
386 unsigned long arch_deref_entry_point(void *);
387 
388 int register_kretprobe(struct kretprobe *rp);
389 void unregister_kretprobe(struct kretprobe *rp);
390 int register_kretprobes(struct kretprobe **rps, int num);
391 void unregister_kretprobes(struct kretprobe **rps, int num);
392 
393 void kprobe_flush_task(struct task_struct *tk);
394 
395 void kprobe_free_init_mem(void);
396 
397 int disable_kprobe(struct kprobe *kp);
398 int enable_kprobe(struct kprobe *kp);
399 
400 void dump_kprobe(struct kprobe *kp);
401 
402 void *alloc_insn_page(void);
403 
404 void *alloc_optinsn_page(void);
405 void free_optinsn_page(void *page);
406 
407 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
408 		       char *sym);
409 
410 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
411 			    char *type, char *sym);
412 #else /* !CONFIG_KPROBES: */
413 
kprobes_built_in(void)414 static inline int kprobes_built_in(void)
415 {
416 	return 0;
417 }
kprobe_fault_handler(struct pt_regs * regs,int trapnr)418 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
419 {
420 	return 0;
421 }
get_kprobe(void * addr)422 static inline struct kprobe *get_kprobe(void *addr)
423 {
424 	return NULL;
425 }
kprobe_running(void)426 static inline struct kprobe *kprobe_running(void)
427 {
428 	return NULL;
429 }
register_kprobe(struct kprobe * p)430 static inline int register_kprobe(struct kprobe *p)
431 {
432 	return -ENOSYS;
433 }
register_kprobes(struct kprobe ** kps,int num)434 static inline int register_kprobes(struct kprobe **kps, int num)
435 {
436 	return -ENOSYS;
437 }
unregister_kprobe(struct kprobe * p)438 static inline void unregister_kprobe(struct kprobe *p)
439 {
440 }
unregister_kprobes(struct kprobe ** kps,int num)441 static inline void unregister_kprobes(struct kprobe **kps, int num)
442 {
443 }
register_kretprobe(struct kretprobe * rp)444 static inline int register_kretprobe(struct kretprobe *rp)
445 {
446 	return -ENOSYS;
447 }
register_kretprobes(struct kretprobe ** rps,int num)448 static inline int register_kretprobes(struct kretprobe **rps, int num)
449 {
450 	return -ENOSYS;
451 }
unregister_kretprobe(struct kretprobe * rp)452 static inline void unregister_kretprobe(struct kretprobe *rp)
453 {
454 }
unregister_kretprobes(struct kretprobe ** rps,int num)455 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
456 {
457 }
kprobe_flush_task(struct task_struct * tk)458 static inline void kprobe_flush_task(struct task_struct *tk)
459 {
460 }
kprobe_free_init_mem(void)461 static inline void kprobe_free_init_mem(void)
462 {
463 }
disable_kprobe(struct kprobe * kp)464 static inline int disable_kprobe(struct kprobe *kp)
465 {
466 	return -ENOSYS;
467 }
enable_kprobe(struct kprobe * kp)468 static inline int enable_kprobe(struct kprobe *kp)
469 {
470 	return -ENOSYS;
471 }
472 
within_kprobe_blacklist(unsigned long addr)473 static inline bool within_kprobe_blacklist(unsigned long addr)
474 {
475 	return true;
476 }
kprobe_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)477 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
478 				     char *type, char *sym)
479 {
480 	return -ERANGE;
481 }
482 #endif /* CONFIG_KPROBES */
disable_kretprobe(struct kretprobe * rp)483 static inline int disable_kretprobe(struct kretprobe *rp)
484 {
485 	return disable_kprobe(&rp->kp);
486 }
enable_kretprobe(struct kretprobe * rp)487 static inline int enable_kretprobe(struct kretprobe *rp)
488 {
489 	return enable_kprobe(&rp->kp);
490 }
491 
492 #ifndef CONFIG_KPROBES
is_kprobe_insn_slot(unsigned long addr)493 static inline bool is_kprobe_insn_slot(unsigned long addr)
494 {
495 	return false;
496 }
497 #endif
498 #ifndef CONFIG_OPTPROBES
is_kprobe_optinsn_slot(unsigned long addr)499 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
500 {
501 	return false;
502 }
503 #endif
504 
505 /* Returns true if kprobes handled the fault */
kprobe_page_fault(struct pt_regs * regs,unsigned int trap)506 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
507 					      unsigned int trap)
508 {
509 	if (!kprobes_built_in())
510 		return false;
511 	if (user_mode(regs))
512 		return false;
513 	/*
514 	 * To be potentially processing a kprobe fault and to be allowed
515 	 * to call kprobe_running(), we have to be non-preemptible.
516 	 */
517 	if (preemptible())
518 		return false;
519 	if (!kprobe_running())
520 		return false;
521 	return kprobe_fault_handler(regs, trap);
522 }
523 
524 #endif /* _LINUX_KPROBES_H */
525