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