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