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 ? */
kprobe_gone(struct kprobe * p)109 static inline bool kprobe_gone(struct kprobe *p)
110 {
111 return p->flags & KPROBE_FLAG_GONE;
112 }
113
114 /* Is this kprobe disabled ? */
kprobe_disabled(struct kprobe * p)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 ? */
kprobe_optimized(struct kprobe * p)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 ? */
kprobe_ftrace(struct kprobe * p)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 __rcu *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
get_kretprobe(struct kretprobe_instance * ri)203 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
204 {
205 /* rethook::data is non-changed field, so that you can access it freely. */
206 return (struct kretprobe *)ri->node.rethook->data;
207 }
get_kretprobe_retaddr(struct kretprobe_instance * ri)208 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
209 {
210 return ri->node.ret_addr;
211 }
212 #else
213 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
214 struct pt_regs *regs);
215 void arch_kretprobe_fixup_return(struct pt_regs *regs,
216 kprobe_opcode_t *correct_ret_addr);
217
218 void __kretprobe_trampoline(void);
219 /*
220 * Since some architecture uses structured function pointer,
221 * use dereference_function_descriptor() to get real function address.
222 */
kretprobe_trampoline_addr(void)223 static nokprobe_inline void *kretprobe_trampoline_addr(void)
224 {
225 return dereference_kernel_function_descriptor(__kretprobe_trampoline);
226 }
227
228 /* If the trampoline handler called from a kprobe, use this version */
229 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
230 void *frame_pointer);
231
232 static nokprobe_inline
kretprobe_trampoline_handler(struct pt_regs * regs,void * frame_pointer)233 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
234 void *frame_pointer)
235 {
236 unsigned long ret;
237 /*
238 * Set a dummy kprobe for avoiding kretprobe recursion.
239 * Since kretprobe never runs in kprobe handler, no kprobe must
240 * be running at this point.
241 */
242 kprobe_busy_begin();
243 ret = __kretprobe_trampoline_handler(regs, frame_pointer);
244 kprobe_busy_end();
245
246 return ret;
247 }
248
get_kretprobe(struct kretprobe_instance * ri)249 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
250 {
251 return rcu_dereference_check(ri->rph->rp, rcu_read_lock_any_held());
252 }
253
get_kretprobe_retaddr(struct kretprobe_instance * ri)254 static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
255 {
256 return (unsigned long)ri->ret_addr;
257 }
258 #endif /* CONFIG_KRETPROBE_ON_RETHOOK */
259
260 #else /* !CONFIG_KRETPROBES */
arch_prepare_kretprobe(struct kretprobe * rp,struct pt_regs * regs)261 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
262 struct pt_regs *regs)
263 {
264 }
arch_trampoline_kprobe(struct kprobe * p)265 static inline int arch_trampoline_kprobe(struct kprobe *p)
266 {
267 return 0;
268 }
269 #endif /* CONFIG_KRETPROBES */
270
271 /* Markers of '_kprobe_blacklist' section */
272 extern unsigned long __start_kprobe_blacklist[];
273 extern unsigned long __stop_kprobe_blacklist[];
274
275 extern struct kretprobe_blackpoint kretprobe_blacklist[];
276
277 #ifdef CONFIG_KPROBES_SANITY_TEST
278 extern int init_test_probes(void);
279 #else /* !CONFIG_KPROBES_SANITY_TEST */
init_test_probes(void)280 static inline int init_test_probes(void)
281 {
282 return 0;
283 }
284 #endif /* CONFIG_KPROBES_SANITY_TEST */
285
286 extern int arch_prepare_kprobe(struct kprobe *p);
287 extern void arch_arm_kprobe(struct kprobe *p);
288 extern void arch_disarm_kprobe(struct kprobe *p);
289 extern int arch_init_kprobes(void);
290 extern void kprobes_inc_nmissed_count(struct kprobe *p);
291 extern bool arch_within_kprobe_blacklist(unsigned long addr);
292 extern int arch_populate_kprobe_blacklist(void);
293 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
294
295 extern bool within_kprobe_blacklist(unsigned long addr);
296 extern int kprobe_add_ksym_blacklist(unsigned long entry);
297 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
298
299 struct kprobe_insn_cache {
300 struct mutex mutex;
301 void *(*alloc)(void); /* allocate insn page */
302 void (*free)(void *); /* free insn page */
303 const char *sym; /* symbol for insn pages */
304 struct list_head pages; /* list of kprobe_insn_page */
305 size_t insn_size; /* size of instruction slot */
306 int nr_garbage;
307 };
308
309 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
310 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
311 extern void __free_insn_slot(struct kprobe_insn_cache *c,
312 kprobe_opcode_t *slot, int dirty);
313 /* sleep-less address checking routine */
314 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
315 unsigned long addr);
316
317 #define DEFINE_INSN_CACHE_OPS(__name) \
318 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
319 \
320 static inline kprobe_opcode_t *get_##__name##_slot(void) \
321 { \
322 return __get_insn_slot(&kprobe_##__name##_slots); \
323 } \
324 \
325 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
326 { \
327 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
328 } \
329 \
330 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
331 { \
332 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
333 }
334 #define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
335 #define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
336 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
337 unsigned long *value, char *type, char *sym);
338 #else /* !__ARCH_WANT_KPROBES_INSN_SLOT */
339 #define DEFINE_INSN_CACHE_OPS(__name) \
340 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
341 { \
342 return 0; \
343 }
344 #endif
345
346 DEFINE_INSN_CACHE_OPS(insn);
347
348 #ifdef CONFIG_OPTPROBES
349 /*
350 * Internal structure for direct jump optimized probe
351 */
352 struct optimized_kprobe {
353 struct kprobe kp;
354 struct list_head list; /* list for optimizing queue */
355 struct arch_optimized_insn optinsn;
356 };
357
358 /* Architecture dependent functions for direct jump optimization */
359 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
360 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
361 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
362 struct kprobe *orig);
363 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
364 extern void arch_optimize_kprobes(struct list_head *oplist);
365 extern void arch_unoptimize_kprobes(struct list_head *oplist,
366 struct list_head *done_list);
367 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
368 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
369 kprobe_opcode_t *addr);
370
371 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
372
373 DEFINE_INSN_CACHE_OPS(optinsn);
374
375 extern void wait_for_kprobe_optimizer(void);
376 bool optprobe_queued_unopt(struct optimized_kprobe *op);
377 bool kprobe_disarmed(struct kprobe *p);
378 #else /* !CONFIG_OPTPROBES */
wait_for_kprobe_optimizer(void)379 static inline void wait_for_kprobe_optimizer(void) { }
380 #endif /* CONFIG_OPTPROBES */
381
382 #ifdef CONFIG_KPROBES_ON_FTRACE
383 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
384 struct ftrace_ops *ops, struct ftrace_regs *fregs);
385 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
386 /* Set when ftrace has been killed: kprobes on ftrace must be disabled for safety */
387 extern bool kprobe_ftrace_disabled __read_mostly;
388 extern void kprobe_ftrace_kill(void);
389 #else
arch_prepare_kprobe_ftrace(struct kprobe * p)390 static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
391 {
392 return -EINVAL;
393 }
kprobe_ftrace_kill(void)394 static inline void kprobe_ftrace_kill(void) {}
395 #endif /* CONFIG_KPROBES_ON_FTRACE */
396
397 /* Get the kprobe at this addr (if any) - called with preemption disabled */
398 struct kprobe *get_kprobe(void *addr);
399
400 /* kprobe_running() will just return the current_kprobe on this CPU */
kprobe_running(void)401 static inline struct kprobe *kprobe_running(void)
402 {
403 return __this_cpu_read(current_kprobe);
404 }
405
reset_current_kprobe(void)406 static inline void reset_current_kprobe(void)
407 {
408 __this_cpu_write(current_kprobe, NULL);
409 }
410
get_kprobe_ctlblk(void)411 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
412 {
413 return this_cpu_ptr(&kprobe_ctlblk);
414 }
415
416 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
417 kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry);
418
419 int register_kprobe(struct kprobe *p);
420 void unregister_kprobe(struct kprobe *p);
421 int register_kprobes(struct kprobe **kps, int num);
422 void unregister_kprobes(struct kprobe **kps, int num);
423
424 int register_kretprobe(struct kretprobe *rp);
425 void unregister_kretprobe(struct kretprobe *rp);
426 int register_kretprobes(struct kretprobe **rps, int num);
427 void unregister_kretprobes(struct kretprobe **rps, int num);
428
429 #if defined(CONFIG_KRETPROBE_ON_RETHOOK) || !defined(CONFIG_KRETPROBES)
430 #define kprobe_flush_task(tk) do {} while (0)
431 #else
432 void kprobe_flush_task(struct task_struct *tk);
433 #endif
434
435 void kprobe_free_init_mem(void);
436
437 int disable_kprobe(struct kprobe *kp);
438 int enable_kprobe(struct kprobe *kp);
439
440 void dump_kprobe(struct kprobe *kp);
441
442 void *alloc_insn_page(void);
443
444 void *alloc_optinsn_page(void);
445 void free_optinsn_page(void *page);
446
447 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
448 char *sym);
449
450 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
451 char *type, char *sym);
452 #else /* !CONFIG_KPROBES: */
453
kprobe_fault_handler(struct pt_regs * regs,int trapnr)454 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
455 {
456 return 0;
457 }
get_kprobe(void * addr)458 static inline struct kprobe *get_kprobe(void *addr)
459 {
460 return NULL;
461 }
kprobe_running(void)462 static inline struct kprobe *kprobe_running(void)
463 {
464 return NULL;
465 }
466 #define kprobe_busy_begin() do {} while (0)
467 #define kprobe_busy_end() do {} while (0)
468
register_kprobe(struct kprobe * p)469 static inline int register_kprobe(struct kprobe *p)
470 {
471 return -EOPNOTSUPP;
472 }
register_kprobes(struct kprobe ** kps,int num)473 static inline int register_kprobes(struct kprobe **kps, int num)
474 {
475 return -EOPNOTSUPP;
476 }
unregister_kprobe(struct kprobe * p)477 static inline void unregister_kprobe(struct kprobe *p)
478 {
479 }
unregister_kprobes(struct kprobe ** kps,int num)480 static inline void unregister_kprobes(struct kprobe **kps, int num)
481 {
482 }
register_kretprobe(struct kretprobe * rp)483 static inline int register_kretprobe(struct kretprobe *rp)
484 {
485 return -EOPNOTSUPP;
486 }
register_kretprobes(struct kretprobe ** rps,int num)487 static inline int register_kretprobes(struct kretprobe **rps, int num)
488 {
489 return -EOPNOTSUPP;
490 }
unregister_kretprobe(struct kretprobe * rp)491 static inline void unregister_kretprobe(struct kretprobe *rp)
492 {
493 }
unregister_kretprobes(struct kretprobe ** rps,int num)494 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
495 {
496 }
kprobe_flush_task(struct task_struct * tk)497 static inline void kprobe_flush_task(struct task_struct *tk)
498 {
499 }
kprobe_free_init_mem(void)500 static inline void kprobe_free_init_mem(void)
501 {
502 }
kprobe_ftrace_kill(void)503 static inline void kprobe_ftrace_kill(void)
504 {
505 }
disable_kprobe(struct kprobe * kp)506 static inline int disable_kprobe(struct kprobe *kp)
507 {
508 return -EOPNOTSUPP;
509 }
enable_kprobe(struct kprobe * kp)510 static inline int enable_kprobe(struct kprobe *kp)
511 {
512 return -EOPNOTSUPP;
513 }
514
within_kprobe_blacklist(unsigned long addr)515 static inline bool within_kprobe_blacklist(unsigned long addr)
516 {
517 return true;
518 }
kprobe_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)519 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
520 char *type, char *sym)
521 {
522 return -ERANGE;
523 }
524 #endif /* CONFIG_KPROBES */
525
disable_kretprobe(struct kretprobe * rp)526 static inline int disable_kretprobe(struct kretprobe *rp)
527 {
528 return disable_kprobe(&rp->kp);
529 }
enable_kretprobe(struct kretprobe * rp)530 static inline int enable_kretprobe(struct kretprobe *rp)
531 {
532 return enable_kprobe(&rp->kp);
533 }
534
535 #ifndef CONFIG_KPROBES
is_kprobe_insn_slot(unsigned long addr)536 static inline bool is_kprobe_insn_slot(unsigned long addr)
537 {
538 return false;
539 }
540 #endif /* !CONFIG_KPROBES */
541
542 #ifndef CONFIG_OPTPROBES
is_kprobe_optinsn_slot(unsigned long addr)543 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
544 {
545 return false;
546 }
547 #endif /* !CONFIG_OPTPROBES */
548
549 #ifdef CONFIG_KRETPROBES
550 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
is_kretprobe_trampoline(unsigned long addr)551 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
552 {
553 return is_rethook_trampoline(addr);
554 }
555
556 static nokprobe_inline
kretprobe_find_ret_addr(struct task_struct * tsk,void * fp,struct llist_node ** cur)557 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
558 struct llist_node **cur)
559 {
560 return rethook_find_ret_addr(tsk, (unsigned long)fp, cur);
561 }
562 #else
is_kretprobe_trampoline(unsigned long addr)563 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
564 {
565 return (void *)addr == kretprobe_trampoline_addr();
566 }
567
568 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
569 struct llist_node **cur);
570 #endif
571 #else
is_kretprobe_trampoline(unsigned long addr)572 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
573 {
574 return false;
575 }
576
577 static nokprobe_inline
kretprobe_find_ret_addr(struct task_struct * tsk,void * fp,struct llist_node ** cur)578 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
579 struct llist_node **cur)
580 {
581 return 0;
582 }
583 #endif
584
585 /* Returns true if kprobes handled the fault */
kprobe_page_fault(struct pt_regs * regs,unsigned int trap)586 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
587 unsigned int trap)
588 {
589 if (!IS_ENABLED(CONFIG_KPROBES))
590 return false;
591 if (user_mode(regs))
592 return false;
593 /*
594 * To be potentially processing a kprobe fault and to be allowed
595 * to call kprobe_running(), we have to be non-preemptible.
596 */
597 if (preemptible())
598 return false;
599 if (!kprobe_running())
600 return false;
601 return kprobe_fault_handler(regs, trap);
602 }
603
604 #endif /* _LINUX_KPROBES_H */
605