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