1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * User-space Probes (UProbes) 4 * 5 * Copyright (C) IBM Corporation, 2008-2012 6 * Authors: 7 * Srikar Dronamraju 8 * Jim Keniston 9 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/highmem.h> 14 #include <linux/pagemap.h> /* read_mapping_page */ 15 #include <linux/slab.h> 16 #include <linux/sched.h> 17 #include <linux/sched/mm.h> 18 #include <linux/export.h> 19 #include <linux/rmap.h> /* anon_vma_prepare */ 20 #include <linux/mmu_notifier.h> 21 #include <linux/swap.h> /* folio_free_swap */ 22 #include <linux/ptrace.h> /* user_enable_single_step */ 23 #include <linux/kdebug.h> /* notifier mechanism */ 24 #include <linux/percpu-rwsem.h> 25 #include <linux/task_work.h> 26 #include <linux/shmem_fs.h> 27 #include <linux/khugepaged.h> 28 #include <linux/rcupdate_trace.h> 29 #include <linux/workqueue.h> 30 #include <linux/srcu.h> 31 #include <linux/oom.h> /* check_stable_address_space */ 32 #include <linux/pagewalk.h> 33 34 #include <linux/uprobes.h> 35 36 #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES) 37 #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE 38 39 static struct rb_root uprobes_tree = RB_ROOT; 40 /* 41 * allows us to skip the uprobe_mmap if there are no uprobe events active 42 * at this time. Probably a fine grained per inode count is better? 43 */ 44 #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree) 45 46 static DEFINE_RWLOCK(uprobes_treelock); /* serialize rbtree access */ 47 static seqcount_rwlock_t uprobes_seqcount = SEQCNT_RWLOCK_ZERO(uprobes_seqcount, &uprobes_treelock); 48 49 #define UPROBES_HASH_SZ 13 50 /* serialize uprobe->pending_list */ 51 static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ]; 52 #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ]) 53 54 DEFINE_STATIC_PERCPU_RWSEM(dup_mmap_sem); 55 56 /* Covers return_instance's uprobe lifetime. */ 57 DEFINE_STATIC_SRCU_FAST_UPDOWN(uretprobes_srcu); 58 59 /* Have a copy of original instruction */ 60 #define UPROBE_COPY_INSN 0 61 62 struct uprobe { 63 struct rb_node rb_node; /* node in the rb tree */ 64 refcount_t ref; 65 struct rw_semaphore register_rwsem; 66 struct rw_semaphore consumer_rwsem; 67 struct list_head pending_list; 68 struct list_head consumers; 69 struct inode *inode; /* Also hold a ref to inode */ 70 union { 71 struct rcu_head rcu; 72 struct work_struct work; 73 }; 74 loff_t offset; 75 loff_t ref_ctr_offset; 76 unsigned long flags; /* "unsigned long" so bitops work */ 77 78 /* 79 * The generic code assumes that it has two members of unknown type 80 * owned by the arch-specific code: 81 * 82 * insn - copy_insn() saves the original instruction here for 83 * arch_uprobe_analyze_insn(). 84 * 85 * ixol - potentially modified instruction to execute out of 86 * line, copied to xol_area by xol_get_insn_slot(). 87 */ 88 struct arch_uprobe arch; 89 }; 90 91 struct delayed_uprobe { 92 struct list_head list; 93 struct uprobe *uprobe; 94 struct mm_struct *mm; 95 }; 96 97 static DEFINE_MUTEX(delayed_uprobe_lock); 98 static LIST_HEAD(delayed_uprobe_list); 99 100 /* 101 * Execute out of line area: anonymous executable mapping installed 102 * by the probed task to execute the copy of the original instruction 103 * mangled by set_swbp(). 104 * 105 * On a breakpoint hit, thread contests for a slot. It frees the 106 * slot after singlestep. Currently a fixed number of slots are 107 * allocated. 108 */ 109 struct xol_area { 110 wait_queue_head_t wq; /* if all slots are busy */ 111 unsigned long *bitmap; /* 0 = free slot */ 112 113 struct page *page; 114 /* 115 * We keep the vma's vm_start rather than a pointer to the vma 116 * itself. The probed process or a naughty kernel module could make 117 * the vma go away, and we must handle that reasonably gracefully. 118 */ 119 unsigned long vaddr; /* Page(s) of instruction slots */ 120 }; 121 122 static void uprobe_warn(struct task_struct *t, const char *msg) 123 { 124 pr_warn("uprobe: %s:%d failed to %s\n", t->comm, t->pid, msg); 125 } 126 127 /* 128 * valid_vma: Verify if the specified vma is an executable vma 129 * Relax restrictions while unregistering: vm_flags might have 130 * changed after breakpoint was inserted. 131 * - is_register: indicates if we are in register context. 132 * - Return 1 if the specified virtual address is in an 133 * executable vma. 134 */ 135 static bool valid_vma(struct vm_area_struct *vma, bool is_register) 136 { 137 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE; 138 139 if (is_register) 140 flags |= VM_WRITE; 141 142 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC; 143 } 144 145 static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset) 146 { 147 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 148 } 149 150 static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr) 151 { 152 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start); 153 } 154 155 /** 156 * is_swbp_insn - check if instruction is breakpoint instruction. 157 * @insn: instruction to be checked. 158 * Default implementation of is_swbp_insn 159 * Returns true if @insn is a breakpoint instruction. 160 */ 161 bool __weak is_swbp_insn(uprobe_opcode_t *insn) 162 { 163 return *insn == UPROBE_SWBP_INSN; 164 } 165 166 /** 167 * is_trap_insn - check if instruction is breakpoint instruction. 168 * @insn: instruction to be checked. 169 * Default implementation of is_trap_insn 170 * Returns true if @insn is a breakpoint instruction. 171 * 172 * This function is needed for the case where an architecture has multiple 173 * trap instructions (like powerpc). 174 */ 175 bool __weak is_trap_insn(uprobe_opcode_t *insn) 176 { 177 return is_swbp_insn(insn); 178 } 179 180 void uprobe_copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len) 181 { 182 void *kaddr = kmap_local_page(page); 183 memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len); 184 kunmap_local(kaddr); 185 } 186 187 static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len) 188 { 189 void *kaddr = kmap_local_page(page); 190 memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len); 191 kunmap_local(kaddr); 192 } 193 194 static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *insn, 195 int nbytes, void *data) 196 { 197 uprobe_opcode_t old_opcode; 198 bool is_swbp; 199 200 /* 201 * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here. 202 * We do not check if it is any other 'trap variant' which could 203 * be conditional trap instruction such as the one powerpc supports. 204 * 205 * The logic is that we do not care if the underlying instruction 206 * is a trap variant; uprobes always wins over any other (gdb) 207 * breakpoint. 208 */ 209 uprobe_copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE); 210 is_swbp = is_swbp_insn(&old_opcode); 211 212 if (is_swbp_insn(insn)) { 213 if (is_swbp) /* register: already installed? */ 214 return 0; 215 } else { 216 if (!is_swbp) /* unregister: was it changed by us? */ 217 return 0; 218 } 219 220 return 1; 221 } 222 223 static struct delayed_uprobe * 224 delayed_uprobe_check(struct uprobe *uprobe, struct mm_struct *mm) 225 { 226 struct delayed_uprobe *du; 227 228 list_for_each_entry(du, &delayed_uprobe_list, list) 229 if (du->uprobe == uprobe && du->mm == mm) 230 return du; 231 return NULL; 232 } 233 234 static int delayed_uprobe_add(struct uprobe *uprobe, struct mm_struct *mm) 235 { 236 struct delayed_uprobe *du; 237 238 if (delayed_uprobe_check(uprobe, mm)) 239 return 0; 240 241 du = kzalloc_obj(*du); 242 if (!du) 243 return -ENOMEM; 244 245 du->uprobe = uprobe; 246 du->mm = mm; 247 list_add(&du->list, &delayed_uprobe_list); 248 return 0; 249 } 250 251 static void delayed_uprobe_delete(struct delayed_uprobe *du) 252 { 253 if (WARN_ON(!du)) 254 return; 255 list_del(&du->list); 256 kfree(du); 257 } 258 259 static void delayed_uprobe_remove(struct uprobe *uprobe, struct mm_struct *mm) 260 { 261 struct list_head *pos, *q; 262 struct delayed_uprobe *du; 263 264 if (!uprobe && !mm) 265 return; 266 267 list_for_each_safe(pos, q, &delayed_uprobe_list) { 268 du = list_entry(pos, struct delayed_uprobe, list); 269 270 if (uprobe && du->uprobe != uprobe) 271 continue; 272 if (mm && du->mm != mm) 273 continue; 274 275 delayed_uprobe_delete(du); 276 } 277 } 278 279 static bool valid_ref_ctr_vma(struct uprobe *uprobe, 280 struct vm_area_struct *vma) 281 { 282 unsigned long vaddr = offset_to_vaddr(vma, uprobe->ref_ctr_offset); 283 284 return uprobe->ref_ctr_offset && 285 vma->vm_file && 286 file_inode(vma->vm_file) == uprobe->inode && 287 (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE && 288 vma->vm_start <= vaddr && 289 vma->vm_end > vaddr; 290 } 291 292 static struct vm_area_struct * 293 find_ref_ctr_vma(struct uprobe *uprobe, struct mm_struct *mm) 294 { 295 VMA_ITERATOR(vmi, mm, 0); 296 struct vm_area_struct *tmp; 297 298 for_each_vma(vmi, tmp) 299 if (valid_ref_ctr_vma(uprobe, tmp)) 300 return tmp; 301 302 return NULL; 303 } 304 305 static int 306 __update_ref_ctr(struct mm_struct *mm, unsigned long vaddr, short d) 307 { 308 void *kaddr; 309 struct page *page; 310 int ret; 311 short *ptr; 312 313 if (!vaddr || !d) 314 return -EINVAL; 315 316 ret = get_user_pages_remote(mm, vaddr, 1, 317 FOLL_WRITE, &page, NULL); 318 if (unlikely(ret <= 0)) { 319 /* 320 * We are asking for 1 page. If get_user_pages_remote() fails, 321 * it may return 0, in that case we have to return error. 322 */ 323 return ret == 0 ? -EBUSY : ret; 324 } 325 326 kaddr = kmap_local_page(page); 327 ptr = kaddr + (vaddr & ~PAGE_MASK); 328 329 if (unlikely(*ptr + d < 0)) { 330 pr_warn("ref_ctr going negative. vaddr: 0x%lx, " 331 "curr val: %d, delta: %d\n", vaddr, *ptr, d); 332 ret = -EINVAL; 333 goto out; 334 } 335 336 *ptr += d; 337 ret = 0; 338 out: 339 kunmap_local(kaddr); 340 put_page(page); 341 return ret; 342 } 343 344 static void update_ref_ctr_warn(struct uprobe *uprobe, 345 struct mm_struct *mm, short d) 346 { 347 pr_warn("ref_ctr %s failed for inode: 0x%llx offset: " 348 "0x%llx ref_ctr_offset: 0x%llx of mm: 0x%p\n", 349 d > 0 ? "increment" : "decrement", uprobe->inode->i_ino, 350 (unsigned long long) uprobe->offset, 351 (unsigned long long) uprobe->ref_ctr_offset, mm); 352 } 353 354 static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm, 355 short d) 356 { 357 struct vm_area_struct *rc_vma; 358 unsigned long rc_vaddr; 359 int ret = 0; 360 361 rc_vma = find_ref_ctr_vma(uprobe, mm); 362 363 if (rc_vma) { 364 rc_vaddr = offset_to_vaddr(rc_vma, uprobe->ref_ctr_offset); 365 ret = __update_ref_ctr(mm, rc_vaddr, d); 366 if (ret) 367 update_ref_ctr_warn(uprobe, mm, d); 368 369 if (d > 0) 370 return ret; 371 } 372 373 mutex_lock(&delayed_uprobe_lock); 374 if (d > 0) 375 ret = delayed_uprobe_add(uprobe, mm); 376 else 377 delayed_uprobe_remove(uprobe, mm); 378 mutex_unlock(&delayed_uprobe_lock); 379 380 return ret; 381 } 382 383 static bool orig_page_is_identical(struct vm_area_struct *vma, 384 unsigned long vaddr, struct page *page, bool *pmd_mappable) 385 { 386 const pgoff_t index = vaddr_to_offset(vma, vaddr) >> PAGE_SHIFT; 387 struct folio *orig_folio = filemap_get_folio(vma->vm_file->f_mapping, 388 index); 389 struct page *orig_page; 390 bool identical; 391 392 if (IS_ERR(orig_folio)) 393 return false; 394 orig_page = folio_file_page(orig_folio, index); 395 396 *pmd_mappable = folio_test_pmd_mappable(orig_folio); 397 identical = folio_test_uptodate(orig_folio) && 398 pages_identical(page, orig_page); 399 folio_put(orig_folio); 400 return identical; 401 } 402 403 static int __uprobe_write(struct vm_area_struct *vma, 404 struct folio_walk *fw, struct folio *folio, 405 unsigned long insn_vaddr, uprobe_opcode_t *insn, int nbytes, 406 bool is_register) 407 { 408 const unsigned long vaddr = insn_vaddr & PAGE_MASK; 409 bool pmd_mappable; 410 411 /* For now, we'll only handle PTE-mapped folios. */ 412 if (fw->level != FW_LEVEL_PTE) 413 return -EFAULT; 414 415 /* 416 * See can_follow_write_pte(): we'd actually prefer a writable PTE here, 417 * but the VMA might not be writable. 418 */ 419 if (!pte_write(fw->pte)) { 420 if (!PageAnonExclusive(fw->page)) 421 return -EFAULT; 422 if (unlikely(userfaultfd_pte_wp(vma, fw->pte))) 423 return -EFAULT; 424 /* SOFTDIRTY is handled via pte_mkdirty() below. */ 425 } 426 427 /* 428 * We'll temporarily unmap the page and flush the TLB, such that we can 429 * modify the page atomically. 430 */ 431 flush_cache_page(vma, vaddr, pte_pfn(fw->pte)); 432 fw->pte = ptep_clear_flush(vma, vaddr, fw->ptep); 433 copy_to_page(fw->page, insn_vaddr, insn, nbytes); 434 435 /* 436 * When unregistering, we may only zap a PTE if uffd is disabled and 437 * there are no unexpected folio references ... 438 */ 439 if (is_register || userfaultfd_missing(vma) || 440 (folio_ref_count(folio) != folio_expected_ref_count(folio) + 1)) 441 goto remap; 442 443 /* 444 * ... and the mapped page is identical to the original page that 445 * would get faulted in on next access. 446 */ 447 if (!orig_page_is_identical(vma, vaddr, fw->page, &pmd_mappable)) 448 goto remap; 449 450 dec_mm_counter(vma->vm_mm, MM_ANONPAGES); 451 folio_remove_rmap_pte(folio, fw->page, vma); 452 if (!folio_mapped(folio) && folio_test_swapcache(folio) && 453 folio_trylock(folio)) { 454 folio_free_swap(folio); 455 folio_unlock(folio); 456 } 457 folio_put(folio); 458 459 return pmd_mappable; 460 remap: 461 /* 462 * Make sure that our copy_to_page() changes become visible before the 463 * set_pte_at() write. 464 */ 465 smp_wmb(); 466 /* We modified the page. Make sure to mark the PTE dirty. */ 467 set_pte_at(vma->vm_mm, vaddr, fw->ptep, pte_mkdirty(fw->pte)); 468 return 0; 469 } 470 471 /* 472 * NOTE: 473 * Expect the breakpoint instruction to be the smallest size instruction for 474 * the architecture. If an arch has variable length instruction and the 475 * breakpoint instruction is not of the smallest length instruction 476 * supported by that architecture then we need to modify is_trap_at_addr and 477 * uprobe_write_opcode accordingly. This would never be a problem for archs 478 * that have fixed length instructions. 479 * 480 * uprobe_write_opcode - write the opcode at a given virtual address. 481 * @auprobe: arch specific probepoint information. 482 * @vma: the probed virtual memory area. 483 * @opcode_vaddr: the virtual address to store the opcode. 484 * @opcode: opcode to be written at @opcode_vaddr. 485 * 486 * Called with mm->mmap_lock held for write. 487 * Return 0 (success) or a negative errno. 488 */ 489 int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, 490 const unsigned long opcode_vaddr, uprobe_opcode_t opcode, 491 bool is_register) 492 { 493 return uprobe_write(auprobe, vma, opcode_vaddr, &opcode, UPROBE_SWBP_INSN_SIZE, 494 verify_opcode, is_register, true /* do_update_ref_ctr */, NULL); 495 } 496 497 int uprobe_write(struct arch_uprobe *auprobe, struct vm_area_struct *vma, 498 const unsigned long insn_vaddr, uprobe_opcode_t *insn, int nbytes, 499 uprobe_write_verify_t verify, bool is_register, bool do_update_ref_ctr, 500 void *data) 501 { 502 const unsigned long vaddr = insn_vaddr & PAGE_MASK; 503 struct mm_struct *mm = vma->vm_mm; 504 struct uprobe *uprobe; 505 int ret, ref_ctr_updated = 0; 506 unsigned int gup_flags = FOLL_FORCE; 507 struct mmu_notifier_range range; 508 struct folio_walk fw; 509 struct folio *folio; 510 struct page *page; 511 512 uprobe = container_of(auprobe, struct uprobe, arch); 513 514 if (WARN_ON_ONCE(!is_cow_mapping(vma->vm_flags))) 515 return -EINVAL; 516 517 /* 518 * When registering, we have to break COW to get an exclusive anonymous 519 * page that we can safely modify. Use FOLL_WRITE to trigger a write 520 * fault if required. When unregistering, we might be lucky and the 521 * anon page is already gone. So defer write faults until really 522 * required. Use FOLL_SPLIT_PMD, because __uprobe_write() 523 * cannot deal with PMDs yet. 524 */ 525 if (is_register) 526 gup_flags |= FOLL_WRITE | FOLL_SPLIT_PMD; 527 528 retry: 529 ret = get_user_pages_remote(mm, vaddr, 1, gup_flags, &page, NULL); 530 if (ret <= 0) 531 goto out; 532 folio = page_folio(page); 533 534 ret = verify(page, insn_vaddr, insn, nbytes, data); 535 if (ret <= 0) { 536 folio_put(folio); 537 goto out; 538 } 539 540 /* We are going to replace instruction, update ref_ctr. */ 541 if (do_update_ref_ctr && !ref_ctr_updated && uprobe->ref_ctr_offset) { 542 ret = update_ref_ctr(uprobe, mm, is_register ? 1 : -1); 543 if (ret) { 544 folio_put(folio); 545 goto out; 546 } 547 548 ref_ctr_updated = 1; 549 } 550 551 ret = 0; 552 if (unlikely(!folio_test_anon(folio) || folio_is_zone_device(folio))) { 553 VM_WARN_ON_ONCE(is_register); 554 folio_put(folio); 555 goto out; 556 } 557 558 if (!is_register) { 559 /* 560 * In the common case, we'll be able to zap the page when 561 * unregistering. So trigger MMU notifiers now, as we won't 562 * be able to do it under PTL. 563 */ 564 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 565 vaddr, vaddr + PAGE_SIZE); 566 mmu_notifier_invalidate_range_start(&range); 567 } 568 569 ret = -EAGAIN; 570 /* Walk the page tables again, to perform the actual update. */ 571 if (folio_walk_start(&fw, vma, vaddr, 0)) { 572 if (fw.page == page) 573 ret = __uprobe_write(vma, &fw, folio, insn_vaddr, insn, nbytes, is_register); 574 folio_walk_end(&fw, vma); 575 } 576 577 if (!is_register) 578 mmu_notifier_invalidate_range_end(&range); 579 580 folio_put(folio); 581 switch (ret) { 582 case -EFAULT: 583 gup_flags |= FOLL_WRITE | FOLL_SPLIT_PMD; 584 fallthrough; 585 case -EAGAIN: 586 goto retry; 587 default: 588 break; 589 } 590 591 out: 592 /* Revert back reference counter if instruction update failed. */ 593 if (do_update_ref_ctr && ret < 0 && ref_ctr_updated) 594 update_ref_ctr(uprobe, mm, is_register ? -1 : 1); 595 596 /* try collapse pmd for compound page */ 597 if (ret > 0) 598 collapse_pte_mapped_thp(mm, vaddr, false); 599 600 return ret < 0 ? ret : 0; 601 } 602 603 /** 604 * set_swbp - store breakpoint at a given address. 605 * @auprobe: arch specific probepoint information. 606 * @vma: the probed virtual memory area. 607 * @vaddr: the virtual address to insert the opcode. 608 * 609 * For mm @mm, store the breakpoint instruction at @vaddr. 610 * Return 0 (success) or a negative errno. 611 */ 612 int __weak set_swbp(struct arch_uprobe *auprobe, struct vm_area_struct *vma, 613 unsigned long vaddr) 614 { 615 return uprobe_write_opcode(auprobe, vma, vaddr, UPROBE_SWBP_INSN, true); 616 } 617 618 /** 619 * set_orig_insn - Restore the original instruction. 620 * @vma: the probed virtual memory area. 621 * @auprobe: arch specific probepoint information. 622 * @vaddr: the virtual address to insert the opcode. 623 * 624 * For mm @mm, restore the original opcode (opcode) at @vaddr. 625 * Return 0 (success) or a negative errno. 626 */ 627 int __weak set_orig_insn(struct arch_uprobe *auprobe, 628 struct vm_area_struct *vma, unsigned long vaddr) 629 { 630 return uprobe_write_opcode(auprobe, vma, vaddr, 631 *(uprobe_opcode_t *)&auprobe->insn, false); 632 } 633 634 /* uprobe should have guaranteed positive refcount */ 635 static struct uprobe *get_uprobe(struct uprobe *uprobe) 636 { 637 refcount_inc(&uprobe->ref); 638 return uprobe; 639 } 640 641 /* 642 * uprobe should have guaranteed lifetime, which can be either of: 643 * - caller already has refcount taken (and wants an extra one); 644 * - uprobe is RCU protected and won't be freed until after grace period; 645 * - we are holding uprobes_treelock (for read or write, doesn't matter). 646 */ 647 static struct uprobe *try_get_uprobe(struct uprobe *uprobe) 648 { 649 if (refcount_inc_not_zero(&uprobe->ref)) 650 return uprobe; 651 return NULL; 652 } 653 654 static inline bool uprobe_is_active(struct uprobe *uprobe) 655 { 656 return !RB_EMPTY_NODE(&uprobe->rb_node); 657 } 658 659 static void uprobe_free_rcu_tasks_trace(struct rcu_head *rcu) 660 { 661 struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu); 662 663 kfree(uprobe); 664 } 665 666 static void uprobe_free_srcu(struct rcu_head *rcu) 667 { 668 struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu); 669 670 call_rcu_tasks_trace(&uprobe->rcu, uprobe_free_rcu_tasks_trace); 671 } 672 673 static void uprobe_free_deferred(struct work_struct *work) 674 { 675 struct uprobe *uprobe = container_of(work, struct uprobe, work); 676 677 write_lock(&uprobes_treelock); 678 679 if (uprobe_is_active(uprobe)) { 680 write_seqcount_begin(&uprobes_seqcount); 681 rb_erase(&uprobe->rb_node, &uprobes_tree); 682 write_seqcount_end(&uprobes_seqcount); 683 } 684 685 write_unlock(&uprobes_treelock); 686 687 /* 688 * If application munmap(exec_vma) before uprobe_unregister() 689 * gets called, we don't get a chance to remove uprobe from 690 * delayed_uprobe_list from remove_breakpoint(). Do it here. 691 */ 692 mutex_lock(&delayed_uprobe_lock); 693 delayed_uprobe_remove(uprobe, NULL); 694 mutex_unlock(&delayed_uprobe_lock); 695 696 /* start srcu -> rcu_tasks_trace -> kfree chain */ 697 call_srcu(&uretprobes_srcu, &uprobe->rcu, uprobe_free_srcu); 698 } 699 700 static void put_uprobe(struct uprobe *uprobe) 701 { 702 if (!refcount_dec_and_test(&uprobe->ref)) 703 return; 704 705 INIT_WORK(&uprobe->work, uprobe_free_deferred); 706 schedule_work(&uprobe->work); 707 } 708 709 /* Initialize hprobe as SRCU-protected "leased" uprobe */ 710 static void hprobe_init_leased(struct hprobe *hprobe, struct uprobe *uprobe, 711 struct srcu_ctr __percpu *srcu_scp) 712 { 713 WARN_ON(!uprobe); 714 hprobe->state = HPROBE_LEASED; 715 hprobe->uprobe = uprobe; 716 hprobe->srcu_scp = srcu_scp; 717 } 718 719 /* Initialize hprobe as refcounted ("stable") uprobe (uprobe can be NULL). */ 720 static void hprobe_init_stable(struct hprobe *hprobe, struct uprobe *uprobe) 721 { 722 hprobe->state = uprobe ? HPROBE_STABLE : HPROBE_GONE; 723 hprobe->uprobe = uprobe; 724 hprobe->srcu_scp = NULL; 725 } 726 727 /* 728 * hprobe_consume() fetches hprobe's underlying uprobe and detects whether 729 * uprobe is SRCU protected or is refcounted. hprobe_consume() can be 730 * used only once for a given hprobe. 731 * 732 * Caller has to call hprobe_finalize() and pass previous hprobe_state, so 733 * that hprobe_finalize() can perform SRCU unlock or put uprobe, whichever 734 * is appropriate. 735 */ 736 static inline struct uprobe *hprobe_consume(struct hprobe *hprobe, enum hprobe_state *hstate) 737 { 738 *hstate = xchg(&hprobe->state, HPROBE_CONSUMED); 739 switch (*hstate) { 740 case HPROBE_LEASED: 741 case HPROBE_STABLE: 742 return hprobe->uprobe; 743 case HPROBE_GONE: /* uprobe is NULL, no SRCU */ 744 case HPROBE_CONSUMED: /* uprobe was finalized already, do nothing */ 745 return NULL; 746 default: 747 WARN(1, "hprobe invalid state %d", *hstate); 748 return NULL; 749 } 750 } 751 752 /* 753 * Reset hprobe state and, if hprobe was LEASED, release SRCU lock. 754 * hprobe_finalize() can only be used from current context after 755 * hprobe_consume() call (which determines uprobe and hstate value). 756 */ 757 static void hprobe_finalize(struct hprobe *hprobe, enum hprobe_state hstate) 758 { 759 switch (hstate) { 760 case HPROBE_LEASED: 761 srcu_up_read_fast(&uretprobes_srcu, hprobe->srcu_scp); 762 break; 763 case HPROBE_STABLE: 764 put_uprobe(hprobe->uprobe); 765 break; 766 case HPROBE_GONE: 767 case HPROBE_CONSUMED: 768 break; 769 default: 770 WARN(1, "hprobe invalid state %d", hstate); 771 break; 772 } 773 } 774 775 /* 776 * Attempt to switch (atomically) uprobe from being SRCU protected (LEASED) 777 * to refcounted (STABLE) state. Competes with hprobe_consume(); only one of 778 * them can win the race to perform SRCU unlocking. Whoever wins must perform 779 * SRCU unlock. 780 * 781 * Returns underlying valid uprobe or NULL, if there was no underlying uprobe 782 * to begin with or we failed to bump its refcount and it's going away. 783 * 784 * Returned non-NULL uprobe can be still safely used within an ongoing SRCU 785 * locked region. If `get` is true, it's guaranteed that non-NULL uprobe has 786 * an extra refcount for caller to assume and use. Otherwise, it's not 787 * guaranteed that returned uprobe has a positive refcount, so caller has to 788 * attempt try_get_uprobe(), if it needs to preserve uprobe beyond current 789 * SRCU lock region. See dup_utask(). 790 */ 791 static struct uprobe *hprobe_expire(struct hprobe *hprobe, bool get) 792 { 793 enum hprobe_state hstate; 794 795 /* 796 * Caller should guarantee that return_instance is not going to be 797 * freed from under us. This can be achieved either through holding 798 * rcu_read_lock() or by owning return_instance in the first place. 799 * 800 * Underlying uprobe is itself protected from reuse by SRCU, so ensure 801 * SRCU lock is held properly. 802 */ 803 lockdep_assert(srcu_read_lock_held(&uretprobes_srcu)); 804 805 hstate = READ_ONCE(hprobe->state); 806 switch (hstate) { 807 case HPROBE_STABLE: 808 /* uprobe has positive refcount, bump refcount, if necessary */ 809 return get ? get_uprobe(hprobe->uprobe) : hprobe->uprobe; 810 case HPROBE_GONE: 811 /* 812 * SRCU was unlocked earlier and we didn't manage to take 813 * uprobe refcnt, so it's effectively NULL 814 */ 815 return NULL; 816 case HPROBE_CONSUMED: 817 /* 818 * uprobe was consumed, so it's effectively NULL as far as 819 * uretprobe processing logic is concerned 820 */ 821 return NULL; 822 case HPROBE_LEASED: { 823 struct uprobe *uprobe = try_get_uprobe(hprobe->uprobe); 824 /* 825 * Try to switch hprobe state, guarding against 826 * hprobe_consume() or another hprobe_expire() racing with us. 827 * Note, if we failed to get uprobe refcount, we use special 828 * HPROBE_GONE state to signal that hprobe->uprobe shouldn't 829 * be used as it will be freed after SRCU is unlocked. 830 */ 831 if (try_cmpxchg(&hprobe->state, &hstate, uprobe ? HPROBE_STABLE : HPROBE_GONE)) { 832 /* We won the race, we are the ones to unlock SRCU */ 833 srcu_up_read_fast(&uretprobes_srcu, hprobe->srcu_scp); 834 return get && uprobe ? get_uprobe(uprobe) : uprobe; 835 } 836 837 /* 838 * We lost the race, undo refcount bump (if it ever happened), 839 * unless caller would like an extra refcount anyways. 840 */ 841 if (uprobe && !get) 842 put_uprobe(uprobe); 843 /* 844 * Even if hprobe_consume() or another hprobe_expire() wins 845 * the state update race and unlocks SRCU from under us, we 846 * still have a guarantee that underyling uprobe won't be 847 * freed due to ongoing caller's SRCU lock region, so we can 848 * return it regardless. Also, if `get` was true, we also have 849 * an extra ref for the caller to own. This is used in dup_utask(). 850 */ 851 return uprobe; 852 } 853 default: 854 WARN(1, "unknown hprobe state %d", hstate); 855 return NULL; 856 } 857 } 858 859 static __always_inline 860 int uprobe_cmp(const struct inode *l_inode, const loff_t l_offset, 861 const struct uprobe *r) 862 { 863 if (l_inode < r->inode) 864 return -1; 865 866 if (l_inode > r->inode) 867 return 1; 868 869 if (l_offset < r->offset) 870 return -1; 871 872 if (l_offset > r->offset) 873 return 1; 874 875 return 0; 876 } 877 878 #define __node_2_uprobe(node) \ 879 rb_entry((node), struct uprobe, rb_node) 880 881 struct __uprobe_key { 882 struct inode *inode; 883 loff_t offset; 884 }; 885 886 static inline int __uprobe_cmp_key(const void *key, const struct rb_node *b) 887 { 888 const struct __uprobe_key *a = key; 889 return uprobe_cmp(a->inode, a->offset, __node_2_uprobe(b)); 890 } 891 892 static inline int __uprobe_cmp(struct rb_node *a, const struct rb_node *b) 893 { 894 struct uprobe *u = __node_2_uprobe(a); 895 return uprobe_cmp(u->inode, u->offset, __node_2_uprobe(b)); 896 } 897 898 /* 899 * Assumes being inside RCU protected region. 900 * No refcount is taken on returned uprobe. 901 */ 902 static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset) 903 { 904 struct __uprobe_key key = { 905 .inode = inode, 906 .offset = offset, 907 }; 908 struct rb_node *node; 909 unsigned int seq; 910 911 lockdep_assert(rcu_read_lock_trace_held()); 912 913 do { 914 seq = read_seqcount_begin(&uprobes_seqcount); 915 node = rb_find_rcu(&key, &uprobes_tree, __uprobe_cmp_key); 916 /* 917 * Lockless RB-tree lookups can result only in false negatives. 918 * If the element is found, it is correct and can be returned 919 * under RCU protection. If we find nothing, we need to 920 * validate that seqcount didn't change. If it did, we have to 921 * try again as we might have missed the element (false 922 * negative). If seqcount is unchanged, search truly failed. 923 */ 924 if (node) 925 return __node_2_uprobe(node); 926 } while (read_seqcount_retry(&uprobes_seqcount, seq)); 927 928 return NULL; 929 } 930 931 /* 932 * Attempt to insert a new uprobe into uprobes_tree. 933 * 934 * If uprobe already exists (for given inode+offset), we just increment 935 * refcount of previously existing uprobe. 936 * 937 * If not, a provided new instance of uprobe is inserted into the tree (with 938 * assumed initial refcount == 1). 939 * 940 * In any case, we return a uprobe instance that ends up being in uprobes_tree. 941 * Caller has to clean up new uprobe instance, if it ended up not being 942 * inserted into the tree. 943 * 944 * We assume that uprobes_treelock is held for writing. 945 */ 946 static struct uprobe *__insert_uprobe(struct uprobe *uprobe) 947 { 948 struct rb_node *node; 949 again: 950 node = rb_find_add_rcu(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp); 951 if (node) { 952 struct uprobe *u = __node_2_uprobe(node); 953 954 if (!try_get_uprobe(u)) { 955 rb_erase(node, &uprobes_tree); 956 RB_CLEAR_NODE(&u->rb_node); 957 goto again; 958 } 959 960 return u; 961 } 962 963 return uprobe; 964 } 965 966 /* 967 * Acquire uprobes_treelock and insert uprobe into uprobes_tree 968 * (or reuse existing one, see __insert_uprobe() comments above). 969 */ 970 static struct uprobe *insert_uprobe(struct uprobe *uprobe) 971 { 972 struct uprobe *u; 973 974 write_lock(&uprobes_treelock); 975 write_seqcount_begin(&uprobes_seqcount); 976 u = __insert_uprobe(uprobe); 977 write_seqcount_end(&uprobes_seqcount); 978 write_unlock(&uprobes_treelock); 979 980 return u; 981 } 982 983 static void 984 ref_ctr_mismatch_warn(struct uprobe *cur_uprobe, struct uprobe *uprobe) 985 { 986 pr_warn("ref_ctr_offset mismatch. inode: 0x%llx offset: 0x%llx " 987 "ref_ctr_offset(old): 0x%llx ref_ctr_offset(new): 0x%llx\n", 988 uprobe->inode->i_ino, (unsigned long long) uprobe->offset, 989 (unsigned long long) cur_uprobe->ref_ctr_offset, 990 (unsigned long long) uprobe->ref_ctr_offset); 991 } 992 993 static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, 994 loff_t ref_ctr_offset) 995 { 996 struct uprobe *uprobe, *cur_uprobe; 997 998 uprobe = kzalloc_obj(struct uprobe); 999 if (!uprobe) 1000 return ERR_PTR(-ENOMEM); 1001 1002 uprobe->inode = inode; 1003 uprobe->offset = offset; 1004 uprobe->ref_ctr_offset = ref_ctr_offset; 1005 INIT_LIST_HEAD(&uprobe->consumers); 1006 init_rwsem(&uprobe->register_rwsem); 1007 init_rwsem(&uprobe->consumer_rwsem); 1008 RB_CLEAR_NODE(&uprobe->rb_node); 1009 refcount_set(&uprobe->ref, 1); 1010 1011 /* add to uprobes_tree, sorted on inode:offset */ 1012 cur_uprobe = insert_uprobe(uprobe); 1013 /* a uprobe exists for this inode:offset combination */ 1014 if (cur_uprobe != uprobe) { 1015 if (cur_uprobe->ref_ctr_offset != uprobe->ref_ctr_offset) { 1016 ref_ctr_mismatch_warn(cur_uprobe, uprobe); 1017 put_uprobe(cur_uprobe); 1018 kfree(uprobe); 1019 return ERR_PTR(-EINVAL); 1020 } 1021 kfree(uprobe); 1022 uprobe = cur_uprobe; 1023 } 1024 1025 return uprobe; 1026 } 1027 1028 static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) 1029 { 1030 static atomic64_t id; 1031 1032 down_write(&uprobe->consumer_rwsem); 1033 list_add_rcu(&uc->cons_node, &uprobe->consumers); 1034 uc->id = (__u64) atomic64_inc_return(&id); 1035 up_write(&uprobe->consumer_rwsem); 1036 } 1037 1038 /* 1039 * For uprobe @uprobe, delete the consumer @uc. 1040 * Should never be called with consumer that's not part of @uprobe->consumers. 1041 */ 1042 static void consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc) 1043 { 1044 down_write(&uprobe->consumer_rwsem); 1045 list_del_rcu(&uc->cons_node); 1046 up_write(&uprobe->consumer_rwsem); 1047 } 1048 1049 static int __copy_insn(struct address_space *mapping, struct file *filp, 1050 void *insn, int nbytes, loff_t offset) 1051 { 1052 struct page *page; 1053 /* 1054 * Ensure that the page that has the original instruction is populated 1055 * and in page-cache. If ->read_folio == NULL it must be shmem_mapping(), 1056 * see uprobe_register(). 1057 */ 1058 if (mapping->a_ops->read_folio) 1059 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp); 1060 else 1061 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT); 1062 if (IS_ERR(page)) 1063 return PTR_ERR(page); 1064 1065 uprobe_copy_from_page(page, offset, insn, nbytes); 1066 put_page(page); 1067 1068 return 0; 1069 } 1070 1071 static int copy_insn(struct uprobe *uprobe, struct file *filp) 1072 { 1073 struct address_space *mapping = uprobe->inode->i_mapping; 1074 loff_t offs = uprobe->offset; 1075 void *insn = &uprobe->arch.insn; 1076 int size = sizeof(uprobe->arch.insn); 1077 int len, err = -EIO; 1078 1079 /* Copy only available bytes, -EIO if nothing was read */ 1080 do { 1081 if (offs >= i_size_read(uprobe->inode)) 1082 break; 1083 1084 len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK)); 1085 err = __copy_insn(mapping, filp, insn, len, offs); 1086 if (err) 1087 break; 1088 1089 insn += len; 1090 offs += len; 1091 size -= len; 1092 } while (size); 1093 1094 return err; 1095 } 1096 1097 static int prepare_uprobe(struct uprobe *uprobe, struct file *file, 1098 struct mm_struct *mm, unsigned long vaddr) 1099 { 1100 int ret = 0; 1101 1102 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) 1103 return ret; 1104 1105 /* TODO: move this into _register, until then we abuse this sem. */ 1106 down_write(&uprobe->consumer_rwsem); 1107 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) 1108 goto out; 1109 1110 ret = copy_insn(uprobe, file); 1111 if (ret) 1112 goto out; 1113 1114 ret = -ENOTSUPP; 1115 if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn)) 1116 goto out; 1117 1118 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr); 1119 if (ret) 1120 goto out; 1121 1122 smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */ 1123 set_bit(UPROBE_COPY_INSN, &uprobe->flags); 1124 1125 out: 1126 up_write(&uprobe->consumer_rwsem); 1127 1128 return ret; 1129 } 1130 1131 static inline bool consumer_filter(struct uprobe_consumer *uc, struct mm_struct *mm) 1132 { 1133 return !uc->filter || uc->filter(uc, mm); 1134 } 1135 1136 static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) 1137 { 1138 struct uprobe_consumer *uc; 1139 bool ret = false; 1140 1141 down_read(&uprobe->consumer_rwsem); 1142 list_for_each_entry(uc, &uprobe->consumers, cons_node) { 1143 ret = consumer_filter(uc, mm); 1144 if (ret) 1145 break; 1146 } 1147 up_read(&uprobe->consumer_rwsem); 1148 1149 return ret; 1150 } 1151 1152 static int install_breakpoint(struct uprobe *uprobe, struct vm_area_struct *vma, 1153 unsigned long vaddr) 1154 { 1155 struct mm_struct *mm = vma->vm_mm; 1156 bool first_uprobe; 1157 int ret; 1158 1159 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr); 1160 if (ret) 1161 return ret; 1162 1163 /* 1164 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(), 1165 * the task can hit this breakpoint right after __replace_page(). 1166 */ 1167 first_uprobe = !mm_flags_test(MMF_HAS_UPROBES, mm); 1168 if (first_uprobe) 1169 mm_flags_set(MMF_HAS_UPROBES, mm); 1170 1171 ret = set_swbp(&uprobe->arch, vma, vaddr); 1172 if (!ret) 1173 mm_flags_clear(MMF_RECALC_UPROBES, mm); 1174 else if (first_uprobe) 1175 mm_flags_clear(MMF_HAS_UPROBES, mm); 1176 1177 return ret; 1178 } 1179 1180 static int remove_breakpoint(struct uprobe *uprobe, struct vm_area_struct *vma, 1181 unsigned long vaddr) 1182 { 1183 struct mm_struct *mm = vma->vm_mm; 1184 1185 mm_flags_set(MMF_RECALC_UPROBES, mm); 1186 return set_orig_insn(&uprobe->arch, vma, vaddr); 1187 } 1188 1189 struct map_info { 1190 struct map_info *next; 1191 struct mm_struct *mm; 1192 unsigned long vaddr; 1193 }; 1194 1195 static inline struct map_info *free_map_info(struct map_info *info) 1196 { 1197 struct map_info *next = info->next; 1198 kfree(info); 1199 return next; 1200 } 1201 1202 static struct map_info * 1203 build_map_info(struct address_space *mapping, loff_t offset, bool is_register) 1204 { 1205 unsigned long pgoff = offset >> PAGE_SHIFT; 1206 struct vm_area_struct *vma; 1207 struct map_info *curr = NULL; 1208 struct map_info *prev = NULL; 1209 struct map_info *info; 1210 int more = 0; 1211 1212 again: 1213 i_mmap_lock_read(mapping); 1214 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { 1215 if (!valid_vma(vma, is_register)) 1216 continue; 1217 1218 if (!prev && !more) { 1219 /* 1220 * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through 1221 * reclaim. This is optimistic, no harm done if it fails. 1222 */ 1223 prev = kmalloc_obj(struct map_info, 1224 GFP_NOWAIT | __GFP_NOMEMALLOC); 1225 if (prev) 1226 prev->next = NULL; 1227 } 1228 if (!prev) { 1229 more++; 1230 continue; 1231 } 1232 1233 if (!mmget_not_zero(vma->vm_mm)) 1234 continue; 1235 1236 info = prev; 1237 prev = prev->next; 1238 info->next = curr; 1239 curr = info; 1240 1241 info->mm = vma->vm_mm; 1242 info->vaddr = offset_to_vaddr(vma, offset); 1243 } 1244 i_mmap_unlock_read(mapping); 1245 1246 if (!more) 1247 goto out; 1248 1249 prev = curr; 1250 while (curr) { 1251 mmput(curr->mm); 1252 curr = curr->next; 1253 } 1254 1255 do { 1256 info = kmalloc_obj(struct map_info); 1257 if (!info) { 1258 curr = ERR_PTR(-ENOMEM); 1259 goto out; 1260 } 1261 info->next = prev; 1262 prev = info; 1263 } while (--more); 1264 1265 goto again; 1266 out: 1267 while (prev) 1268 prev = free_map_info(prev); 1269 return curr; 1270 } 1271 1272 static int 1273 register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) 1274 { 1275 bool is_register = !!new; 1276 struct map_info *info; 1277 int err = 0; 1278 1279 percpu_down_write(&dup_mmap_sem); 1280 info = build_map_info(uprobe->inode->i_mapping, 1281 uprobe->offset, is_register); 1282 if (IS_ERR(info)) { 1283 err = PTR_ERR(info); 1284 goto out; 1285 } 1286 1287 while (info) { 1288 struct mm_struct *mm = info->mm; 1289 struct vm_area_struct *vma; 1290 1291 if (err && is_register) 1292 goto free; 1293 /* 1294 * We take mmap_lock for writing to avoid the race with 1295 * find_active_uprobe_rcu() which takes mmap_lock for reading. 1296 * Thus this install_breakpoint() can not make 1297 * is_trap_at_addr() true right after find_uprobe_rcu() 1298 * returns NULL in find_active_uprobe_rcu(). 1299 */ 1300 mmap_write_lock(mm); 1301 if (check_stable_address_space(mm)) 1302 goto unlock; 1303 1304 vma = find_vma(mm, info->vaddr); 1305 if (!vma || !valid_vma(vma, is_register) || 1306 file_inode(vma->vm_file) != uprobe->inode) 1307 goto unlock; 1308 1309 if (vma->vm_start > info->vaddr || 1310 vaddr_to_offset(vma, info->vaddr) != uprobe->offset) 1311 goto unlock; 1312 1313 if (is_register) { 1314 /* consult only the "caller", new consumer. */ 1315 if (consumer_filter(new, mm)) 1316 err = install_breakpoint(uprobe, vma, info->vaddr); 1317 } else if (mm_flags_test(MMF_HAS_UPROBES, mm)) { 1318 if (!filter_chain(uprobe, mm)) 1319 err |= remove_breakpoint(uprobe, vma, info->vaddr); 1320 } 1321 1322 unlock: 1323 mmap_write_unlock(mm); 1324 free: 1325 mmput(mm); 1326 info = free_map_info(info); 1327 } 1328 out: 1329 percpu_up_write(&dup_mmap_sem); 1330 return err; 1331 } 1332 1333 /** 1334 * uprobe_unregister_nosync - unregister an already registered probe. 1335 * @uprobe: uprobe to remove 1336 * @uc: identify which probe if multiple probes are colocated. 1337 */ 1338 void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc) 1339 { 1340 int err; 1341 1342 down_write(&uprobe->register_rwsem); 1343 consumer_del(uprobe, uc); 1344 err = register_for_each_vma(uprobe, NULL); 1345 up_write(&uprobe->register_rwsem); 1346 1347 /* TODO : cant unregister? schedule a worker thread */ 1348 if (unlikely(err)) { 1349 uprobe_warn(current, "unregister, leaking uprobe"); 1350 return; 1351 } 1352 1353 put_uprobe(uprobe); 1354 } 1355 EXPORT_SYMBOL_GPL(uprobe_unregister_nosync); 1356 1357 void uprobe_unregister_sync(void) 1358 { 1359 /* 1360 * Now that handler_chain() and handle_uretprobe_chain() iterate over 1361 * uprobe->consumers list under RCU protection without holding 1362 * uprobe->register_rwsem, we need to wait for RCU grace period to 1363 * make sure that we can't call into just unregistered 1364 * uprobe_consumer's callbacks anymore. If we don't do that, fast and 1365 * unlucky enough caller can free consumer's memory and cause 1366 * handler_chain() or handle_uretprobe_chain() to do an use-after-free. 1367 */ 1368 synchronize_rcu_tasks_trace(); 1369 synchronize_srcu(&uretprobes_srcu); 1370 } 1371 EXPORT_SYMBOL_GPL(uprobe_unregister_sync); 1372 1373 /** 1374 * uprobe_register - register a probe 1375 * @inode: the file in which the probe has to be placed. 1376 * @offset: offset from the start of the file. 1377 * @ref_ctr_offset: offset of SDT marker / reference counter 1378 * @uc: information on howto handle the probe.. 1379 * 1380 * Apart from the access refcount, uprobe_register() takes a creation 1381 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting 1382 * inserted into the rbtree (i.e first consumer for a @inode:@offset 1383 * tuple). Creation refcount stops uprobe_unregister from freeing the 1384 * @uprobe even before the register operation is complete. Creation 1385 * refcount is released when the last @uc for the @uprobe 1386 * unregisters. Caller of uprobe_register() is required to keep @inode 1387 * (and the containing mount) referenced. 1388 * 1389 * Return: pointer to the new uprobe on success or an ERR_PTR on failure. 1390 */ 1391 struct uprobe *uprobe_register(struct inode *inode, 1392 loff_t offset, loff_t ref_ctr_offset, 1393 struct uprobe_consumer *uc) 1394 { 1395 struct uprobe *uprobe; 1396 int ret; 1397 1398 /* Uprobe must have at least one set consumer */ 1399 if (!uc->handler && !uc->ret_handler) 1400 return ERR_PTR(-EINVAL); 1401 1402 /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */ 1403 if (!inode->i_mapping->a_ops->read_folio && 1404 !shmem_mapping(inode->i_mapping)) 1405 return ERR_PTR(-EIO); 1406 /* Racy, just to catch the obvious mistakes */ 1407 if (offset > i_size_read(inode)) 1408 return ERR_PTR(-EINVAL); 1409 1410 /* 1411 * This ensures that uprobe_copy_from_page(), copy_to_page() and 1412 * __update_ref_ctr() can't cross page boundary. 1413 */ 1414 if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE)) 1415 return ERR_PTR(-EINVAL); 1416 if (!IS_ALIGNED(ref_ctr_offset, sizeof(short))) 1417 return ERR_PTR(-EINVAL); 1418 1419 uprobe = alloc_uprobe(inode, offset, ref_ctr_offset); 1420 if (IS_ERR(uprobe)) 1421 return uprobe; 1422 1423 down_write(&uprobe->register_rwsem); 1424 consumer_add(uprobe, uc); 1425 ret = register_for_each_vma(uprobe, uc); 1426 up_write(&uprobe->register_rwsem); 1427 1428 if (ret) { 1429 uprobe_unregister_nosync(uprobe, uc); 1430 /* 1431 * Registration might have partially succeeded, so we can have 1432 * this consumer being called right at this time. We need to 1433 * sync here. It's ok, it's unlikely slow path. 1434 */ 1435 uprobe_unregister_sync(); 1436 return ERR_PTR(ret); 1437 } 1438 1439 return uprobe; 1440 } 1441 EXPORT_SYMBOL_GPL(uprobe_register); 1442 1443 /** 1444 * uprobe_apply - add or remove the breakpoints according to @uc->filter 1445 * @uprobe: uprobe which "owns" the breakpoint 1446 * @uc: consumer which wants to add more or remove some breakpoints 1447 * @add: add or remove the breakpoints 1448 * Return: 0 on success or negative error code. 1449 */ 1450 int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool add) 1451 { 1452 struct uprobe_consumer *con; 1453 int ret = -ENOENT; 1454 1455 down_write(&uprobe->register_rwsem); 1456 1457 rcu_read_lock_trace(); 1458 list_for_each_entry_rcu(con, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { 1459 if (con == uc) { 1460 ret = register_for_each_vma(uprobe, add ? uc : NULL); 1461 break; 1462 } 1463 } 1464 rcu_read_unlock_trace(); 1465 1466 up_write(&uprobe->register_rwsem); 1467 1468 return ret; 1469 } 1470 1471 static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm) 1472 { 1473 VMA_ITERATOR(vmi, mm, 0); 1474 struct vm_area_struct *vma; 1475 int err = 0; 1476 1477 mmap_write_lock(mm); 1478 for_each_vma(vmi, vma) { 1479 unsigned long vaddr; 1480 loff_t offset; 1481 1482 if (!valid_vma(vma, false) || 1483 file_inode(vma->vm_file) != uprobe->inode) 1484 continue; 1485 1486 offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT; 1487 if (uprobe->offset < offset || 1488 uprobe->offset >= offset + vma->vm_end - vma->vm_start) 1489 continue; 1490 1491 vaddr = offset_to_vaddr(vma, uprobe->offset); 1492 err |= remove_breakpoint(uprobe, vma, vaddr); 1493 } 1494 mmap_write_unlock(mm); 1495 1496 return err; 1497 } 1498 1499 static struct rb_node * 1500 find_node_in_range(struct inode *inode, loff_t min, loff_t max) 1501 { 1502 struct rb_node *n = uprobes_tree.rb_node; 1503 1504 while (n) { 1505 struct uprobe *u = rb_entry(n, struct uprobe, rb_node); 1506 1507 if (inode < u->inode) { 1508 n = n->rb_left; 1509 } else if (inode > u->inode) { 1510 n = n->rb_right; 1511 } else { 1512 if (max < u->offset) 1513 n = n->rb_left; 1514 else if (min > u->offset) 1515 n = n->rb_right; 1516 else 1517 break; 1518 } 1519 } 1520 1521 return n; 1522 } 1523 1524 /* 1525 * For a given range in vma, build a list of probes that need to be inserted. 1526 */ 1527 static void build_probe_list(struct inode *inode, 1528 struct vm_area_struct *vma, 1529 unsigned long start, unsigned long end, 1530 struct list_head *head) 1531 { 1532 loff_t min, max; 1533 struct rb_node *n, *t; 1534 struct uprobe *u; 1535 1536 INIT_LIST_HEAD(head); 1537 min = vaddr_to_offset(vma, start); 1538 max = min + (end - start) - 1; 1539 1540 read_lock(&uprobes_treelock); 1541 n = find_node_in_range(inode, min, max); 1542 if (n) { 1543 for (t = n; t; t = rb_prev(t)) { 1544 u = rb_entry(t, struct uprobe, rb_node); 1545 if (u->inode != inode || u->offset < min) 1546 break; 1547 /* if uprobe went away, it's safe to ignore it */ 1548 if (try_get_uprobe(u)) 1549 list_add(&u->pending_list, head); 1550 } 1551 for (t = n; (t = rb_next(t)); ) { 1552 u = rb_entry(t, struct uprobe, rb_node); 1553 if (u->inode != inode || u->offset > max) 1554 break; 1555 /* if uprobe went away, it's safe to ignore it */ 1556 if (try_get_uprobe(u)) 1557 list_add(&u->pending_list, head); 1558 } 1559 } 1560 read_unlock(&uprobes_treelock); 1561 } 1562 1563 /* @vma contains reference counter, not the probed instruction. */ 1564 static int delayed_ref_ctr_inc(struct vm_area_struct *vma) 1565 { 1566 struct list_head *pos, *q; 1567 struct delayed_uprobe *du; 1568 unsigned long vaddr; 1569 int ret = 0, err = 0; 1570 1571 mutex_lock(&delayed_uprobe_lock); 1572 list_for_each_safe(pos, q, &delayed_uprobe_list) { 1573 du = list_entry(pos, struct delayed_uprobe, list); 1574 1575 if (du->mm != vma->vm_mm || 1576 !valid_ref_ctr_vma(du->uprobe, vma)) 1577 continue; 1578 1579 vaddr = offset_to_vaddr(vma, du->uprobe->ref_ctr_offset); 1580 ret = __update_ref_ctr(vma->vm_mm, vaddr, 1); 1581 if (ret) { 1582 update_ref_ctr_warn(du->uprobe, vma->vm_mm, 1); 1583 if (!err) 1584 err = ret; 1585 } 1586 delayed_uprobe_delete(du); 1587 } 1588 mutex_unlock(&delayed_uprobe_lock); 1589 return err; 1590 } 1591 1592 /* 1593 * Called from mmap_region/vma_merge with mm->mmap_lock acquired. 1594 * 1595 * Currently we ignore all errors and always return 0, the callers 1596 * can't handle the failure anyway. 1597 */ 1598 int uprobe_mmap(struct vm_area_struct *vma) 1599 { 1600 struct list_head tmp_list; 1601 struct uprobe *uprobe, *u; 1602 struct inode *inode; 1603 1604 if (no_uprobe_events()) 1605 return 0; 1606 1607 if (vma->vm_file && 1608 (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE && 1609 mm_flags_test(MMF_HAS_UPROBES, vma->vm_mm)) 1610 delayed_ref_ctr_inc(vma); 1611 1612 if (!valid_vma(vma, true)) 1613 return 0; 1614 1615 inode = file_inode(vma->vm_file); 1616 if (!inode) 1617 return 0; 1618 1619 mutex_lock(uprobes_mmap_hash(inode)); 1620 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list); 1621 /* 1622 * We can race with uprobe_unregister(), this uprobe can be already 1623 * removed. But in this case filter_chain() must return false, all 1624 * consumers have gone away. 1625 */ 1626 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) { 1627 if (!fatal_signal_pending(current) && 1628 filter_chain(uprobe, vma->vm_mm)) { 1629 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset); 1630 install_breakpoint(uprobe, vma, vaddr); 1631 } 1632 put_uprobe(uprobe); 1633 } 1634 mutex_unlock(uprobes_mmap_hash(inode)); 1635 1636 return 0; 1637 } 1638 1639 static bool 1640 vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end) 1641 { 1642 loff_t min, max; 1643 struct inode *inode; 1644 struct rb_node *n; 1645 1646 inode = file_inode(vma->vm_file); 1647 1648 min = vaddr_to_offset(vma, start); 1649 max = min + (end - start) - 1; 1650 1651 read_lock(&uprobes_treelock); 1652 n = find_node_in_range(inode, min, max); 1653 read_unlock(&uprobes_treelock); 1654 1655 return !!n; 1656 } 1657 1658 /* 1659 * Called in context of a munmap of a vma. 1660 */ 1661 void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end) 1662 { 1663 if (no_uprobe_events() || !valid_vma(vma, false)) 1664 return; 1665 1666 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */ 1667 return; 1668 1669 if (!mm_flags_test(MMF_HAS_UPROBES, vma->vm_mm) || 1670 mm_flags_test(MMF_RECALC_UPROBES, vma->vm_mm)) 1671 return; 1672 1673 if (vma_has_uprobes(vma, start, end)) 1674 mm_flags_set(MMF_RECALC_UPROBES, vma->vm_mm); 1675 } 1676 1677 static vm_fault_t xol_fault(const struct vm_special_mapping *sm, 1678 struct vm_area_struct *vma, struct vm_fault *vmf) 1679 { 1680 struct xol_area *area = vma->vm_mm->uprobes_state.xol_area; 1681 1682 vmf->page = area->page; 1683 get_page(vmf->page); 1684 return 0; 1685 } 1686 1687 static int xol_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma) 1688 { 1689 return -EPERM; 1690 } 1691 1692 static const struct vm_special_mapping xol_mapping = { 1693 .name = "[uprobes]", 1694 .fault = xol_fault, 1695 .mremap = xol_mremap, 1696 }; 1697 1698 unsigned long __weak arch_uprobe_get_xol_area(void) 1699 { 1700 /* Try to map as high as possible, this is only a hint. */ 1701 return get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0); 1702 } 1703 1704 /* Slot allocation for XOL */ 1705 static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) 1706 { 1707 struct vm_area_struct *vma; 1708 int ret; 1709 1710 if (mmap_write_lock_killable(mm)) 1711 return -EINTR; 1712 1713 if (mm->uprobes_state.xol_area) { 1714 ret = -EALREADY; 1715 goto fail; 1716 } 1717 1718 if (!area->vaddr) { 1719 area->vaddr = arch_uprobe_get_xol_area(); 1720 if (IS_ERR_VALUE(area->vaddr)) { 1721 ret = area->vaddr; 1722 goto fail; 1723 } 1724 } 1725 1726 vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE, 1727 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO| 1728 VM_SEALED_SYSMAP, 1729 &xol_mapping); 1730 if (IS_ERR(vma)) { 1731 ret = PTR_ERR(vma); 1732 goto fail; 1733 } 1734 1735 ret = 0; 1736 /* pairs with get_xol_area() */ 1737 smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */ 1738 fail: 1739 mmap_write_unlock(mm); 1740 1741 return ret; 1742 } 1743 1744 void * __weak arch_uretprobe_trampoline(unsigned long *psize) 1745 { 1746 static uprobe_opcode_t insn = UPROBE_SWBP_INSN; 1747 1748 *psize = UPROBE_SWBP_INSN_SIZE; 1749 return &insn; 1750 } 1751 1752 static struct xol_area *__create_xol_area(unsigned long vaddr) 1753 { 1754 struct mm_struct *mm = current->mm; 1755 unsigned long insns_size; 1756 struct xol_area *area; 1757 void *insns; 1758 1759 area = kzalloc_obj(*area); 1760 if (unlikely(!area)) 1761 goto out; 1762 1763 area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long), 1764 GFP_KERNEL); 1765 if (!area->bitmap) 1766 goto free_area; 1767 1768 area->page = alloc_page(GFP_HIGHUSER | __GFP_ZERO); 1769 if (!area->page) 1770 goto free_bitmap; 1771 1772 area->vaddr = vaddr; 1773 init_waitqueue_head(&area->wq); 1774 /* Reserve the 1st slot for get_trampoline_vaddr() */ 1775 set_bit(0, area->bitmap); 1776 insns = arch_uretprobe_trampoline(&insns_size); 1777 arch_uprobe_copy_ixol(area->page, 0, insns, insns_size); 1778 1779 if (!xol_add_vma(mm, area)) 1780 return area; 1781 1782 __free_page(area->page); 1783 free_bitmap: 1784 kfree(area->bitmap); 1785 free_area: 1786 kfree(area); 1787 out: 1788 return NULL; 1789 } 1790 1791 /* 1792 * get_xol_area - Allocate process's xol_area if necessary. 1793 * This area will be used for storing instructions for execution out of line. 1794 * 1795 * Returns the allocated area or NULL. 1796 */ 1797 static struct xol_area *get_xol_area(void) 1798 { 1799 struct mm_struct *mm = current->mm; 1800 struct xol_area *area; 1801 1802 if (!mm->uprobes_state.xol_area) 1803 __create_xol_area(0); 1804 1805 /* Pairs with xol_add_vma() smp_store_release() */ 1806 area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */ 1807 return area; 1808 } 1809 1810 /* 1811 * uprobe_clear_state - Free the area allocated for slots. 1812 */ 1813 void uprobe_clear_state(struct mm_struct *mm) 1814 { 1815 struct xol_area *area = mm->uprobes_state.xol_area; 1816 1817 mutex_lock(&delayed_uprobe_lock); 1818 delayed_uprobe_remove(NULL, mm); 1819 mutex_unlock(&delayed_uprobe_lock); 1820 1821 if (!area) 1822 return; 1823 1824 put_page(area->page); 1825 kfree(area->bitmap); 1826 kfree(area); 1827 } 1828 1829 void uprobe_start_dup_mmap(void) 1830 { 1831 percpu_down_read(&dup_mmap_sem); 1832 } 1833 1834 void uprobe_end_dup_mmap(void) 1835 { 1836 percpu_up_read(&dup_mmap_sem); 1837 } 1838 1839 void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm) 1840 { 1841 if (mm_flags_test(MMF_HAS_UPROBES, oldmm)) { 1842 mm_flags_set(MMF_HAS_UPROBES, newmm); 1843 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */ 1844 mm_flags_set(MMF_RECALC_UPROBES, newmm); 1845 } 1846 } 1847 1848 static unsigned long xol_get_slot_nr(struct xol_area *area) 1849 { 1850 unsigned long slot_nr; 1851 1852 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE); 1853 if (slot_nr < UINSNS_PER_PAGE) { 1854 if (!test_and_set_bit(slot_nr, area->bitmap)) 1855 return slot_nr; 1856 } 1857 1858 return UINSNS_PER_PAGE; 1859 } 1860 1861 /* 1862 * xol_get_insn_slot - allocate a slot for xol. 1863 */ 1864 static bool xol_get_insn_slot(struct uprobe *uprobe, struct uprobe_task *utask) 1865 { 1866 struct xol_area *area = get_xol_area(); 1867 unsigned long slot_nr; 1868 1869 if (!area) 1870 return false; 1871 1872 wait_event(area->wq, (slot_nr = xol_get_slot_nr(area)) < UINSNS_PER_PAGE); 1873 1874 utask->xol_vaddr = area->vaddr + slot_nr * UPROBE_XOL_SLOT_BYTES; 1875 arch_uprobe_copy_ixol(area->page, utask->xol_vaddr, 1876 &uprobe->arch.ixol, sizeof(uprobe->arch.ixol)); 1877 return true; 1878 } 1879 1880 /* 1881 * xol_free_insn_slot - free the slot allocated by xol_get_insn_slot() 1882 */ 1883 static void xol_free_insn_slot(struct uprobe_task *utask) 1884 { 1885 struct xol_area *area = current->mm->uprobes_state.xol_area; 1886 unsigned long offset = utask->xol_vaddr - area->vaddr; 1887 unsigned int slot_nr; 1888 1889 utask->xol_vaddr = 0; 1890 /* xol_vaddr must fit into [area->vaddr, area->vaddr + PAGE_SIZE) */ 1891 if (WARN_ON_ONCE(offset >= PAGE_SIZE)) 1892 return; 1893 1894 slot_nr = offset / UPROBE_XOL_SLOT_BYTES; 1895 clear_bit(slot_nr, area->bitmap); 1896 smp_mb__after_atomic(); /* pairs with prepare_to_wait() */ 1897 if (waitqueue_active(&area->wq)) 1898 wake_up(&area->wq); 1899 } 1900 1901 void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, 1902 void *src, unsigned long len) 1903 { 1904 /* Initialize the slot */ 1905 copy_to_page(page, vaddr, src, len); 1906 1907 /* 1908 * We probably need flush_icache_user_page() but it needs vma. 1909 * This should work on most of architectures by default. If 1910 * architecture needs to do something different it can define 1911 * its own version of the function. 1912 */ 1913 flush_dcache_page(page); 1914 } 1915 1916 /** 1917 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs 1918 * @regs: Reflects the saved state of the task after it has hit a breakpoint 1919 * instruction. 1920 * Return the address of the breakpoint instruction. 1921 */ 1922 unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs) 1923 { 1924 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE; 1925 } 1926 1927 unsigned long uprobe_get_trap_addr(struct pt_regs *regs) 1928 { 1929 struct uprobe_task *utask = current->utask; 1930 1931 if (unlikely(utask && utask->active_uprobe)) 1932 return utask->vaddr; 1933 1934 return instruction_pointer(regs); 1935 } 1936 1937 static void ri_pool_push(struct uprobe_task *utask, struct return_instance *ri) 1938 { 1939 ri->cons_cnt = 0; 1940 ri->next = utask->ri_pool; 1941 utask->ri_pool = ri; 1942 } 1943 1944 static struct return_instance *ri_pool_pop(struct uprobe_task *utask) 1945 { 1946 struct return_instance *ri = utask->ri_pool; 1947 1948 if (likely(ri)) 1949 utask->ri_pool = ri->next; 1950 1951 return ri; 1952 } 1953 1954 static void ri_free(struct return_instance *ri) 1955 { 1956 kfree(ri->extra_consumers); 1957 kfree_rcu(ri, rcu); 1958 } 1959 1960 static void free_ret_instance(struct uprobe_task *utask, 1961 struct return_instance *ri, bool cleanup_hprobe) 1962 { 1963 unsigned seq; 1964 1965 if (cleanup_hprobe) { 1966 enum hprobe_state hstate; 1967 1968 (void)hprobe_consume(&ri->hprobe, &hstate); 1969 hprobe_finalize(&ri->hprobe, hstate); 1970 } 1971 1972 /* 1973 * At this point return_instance is unlinked from utask's 1974 * return_instances list and this has become visible to ri_timer(). 1975 * If seqcount now indicates that ri_timer's return instance 1976 * processing loop isn't active, we can return ri into the pool of 1977 * to-be-reused return instances for future uretprobes. If ri_timer() 1978 * happens to be running right now, though, we fallback to safety and 1979 * just perform RCU-delated freeing of ri. 1980 * Admittedly, this is a rather simple use of seqcount, but it nicely 1981 * abstracts away all the necessary memory barriers, so we use 1982 * a well-supported kernel primitive here. 1983 */ 1984 if (raw_seqcount_try_begin(&utask->ri_seqcount, seq)) { 1985 /* immediate reuse of ri without RCU GP is OK */ 1986 ri_pool_push(utask, ri); 1987 } else { 1988 /* we might be racing with ri_timer(), so play it safe */ 1989 ri_free(ri); 1990 } 1991 } 1992 1993 /* 1994 * Called with no locks held. 1995 * Called in context of an exiting or an exec-ing thread. 1996 */ 1997 void uprobe_free_utask(struct task_struct *t) 1998 { 1999 struct uprobe_task *utask = t->utask; 2000 struct return_instance *ri, *ri_next; 2001 2002 if (!utask) 2003 return; 2004 2005 t->utask = NULL; 2006 WARN_ON_ONCE(utask->active_uprobe || utask->xol_vaddr); 2007 2008 timer_delete_sync(&utask->ri_timer); 2009 2010 ri = utask->return_instances; 2011 while (ri) { 2012 ri_next = ri->next; 2013 free_ret_instance(utask, ri, true /* cleanup_hprobe */); 2014 ri = ri_next; 2015 } 2016 2017 /* free_ret_instance() above might add to ri_pool, so this loop should come last */ 2018 ri = utask->ri_pool; 2019 while (ri) { 2020 ri_next = ri->next; 2021 ri_free(ri); 2022 ri = ri_next; 2023 } 2024 2025 kfree(utask); 2026 } 2027 2028 #define RI_TIMER_PERIOD (HZ / 10) /* 100 ms */ 2029 2030 #define for_each_ret_instance_rcu(pos, head) \ 2031 for (pos = rcu_dereference_raw(head); pos; pos = rcu_dereference_raw(pos->next)) 2032 2033 static void ri_timer(struct timer_list *timer) 2034 { 2035 struct uprobe_task *utask = container_of(timer, struct uprobe_task, ri_timer); 2036 struct return_instance *ri; 2037 2038 /* SRCU protects uprobe from reuse for the cmpxchg() inside hprobe_expire(). */ 2039 guard(srcu_fast_updown)(&uretprobes_srcu); 2040 /* RCU protects return_instance from freeing. */ 2041 guard(rcu)(); 2042 2043 /* 2044 * See free_ret_instance() for notes on seqcount use. 2045 * We also employ raw API variants to avoid lockdep false-positive 2046 * warning complaining about enabled preemption. The timer can only be 2047 * invoked once for a uprobe_task. Therefore there can only be one 2048 * writer. The reader does not require an even sequence count to make 2049 * progress, so it is OK to remain preemptible on PREEMPT_RT. 2050 */ 2051 raw_write_seqcount_begin(&utask->ri_seqcount); 2052 2053 for_each_ret_instance_rcu(ri, utask->return_instances) 2054 hprobe_expire(&ri->hprobe, false); 2055 2056 raw_write_seqcount_end(&utask->ri_seqcount); 2057 } 2058 2059 static struct uprobe_task *alloc_utask(void) 2060 { 2061 struct uprobe_task *utask; 2062 2063 utask = kzalloc_obj(*utask); 2064 if (!utask) 2065 return NULL; 2066 2067 timer_setup(&utask->ri_timer, ri_timer, 0); 2068 seqcount_init(&utask->ri_seqcount); 2069 2070 return utask; 2071 } 2072 2073 /* 2074 * Allocate a uprobe_task object for the task if necessary. 2075 * Called when the thread hits a breakpoint. 2076 * 2077 * Returns: 2078 * - pointer to new uprobe_task on success 2079 * - NULL otherwise 2080 */ 2081 static struct uprobe_task *get_utask(void) 2082 { 2083 if (!current->utask) 2084 current->utask = alloc_utask(); 2085 return current->utask; 2086 } 2087 2088 static struct return_instance *alloc_return_instance(struct uprobe_task *utask) 2089 { 2090 struct return_instance *ri; 2091 2092 ri = ri_pool_pop(utask); 2093 if (ri) 2094 return ri; 2095 2096 ri = kzalloc_obj(*ri); 2097 if (!ri) 2098 return ZERO_SIZE_PTR; 2099 2100 return ri; 2101 } 2102 2103 static struct return_instance *dup_return_instance(struct return_instance *old) 2104 { 2105 struct return_instance *ri; 2106 2107 ri = kmemdup(old, sizeof(*ri), GFP_KERNEL); 2108 if (!ri) 2109 return NULL; 2110 2111 if (unlikely(old->cons_cnt > 1)) { 2112 ri->extra_consumers = kmemdup(old->extra_consumers, 2113 sizeof(ri->extra_consumers[0]) * (old->cons_cnt - 1), 2114 GFP_KERNEL); 2115 if (!ri->extra_consumers) { 2116 kfree(ri); 2117 return NULL; 2118 } 2119 } 2120 2121 return ri; 2122 } 2123 2124 static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) 2125 { 2126 struct uprobe_task *n_utask; 2127 struct return_instance **p, *o, *n; 2128 struct uprobe *uprobe; 2129 2130 n_utask = alloc_utask(); 2131 if (!n_utask) 2132 return -ENOMEM; 2133 t->utask = n_utask; 2134 2135 /* protect uprobes from freeing, we'll need try_get_uprobe() them */ 2136 guard(srcu_fast_updown)(&uretprobes_srcu); 2137 2138 p = &n_utask->return_instances; 2139 for (o = o_utask->return_instances; o; o = o->next) { 2140 n = dup_return_instance(o); 2141 if (!n) 2142 return -ENOMEM; 2143 2144 /* if uprobe is non-NULL, we'll have an extra refcount for uprobe */ 2145 uprobe = hprobe_expire(&o->hprobe, true); 2146 2147 /* 2148 * New utask will have stable properly refcounted uprobe or 2149 * NULL. Even if we failed to get refcounted uprobe, we still 2150 * need to preserve full set of return_instances for proper 2151 * uretprobe handling and nesting in forked task. 2152 */ 2153 hprobe_init_stable(&n->hprobe, uprobe); 2154 2155 n->next = NULL; 2156 rcu_assign_pointer(*p, n); 2157 p = &n->next; 2158 2159 n_utask->depth++; 2160 } 2161 2162 return 0; 2163 } 2164 2165 static void dup_xol_work(struct callback_head *work) 2166 { 2167 if (current->flags & PF_EXITING) 2168 return; 2169 2170 if (!__create_xol_area(current->utask->dup_xol_addr) && 2171 !fatal_signal_pending(current)) 2172 uprobe_warn(current, "dup xol area"); 2173 } 2174 2175 /* 2176 * Called in context of a new clone/fork from copy_process. 2177 */ 2178 void uprobe_copy_process(struct task_struct *t, u64 flags) 2179 { 2180 struct uprobe_task *utask = current->utask; 2181 struct mm_struct *mm = current->mm; 2182 struct xol_area *area; 2183 2184 t->utask = NULL; 2185 2186 if (!utask || !utask->return_instances) 2187 return; 2188 2189 if (mm == t->mm && !(flags & CLONE_VFORK)) 2190 return; 2191 2192 if (dup_utask(t, utask)) 2193 return uprobe_warn(t, "dup ret instances"); 2194 2195 /* The task can fork() after dup_xol_work() fails */ 2196 area = mm->uprobes_state.xol_area; 2197 if (!area) 2198 return uprobe_warn(t, "dup xol area"); 2199 2200 if (mm == t->mm) 2201 return; 2202 2203 t->utask->dup_xol_addr = area->vaddr; 2204 init_task_work(&t->utask->dup_xol_work, dup_xol_work); 2205 task_work_add(t, &t->utask->dup_xol_work, TWA_RESUME); 2206 } 2207 2208 /* 2209 * Current area->vaddr notion assume the trampoline address is always 2210 * equal area->vaddr. 2211 * 2212 * Returns -1 in case the xol_area is not allocated. 2213 */ 2214 unsigned long uprobe_get_trampoline_vaddr(void) 2215 { 2216 unsigned long trampoline_vaddr = UPROBE_NO_TRAMPOLINE_VADDR; 2217 struct xol_area *area; 2218 2219 /* Pairs with xol_add_vma() smp_store_release() */ 2220 area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */ 2221 if (area) 2222 trampoline_vaddr = area->vaddr; 2223 2224 return trampoline_vaddr; 2225 } 2226 2227 static void cleanup_return_instances(struct uprobe_task *utask, bool chained, 2228 struct pt_regs *regs) 2229 { 2230 struct return_instance *ri = utask->return_instances, *ri_next; 2231 enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL; 2232 2233 while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) { 2234 ri_next = ri->next; 2235 rcu_assign_pointer(utask->return_instances, ri_next); 2236 utask->depth--; 2237 2238 free_ret_instance(utask, ri, true /* cleanup_hprobe */); 2239 ri = ri_next; 2240 } 2241 } 2242 2243 static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs, 2244 struct return_instance *ri) 2245 { 2246 struct uprobe_task *utask = current->utask; 2247 unsigned long orig_ret_vaddr, trampoline_vaddr; 2248 struct srcu_ctr __percpu *srcu_scp; 2249 bool chained; 2250 2251 if (!get_xol_area()) 2252 goto free; 2253 2254 if (utask->depth >= MAX_URETPROBE_DEPTH) { 2255 printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to" 2256 " nestedness limit pid/tgid=%d/%d\n", 2257 current->pid, current->tgid); 2258 goto free; 2259 } 2260 2261 trampoline_vaddr = uprobe_get_trampoline_vaddr(); 2262 orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs); 2263 if (orig_ret_vaddr == -1) 2264 goto free; 2265 2266 /* drop the entries invalidated by longjmp() */ 2267 chained = (orig_ret_vaddr == trampoline_vaddr); 2268 cleanup_return_instances(utask, chained, regs); 2269 2270 /* 2271 * We don't want to keep trampoline address in stack, rather keep the 2272 * original return address of first caller thru all the consequent 2273 * instances. This also makes breakpoint unwrapping easier. 2274 */ 2275 if (chained) { 2276 if (!utask->return_instances) { 2277 /* 2278 * This situation is not possible. Likely we have an 2279 * attack from user-space. 2280 */ 2281 uprobe_warn(current, "handle tail call"); 2282 goto free; 2283 } 2284 orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; 2285 } 2286 2287 /* 2288 * Use srcu_down_read_fast() because the SRCU lock survives a switch to 2289 * user space and can be unlocked from a different context by ri_timer() 2290 * or dup_utask(). 2291 */ 2292 srcu_scp = srcu_down_read_fast(&uretprobes_srcu); 2293 2294 ri->func = instruction_pointer(regs); 2295 ri->stack = user_stack_pointer(regs); 2296 ri->orig_ret_vaddr = orig_ret_vaddr; 2297 ri->chained = chained; 2298 2299 utask->depth++; 2300 2301 hprobe_init_leased(&ri->hprobe, uprobe, srcu_scp); 2302 ri->next = utask->return_instances; 2303 rcu_assign_pointer(utask->return_instances, ri); 2304 2305 mod_timer(&utask->ri_timer, jiffies + RI_TIMER_PERIOD); 2306 2307 return; 2308 free: 2309 ri_free(ri); 2310 } 2311 2312 /* Prepare to single-step probed instruction out of line. */ 2313 static int 2314 pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr) 2315 { 2316 struct uprobe_task *utask = current->utask; 2317 int err; 2318 2319 if (!try_get_uprobe(uprobe)) 2320 return -EINVAL; 2321 2322 if (!xol_get_insn_slot(uprobe, utask)) { 2323 err = -ENOMEM; 2324 goto err_out; 2325 } 2326 2327 utask->vaddr = bp_vaddr; 2328 err = arch_uprobe_pre_xol(&uprobe->arch, regs); 2329 if (unlikely(err)) { 2330 xol_free_insn_slot(utask); 2331 goto err_out; 2332 } 2333 2334 utask->active_uprobe = uprobe; 2335 utask->state = UTASK_SSTEP; 2336 return 0; 2337 err_out: 2338 put_uprobe(uprobe); 2339 return err; 2340 } 2341 2342 /* 2343 * If we are singlestepping, then ensure this thread is not connected to 2344 * non-fatal signals until completion of singlestep. When xol insn itself 2345 * triggers the signal, restart the original insn even if the task is 2346 * already SIGKILL'ed (since coredump should report the correct ip). This 2347 * is even more important if the task has a handler for SIGSEGV/etc, The 2348 * _same_ instruction should be repeated again after return from the signal 2349 * handler, and SSTEP can never finish in this case. 2350 */ 2351 bool uprobe_deny_signal(void) 2352 { 2353 struct task_struct *t = current; 2354 struct uprobe_task *utask = t->utask; 2355 2356 if (likely(!utask || !utask->active_uprobe)) 2357 return false; 2358 2359 WARN_ON_ONCE(utask->state != UTASK_SSTEP); 2360 2361 if (task_sigpending(t)) { 2362 utask->signal_denied = true; 2363 clear_tsk_thread_flag(t, TIF_SIGPENDING); 2364 2365 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) { 2366 utask->state = UTASK_SSTEP_TRAPPED; 2367 set_tsk_thread_flag(t, TIF_UPROBE); 2368 } 2369 } 2370 2371 return true; 2372 } 2373 2374 static void mmf_recalc_uprobes(struct mm_struct *mm) 2375 { 2376 VMA_ITERATOR(vmi, mm, 0); 2377 struct vm_area_struct *vma; 2378 2379 for_each_vma(vmi, vma) { 2380 if (!valid_vma(vma, false)) 2381 continue; 2382 /* 2383 * This is not strictly accurate, we can race with 2384 * uprobe_unregister() and see the already removed 2385 * uprobe if delete_uprobe() was not yet called. 2386 * Or this uprobe can be filtered out. 2387 */ 2388 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end)) 2389 return; 2390 } 2391 2392 mm_flags_clear(MMF_HAS_UPROBES, mm); 2393 } 2394 2395 static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr) 2396 { 2397 struct page *page; 2398 uprobe_opcode_t opcode; 2399 int result; 2400 2401 if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE))) 2402 return -EINVAL; 2403 2404 pagefault_disable(); 2405 result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr); 2406 pagefault_enable(); 2407 2408 if (likely(result == 0)) 2409 goto out; 2410 2411 result = get_user_pages(vaddr, 1, FOLL_FORCE, &page); 2412 if (result < 0) 2413 return result; 2414 2415 uprobe_copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); 2416 put_page(page); 2417 out: 2418 /* This needs to return true for any variant of the trap insn */ 2419 return is_trap_insn(&opcode); 2420 } 2421 2422 static struct uprobe *find_active_uprobe_speculative(unsigned long bp_vaddr) 2423 { 2424 struct mm_struct *mm = current->mm; 2425 struct uprobe *uprobe = NULL; 2426 struct vm_area_struct *vma; 2427 struct file *vm_file; 2428 loff_t offset; 2429 unsigned int seq; 2430 2431 guard(rcu)(); 2432 2433 if (!mmap_lock_speculate_try_begin(mm, &seq)) 2434 return NULL; 2435 2436 vma = vma_lookup(mm, bp_vaddr); 2437 if (!vma) 2438 return NULL; 2439 2440 /* 2441 * vm_file memory can be reused for another instance of struct file, 2442 * but can't be freed from under us, so it's safe to read fields from 2443 * it, even if the values are some garbage values; ultimately 2444 * find_uprobe_rcu() + mmap_lock_speculation_end() check will ensure 2445 * that whatever we speculatively found is correct 2446 */ 2447 vm_file = READ_ONCE(vma->vm_file); 2448 if (!vm_file) 2449 return NULL; 2450 2451 offset = (loff_t)(vma->vm_pgoff << PAGE_SHIFT) + (bp_vaddr - vma->vm_start); 2452 uprobe = find_uprobe_rcu(vm_file->f_inode, offset); 2453 if (!uprobe) 2454 return NULL; 2455 2456 /* now double check that nothing about MM changed */ 2457 if (mmap_lock_speculate_retry(mm, seq)) 2458 return NULL; 2459 2460 return uprobe; 2461 } 2462 2463 /* assumes being inside RCU protected region */ 2464 static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swbp) 2465 { 2466 struct mm_struct *mm = current->mm; 2467 struct uprobe *uprobe = NULL; 2468 struct vm_area_struct *vma; 2469 2470 uprobe = find_active_uprobe_speculative(bp_vaddr); 2471 if (uprobe) 2472 return uprobe; 2473 2474 mmap_read_lock(mm); 2475 vma = vma_lookup(mm, bp_vaddr); 2476 if (vma) { 2477 if (vma->vm_file) { 2478 struct inode *inode = file_inode(vma->vm_file); 2479 loff_t offset = vaddr_to_offset(vma, bp_vaddr); 2480 2481 uprobe = find_uprobe_rcu(inode, offset); 2482 } 2483 2484 if (!uprobe) 2485 *is_swbp = is_trap_at_addr(mm, bp_vaddr); 2486 } else { 2487 *is_swbp = -EFAULT; 2488 } 2489 2490 if (!uprobe && mm_flags_test_and_clear(MMF_RECALC_UPROBES, mm)) 2491 mmf_recalc_uprobes(mm); 2492 mmap_read_unlock(mm); 2493 2494 return uprobe; 2495 } 2496 2497 static struct return_instance *push_consumer(struct return_instance *ri, __u64 id, __u64 cookie) 2498 { 2499 struct return_consumer *ric; 2500 2501 if (unlikely(ri == ZERO_SIZE_PTR)) 2502 return ri; 2503 2504 if (unlikely(ri->cons_cnt > 0)) { 2505 ric = krealloc(ri->extra_consumers, sizeof(*ric) * ri->cons_cnt, GFP_KERNEL); 2506 if (!ric) { 2507 ri_free(ri); 2508 return ZERO_SIZE_PTR; 2509 } 2510 ri->extra_consumers = ric; 2511 } 2512 2513 ric = likely(ri->cons_cnt == 0) ? &ri->consumer : &ri->extra_consumers[ri->cons_cnt - 1]; 2514 ric->id = id; 2515 ric->cookie = cookie; 2516 2517 ri->cons_cnt++; 2518 return ri; 2519 } 2520 2521 static struct return_consumer * 2522 return_consumer_find(struct return_instance *ri, int *iter, int id) 2523 { 2524 struct return_consumer *ric; 2525 int idx; 2526 2527 for (idx = *iter; idx < ri->cons_cnt; idx++) 2528 { 2529 ric = likely(idx == 0) ? &ri->consumer : &ri->extra_consumers[idx - 1]; 2530 if (ric->id == id) { 2531 *iter = idx + 1; 2532 return ric; 2533 } 2534 } 2535 2536 return NULL; 2537 } 2538 2539 static bool ignore_ret_handler(int rc) 2540 { 2541 return rc == UPROBE_HANDLER_REMOVE || rc == UPROBE_HANDLER_IGNORE; 2542 } 2543 2544 static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) 2545 { 2546 struct uprobe_consumer *uc; 2547 bool has_consumers = false, remove = true; 2548 struct return_instance *ri = NULL; 2549 struct uprobe_task *utask = current->utask; 2550 2551 utask->auprobe = &uprobe->arch; 2552 2553 list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { 2554 bool session = uc->handler && uc->ret_handler; 2555 __u64 cookie = 0; 2556 int rc = 0; 2557 2558 if (uc->handler) { 2559 rc = uc->handler(uc, regs, &cookie); 2560 WARN(rc < 0 || rc > 2, 2561 "bad rc=0x%x from %ps()\n", rc, uc->handler); 2562 } 2563 2564 remove &= rc == UPROBE_HANDLER_REMOVE; 2565 has_consumers = true; 2566 2567 if (!uc->ret_handler || ignore_ret_handler(rc)) 2568 continue; 2569 2570 if (!ri) 2571 ri = alloc_return_instance(utask); 2572 2573 if (session) 2574 ri = push_consumer(ri, uc->id, cookie); 2575 } 2576 utask->auprobe = NULL; 2577 2578 if (!ZERO_OR_NULL_PTR(ri)) 2579 prepare_uretprobe(uprobe, regs, ri); 2580 2581 if (remove && has_consumers) { 2582 down_read(&uprobe->register_rwsem); 2583 2584 /* re-check that removal is still required, this time under lock */ 2585 if (!filter_chain(uprobe, current->mm)) { 2586 WARN_ON(!uprobe_is_active(uprobe)); 2587 unapply_uprobe(uprobe, current->mm); 2588 } 2589 2590 up_read(&uprobe->register_rwsem); 2591 } 2592 } 2593 2594 static void 2595 handle_uretprobe_chain(struct return_instance *ri, struct uprobe *uprobe, struct pt_regs *regs) 2596 { 2597 struct return_consumer *ric; 2598 struct uprobe_consumer *uc; 2599 int ric_idx = 0; 2600 2601 /* all consumers unsubscribed meanwhile */ 2602 if (unlikely(!uprobe)) 2603 return; 2604 2605 rcu_read_lock_trace(); 2606 list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { 2607 bool session = uc->handler && uc->ret_handler; 2608 2609 if (uc->ret_handler) { 2610 ric = return_consumer_find(ri, &ric_idx, uc->id); 2611 if (!session || ric) 2612 uc->ret_handler(uc, ri->func, regs, ric ? &ric->cookie : NULL); 2613 } 2614 } 2615 rcu_read_unlock_trace(); 2616 } 2617 2618 static struct return_instance *find_next_ret_chain(struct return_instance *ri) 2619 { 2620 bool chained; 2621 2622 do { 2623 chained = ri->chained; 2624 ri = ri->next; /* can't be NULL if chained */ 2625 } while (chained); 2626 2627 return ri; 2628 } 2629 2630 void uprobe_handle_trampoline(struct pt_regs *regs) 2631 { 2632 struct uprobe_task *utask; 2633 struct return_instance *ri, *ri_next, *next_chain; 2634 struct uprobe *uprobe; 2635 enum hprobe_state hstate; 2636 bool valid; 2637 2638 utask = current->utask; 2639 if (!utask) 2640 goto sigill; 2641 2642 ri = utask->return_instances; 2643 if (!ri) 2644 goto sigill; 2645 2646 do { 2647 /* 2648 * We should throw out the frames invalidated by longjmp(). 2649 * If this chain is valid, then the next one should be alive 2650 * or NULL; the latter case means that nobody but ri->func 2651 * could hit this trampoline on return. TODO: sigaltstack(). 2652 */ 2653 next_chain = find_next_ret_chain(ri); 2654 valid = !next_chain || arch_uretprobe_is_alive(next_chain, RP_CHECK_RET, regs); 2655 2656 instruction_pointer_set(regs, ri->orig_ret_vaddr); 2657 do { 2658 /* pop current instance from the stack of pending return instances, 2659 * as it's not pending anymore: we just fixed up original 2660 * instruction pointer in regs and are about to call handlers; 2661 * this allows fixup_uretprobe_trampoline_entries() to properly fix up 2662 * captured stack traces from uretprobe handlers, in which pending 2663 * trampoline addresses on the stack are replaced with correct 2664 * original return addresses 2665 */ 2666 ri_next = ri->next; 2667 rcu_assign_pointer(utask->return_instances, ri_next); 2668 utask->depth--; 2669 2670 uprobe = hprobe_consume(&ri->hprobe, &hstate); 2671 if (valid) 2672 handle_uretprobe_chain(ri, uprobe, regs); 2673 hprobe_finalize(&ri->hprobe, hstate); 2674 2675 /* We already took care of hprobe, no need to waste more time on that. */ 2676 free_ret_instance(utask, ri, false /* !cleanup_hprobe */); 2677 ri = ri_next; 2678 } while (ri != next_chain); 2679 } while (!valid); 2680 2681 return; 2682 2683 sigill: 2684 uprobe_warn(current, "handle uretprobe, sending SIGILL."); 2685 force_sig(SIGILL); 2686 } 2687 2688 bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs) 2689 { 2690 return false; 2691 } 2692 2693 bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, 2694 struct pt_regs *regs) 2695 { 2696 return true; 2697 } 2698 2699 void __weak arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr) 2700 { 2701 } 2702 2703 /* 2704 * Run handler and ask thread to singlestep. 2705 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps. 2706 */ 2707 static void handle_swbp(struct pt_regs *regs) 2708 { 2709 struct uprobe *uprobe; 2710 unsigned long bp_vaddr; 2711 int is_swbp; 2712 2713 bp_vaddr = uprobe_get_swbp_addr(regs); 2714 if (bp_vaddr == uprobe_get_trampoline_vaddr()) 2715 return uprobe_handle_trampoline(regs); 2716 2717 rcu_read_lock_trace(); 2718 2719 uprobe = find_active_uprobe_rcu(bp_vaddr, &is_swbp); 2720 if (!uprobe) { 2721 if (is_swbp > 0) { 2722 /* No matching uprobe; signal SIGTRAP. */ 2723 force_sig(SIGTRAP); 2724 } else { 2725 /* 2726 * Either we raced with uprobe_unregister() or we can't 2727 * access this memory. The latter is only possible if 2728 * another thread plays with our ->mm. In both cases 2729 * we can simply restart. If this vma was unmapped we 2730 * can pretend this insn was not executed yet and get 2731 * the (correct) SIGSEGV after restart. 2732 */ 2733 instruction_pointer_set(regs, bp_vaddr); 2734 } 2735 goto out; 2736 } 2737 2738 /* change it in advance for ->handler() and restart */ 2739 instruction_pointer_set(regs, bp_vaddr); 2740 2741 /* 2742 * TODO: move copy_insn/etc into _register and remove this hack. 2743 * After we hit the bp, _unregister + _register can install the 2744 * new and not-yet-analyzed uprobe at the same address, restart. 2745 */ 2746 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags))) 2747 goto out; 2748 2749 /* 2750 * Pairs with the smp_wmb() in prepare_uprobe(). 2751 * 2752 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then 2753 * we must also see the stores to &uprobe->arch performed by the 2754 * prepare_uprobe() call. 2755 */ 2756 smp_rmb(); 2757 2758 /* Tracing handlers use ->utask to communicate with fetch methods */ 2759 if (!get_utask()) 2760 goto out; 2761 2762 if (arch_uprobe_ignore(&uprobe->arch, regs)) 2763 goto out; 2764 2765 handler_chain(uprobe, regs); 2766 2767 /* Try to optimize after first hit. */ 2768 arch_uprobe_optimize(&uprobe->arch, bp_vaddr); 2769 2770 /* 2771 * If user decided to take execution elsewhere, it makes little sense 2772 * to execute the original instruction, so let's skip it. 2773 */ 2774 if (instruction_pointer(regs) != bp_vaddr) 2775 goto out; 2776 2777 if (arch_uprobe_skip_sstep(&uprobe->arch, regs)) 2778 goto out; 2779 2780 if (pre_ssout(uprobe, regs, bp_vaddr)) 2781 goto out; 2782 2783 out: 2784 /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */ 2785 rcu_read_unlock_trace(); 2786 } 2787 2788 void handle_syscall_uprobe(struct pt_regs *regs, unsigned long bp_vaddr) 2789 { 2790 struct uprobe *uprobe; 2791 int is_swbp; 2792 2793 guard(rcu_tasks_trace)(); 2794 2795 uprobe = find_active_uprobe_rcu(bp_vaddr, &is_swbp); 2796 if (!uprobe) 2797 return; 2798 if (!get_utask()) 2799 return; 2800 if (arch_uprobe_ignore(&uprobe->arch, regs)) 2801 return; 2802 handler_chain(uprobe, regs); 2803 } 2804 2805 /* 2806 * Perform required fix-ups and disable singlestep. 2807 * Allow pending signals to take effect. 2808 */ 2809 static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs) 2810 { 2811 struct uprobe *uprobe; 2812 int err = 0; 2813 2814 uprobe = utask->active_uprobe; 2815 if (utask->state == UTASK_SSTEP_ACK) 2816 err = arch_uprobe_post_xol(&uprobe->arch, regs); 2817 else if (utask->state == UTASK_SSTEP_TRAPPED) 2818 arch_uprobe_abort_xol(&uprobe->arch, regs); 2819 else 2820 WARN_ON_ONCE(1); 2821 2822 put_uprobe(uprobe); 2823 utask->active_uprobe = NULL; 2824 utask->state = UTASK_RUNNING; 2825 xol_free_insn_slot(utask); 2826 2827 if (utask->signal_denied) { 2828 set_thread_flag(TIF_SIGPENDING); 2829 utask->signal_denied = false; 2830 } 2831 2832 if (unlikely(err)) { 2833 uprobe_warn(current, "execute the probed insn, sending SIGILL."); 2834 force_sig(SIGILL); 2835 } 2836 } 2837 2838 /* 2839 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and 2840 * allows the thread to return from interrupt. After that handle_swbp() 2841 * sets utask->active_uprobe. 2842 * 2843 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag 2844 * and allows the thread to return from interrupt. 2845 * 2846 * While returning to userspace, thread notices the TIF_UPROBE flag and calls 2847 * uprobe_notify_resume(). 2848 */ 2849 void uprobe_notify_resume(struct pt_regs *regs) 2850 { 2851 struct uprobe_task *utask; 2852 2853 clear_thread_flag(TIF_UPROBE); 2854 2855 utask = current->utask; 2856 if (utask && utask->active_uprobe) 2857 handle_singlestep(utask, regs); 2858 else 2859 handle_swbp(regs); 2860 } 2861 2862 /* 2863 * uprobe_pre_sstep_notifier gets called from interrupt context as part of 2864 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit. 2865 */ 2866 int uprobe_pre_sstep_notifier(struct pt_regs *regs) 2867 { 2868 if (!current->mm) 2869 return 0; 2870 2871 if (!mm_flags_test(MMF_HAS_UPROBES, current->mm) && 2872 (!current->utask || !current->utask->return_instances)) 2873 return 0; 2874 2875 set_thread_flag(TIF_UPROBE); 2876 return 1; 2877 } 2878 2879 /* 2880 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier 2881 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep. 2882 */ 2883 int uprobe_post_sstep_notifier(struct pt_regs *regs) 2884 { 2885 struct uprobe_task *utask = current->utask; 2886 2887 if (!current->mm || !utask || !utask->active_uprobe) 2888 /* task is currently not uprobed */ 2889 return 0; 2890 2891 utask->state = UTASK_SSTEP_ACK; 2892 set_thread_flag(TIF_UPROBE); 2893 return 1; 2894 } 2895 2896 static struct notifier_block uprobe_exception_nb = { 2897 .notifier_call = arch_uprobe_exception_notify, 2898 .priority = INT_MAX-1, /* notified after kprobes, kgdb */ 2899 }; 2900 2901 void __init uprobes_init(void) 2902 { 2903 int i; 2904 2905 for (i = 0; i < UPROBES_HASH_SZ; i++) 2906 mutex_init(&uprobes_mmap_mutex[i]); 2907 2908 BUG_ON(register_die_notifier(&uprobe_exception_nb)); 2909 } 2910