1 /* 2 * User-space Probes (UProbes) 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * Copyright (C) IBM Corporation, 2008-2012 19 * Authors: 20 * Srikar Dronamraju 21 * Jim Keniston 22 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra 23 */ 24 25 #include <linux/kernel.h> 26 #include <linux/highmem.h> 27 #include <linux/pagemap.h> /* read_mapping_page */ 28 #include <linux/slab.h> 29 #include <linux/sched.h> 30 #include <linux/sched/mm.h> 31 #include <linux/sched/coredump.h> 32 #include <linux/export.h> 33 #include <linux/rmap.h> /* anon_vma_prepare */ 34 #include <linux/mmu_notifier.h> /* set_pte_at_notify */ 35 #include <linux/swap.h> /* try_to_free_swap */ 36 #include <linux/ptrace.h> /* user_enable_single_step */ 37 #include <linux/kdebug.h> /* notifier mechanism */ 38 #include "../../mm/internal.h" /* munlock_vma_page */ 39 #include <linux/percpu-rwsem.h> 40 #include <linux/task_work.h> 41 #include <linux/shmem_fs.h> 42 43 #include <linux/uprobes.h> 44 45 #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES) 46 #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE 47 48 static struct rb_root uprobes_tree = RB_ROOT; 49 /* 50 * allows us to skip the uprobe_mmap if there are no uprobe events active 51 * at this time. Probably a fine grained per inode count is better? 52 */ 53 #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree) 54 55 static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */ 56 57 #define UPROBES_HASH_SZ 13 58 /* serialize uprobe->pending_list */ 59 static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ]; 60 #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ]) 61 62 static struct percpu_rw_semaphore dup_mmap_sem; 63 64 /* Have a copy of original instruction */ 65 #define UPROBE_COPY_INSN 0 66 67 struct uprobe { 68 struct rb_node rb_node; /* node in the rb tree */ 69 atomic_t ref; 70 struct rw_semaphore register_rwsem; 71 struct rw_semaphore consumer_rwsem; 72 struct list_head pending_list; 73 struct uprobe_consumer *consumers; 74 struct inode *inode; /* Also hold a ref to inode */ 75 loff_t offset; 76 loff_t ref_ctr_offset; 77 unsigned long flags; 78 79 /* 80 * The generic code assumes that it has two members of unknown type 81 * owned by the arch-specific code: 82 * 83 * insn - copy_insn() saves the original instruction here for 84 * arch_uprobe_analyze_insn(). 85 * 86 * ixol - potentially modified instruction to execute out of 87 * line, copied to xol_area by xol_get_insn_slot(). 88 */ 89 struct arch_uprobe arch; 90 }; 91 92 struct delayed_uprobe { 93 struct list_head list; 94 struct uprobe *uprobe; 95 struct mm_struct *mm; 96 }; 97 98 static DEFINE_MUTEX(delayed_uprobe_lock); 99 static LIST_HEAD(delayed_uprobe_list); 100 101 /* 102 * Execute out of line area: anonymous executable mapping installed 103 * by the probed task to execute the copy of the original instruction 104 * mangled by set_swbp(). 105 * 106 * On a breakpoint hit, thread contests for a slot. It frees the 107 * slot after singlestep. Currently a fixed number of slots are 108 * allocated. 109 */ 110 struct xol_area { 111 wait_queue_head_t wq; /* if all slots are busy */ 112 atomic_t slot_count; /* number of in-use slots */ 113 unsigned long *bitmap; /* 0 = free slot */ 114 115 struct vm_special_mapping xol_mapping; 116 struct page *pages[2]; 117 /* 118 * We keep the vma's vm_start rather than a pointer to the vma 119 * itself. The probed process or a naughty kernel module could make 120 * the vma go away, and we must handle that reasonably gracefully. 121 */ 122 unsigned long vaddr; /* Page(s) of instruction slots */ 123 }; 124 125 /* 126 * valid_vma: Verify if the specified vma is an executable vma 127 * Relax restrictions while unregistering: vm_flags might have 128 * changed after breakpoint was inserted. 129 * - is_register: indicates if we are in register context. 130 * - Return 1 if the specified virtual address is in an 131 * executable vma. 132 */ 133 static bool valid_vma(struct vm_area_struct *vma, bool is_register) 134 { 135 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE; 136 137 if (is_register) 138 flags |= VM_WRITE; 139 140 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC; 141 } 142 143 static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset) 144 { 145 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 146 } 147 148 static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr) 149 { 150 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start); 151 } 152 153 /** 154 * __replace_page - replace page in vma by new page. 155 * based on replace_page in mm/ksm.c 156 * 157 * @vma: vma that holds the pte pointing to page 158 * @addr: address the old @page is mapped at 159 * @page: the cowed page we are replacing by kpage 160 * @kpage: the modified page we replace page by 161 * 162 * Returns 0 on success, -EFAULT on failure. 163 */ 164 static int __replace_page(struct vm_area_struct *vma, unsigned long addr, 165 struct page *old_page, struct page *new_page) 166 { 167 struct mm_struct *mm = vma->vm_mm; 168 struct page_vma_mapped_walk pvmw = { 169 .page = old_page, 170 .vma = vma, 171 .address = addr, 172 }; 173 int err; 174 struct mmu_notifier_range range; 175 struct mem_cgroup *memcg; 176 177 mmu_notifier_range_init(&range, mm, addr, addr + PAGE_SIZE); 178 179 VM_BUG_ON_PAGE(PageTransHuge(old_page), old_page); 180 181 err = mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL, &memcg, 182 false); 183 if (err) 184 return err; 185 186 /* For try_to_free_swap() and munlock_vma_page() below */ 187 lock_page(old_page); 188 189 mmu_notifier_invalidate_range_start(&range); 190 err = -EAGAIN; 191 if (!page_vma_mapped_walk(&pvmw)) { 192 mem_cgroup_cancel_charge(new_page, memcg, false); 193 goto unlock; 194 } 195 VM_BUG_ON_PAGE(addr != pvmw.address, old_page); 196 197 get_page(new_page); 198 page_add_new_anon_rmap(new_page, vma, addr, false); 199 mem_cgroup_commit_charge(new_page, memcg, false, false); 200 lru_cache_add_active_or_unevictable(new_page, vma); 201 202 if (!PageAnon(old_page)) { 203 dec_mm_counter(mm, mm_counter_file(old_page)); 204 inc_mm_counter(mm, MM_ANONPAGES); 205 } 206 207 flush_cache_page(vma, addr, pte_pfn(*pvmw.pte)); 208 ptep_clear_flush_notify(vma, addr, pvmw.pte); 209 set_pte_at_notify(mm, addr, pvmw.pte, 210 mk_pte(new_page, vma->vm_page_prot)); 211 212 page_remove_rmap(old_page, false); 213 if (!page_mapped(old_page)) 214 try_to_free_swap(old_page); 215 page_vma_mapped_walk_done(&pvmw); 216 217 if (vma->vm_flags & VM_LOCKED) 218 munlock_vma_page(old_page); 219 put_page(old_page); 220 221 err = 0; 222 unlock: 223 mmu_notifier_invalidate_range_end(&range); 224 unlock_page(old_page); 225 return err; 226 } 227 228 /** 229 * is_swbp_insn - check if instruction is breakpoint instruction. 230 * @insn: instruction to be checked. 231 * Default implementation of is_swbp_insn 232 * Returns true if @insn is a breakpoint instruction. 233 */ 234 bool __weak is_swbp_insn(uprobe_opcode_t *insn) 235 { 236 return *insn == UPROBE_SWBP_INSN; 237 } 238 239 /** 240 * is_trap_insn - check if instruction is breakpoint instruction. 241 * @insn: instruction to be checked. 242 * Default implementation of is_trap_insn 243 * Returns true if @insn is a breakpoint instruction. 244 * 245 * This function is needed for the case where an architecture has multiple 246 * trap instructions (like powerpc). 247 */ 248 bool __weak is_trap_insn(uprobe_opcode_t *insn) 249 { 250 return is_swbp_insn(insn); 251 } 252 253 static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len) 254 { 255 void *kaddr = kmap_atomic(page); 256 memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len); 257 kunmap_atomic(kaddr); 258 } 259 260 static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len) 261 { 262 void *kaddr = kmap_atomic(page); 263 memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len); 264 kunmap_atomic(kaddr); 265 } 266 267 static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode) 268 { 269 uprobe_opcode_t old_opcode; 270 bool is_swbp; 271 272 /* 273 * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here. 274 * We do not check if it is any other 'trap variant' which could 275 * be conditional trap instruction such as the one powerpc supports. 276 * 277 * The logic is that we do not care if the underlying instruction 278 * is a trap variant; uprobes always wins over any other (gdb) 279 * breakpoint. 280 */ 281 copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE); 282 is_swbp = is_swbp_insn(&old_opcode); 283 284 if (is_swbp_insn(new_opcode)) { 285 if (is_swbp) /* register: already installed? */ 286 return 0; 287 } else { 288 if (!is_swbp) /* unregister: was it changed by us? */ 289 return 0; 290 } 291 292 return 1; 293 } 294 295 static struct delayed_uprobe * 296 delayed_uprobe_check(struct uprobe *uprobe, struct mm_struct *mm) 297 { 298 struct delayed_uprobe *du; 299 300 list_for_each_entry(du, &delayed_uprobe_list, list) 301 if (du->uprobe == uprobe && du->mm == mm) 302 return du; 303 return NULL; 304 } 305 306 static int delayed_uprobe_add(struct uprobe *uprobe, struct mm_struct *mm) 307 { 308 struct delayed_uprobe *du; 309 310 if (delayed_uprobe_check(uprobe, mm)) 311 return 0; 312 313 du = kzalloc(sizeof(*du), GFP_KERNEL); 314 if (!du) 315 return -ENOMEM; 316 317 du->uprobe = uprobe; 318 du->mm = mm; 319 list_add(&du->list, &delayed_uprobe_list); 320 return 0; 321 } 322 323 static void delayed_uprobe_delete(struct delayed_uprobe *du) 324 { 325 if (WARN_ON(!du)) 326 return; 327 list_del(&du->list); 328 kfree(du); 329 } 330 331 static void delayed_uprobe_remove(struct uprobe *uprobe, struct mm_struct *mm) 332 { 333 struct list_head *pos, *q; 334 struct delayed_uprobe *du; 335 336 if (!uprobe && !mm) 337 return; 338 339 list_for_each_safe(pos, q, &delayed_uprobe_list) { 340 du = list_entry(pos, struct delayed_uprobe, list); 341 342 if (uprobe && du->uprobe != uprobe) 343 continue; 344 if (mm && du->mm != mm) 345 continue; 346 347 delayed_uprobe_delete(du); 348 } 349 } 350 351 static bool valid_ref_ctr_vma(struct uprobe *uprobe, 352 struct vm_area_struct *vma) 353 { 354 unsigned long vaddr = offset_to_vaddr(vma, uprobe->ref_ctr_offset); 355 356 return uprobe->ref_ctr_offset && 357 vma->vm_file && 358 file_inode(vma->vm_file) == uprobe->inode && 359 (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE && 360 vma->vm_start <= vaddr && 361 vma->vm_end > vaddr; 362 } 363 364 static struct vm_area_struct * 365 find_ref_ctr_vma(struct uprobe *uprobe, struct mm_struct *mm) 366 { 367 struct vm_area_struct *tmp; 368 369 for (tmp = mm->mmap; tmp; tmp = tmp->vm_next) 370 if (valid_ref_ctr_vma(uprobe, tmp)) 371 return tmp; 372 373 return NULL; 374 } 375 376 static int 377 __update_ref_ctr(struct mm_struct *mm, unsigned long vaddr, short d) 378 { 379 void *kaddr; 380 struct page *page; 381 struct vm_area_struct *vma; 382 int ret; 383 short *ptr; 384 385 if (!vaddr || !d) 386 return -EINVAL; 387 388 ret = get_user_pages_remote(NULL, mm, vaddr, 1, 389 FOLL_WRITE, &page, &vma, NULL); 390 if (unlikely(ret <= 0)) { 391 /* 392 * We are asking for 1 page. If get_user_pages_remote() fails, 393 * it may return 0, in that case we have to return error. 394 */ 395 return ret == 0 ? -EBUSY : ret; 396 } 397 398 kaddr = kmap_atomic(page); 399 ptr = kaddr + (vaddr & ~PAGE_MASK); 400 401 if (unlikely(*ptr + d < 0)) { 402 pr_warn("ref_ctr going negative. vaddr: 0x%lx, " 403 "curr val: %d, delta: %d\n", vaddr, *ptr, d); 404 ret = -EINVAL; 405 goto out; 406 } 407 408 *ptr += d; 409 ret = 0; 410 out: 411 kunmap_atomic(kaddr); 412 put_page(page); 413 return ret; 414 } 415 416 static void update_ref_ctr_warn(struct uprobe *uprobe, 417 struct mm_struct *mm, short d) 418 { 419 pr_warn("ref_ctr %s failed for inode: 0x%lx offset: " 420 "0x%llx ref_ctr_offset: 0x%llx of mm: 0x%pK\n", 421 d > 0 ? "increment" : "decrement", uprobe->inode->i_ino, 422 (unsigned long long) uprobe->offset, 423 (unsigned long long) uprobe->ref_ctr_offset, mm); 424 } 425 426 static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm, 427 short d) 428 { 429 struct vm_area_struct *rc_vma; 430 unsigned long rc_vaddr; 431 int ret = 0; 432 433 rc_vma = find_ref_ctr_vma(uprobe, mm); 434 435 if (rc_vma) { 436 rc_vaddr = offset_to_vaddr(rc_vma, uprobe->ref_ctr_offset); 437 ret = __update_ref_ctr(mm, rc_vaddr, d); 438 if (ret) 439 update_ref_ctr_warn(uprobe, mm, d); 440 441 if (d > 0) 442 return ret; 443 } 444 445 mutex_lock(&delayed_uprobe_lock); 446 if (d > 0) 447 ret = delayed_uprobe_add(uprobe, mm); 448 else 449 delayed_uprobe_remove(uprobe, mm); 450 mutex_unlock(&delayed_uprobe_lock); 451 452 return ret; 453 } 454 455 /* 456 * NOTE: 457 * Expect the breakpoint instruction to be the smallest size instruction for 458 * the architecture. If an arch has variable length instruction and the 459 * breakpoint instruction is not of the smallest length instruction 460 * supported by that architecture then we need to modify is_trap_at_addr and 461 * uprobe_write_opcode accordingly. This would never be a problem for archs 462 * that have fixed length instructions. 463 * 464 * uprobe_write_opcode - write the opcode at a given virtual address. 465 * @mm: the probed process address space. 466 * @vaddr: the virtual address to store the opcode. 467 * @opcode: opcode to be written at @vaddr. 468 * 469 * Called with mm->mmap_sem held for write. 470 * Return 0 (success) or a negative errno. 471 */ 472 int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, 473 unsigned long vaddr, uprobe_opcode_t opcode) 474 { 475 struct uprobe *uprobe; 476 struct page *old_page, *new_page; 477 struct vm_area_struct *vma; 478 int ret, is_register, ref_ctr_updated = 0; 479 480 is_register = is_swbp_insn(&opcode); 481 uprobe = container_of(auprobe, struct uprobe, arch); 482 483 retry: 484 /* Read the page with vaddr into memory */ 485 ret = get_user_pages_remote(NULL, mm, vaddr, 1, 486 FOLL_FORCE | FOLL_SPLIT, &old_page, &vma, NULL); 487 if (ret <= 0) 488 return ret; 489 490 ret = verify_opcode(old_page, vaddr, &opcode); 491 if (ret <= 0) 492 goto put_old; 493 494 /* We are going to replace instruction, update ref_ctr. */ 495 if (!ref_ctr_updated && uprobe->ref_ctr_offset) { 496 ret = update_ref_ctr(uprobe, mm, is_register ? 1 : -1); 497 if (ret) 498 goto put_old; 499 500 ref_ctr_updated = 1; 501 } 502 503 ret = anon_vma_prepare(vma); 504 if (ret) 505 goto put_old; 506 507 ret = -ENOMEM; 508 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr); 509 if (!new_page) 510 goto put_old; 511 512 __SetPageUptodate(new_page); 513 copy_highpage(new_page, old_page); 514 copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); 515 516 ret = __replace_page(vma, vaddr, old_page, new_page); 517 put_page(new_page); 518 put_old: 519 put_page(old_page); 520 521 if (unlikely(ret == -EAGAIN)) 522 goto retry; 523 524 /* Revert back reference counter if instruction update failed. */ 525 if (ret && is_register && ref_ctr_updated) 526 update_ref_ctr(uprobe, mm, -1); 527 528 return ret; 529 } 530 531 /** 532 * set_swbp - store breakpoint at a given address. 533 * @auprobe: arch specific probepoint information. 534 * @mm: the probed process address space. 535 * @vaddr: the virtual address to insert the opcode. 536 * 537 * For mm @mm, store the breakpoint instruction at @vaddr. 538 * Return 0 (success) or a negative errno. 539 */ 540 int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) 541 { 542 return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN); 543 } 544 545 /** 546 * set_orig_insn - Restore the original instruction. 547 * @mm: the probed process address space. 548 * @auprobe: arch specific probepoint information. 549 * @vaddr: the virtual address to insert the opcode. 550 * 551 * For mm @mm, restore the original opcode (opcode) at @vaddr. 552 * Return 0 (success) or a negative errno. 553 */ 554 int __weak 555 set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) 556 { 557 return uprobe_write_opcode(auprobe, mm, vaddr, 558 *(uprobe_opcode_t *)&auprobe->insn); 559 } 560 561 static struct uprobe *get_uprobe(struct uprobe *uprobe) 562 { 563 atomic_inc(&uprobe->ref); 564 return uprobe; 565 } 566 567 static void put_uprobe(struct uprobe *uprobe) 568 { 569 if (atomic_dec_and_test(&uprobe->ref)) { 570 /* 571 * If application munmap(exec_vma) before uprobe_unregister() 572 * gets called, we don't get a chance to remove uprobe from 573 * delayed_uprobe_list from remove_breakpoint(). Do it here. 574 */ 575 mutex_lock(&delayed_uprobe_lock); 576 delayed_uprobe_remove(uprobe, NULL); 577 mutex_unlock(&delayed_uprobe_lock); 578 kfree(uprobe); 579 } 580 } 581 582 static int match_uprobe(struct uprobe *l, struct uprobe *r) 583 { 584 if (l->inode < r->inode) 585 return -1; 586 587 if (l->inode > r->inode) 588 return 1; 589 590 if (l->offset < r->offset) 591 return -1; 592 593 if (l->offset > r->offset) 594 return 1; 595 596 return 0; 597 } 598 599 static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset) 600 { 601 struct uprobe u = { .inode = inode, .offset = offset }; 602 struct rb_node *n = uprobes_tree.rb_node; 603 struct uprobe *uprobe; 604 int match; 605 606 while (n) { 607 uprobe = rb_entry(n, struct uprobe, rb_node); 608 match = match_uprobe(&u, uprobe); 609 if (!match) 610 return get_uprobe(uprobe); 611 612 if (match < 0) 613 n = n->rb_left; 614 else 615 n = n->rb_right; 616 } 617 return NULL; 618 } 619 620 /* 621 * Find a uprobe corresponding to a given inode:offset 622 * Acquires uprobes_treelock 623 */ 624 static struct uprobe *find_uprobe(struct inode *inode, loff_t offset) 625 { 626 struct uprobe *uprobe; 627 628 spin_lock(&uprobes_treelock); 629 uprobe = __find_uprobe(inode, offset); 630 spin_unlock(&uprobes_treelock); 631 632 return uprobe; 633 } 634 635 static struct uprobe *__insert_uprobe(struct uprobe *uprobe) 636 { 637 struct rb_node **p = &uprobes_tree.rb_node; 638 struct rb_node *parent = NULL; 639 struct uprobe *u; 640 int match; 641 642 while (*p) { 643 parent = *p; 644 u = rb_entry(parent, struct uprobe, rb_node); 645 match = match_uprobe(uprobe, u); 646 if (!match) 647 return get_uprobe(u); 648 649 if (match < 0) 650 p = &parent->rb_left; 651 else 652 p = &parent->rb_right; 653 654 } 655 656 u = NULL; 657 rb_link_node(&uprobe->rb_node, parent, p); 658 rb_insert_color(&uprobe->rb_node, &uprobes_tree); 659 /* get access + creation ref */ 660 atomic_set(&uprobe->ref, 2); 661 662 return u; 663 } 664 665 /* 666 * Acquire uprobes_treelock. 667 * Matching uprobe already exists in rbtree; 668 * increment (access refcount) and return the matching uprobe. 669 * 670 * No matching uprobe; insert the uprobe in rb_tree; 671 * get a double refcount (access + creation) and return NULL. 672 */ 673 static struct uprobe *insert_uprobe(struct uprobe *uprobe) 674 { 675 struct uprobe *u; 676 677 spin_lock(&uprobes_treelock); 678 u = __insert_uprobe(uprobe); 679 spin_unlock(&uprobes_treelock); 680 681 return u; 682 } 683 684 static void 685 ref_ctr_mismatch_warn(struct uprobe *cur_uprobe, struct uprobe *uprobe) 686 { 687 pr_warn("ref_ctr_offset mismatch. inode: 0x%lx offset: 0x%llx " 688 "ref_ctr_offset(old): 0x%llx ref_ctr_offset(new): 0x%llx\n", 689 uprobe->inode->i_ino, (unsigned long long) uprobe->offset, 690 (unsigned long long) cur_uprobe->ref_ctr_offset, 691 (unsigned long long) uprobe->ref_ctr_offset); 692 } 693 694 static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, 695 loff_t ref_ctr_offset) 696 { 697 struct uprobe *uprobe, *cur_uprobe; 698 699 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL); 700 if (!uprobe) 701 return NULL; 702 703 uprobe->inode = inode; 704 uprobe->offset = offset; 705 uprobe->ref_ctr_offset = ref_ctr_offset; 706 init_rwsem(&uprobe->register_rwsem); 707 init_rwsem(&uprobe->consumer_rwsem); 708 709 /* add to uprobes_tree, sorted on inode:offset */ 710 cur_uprobe = insert_uprobe(uprobe); 711 /* a uprobe exists for this inode:offset combination */ 712 if (cur_uprobe) { 713 if (cur_uprobe->ref_ctr_offset != uprobe->ref_ctr_offset) { 714 ref_ctr_mismatch_warn(cur_uprobe, uprobe); 715 put_uprobe(cur_uprobe); 716 kfree(uprobe); 717 return ERR_PTR(-EINVAL); 718 } 719 kfree(uprobe); 720 uprobe = cur_uprobe; 721 } 722 723 return uprobe; 724 } 725 726 static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) 727 { 728 down_write(&uprobe->consumer_rwsem); 729 uc->next = uprobe->consumers; 730 uprobe->consumers = uc; 731 up_write(&uprobe->consumer_rwsem); 732 } 733 734 /* 735 * For uprobe @uprobe, delete the consumer @uc. 736 * Return true if the @uc is deleted successfully 737 * or return false. 738 */ 739 static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc) 740 { 741 struct uprobe_consumer **con; 742 bool ret = false; 743 744 down_write(&uprobe->consumer_rwsem); 745 for (con = &uprobe->consumers; *con; con = &(*con)->next) { 746 if (*con == uc) { 747 *con = uc->next; 748 ret = true; 749 break; 750 } 751 } 752 up_write(&uprobe->consumer_rwsem); 753 754 return ret; 755 } 756 757 static int __copy_insn(struct address_space *mapping, struct file *filp, 758 void *insn, int nbytes, loff_t offset) 759 { 760 struct page *page; 761 /* 762 * Ensure that the page that has the original instruction is populated 763 * and in page-cache. If ->readpage == NULL it must be shmem_mapping(), 764 * see uprobe_register(). 765 */ 766 if (mapping->a_ops->readpage) 767 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp); 768 else 769 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT); 770 if (IS_ERR(page)) 771 return PTR_ERR(page); 772 773 copy_from_page(page, offset, insn, nbytes); 774 put_page(page); 775 776 return 0; 777 } 778 779 static int copy_insn(struct uprobe *uprobe, struct file *filp) 780 { 781 struct address_space *mapping = uprobe->inode->i_mapping; 782 loff_t offs = uprobe->offset; 783 void *insn = &uprobe->arch.insn; 784 int size = sizeof(uprobe->arch.insn); 785 int len, err = -EIO; 786 787 /* Copy only available bytes, -EIO if nothing was read */ 788 do { 789 if (offs >= i_size_read(uprobe->inode)) 790 break; 791 792 len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK)); 793 err = __copy_insn(mapping, filp, insn, len, offs); 794 if (err) 795 break; 796 797 insn += len; 798 offs += len; 799 size -= len; 800 } while (size); 801 802 return err; 803 } 804 805 static int prepare_uprobe(struct uprobe *uprobe, struct file *file, 806 struct mm_struct *mm, unsigned long vaddr) 807 { 808 int ret = 0; 809 810 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) 811 return ret; 812 813 /* TODO: move this into _register, until then we abuse this sem. */ 814 down_write(&uprobe->consumer_rwsem); 815 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) 816 goto out; 817 818 ret = copy_insn(uprobe, file); 819 if (ret) 820 goto out; 821 822 ret = -ENOTSUPP; 823 if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn)) 824 goto out; 825 826 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr); 827 if (ret) 828 goto out; 829 830 /* uprobe_write_opcode() assumes we don't cross page boundary */ 831 BUG_ON((uprobe->offset & ~PAGE_MASK) + 832 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE); 833 834 smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */ 835 set_bit(UPROBE_COPY_INSN, &uprobe->flags); 836 837 out: 838 up_write(&uprobe->consumer_rwsem); 839 840 return ret; 841 } 842 843 static inline bool consumer_filter(struct uprobe_consumer *uc, 844 enum uprobe_filter_ctx ctx, struct mm_struct *mm) 845 { 846 return !uc->filter || uc->filter(uc, ctx, mm); 847 } 848 849 static bool filter_chain(struct uprobe *uprobe, 850 enum uprobe_filter_ctx ctx, struct mm_struct *mm) 851 { 852 struct uprobe_consumer *uc; 853 bool ret = false; 854 855 down_read(&uprobe->consumer_rwsem); 856 for (uc = uprobe->consumers; uc; uc = uc->next) { 857 ret = consumer_filter(uc, ctx, mm); 858 if (ret) 859 break; 860 } 861 up_read(&uprobe->consumer_rwsem); 862 863 return ret; 864 } 865 866 static int 867 install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, 868 struct vm_area_struct *vma, unsigned long vaddr) 869 { 870 bool first_uprobe; 871 int ret; 872 873 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr); 874 if (ret) 875 return ret; 876 877 /* 878 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(), 879 * the task can hit this breakpoint right after __replace_page(). 880 */ 881 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags); 882 if (first_uprobe) 883 set_bit(MMF_HAS_UPROBES, &mm->flags); 884 885 ret = set_swbp(&uprobe->arch, mm, vaddr); 886 if (!ret) 887 clear_bit(MMF_RECALC_UPROBES, &mm->flags); 888 else if (first_uprobe) 889 clear_bit(MMF_HAS_UPROBES, &mm->flags); 890 891 return ret; 892 } 893 894 static int 895 remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr) 896 { 897 set_bit(MMF_RECALC_UPROBES, &mm->flags); 898 return set_orig_insn(&uprobe->arch, mm, vaddr); 899 } 900 901 static inline bool uprobe_is_active(struct uprobe *uprobe) 902 { 903 return !RB_EMPTY_NODE(&uprobe->rb_node); 904 } 905 /* 906 * There could be threads that have already hit the breakpoint. They 907 * will recheck the current insn and restart if find_uprobe() fails. 908 * See find_active_uprobe(). 909 */ 910 static void delete_uprobe(struct uprobe *uprobe) 911 { 912 if (WARN_ON(!uprobe_is_active(uprobe))) 913 return; 914 915 spin_lock(&uprobes_treelock); 916 rb_erase(&uprobe->rb_node, &uprobes_tree); 917 spin_unlock(&uprobes_treelock); 918 RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */ 919 put_uprobe(uprobe); 920 } 921 922 struct map_info { 923 struct map_info *next; 924 struct mm_struct *mm; 925 unsigned long vaddr; 926 }; 927 928 static inline struct map_info *free_map_info(struct map_info *info) 929 { 930 struct map_info *next = info->next; 931 kfree(info); 932 return next; 933 } 934 935 static struct map_info * 936 build_map_info(struct address_space *mapping, loff_t offset, bool is_register) 937 { 938 unsigned long pgoff = offset >> PAGE_SHIFT; 939 struct vm_area_struct *vma; 940 struct map_info *curr = NULL; 941 struct map_info *prev = NULL; 942 struct map_info *info; 943 int more = 0; 944 945 again: 946 i_mmap_lock_read(mapping); 947 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { 948 if (!valid_vma(vma, is_register)) 949 continue; 950 951 if (!prev && !more) { 952 /* 953 * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through 954 * reclaim. This is optimistic, no harm done if it fails. 955 */ 956 prev = kmalloc(sizeof(struct map_info), 957 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN); 958 if (prev) 959 prev->next = NULL; 960 } 961 if (!prev) { 962 more++; 963 continue; 964 } 965 966 if (!mmget_not_zero(vma->vm_mm)) 967 continue; 968 969 info = prev; 970 prev = prev->next; 971 info->next = curr; 972 curr = info; 973 974 info->mm = vma->vm_mm; 975 info->vaddr = offset_to_vaddr(vma, offset); 976 } 977 i_mmap_unlock_read(mapping); 978 979 if (!more) 980 goto out; 981 982 prev = curr; 983 while (curr) { 984 mmput(curr->mm); 985 curr = curr->next; 986 } 987 988 do { 989 info = kmalloc(sizeof(struct map_info), GFP_KERNEL); 990 if (!info) { 991 curr = ERR_PTR(-ENOMEM); 992 goto out; 993 } 994 info->next = prev; 995 prev = info; 996 } while (--more); 997 998 goto again; 999 out: 1000 while (prev) 1001 prev = free_map_info(prev); 1002 return curr; 1003 } 1004 1005 static int 1006 register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) 1007 { 1008 bool is_register = !!new; 1009 struct map_info *info; 1010 int err = 0; 1011 1012 percpu_down_write(&dup_mmap_sem); 1013 info = build_map_info(uprobe->inode->i_mapping, 1014 uprobe->offset, is_register); 1015 if (IS_ERR(info)) { 1016 err = PTR_ERR(info); 1017 goto out; 1018 } 1019 1020 while (info) { 1021 struct mm_struct *mm = info->mm; 1022 struct vm_area_struct *vma; 1023 1024 if (err && is_register) 1025 goto free; 1026 1027 down_write(&mm->mmap_sem); 1028 vma = find_vma(mm, info->vaddr); 1029 if (!vma || !valid_vma(vma, is_register) || 1030 file_inode(vma->vm_file) != uprobe->inode) 1031 goto unlock; 1032 1033 if (vma->vm_start > info->vaddr || 1034 vaddr_to_offset(vma, info->vaddr) != uprobe->offset) 1035 goto unlock; 1036 1037 if (is_register) { 1038 /* consult only the "caller", new consumer. */ 1039 if (consumer_filter(new, 1040 UPROBE_FILTER_REGISTER, mm)) 1041 err = install_breakpoint(uprobe, mm, vma, info->vaddr); 1042 } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) { 1043 if (!filter_chain(uprobe, 1044 UPROBE_FILTER_UNREGISTER, mm)) 1045 err |= remove_breakpoint(uprobe, mm, info->vaddr); 1046 } 1047 1048 unlock: 1049 up_write(&mm->mmap_sem); 1050 free: 1051 mmput(mm); 1052 info = free_map_info(info); 1053 } 1054 out: 1055 percpu_up_write(&dup_mmap_sem); 1056 return err; 1057 } 1058 1059 static void 1060 __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) 1061 { 1062 int err; 1063 1064 if (WARN_ON(!consumer_del(uprobe, uc))) 1065 return; 1066 1067 err = register_for_each_vma(uprobe, NULL); 1068 /* TODO : cant unregister? schedule a worker thread */ 1069 if (!uprobe->consumers && !err) 1070 delete_uprobe(uprobe); 1071 } 1072 1073 /* 1074 * uprobe_unregister - unregister an already registered probe. 1075 * @inode: the file in which the probe has to be removed. 1076 * @offset: offset from the start of the file. 1077 * @uc: identify which probe if multiple probes are colocated. 1078 */ 1079 void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc) 1080 { 1081 struct uprobe *uprobe; 1082 1083 uprobe = find_uprobe(inode, offset); 1084 if (WARN_ON(!uprobe)) 1085 return; 1086 1087 down_write(&uprobe->register_rwsem); 1088 __uprobe_unregister(uprobe, uc); 1089 up_write(&uprobe->register_rwsem); 1090 put_uprobe(uprobe); 1091 } 1092 EXPORT_SYMBOL_GPL(uprobe_unregister); 1093 1094 /* 1095 * __uprobe_register - register a probe 1096 * @inode: the file in which the probe has to be placed. 1097 * @offset: offset from the start of the file. 1098 * @uc: information on howto handle the probe.. 1099 * 1100 * Apart from the access refcount, __uprobe_register() takes a creation 1101 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting 1102 * inserted into the rbtree (i.e first consumer for a @inode:@offset 1103 * tuple). Creation refcount stops uprobe_unregister from freeing the 1104 * @uprobe even before the register operation is complete. Creation 1105 * refcount is released when the last @uc for the @uprobe 1106 * unregisters. Caller of __uprobe_register() is required to keep @inode 1107 * (and the containing mount) referenced. 1108 * 1109 * Return errno if it cannot successully install probes 1110 * else return 0 (success) 1111 */ 1112 static int __uprobe_register(struct inode *inode, loff_t offset, 1113 loff_t ref_ctr_offset, struct uprobe_consumer *uc) 1114 { 1115 struct uprobe *uprobe; 1116 int ret; 1117 1118 /* Uprobe must have at least one set consumer */ 1119 if (!uc->handler && !uc->ret_handler) 1120 return -EINVAL; 1121 1122 /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */ 1123 if (!inode->i_mapping->a_ops->readpage && !shmem_mapping(inode->i_mapping)) 1124 return -EIO; 1125 /* Racy, just to catch the obvious mistakes */ 1126 if (offset > i_size_read(inode)) 1127 return -EINVAL; 1128 1129 retry: 1130 uprobe = alloc_uprobe(inode, offset, ref_ctr_offset); 1131 if (!uprobe) 1132 return -ENOMEM; 1133 if (IS_ERR(uprobe)) 1134 return PTR_ERR(uprobe); 1135 1136 /* 1137 * We can race with uprobe_unregister()->delete_uprobe(). 1138 * Check uprobe_is_active() and retry if it is false. 1139 */ 1140 down_write(&uprobe->register_rwsem); 1141 ret = -EAGAIN; 1142 if (likely(uprobe_is_active(uprobe))) { 1143 consumer_add(uprobe, uc); 1144 ret = register_for_each_vma(uprobe, uc); 1145 if (ret) 1146 __uprobe_unregister(uprobe, uc); 1147 } 1148 up_write(&uprobe->register_rwsem); 1149 put_uprobe(uprobe); 1150 1151 if (unlikely(ret == -EAGAIN)) 1152 goto retry; 1153 return ret; 1154 } 1155 1156 int uprobe_register(struct inode *inode, loff_t offset, 1157 struct uprobe_consumer *uc) 1158 { 1159 return __uprobe_register(inode, offset, 0, uc); 1160 } 1161 EXPORT_SYMBOL_GPL(uprobe_register); 1162 1163 int uprobe_register_refctr(struct inode *inode, loff_t offset, 1164 loff_t ref_ctr_offset, struct uprobe_consumer *uc) 1165 { 1166 return __uprobe_register(inode, offset, ref_ctr_offset, uc); 1167 } 1168 EXPORT_SYMBOL_GPL(uprobe_register_refctr); 1169 1170 /* 1171 * uprobe_apply - unregister an already registered probe. 1172 * @inode: the file in which the probe has to be removed. 1173 * @offset: offset from the start of the file. 1174 * @uc: consumer which wants to add more or remove some breakpoints 1175 * @add: add or remove the breakpoints 1176 */ 1177 int uprobe_apply(struct inode *inode, loff_t offset, 1178 struct uprobe_consumer *uc, bool add) 1179 { 1180 struct uprobe *uprobe; 1181 struct uprobe_consumer *con; 1182 int ret = -ENOENT; 1183 1184 uprobe = find_uprobe(inode, offset); 1185 if (WARN_ON(!uprobe)) 1186 return ret; 1187 1188 down_write(&uprobe->register_rwsem); 1189 for (con = uprobe->consumers; con && con != uc ; con = con->next) 1190 ; 1191 if (con) 1192 ret = register_for_each_vma(uprobe, add ? uc : NULL); 1193 up_write(&uprobe->register_rwsem); 1194 put_uprobe(uprobe); 1195 1196 return ret; 1197 } 1198 1199 static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm) 1200 { 1201 struct vm_area_struct *vma; 1202 int err = 0; 1203 1204 down_read(&mm->mmap_sem); 1205 for (vma = mm->mmap; vma; vma = vma->vm_next) { 1206 unsigned long vaddr; 1207 loff_t offset; 1208 1209 if (!valid_vma(vma, false) || 1210 file_inode(vma->vm_file) != uprobe->inode) 1211 continue; 1212 1213 offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT; 1214 if (uprobe->offset < offset || 1215 uprobe->offset >= offset + vma->vm_end - vma->vm_start) 1216 continue; 1217 1218 vaddr = offset_to_vaddr(vma, uprobe->offset); 1219 err |= remove_breakpoint(uprobe, mm, vaddr); 1220 } 1221 up_read(&mm->mmap_sem); 1222 1223 return err; 1224 } 1225 1226 static struct rb_node * 1227 find_node_in_range(struct inode *inode, loff_t min, loff_t max) 1228 { 1229 struct rb_node *n = uprobes_tree.rb_node; 1230 1231 while (n) { 1232 struct uprobe *u = rb_entry(n, struct uprobe, rb_node); 1233 1234 if (inode < u->inode) { 1235 n = n->rb_left; 1236 } else if (inode > u->inode) { 1237 n = n->rb_right; 1238 } else { 1239 if (max < u->offset) 1240 n = n->rb_left; 1241 else if (min > u->offset) 1242 n = n->rb_right; 1243 else 1244 break; 1245 } 1246 } 1247 1248 return n; 1249 } 1250 1251 /* 1252 * For a given range in vma, build a list of probes that need to be inserted. 1253 */ 1254 static void build_probe_list(struct inode *inode, 1255 struct vm_area_struct *vma, 1256 unsigned long start, unsigned long end, 1257 struct list_head *head) 1258 { 1259 loff_t min, max; 1260 struct rb_node *n, *t; 1261 struct uprobe *u; 1262 1263 INIT_LIST_HEAD(head); 1264 min = vaddr_to_offset(vma, start); 1265 max = min + (end - start) - 1; 1266 1267 spin_lock(&uprobes_treelock); 1268 n = find_node_in_range(inode, min, max); 1269 if (n) { 1270 for (t = n; t; t = rb_prev(t)) { 1271 u = rb_entry(t, struct uprobe, rb_node); 1272 if (u->inode != inode || u->offset < min) 1273 break; 1274 list_add(&u->pending_list, head); 1275 get_uprobe(u); 1276 } 1277 for (t = n; (t = rb_next(t)); ) { 1278 u = rb_entry(t, struct uprobe, rb_node); 1279 if (u->inode != inode || u->offset > max) 1280 break; 1281 list_add(&u->pending_list, head); 1282 get_uprobe(u); 1283 } 1284 } 1285 spin_unlock(&uprobes_treelock); 1286 } 1287 1288 /* @vma contains reference counter, not the probed instruction. */ 1289 static int delayed_ref_ctr_inc(struct vm_area_struct *vma) 1290 { 1291 struct list_head *pos, *q; 1292 struct delayed_uprobe *du; 1293 unsigned long vaddr; 1294 int ret = 0, err = 0; 1295 1296 mutex_lock(&delayed_uprobe_lock); 1297 list_for_each_safe(pos, q, &delayed_uprobe_list) { 1298 du = list_entry(pos, struct delayed_uprobe, list); 1299 1300 if (du->mm != vma->vm_mm || 1301 !valid_ref_ctr_vma(du->uprobe, vma)) 1302 continue; 1303 1304 vaddr = offset_to_vaddr(vma, du->uprobe->ref_ctr_offset); 1305 ret = __update_ref_ctr(vma->vm_mm, vaddr, 1); 1306 if (ret) { 1307 update_ref_ctr_warn(du->uprobe, vma->vm_mm, 1); 1308 if (!err) 1309 err = ret; 1310 } 1311 delayed_uprobe_delete(du); 1312 } 1313 mutex_unlock(&delayed_uprobe_lock); 1314 return err; 1315 } 1316 1317 /* 1318 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired. 1319 * 1320 * Currently we ignore all errors and always return 0, the callers 1321 * can't handle the failure anyway. 1322 */ 1323 int uprobe_mmap(struct vm_area_struct *vma) 1324 { 1325 struct list_head tmp_list; 1326 struct uprobe *uprobe, *u; 1327 struct inode *inode; 1328 1329 if (no_uprobe_events()) 1330 return 0; 1331 1332 if (vma->vm_file && 1333 (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE && 1334 test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags)) 1335 delayed_ref_ctr_inc(vma); 1336 1337 if (!valid_vma(vma, true)) 1338 return 0; 1339 1340 inode = file_inode(vma->vm_file); 1341 if (!inode) 1342 return 0; 1343 1344 mutex_lock(uprobes_mmap_hash(inode)); 1345 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list); 1346 /* 1347 * We can race with uprobe_unregister(), this uprobe can be already 1348 * removed. But in this case filter_chain() must return false, all 1349 * consumers have gone away. 1350 */ 1351 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) { 1352 if (!fatal_signal_pending(current) && 1353 filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) { 1354 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset); 1355 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); 1356 } 1357 put_uprobe(uprobe); 1358 } 1359 mutex_unlock(uprobes_mmap_hash(inode)); 1360 1361 return 0; 1362 } 1363 1364 static bool 1365 vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end) 1366 { 1367 loff_t min, max; 1368 struct inode *inode; 1369 struct rb_node *n; 1370 1371 inode = file_inode(vma->vm_file); 1372 1373 min = vaddr_to_offset(vma, start); 1374 max = min + (end - start) - 1; 1375 1376 spin_lock(&uprobes_treelock); 1377 n = find_node_in_range(inode, min, max); 1378 spin_unlock(&uprobes_treelock); 1379 1380 return !!n; 1381 } 1382 1383 /* 1384 * Called in context of a munmap of a vma. 1385 */ 1386 void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end) 1387 { 1388 if (no_uprobe_events() || !valid_vma(vma, false)) 1389 return; 1390 1391 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */ 1392 return; 1393 1394 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) || 1395 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags)) 1396 return; 1397 1398 if (vma_has_uprobes(vma, start, end)) 1399 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags); 1400 } 1401 1402 /* Slot allocation for XOL */ 1403 static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) 1404 { 1405 struct vm_area_struct *vma; 1406 int ret; 1407 1408 if (down_write_killable(&mm->mmap_sem)) 1409 return -EINTR; 1410 1411 if (mm->uprobes_state.xol_area) { 1412 ret = -EALREADY; 1413 goto fail; 1414 } 1415 1416 if (!area->vaddr) { 1417 /* Try to map as high as possible, this is only a hint. */ 1418 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, 1419 PAGE_SIZE, 0, 0); 1420 if (area->vaddr & ~PAGE_MASK) { 1421 ret = area->vaddr; 1422 goto fail; 1423 } 1424 } 1425 1426 vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE, 1427 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, 1428 &area->xol_mapping); 1429 if (IS_ERR(vma)) { 1430 ret = PTR_ERR(vma); 1431 goto fail; 1432 } 1433 1434 ret = 0; 1435 /* pairs with get_xol_area() */ 1436 smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */ 1437 fail: 1438 up_write(&mm->mmap_sem); 1439 1440 return ret; 1441 } 1442 1443 static struct xol_area *__create_xol_area(unsigned long vaddr) 1444 { 1445 struct mm_struct *mm = current->mm; 1446 uprobe_opcode_t insn = UPROBE_SWBP_INSN; 1447 struct xol_area *area; 1448 1449 area = kmalloc(sizeof(*area), GFP_KERNEL); 1450 if (unlikely(!area)) 1451 goto out; 1452 1453 area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long), 1454 GFP_KERNEL); 1455 if (!area->bitmap) 1456 goto free_area; 1457 1458 area->xol_mapping.name = "[uprobes]"; 1459 area->xol_mapping.fault = NULL; 1460 area->xol_mapping.pages = area->pages; 1461 area->pages[0] = alloc_page(GFP_HIGHUSER); 1462 if (!area->pages[0]) 1463 goto free_bitmap; 1464 area->pages[1] = NULL; 1465 1466 area->vaddr = vaddr; 1467 init_waitqueue_head(&area->wq); 1468 /* Reserve the 1st slot for get_trampoline_vaddr() */ 1469 set_bit(0, area->bitmap); 1470 atomic_set(&area->slot_count, 1); 1471 arch_uprobe_copy_ixol(area->pages[0], 0, &insn, UPROBE_SWBP_INSN_SIZE); 1472 1473 if (!xol_add_vma(mm, area)) 1474 return area; 1475 1476 __free_page(area->pages[0]); 1477 free_bitmap: 1478 kfree(area->bitmap); 1479 free_area: 1480 kfree(area); 1481 out: 1482 return NULL; 1483 } 1484 1485 /* 1486 * get_xol_area - Allocate process's xol_area if necessary. 1487 * This area will be used for storing instructions for execution out of line. 1488 * 1489 * Returns the allocated area or NULL. 1490 */ 1491 static struct xol_area *get_xol_area(void) 1492 { 1493 struct mm_struct *mm = current->mm; 1494 struct xol_area *area; 1495 1496 if (!mm->uprobes_state.xol_area) 1497 __create_xol_area(0); 1498 1499 /* Pairs with xol_add_vma() smp_store_release() */ 1500 area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */ 1501 return area; 1502 } 1503 1504 /* 1505 * uprobe_clear_state - Free the area allocated for slots. 1506 */ 1507 void uprobe_clear_state(struct mm_struct *mm) 1508 { 1509 struct xol_area *area = mm->uprobes_state.xol_area; 1510 1511 mutex_lock(&delayed_uprobe_lock); 1512 delayed_uprobe_remove(NULL, mm); 1513 mutex_unlock(&delayed_uprobe_lock); 1514 1515 if (!area) 1516 return; 1517 1518 put_page(area->pages[0]); 1519 kfree(area->bitmap); 1520 kfree(area); 1521 } 1522 1523 void uprobe_start_dup_mmap(void) 1524 { 1525 percpu_down_read(&dup_mmap_sem); 1526 } 1527 1528 void uprobe_end_dup_mmap(void) 1529 { 1530 percpu_up_read(&dup_mmap_sem); 1531 } 1532 1533 void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm) 1534 { 1535 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) { 1536 set_bit(MMF_HAS_UPROBES, &newmm->flags); 1537 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */ 1538 set_bit(MMF_RECALC_UPROBES, &newmm->flags); 1539 } 1540 } 1541 1542 /* 1543 * - search for a free slot. 1544 */ 1545 static unsigned long xol_take_insn_slot(struct xol_area *area) 1546 { 1547 unsigned long slot_addr; 1548 int slot_nr; 1549 1550 do { 1551 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE); 1552 if (slot_nr < UINSNS_PER_PAGE) { 1553 if (!test_and_set_bit(slot_nr, area->bitmap)) 1554 break; 1555 1556 slot_nr = UINSNS_PER_PAGE; 1557 continue; 1558 } 1559 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE)); 1560 } while (slot_nr >= UINSNS_PER_PAGE); 1561 1562 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES); 1563 atomic_inc(&area->slot_count); 1564 1565 return slot_addr; 1566 } 1567 1568 /* 1569 * xol_get_insn_slot - allocate a slot for xol. 1570 * Returns the allocated slot address or 0. 1571 */ 1572 static unsigned long xol_get_insn_slot(struct uprobe *uprobe) 1573 { 1574 struct xol_area *area; 1575 unsigned long xol_vaddr; 1576 1577 area = get_xol_area(); 1578 if (!area) 1579 return 0; 1580 1581 xol_vaddr = xol_take_insn_slot(area); 1582 if (unlikely(!xol_vaddr)) 1583 return 0; 1584 1585 arch_uprobe_copy_ixol(area->pages[0], xol_vaddr, 1586 &uprobe->arch.ixol, sizeof(uprobe->arch.ixol)); 1587 1588 return xol_vaddr; 1589 } 1590 1591 /* 1592 * xol_free_insn_slot - If slot was earlier allocated by 1593 * @xol_get_insn_slot(), make the slot available for 1594 * subsequent requests. 1595 */ 1596 static void xol_free_insn_slot(struct task_struct *tsk) 1597 { 1598 struct xol_area *area; 1599 unsigned long vma_end; 1600 unsigned long slot_addr; 1601 1602 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask) 1603 return; 1604 1605 slot_addr = tsk->utask->xol_vaddr; 1606 if (unlikely(!slot_addr)) 1607 return; 1608 1609 area = tsk->mm->uprobes_state.xol_area; 1610 vma_end = area->vaddr + PAGE_SIZE; 1611 if (area->vaddr <= slot_addr && slot_addr < vma_end) { 1612 unsigned long offset; 1613 int slot_nr; 1614 1615 offset = slot_addr - area->vaddr; 1616 slot_nr = offset / UPROBE_XOL_SLOT_BYTES; 1617 if (slot_nr >= UINSNS_PER_PAGE) 1618 return; 1619 1620 clear_bit(slot_nr, area->bitmap); 1621 atomic_dec(&area->slot_count); 1622 smp_mb__after_atomic(); /* pairs with prepare_to_wait() */ 1623 if (waitqueue_active(&area->wq)) 1624 wake_up(&area->wq); 1625 1626 tsk->utask->xol_vaddr = 0; 1627 } 1628 } 1629 1630 void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, 1631 void *src, unsigned long len) 1632 { 1633 /* Initialize the slot */ 1634 copy_to_page(page, vaddr, src, len); 1635 1636 /* 1637 * We probably need flush_icache_user_range() but it needs vma. 1638 * This should work on most of architectures by default. If 1639 * architecture needs to do something different it can define 1640 * its own version of the function. 1641 */ 1642 flush_dcache_page(page); 1643 } 1644 1645 /** 1646 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs 1647 * @regs: Reflects the saved state of the task after it has hit a breakpoint 1648 * instruction. 1649 * Return the address of the breakpoint instruction. 1650 */ 1651 unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs) 1652 { 1653 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE; 1654 } 1655 1656 unsigned long uprobe_get_trap_addr(struct pt_regs *regs) 1657 { 1658 struct uprobe_task *utask = current->utask; 1659 1660 if (unlikely(utask && utask->active_uprobe)) 1661 return utask->vaddr; 1662 1663 return instruction_pointer(regs); 1664 } 1665 1666 static struct return_instance *free_ret_instance(struct return_instance *ri) 1667 { 1668 struct return_instance *next = ri->next; 1669 put_uprobe(ri->uprobe); 1670 kfree(ri); 1671 return next; 1672 } 1673 1674 /* 1675 * Called with no locks held. 1676 * Called in context of an exiting or an exec-ing thread. 1677 */ 1678 void uprobe_free_utask(struct task_struct *t) 1679 { 1680 struct uprobe_task *utask = t->utask; 1681 struct return_instance *ri; 1682 1683 if (!utask) 1684 return; 1685 1686 if (utask->active_uprobe) 1687 put_uprobe(utask->active_uprobe); 1688 1689 ri = utask->return_instances; 1690 while (ri) 1691 ri = free_ret_instance(ri); 1692 1693 xol_free_insn_slot(t); 1694 kfree(utask); 1695 t->utask = NULL; 1696 } 1697 1698 /* 1699 * Allocate a uprobe_task object for the task if if necessary. 1700 * Called when the thread hits a breakpoint. 1701 * 1702 * Returns: 1703 * - pointer to new uprobe_task on success 1704 * - NULL otherwise 1705 */ 1706 static struct uprobe_task *get_utask(void) 1707 { 1708 if (!current->utask) 1709 current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL); 1710 return current->utask; 1711 } 1712 1713 static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) 1714 { 1715 struct uprobe_task *n_utask; 1716 struct return_instance **p, *o, *n; 1717 1718 n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL); 1719 if (!n_utask) 1720 return -ENOMEM; 1721 t->utask = n_utask; 1722 1723 p = &n_utask->return_instances; 1724 for (o = o_utask->return_instances; o; o = o->next) { 1725 n = kmalloc(sizeof(struct return_instance), GFP_KERNEL); 1726 if (!n) 1727 return -ENOMEM; 1728 1729 *n = *o; 1730 get_uprobe(n->uprobe); 1731 n->next = NULL; 1732 1733 *p = n; 1734 p = &n->next; 1735 n_utask->depth++; 1736 } 1737 1738 return 0; 1739 } 1740 1741 static void uprobe_warn(struct task_struct *t, const char *msg) 1742 { 1743 pr_warn("uprobe: %s:%d failed to %s\n", 1744 current->comm, current->pid, msg); 1745 } 1746 1747 static void dup_xol_work(struct callback_head *work) 1748 { 1749 if (current->flags & PF_EXITING) 1750 return; 1751 1752 if (!__create_xol_area(current->utask->dup_xol_addr) && 1753 !fatal_signal_pending(current)) 1754 uprobe_warn(current, "dup xol area"); 1755 } 1756 1757 /* 1758 * Called in context of a new clone/fork from copy_process. 1759 */ 1760 void uprobe_copy_process(struct task_struct *t, unsigned long flags) 1761 { 1762 struct uprobe_task *utask = current->utask; 1763 struct mm_struct *mm = current->mm; 1764 struct xol_area *area; 1765 1766 t->utask = NULL; 1767 1768 if (!utask || !utask->return_instances) 1769 return; 1770 1771 if (mm == t->mm && !(flags & CLONE_VFORK)) 1772 return; 1773 1774 if (dup_utask(t, utask)) 1775 return uprobe_warn(t, "dup ret instances"); 1776 1777 /* The task can fork() after dup_xol_work() fails */ 1778 area = mm->uprobes_state.xol_area; 1779 if (!area) 1780 return uprobe_warn(t, "dup xol area"); 1781 1782 if (mm == t->mm) 1783 return; 1784 1785 t->utask->dup_xol_addr = area->vaddr; 1786 init_task_work(&t->utask->dup_xol_work, dup_xol_work); 1787 task_work_add(t, &t->utask->dup_xol_work, true); 1788 } 1789 1790 /* 1791 * Current area->vaddr notion assume the trampoline address is always 1792 * equal area->vaddr. 1793 * 1794 * Returns -1 in case the xol_area is not allocated. 1795 */ 1796 static unsigned long get_trampoline_vaddr(void) 1797 { 1798 struct xol_area *area; 1799 unsigned long trampoline_vaddr = -1; 1800 1801 /* Pairs with xol_add_vma() smp_store_release() */ 1802 area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */ 1803 if (area) 1804 trampoline_vaddr = area->vaddr; 1805 1806 return trampoline_vaddr; 1807 } 1808 1809 static void cleanup_return_instances(struct uprobe_task *utask, bool chained, 1810 struct pt_regs *regs) 1811 { 1812 struct return_instance *ri = utask->return_instances; 1813 enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL; 1814 1815 while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) { 1816 ri = free_ret_instance(ri); 1817 utask->depth--; 1818 } 1819 utask->return_instances = ri; 1820 } 1821 1822 static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) 1823 { 1824 struct return_instance *ri; 1825 struct uprobe_task *utask; 1826 unsigned long orig_ret_vaddr, trampoline_vaddr; 1827 bool chained; 1828 1829 if (!get_xol_area()) 1830 return; 1831 1832 utask = get_utask(); 1833 if (!utask) 1834 return; 1835 1836 if (utask->depth >= MAX_URETPROBE_DEPTH) { 1837 printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to" 1838 " nestedness limit pid/tgid=%d/%d\n", 1839 current->pid, current->tgid); 1840 return; 1841 } 1842 1843 ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL); 1844 if (!ri) 1845 return; 1846 1847 trampoline_vaddr = get_trampoline_vaddr(); 1848 orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs); 1849 if (orig_ret_vaddr == -1) 1850 goto fail; 1851 1852 /* drop the entries invalidated by longjmp() */ 1853 chained = (orig_ret_vaddr == trampoline_vaddr); 1854 cleanup_return_instances(utask, chained, regs); 1855 1856 /* 1857 * We don't want to keep trampoline address in stack, rather keep the 1858 * original return address of first caller thru all the consequent 1859 * instances. This also makes breakpoint unwrapping easier. 1860 */ 1861 if (chained) { 1862 if (!utask->return_instances) { 1863 /* 1864 * This situation is not possible. Likely we have an 1865 * attack from user-space. 1866 */ 1867 uprobe_warn(current, "handle tail call"); 1868 goto fail; 1869 } 1870 orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; 1871 } 1872 1873 ri->uprobe = get_uprobe(uprobe); 1874 ri->func = instruction_pointer(regs); 1875 ri->stack = user_stack_pointer(regs); 1876 ri->orig_ret_vaddr = orig_ret_vaddr; 1877 ri->chained = chained; 1878 1879 utask->depth++; 1880 ri->next = utask->return_instances; 1881 utask->return_instances = ri; 1882 1883 return; 1884 fail: 1885 kfree(ri); 1886 } 1887 1888 /* Prepare to single-step probed instruction out of line. */ 1889 static int 1890 pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr) 1891 { 1892 struct uprobe_task *utask; 1893 unsigned long xol_vaddr; 1894 int err; 1895 1896 utask = get_utask(); 1897 if (!utask) 1898 return -ENOMEM; 1899 1900 xol_vaddr = xol_get_insn_slot(uprobe); 1901 if (!xol_vaddr) 1902 return -ENOMEM; 1903 1904 utask->xol_vaddr = xol_vaddr; 1905 utask->vaddr = bp_vaddr; 1906 1907 err = arch_uprobe_pre_xol(&uprobe->arch, regs); 1908 if (unlikely(err)) { 1909 xol_free_insn_slot(current); 1910 return err; 1911 } 1912 1913 utask->active_uprobe = uprobe; 1914 utask->state = UTASK_SSTEP; 1915 return 0; 1916 } 1917 1918 /* 1919 * If we are singlestepping, then ensure this thread is not connected to 1920 * non-fatal signals until completion of singlestep. When xol insn itself 1921 * triggers the signal, restart the original insn even if the task is 1922 * already SIGKILL'ed (since coredump should report the correct ip). This 1923 * is even more important if the task has a handler for SIGSEGV/etc, The 1924 * _same_ instruction should be repeated again after return from the signal 1925 * handler, and SSTEP can never finish in this case. 1926 */ 1927 bool uprobe_deny_signal(void) 1928 { 1929 struct task_struct *t = current; 1930 struct uprobe_task *utask = t->utask; 1931 1932 if (likely(!utask || !utask->active_uprobe)) 1933 return false; 1934 1935 WARN_ON_ONCE(utask->state != UTASK_SSTEP); 1936 1937 if (signal_pending(t)) { 1938 spin_lock_irq(&t->sighand->siglock); 1939 clear_tsk_thread_flag(t, TIF_SIGPENDING); 1940 spin_unlock_irq(&t->sighand->siglock); 1941 1942 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) { 1943 utask->state = UTASK_SSTEP_TRAPPED; 1944 set_tsk_thread_flag(t, TIF_UPROBE); 1945 } 1946 } 1947 1948 return true; 1949 } 1950 1951 static void mmf_recalc_uprobes(struct mm_struct *mm) 1952 { 1953 struct vm_area_struct *vma; 1954 1955 for (vma = mm->mmap; vma; vma = vma->vm_next) { 1956 if (!valid_vma(vma, false)) 1957 continue; 1958 /* 1959 * This is not strictly accurate, we can race with 1960 * uprobe_unregister() and see the already removed 1961 * uprobe if delete_uprobe() was not yet called. 1962 * Or this uprobe can be filtered out. 1963 */ 1964 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end)) 1965 return; 1966 } 1967 1968 clear_bit(MMF_HAS_UPROBES, &mm->flags); 1969 } 1970 1971 static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr) 1972 { 1973 struct page *page; 1974 uprobe_opcode_t opcode; 1975 int result; 1976 1977 pagefault_disable(); 1978 result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr); 1979 pagefault_enable(); 1980 1981 if (likely(result == 0)) 1982 goto out; 1983 1984 /* 1985 * The NULL 'tsk' here ensures that any faults that occur here 1986 * will not be accounted to the task. 'mm' *is* current->mm, 1987 * but we treat this as a 'remote' access since it is 1988 * essentially a kernel access to the memory. 1989 */ 1990 result = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &page, 1991 NULL, NULL); 1992 if (result < 0) 1993 return result; 1994 1995 copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); 1996 put_page(page); 1997 out: 1998 /* This needs to return true for any variant of the trap insn */ 1999 return is_trap_insn(&opcode); 2000 } 2001 2002 static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp) 2003 { 2004 struct mm_struct *mm = current->mm; 2005 struct uprobe *uprobe = NULL; 2006 struct vm_area_struct *vma; 2007 2008 down_read(&mm->mmap_sem); 2009 vma = find_vma(mm, bp_vaddr); 2010 if (vma && vma->vm_start <= bp_vaddr) { 2011 if (valid_vma(vma, false)) { 2012 struct inode *inode = file_inode(vma->vm_file); 2013 loff_t offset = vaddr_to_offset(vma, bp_vaddr); 2014 2015 uprobe = find_uprobe(inode, offset); 2016 } 2017 2018 if (!uprobe) 2019 *is_swbp = is_trap_at_addr(mm, bp_vaddr); 2020 } else { 2021 *is_swbp = -EFAULT; 2022 } 2023 2024 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags)) 2025 mmf_recalc_uprobes(mm); 2026 up_read(&mm->mmap_sem); 2027 2028 return uprobe; 2029 } 2030 2031 static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) 2032 { 2033 struct uprobe_consumer *uc; 2034 int remove = UPROBE_HANDLER_REMOVE; 2035 bool need_prep = false; /* prepare return uprobe, when needed */ 2036 2037 down_read(&uprobe->register_rwsem); 2038 for (uc = uprobe->consumers; uc; uc = uc->next) { 2039 int rc = 0; 2040 2041 if (uc->handler) { 2042 rc = uc->handler(uc, regs); 2043 WARN(rc & ~UPROBE_HANDLER_MASK, 2044 "bad rc=0x%x from %pf()\n", rc, uc->handler); 2045 } 2046 2047 if (uc->ret_handler) 2048 need_prep = true; 2049 2050 remove &= rc; 2051 } 2052 2053 if (need_prep && !remove) 2054 prepare_uretprobe(uprobe, regs); /* put bp at return */ 2055 2056 if (remove && uprobe->consumers) { 2057 WARN_ON(!uprobe_is_active(uprobe)); 2058 unapply_uprobe(uprobe, current->mm); 2059 } 2060 up_read(&uprobe->register_rwsem); 2061 } 2062 2063 static void 2064 handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) 2065 { 2066 struct uprobe *uprobe = ri->uprobe; 2067 struct uprobe_consumer *uc; 2068 2069 down_read(&uprobe->register_rwsem); 2070 for (uc = uprobe->consumers; uc; uc = uc->next) { 2071 if (uc->ret_handler) 2072 uc->ret_handler(uc, ri->func, regs); 2073 } 2074 up_read(&uprobe->register_rwsem); 2075 } 2076 2077 static struct return_instance *find_next_ret_chain(struct return_instance *ri) 2078 { 2079 bool chained; 2080 2081 do { 2082 chained = ri->chained; 2083 ri = ri->next; /* can't be NULL if chained */ 2084 } while (chained); 2085 2086 return ri; 2087 } 2088 2089 static void handle_trampoline(struct pt_regs *regs) 2090 { 2091 struct uprobe_task *utask; 2092 struct return_instance *ri, *next; 2093 bool valid; 2094 2095 utask = current->utask; 2096 if (!utask) 2097 goto sigill; 2098 2099 ri = utask->return_instances; 2100 if (!ri) 2101 goto sigill; 2102 2103 do { 2104 /* 2105 * We should throw out the frames invalidated by longjmp(). 2106 * If this chain is valid, then the next one should be alive 2107 * or NULL; the latter case means that nobody but ri->func 2108 * could hit this trampoline on return. TODO: sigaltstack(). 2109 */ 2110 next = find_next_ret_chain(ri); 2111 valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs); 2112 2113 instruction_pointer_set(regs, ri->orig_ret_vaddr); 2114 do { 2115 if (valid) 2116 handle_uretprobe_chain(ri, regs); 2117 ri = free_ret_instance(ri); 2118 utask->depth--; 2119 } while (ri != next); 2120 } while (!valid); 2121 2122 utask->return_instances = ri; 2123 return; 2124 2125 sigill: 2126 uprobe_warn(current, "handle uretprobe, sending SIGILL."); 2127 force_sig(SIGILL, current); 2128 2129 } 2130 2131 bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs) 2132 { 2133 return false; 2134 } 2135 2136 bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, 2137 struct pt_regs *regs) 2138 { 2139 return true; 2140 } 2141 2142 /* 2143 * Run handler and ask thread to singlestep. 2144 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps. 2145 */ 2146 static void handle_swbp(struct pt_regs *regs) 2147 { 2148 struct uprobe *uprobe; 2149 unsigned long bp_vaddr; 2150 int uninitialized_var(is_swbp); 2151 2152 bp_vaddr = uprobe_get_swbp_addr(regs); 2153 if (bp_vaddr == get_trampoline_vaddr()) 2154 return handle_trampoline(regs); 2155 2156 uprobe = find_active_uprobe(bp_vaddr, &is_swbp); 2157 if (!uprobe) { 2158 if (is_swbp > 0) { 2159 /* No matching uprobe; signal SIGTRAP. */ 2160 send_sig(SIGTRAP, current, 0); 2161 } else { 2162 /* 2163 * Either we raced with uprobe_unregister() or we can't 2164 * access this memory. The latter is only possible if 2165 * another thread plays with our ->mm. In both cases 2166 * we can simply restart. If this vma was unmapped we 2167 * can pretend this insn was not executed yet and get 2168 * the (correct) SIGSEGV after restart. 2169 */ 2170 instruction_pointer_set(regs, bp_vaddr); 2171 } 2172 return; 2173 } 2174 2175 /* change it in advance for ->handler() and restart */ 2176 instruction_pointer_set(regs, bp_vaddr); 2177 2178 /* 2179 * TODO: move copy_insn/etc into _register and remove this hack. 2180 * After we hit the bp, _unregister + _register can install the 2181 * new and not-yet-analyzed uprobe at the same address, restart. 2182 */ 2183 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags))) 2184 goto out; 2185 2186 /* 2187 * Pairs with the smp_wmb() in prepare_uprobe(). 2188 * 2189 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then 2190 * we must also see the stores to &uprobe->arch performed by the 2191 * prepare_uprobe() call. 2192 */ 2193 smp_rmb(); 2194 2195 /* Tracing handlers use ->utask to communicate with fetch methods */ 2196 if (!get_utask()) 2197 goto out; 2198 2199 if (arch_uprobe_ignore(&uprobe->arch, regs)) 2200 goto out; 2201 2202 handler_chain(uprobe, regs); 2203 2204 if (arch_uprobe_skip_sstep(&uprobe->arch, regs)) 2205 goto out; 2206 2207 if (!pre_ssout(uprobe, regs, bp_vaddr)) 2208 return; 2209 2210 /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */ 2211 out: 2212 put_uprobe(uprobe); 2213 } 2214 2215 /* 2216 * Perform required fix-ups and disable singlestep. 2217 * Allow pending signals to take effect. 2218 */ 2219 static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs) 2220 { 2221 struct uprobe *uprobe; 2222 int err = 0; 2223 2224 uprobe = utask->active_uprobe; 2225 if (utask->state == UTASK_SSTEP_ACK) 2226 err = arch_uprobe_post_xol(&uprobe->arch, regs); 2227 else if (utask->state == UTASK_SSTEP_TRAPPED) 2228 arch_uprobe_abort_xol(&uprobe->arch, regs); 2229 else 2230 WARN_ON_ONCE(1); 2231 2232 put_uprobe(uprobe); 2233 utask->active_uprobe = NULL; 2234 utask->state = UTASK_RUNNING; 2235 xol_free_insn_slot(current); 2236 2237 spin_lock_irq(¤t->sighand->siglock); 2238 recalc_sigpending(); /* see uprobe_deny_signal() */ 2239 spin_unlock_irq(¤t->sighand->siglock); 2240 2241 if (unlikely(err)) { 2242 uprobe_warn(current, "execute the probed insn, sending SIGILL."); 2243 force_sig(SIGILL, current); 2244 } 2245 } 2246 2247 /* 2248 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and 2249 * allows the thread to return from interrupt. After that handle_swbp() 2250 * sets utask->active_uprobe. 2251 * 2252 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag 2253 * and allows the thread to return from interrupt. 2254 * 2255 * While returning to userspace, thread notices the TIF_UPROBE flag and calls 2256 * uprobe_notify_resume(). 2257 */ 2258 void uprobe_notify_resume(struct pt_regs *regs) 2259 { 2260 struct uprobe_task *utask; 2261 2262 clear_thread_flag(TIF_UPROBE); 2263 2264 utask = current->utask; 2265 if (utask && utask->active_uprobe) 2266 handle_singlestep(utask, regs); 2267 else 2268 handle_swbp(regs); 2269 } 2270 2271 /* 2272 * uprobe_pre_sstep_notifier gets called from interrupt context as part of 2273 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit. 2274 */ 2275 int uprobe_pre_sstep_notifier(struct pt_regs *regs) 2276 { 2277 if (!current->mm) 2278 return 0; 2279 2280 if (!test_bit(MMF_HAS_UPROBES, ¤t->mm->flags) && 2281 (!current->utask || !current->utask->return_instances)) 2282 return 0; 2283 2284 set_thread_flag(TIF_UPROBE); 2285 return 1; 2286 } 2287 2288 /* 2289 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier 2290 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep. 2291 */ 2292 int uprobe_post_sstep_notifier(struct pt_regs *regs) 2293 { 2294 struct uprobe_task *utask = current->utask; 2295 2296 if (!current->mm || !utask || !utask->active_uprobe) 2297 /* task is currently not uprobed */ 2298 return 0; 2299 2300 utask->state = UTASK_SSTEP_ACK; 2301 set_thread_flag(TIF_UPROBE); 2302 return 1; 2303 } 2304 2305 static struct notifier_block uprobe_exception_nb = { 2306 .notifier_call = arch_uprobe_exception_notify, 2307 .priority = INT_MAX-1, /* notified after kprobes, kgdb */ 2308 }; 2309 2310 static int __init init_uprobes(void) 2311 { 2312 int i; 2313 2314 for (i = 0; i < UPROBES_HASH_SZ; i++) 2315 mutex_init(&uprobes_mmap_mutex[i]); 2316 2317 if (percpu_init_rwsem(&dup_mmap_sem)) 2318 return -ENOMEM; 2319 2320 return register_die_notifier(&uprobe_exception_nb); 2321 } 2322 __initcall(init_uprobes); 2323