This source file includes following definitions.
- mpx_bd_size_bytes
- mpx_bt_size_bytes
- mpx_mmap
- mpx_insn_decode
- mpx_fault_info
- mpx_get_bounds_dir
- mpx_enable_management
- mpx_disable_management
- mpx_cmpxchg_bd_entry
- allocate_bt
- do_mpx_bt_fault
- mpx_handle_bd_fault
- mpx_resolve_fault
- mpx_bd_entry_to_bt_addr
- get_user_bd_entry
- get_bt_addr
- bt_entry_size_bytes
- mpx_get_bt_entry_offset_bytes
- bd_entry_virt_space
- zap_bt_entries_mapping
- mpx_get_bd_entry_offset
- unmap_entire_bt
- try_unmap_single_bt
- mpx_unmap_tables
- mpx_notify_unmap
- mpx_unmapped_area_check
1
2
3
4
5
6
7
8
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/mm_types.h>
12 #include <linux/mman.h>
13 #include <linux/syscalls.h>
14 #include <linux/sched/sysctl.h>
15
16 #include <asm/insn.h>
17 #include <asm/insn-eval.h>
18 #include <asm/mmu_context.h>
19 #include <asm/mpx.h>
20 #include <asm/processor.h>
21 #include <asm/fpu/internal.h>
22
23 #define CREATE_TRACE_POINTS
24 #include <asm/trace/mpx.h>
25
26 static inline unsigned long mpx_bd_size_bytes(struct mm_struct *mm)
27 {
28 if (is_64bit_mm(mm))
29 return MPX_BD_SIZE_BYTES_64;
30 else
31 return MPX_BD_SIZE_BYTES_32;
32 }
33
34 static inline unsigned long mpx_bt_size_bytes(struct mm_struct *mm)
35 {
36 if (is_64bit_mm(mm))
37 return MPX_BT_SIZE_BYTES_64;
38 else
39 return MPX_BT_SIZE_BYTES_32;
40 }
41
42
43
44
45
46 static unsigned long mpx_mmap(unsigned long len)
47 {
48 struct mm_struct *mm = current->mm;
49 unsigned long addr, populate;
50
51
52 if (len != mpx_bt_size_bytes(mm))
53 return -EINVAL;
54
55 down_write(&mm->mmap_sem);
56 addr = do_mmap(NULL, 0, len, PROT_READ | PROT_WRITE,
57 MAP_ANONYMOUS | MAP_PRIVATE, VM_MPX, 0, &populate, NULL);
58 up_write(&mm->mmap_sem);
59 if (populate)
60 mm_populate(addr, populate);
61
62 return addr;
63 }
64
65 static int mpx_insn_decode(struct insn *insn,
66 struct pt_regs *regs)
67 {
68 unsigned char buf[MAX_INSN_SIZE];
69 int x86_64 = !test_thread_flag(TIF_IA32);
70 int not_copied;
71 int nr_copied;
72
73 not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf));
74 nr_copied = sizeof(buf) - not_copied;
75
76
77
78
79
80 if (!nr_copied)
81 return -EFAULT;
82 insn_init(insn, buf, nr_copied, x86_64);
83 insn_get_length(insn);
84
85
86
87
88
89
90
91
92 if (nr_copied < insn->length)
93 return -EFAULT;
94
95 insn_get_opcode(insn);
96
97
98
99
100 if (insn->opcode.bytes[0] != 0x0f)
101 goto bad_opcode;
102 if ((insn->opcode.bytes[1] != 0x1a) &&
103 (insn->opcode.bytes[1] != 0x1b))
104 goto bad_opcode;
105
106 return 0;
107 bad_opcode:
108 return -EINVAL;
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs)
123 {
124 const struct mpx_bndreg_state *bndregs;
125 const struct mpx_bndreg *bndreg;
126 struct insn insn;
127 uint8_t bndregno;
128 int err;
129
130 err = mpx_insn_decode(&insn, regs);
131 if (err)
132 goto err_out;
133
134
135
136
137
138 insn_get_modrm(&insn);
139 bndregno = X86_MODRM_REG(insn.modrm.value);
140 if (bndregno > 3) {
141 err = -EINVAL;
142 goto err_out;
143 }
144
145 bndregs = get_xsave_field_ptr(XFEATURE_BNDREGS);
146 if (!bndregs) {
147 err = -EINVAL;
148 goto err_out;
149 }
150
151 bndreg = &bndregs->bndreg[bndregno];
152
153
154
155
156
157
158
159
160
161
162
163 info->lower = (void __user *)(unsigned long)bndreg->lower_bound;
164 info->upper = (void __user *)(unsigned long)~bndreg->upper_bound;
165 info->addr = insn_get_addr_ref(&insn, regs);
166
167
168
169
170
171 if (info->addr == (void __user *)-1) {
172 err = -EINVAL;
173 goto err_out;
174 }
175 trace_mpx_bounds_register_exception(info->addr, bndreg);
176 return 0;
177 err_out:
178
179 return err;
180 }
181
182 static __user void *mpx_get_bounds_dir(void)
183 {
184 const struct mpx_bndcsr *bndcsr;
185
186 if (!cpu_feature_enabled(X86_FEATURE_MPX))
187 return MPX_INVALID_BOUNDS_DIR;
188
189
190
191
192
193 bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR);
194 if (!bndcsr)
195 return MPX_INVALID_BOUNDS_DIR;
196
197
198
199
200
201 if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
202 return MPX_INVALID_BOUNDS_DIR;
203
204
205
206
207
208 return (void __user *)(unsigned long)
209 (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
210 }
211
212 int mpx_enable_management(void)
213 {
214 void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
215 struct mm_struct *mm = current->mm;
216 int ret = 0;
217
218
219
220
221
222
223
224
225
226
227
228
229 bd_base = mpx_get_bounds_dir();
230 down_write(&mm->mmap_sem);
231
232
233 if (find_vma(mm, DEFAULT_MAP_WINDOW)) {
234 pr_warn_once("%s (%d): MPX cannot handle addresses "
235 "above 47-bits. Disabling.",
236 current->comm, current->pid);
237 ret = -ENXIO;
238 goto out;
239 }
240 mm->context.bd_addr = bd_base;
241 if (mm->context.bd_addr == MPX_INVALID_BOUNDS_DIR)
242 ret = -ENXIO;
243 out:
244 up_write(&mm->mmap_sem);
245 return ret;
246 }
247
248 int mpx_disable_management(void)
249 {
250 struct mm_struct *mm = current->mm;
251
252 if (!cpu_feature_enabled(X86_FEATURE_MPX))
253 return -ENXIO;
254
255 down_write(&mm->mmap_sem);
256 mm->context.bd_addr = MPX_INVALID_BOUNDS_DIR;
257 up_write(&mm->mmap_sem);
258 return 0;
259 }
260
261 static int mpx_cmpxchg_bd_entry(struct mm_struct *mm,
262 unsigned long *curval,
263 unsigned long __user *addr,
264 unsigned long old_val, unsigned long new_val)
265 {
266 int ret;
267
268
269
270
271
272
273
274 if (is_64bit_mm(mm)) {
275 ret = user_atomic_cmpxchg_inatomic(curval,
276 addr, old_val, new_val);
277 } else {
278 u32 uninitialized_var(curval_32);
279 u32 old_val_32 = old_val;
280 u32 new_val_32 = new_val;
281 u32 __user *addr_32 = (u32 __user *)addr;
282
283 ret = user_atomic_cmpxchg_inatomic(&curval_32,
284 addr_32, old_val_32, new_val_32);
285 *curval = curval_32;
286 }
287 return ret;
288 }
289
290
291
292
293
294
295 static int allocate_bt(struct mm_struct *mm, long __user *bd_entry)
296 {
297 unsigned long expected_old_val = 0;
298 unsigned long actual_old_val = 0;
299 unsigned long bt_addr;
300 unsigned long bd_new_entry;
301 int ret = 0;
302
303
304
305
306
307 bt_addr = mpx_mmap(mpx_bt_size_bytes(mm));
308 if (IS_ERR((void *)bt_addr))
309 return PTR_ERR((void *)bt_addr);
310
311
312
313 bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
314
315
316
317
318
319
320
321
322
323
324
325
326 ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry,
327 expected_old_val, bd_new_entry);
328 if (ret)
329 goto out_unmap;
330
331
332
333
334
335
336
337
338
339
340
341 if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
342 ret = 0;
343 goto out_unmap;
344 }
345
346
347
348
349
350
351 if (expected_old_val != actual_old_val) {
352 ret = -EINVAL;
353 goto out_unmap;
354 }
355 trace_mpx_new_bounds_table(bt_addr);
356 return 0;
357 out_unmap:
358 vm_munmap(bt_addr, mpx_bt_size_bytes(mm));
359 return ret;
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373 static int do_mpx_bt_fault(void)
374 {
375 unsigned long bd_entry, bd_base;
376 const struct mpx_bndcsr *bndcsr;
377 struct mm_struct *mm = current->mm;
378
379 bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR);
380 if (!bndcsr)
381 return -EINVAL;
382
383
384
385 bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
386
387
388
389
390 bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
391
392
393
394
395 if ((bd_entry < bd_base) ||
396 (bd_entry >= bd_base + mpx_bd_size_bytes(mm)))
397 return -EINVAL;
398
399 return allocate_bt(mm, (long __user *)bd_entry);
400 }
401
402 int mpx_handle_bd_fault(void)
403 {
404
405
406
407
408 if (!kernel_managing_mpx_tables(current->mm))
409 return -EINVAL;
410
411 return do_mpx_bt_fault();
412 }
413
414
415
416
417
418 static int mpx_resolve_fault(long __user *addr, int write)
419 {
420 long gup_ret;
421 int nr_pages = 1;
422
423 gup_ret = get_user_pages((unsigned long)addr, nr_pages,
424 write ? FOLL_WRITE : 0, NULL, NULL);
425
426
427
428
429
430 if (!gup_ret)
431 return -EFAULT;
432
433 if (gup_ret < 0)
434 return gup_ret;
435
436 return 0;
437 }
438
439 static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm,
440 unsigned long bd_entry)
441 {
442 unsigned long bt_addr = bd_entry;
443 int align_to_bytes;
444
445
446
447 bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG;
448
449
450
451
452
453
454 if (is_64bit_mm(mm))
455 align_to_bytes = 8;
456 else
457 align_to_bytes = 4;
458 bt_addr &= ~(align_to_bytes-1);
459 return bt_addr;
460 }
461
462
463
464
465
466
467 static int get_user_bd_entry(struct mm_struct *mm, unsigned long *bd_entry_ret,
468 long __user *bd_entry_ptr)
469 {
470 u32 bd_entry_32;
471 int ret;
472
473 if (is_64bit_mm(mm))
474 return get_user(*bd_entry_ret, bd_entry_ptr);
475
476
477
478
479
480 ret = get_user(bd_entry_32, (u32 __user *)bd_entry_ptr);
481 *bd_entry_ret = bd_entry_32;
482 return ret;
483 }
484
485
486
487
488
489 static int get_bt_addr(struct mm_struct *mm,
490 long __user *bd_entry_ptr,
491 unsigned long *bt_addr_result)
492 {
493 int ret;
494 int valid_bit;
495 unsigned long bd_entry;
496 unsigned long bt_addr;
497
498 if (!access_ok((bd_entry_ptr), sizeof(*bd_entry_ptr)))
499 return -EFAULT;
500
501 while (1) {
502 int need_write = 0;
503
504 pagefault_disable();
505 ret = get_user_bd_entry(mm, &bd_entry, bd_entry_ptr);
506 pagefault_enable();
507 if (!ret)
508 break;
509 if (ret == -EFAULT)
510 ret = mpx_resolve_fault(bd_entry_ptr, need_write);
511
512
513
514
515 if (ret)
516 return ret;
517 }
518
519 valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG;
520 bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry);
521
522
523
524
525
526
527
528
529 if (!valid_bit && bt_addr)
530 return -EINVAL;
531
532
533
534
535
536
537 if (!valid_bit)
538 return -ENOENT;
539
540 *bt_addr_result = bt_addr;
541 return 0;
542 }
543
544 static inline int bt_entry_size_bytes(struct mm_struct *mm)
545 {
546 if (is_64bit_mm(mm))
547 return MPX_BT_ENTRY_BYTES_64;
548 else
549 return MPX_BT_ENTRY_BYTES_32;
550 }
551
552
553
554
555
556
557 static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm,
558 unsigned long addr)
559 {
560 unsigned long bt_table_nr_entries;
561 unsigned long offset = addr;
562
563 if (is_64bit_mm(mm)) {
564
565 offset >>= 3;
566 bt_table_nr_entries = MPX_BT_NR_ENTRIES_64;
567 } else {
568
569 offset >>= 2;
570 bt_table_nr_entries = MPX_BT_NR_ENTRIES_32;
571 }
572
573
574
575
576
577
578
579
580
581
582 offset &= (bt_table_nr_entries-1);
583
584
585
586
587 offset *= bt_entry_size_bytes(mm);
588 return offset;
589 }
590
591
592
593
594
595
596
597
598 static inline unsigned long bd_entry_virt_space(struct mm_struct *mm)
599 {
600 unsigned long long virt_space;
601 unsigned long long GB = (1ULL << 30);
602
603
604
605
606
607 if (!is_64bit_mm(mm))
608 return (4ULL * GB) / MPX_BD_NR_ENTRIES_32;
609
610
611
612
613
614
615 virt_space = (1ULL << boot_cpu_data.x86_virt_bits);
616 return virt_space / MPX_BD_NR_ENTRIES_64;
617 }
618
619
620
621
622
623 static noinline int zap_bt_entries_mapping(struct mm_struct *mm,
624 unsigned long bt_addr,
625 unsigned long start_mapping, unsigned long end_mapping)
626 {
627 struct vm_area_struct *vma;
628 unsigned long addr, len;
629 unsigned long start;
630 unsigned long end;
631
632
633
634
635
636
637
638 start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping);
639 end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1);
640
641
642
643
644
645 end += bt_entry_size_bytes(mm);
646
647
648
649
650
651
652 vma = find_vma(mm, start);
653 if (!vma || vma->vm_start > start)
654 return -EINVAL;
655
656
657
658
659
660
661
662 addr = start;
663 while (vma && vma->vm_start < end) {
664
665
666
667
668
669
670 if (!(vma->vm_flags & VM_MPX))
671 return -EINVAL;
672
673 len = min(vma->vm_end, end) - addr;
674 zap_page_range(vma, addr, len);
675 trace_mpx_unmap_zap(addr, addr+len);
676
677 vma = vma->vm_next;
678 addr = vma->vm_start;
679 }
680 return 0;
681 }
682
683 static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm,
684 unsigned long addr)
685 {
686
687
688
689
690
691
692
693
694
695
696
697 if (is_64bit_mm(mm)) {
698 int bd_entry_size = 8;
699
700
701
702 addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1);
703 return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
704 } else {
705 int bd_entry_size = 4;
706
707
708
709 return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
710 }
711
712
713
714
715
716
717
718 }
719
720 static int unmap_entire_bt(struct mm_struct *mm,
721 long __user *bd_entry, unsigned long bt_addr)
722 {
723 unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
724 unsigned long uninitialized_var(actual_old_val);
725 int ret;
726
727 while (1) {
728 int need_write = 1;
729 unsigned long cleared_bd_entry = 0;
730
731 pagefault_disable();
732 ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val,
733 bd_entry, expected_old_val, cleared_bd_entry);
734 pagefault_enable();
735 if (!ret)
736 break;
737 if (ret == -EFAULT)
738 ret = mpx_resolve_fault(bd_entry, need_write);
739
740
741
742
743 if (ret)
744 return ret;
745 }
746
747
748
749 if (actual_old_val != expected_old_val) {
750
751
752
753
754
755 if (!actual_old_val)
756 return 0;
757
758
759
760
761
762
763
764 return -EINVAL;
765 }
766
767
768
769
770
771 return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm), NULL);
772 }
773
774 static int try_unmap_single_bt(struct mm_struct *mm,
775 unsigned long start, unsigned long end)
776 {
777 struct vm_area_struct *next;
778 struct vm_area_struct *prev;
779
780
781
782
783 unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1);
784 unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm);
785 unsigned long uninitialized_var(bt_addr);
786 void __user *bde_vaddr;
787 int ret;
788
789
790
791
792
793
794 next = find_vma_prev(mm, start, &prev);
795
796
797
798
799
800
801
802
803 while (next && (next->vm_flags & VM_MPX))
804 next = next->vm_next;
805 while (prev && (prev->vm_flags & VM_MPX))
806 prev = prev->vm_prev;
807
808
809
810
811
812
813
814 next = find_vma_prev(mm, start, &prev);
815 if ((!prev || prev->vm_end <= bta_start_vaddr) &&
816 (!next || next->vm_start >= bta_end_vaddr)) {
817
818
819
820
821 start = bta_start_vaddr;
822 end = bta_end_vaddr;
823 }
824
825 bde_vaddr = mm->context.bd_addr + mpx_get_bd_entry_offset(mm, start);
826 ret = get_bt_addr(mm, bde_vaddr, &bt_addr);
827
828
829
830 if (ret == -ENOENT) {
831 ret = 0;
832 return 0;
833 }
834 if (ret)
835 return ret;
836
837
838
839
840
841
842 if ((start == bta_start_vaddr) &&
843 (end == bta_end_vaddr))
844 return unmap_entire_bt(mm, bde_vaddr, bt_addr);
845 return zap_bt_entries_mapping(mm, bt_addr, start, end);
846 }
847
848 static int mpx_unmap_tables(struct mm_struct *mm,
849 unsigned long start, unsigned long end)
850 {
851 unsigned long one_unmap_start;
852 trace_mpx_unmap_search(start, end);
853
854 one_unmap_start = start;
855 while (one_unmap_start < end) {
856 int ret;
857 unsigned long next_unmap_start = ALIGN(one_unmap_start+1,
858 bd_entry_virt_space(mm));
859 unsigned long one_unmap_end = end;
860
861
862
863
864
865 if (one_unmap_end > next_unmap_start)
866 one_unmap_end = next_unmap_start;
867 ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end);
868 if (ret)
869 return ret;
870
871 one_unmap_start = next_unmap_start;
872 }
873 return 0;
874 }
875
876
877
878
879
880
881
882
883
884 void mpx_notify_unmap(struct mm_struct *mm, unsigned long start,
885 unsigned long end)
886 {
887 struct vm_area_struct *vma;
888 int ret;
889
890
891
892
893
894 if (!kernel_managing_mpx_tables(current->mm))
895 return;
896
897
898
899
900
901
902
903
904
905
906 vma = find_vma(mm, start);
907 while (vma && vma->vm_start < end) {
908 if (vma->vm_flags & VM_MPX)
909 return;
910 vma = vma->vm_next;
911 }
912
913 ret = mpx_unmap_tables(mm, start, end);
914 if (ret)
915 force_sig(SIGSEGV);
916 }
917
918
919 unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len,
920 unsigned long flags)
921 {
922 if (!kernel_managing_mpx_tables(current->mm))
923 return addr;
924 if (addr + len <= DEFAULT_MAP_WINDOW)
925 return addr;
926 if (flags & MAP_FIXED)
927 return -ENOMEM;
928
929
930
931
932
933 if (len > DEFAULT_MAP_WINDOW)
934 return -ENOMEM;
935
936
937 return 0;
938 }