2 * mpx.c - Memory Protection eXtensions
4 * Copyright (c) 2014, Intel Corporation.
5 * Qiaowei Ren <qiaowei.ren@intel.com>
6 * Dave Hansen <dave.hansen@intel.com>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/syscalls.h>
11 #include <linux/sched/sysctl.h>
15 #include <asm/mmu_context.h>
17 #include <asm/processor.h>
18 #include <asm/fpu/internal.h>
20 #define CREATE_TRACE_POINTS
21 #include <asm/trace/mpx.h>
23 static const char *mpx_mapping_name(struct vm_area_struct
*vma
)
28 static struct vm_operations_struct mpx_vma_ops
= {
29 .name
= mpx_mapping_name
,
32 static int is_mpx_vma(struct vm_area_struct
*vma
)
34 return (vma
->vm_ops
== &mpx_vma_ops
);
37 static inline unsigned long mpx_bd_size_bytes(struct mm_struct
*mm
)
40 return MPX_BD_SIZE_BYTES_64
;
42 return MPX_BD_SIZE_BYTES_32
;
45 static inline unsigned long mpx_bt_size_bytes(struct mm_struct
*mm
)
48 return MPX_BT_SIZE_BYTES_64
;
50 return MPX_BT_SIZE_BYTES_32
;
54 * This is really a simplified "vm_mmap". it only handles MPX
55 * bounds tables (the bounds directory is user-allocated).
57 * Later on, we use the vma->vm_ops to uniquely identify these
60 static unsigned long mpx_mmap(unsigned long len
)
63 unsigned long addr
, pgoff
;
64 struct mm_struct
*mm
= current
->mm
;
66 struct vm_area_struct
*vma
;
68 /* Only bounds table can be allocated here */
69 if (len
!= mpx_bt_size_bytes(mm
))
72 down_write(&mm
->mmap_sem
);
74 /* Too many mappings? */
75 if (mm
->map_count
> sysctl_max_map_count
) {
80 /* Obtain the address to map to. we verify (or select) it and ensure
81 * that it represents a valid section of the address space.
83 addr
= get_unmapped_area(NULL
, 0, len
, 0, MAP_ANONYMOUS
| MAP_PRIVATE
);
84 if (addr
& ~PAGE_MASK
) {
89 vm_flags
= VM_READ
| VM_WRITE
| VM_MPX
|
90 mm
->def_flags
| VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
92 /* Set pgoff according to addr for anon_vma */
93 pgoff
= addr
>> PAGE_SHIFT
;
95 ret
= mmap_region(NULL
, addr
, len
, vm_flags
, pgoff
);
96 if (IS_ERR_VALUE(ret
))
99 vma
= find_vma(mm
, ret
);
104 vma
->vm_ops
= &mpx_vma_ops
;
106 if (vm_flags
& VM_LOCKED
) {
107 up_write(&mm
->mmap_sem
);
108 mm_populate(ret
, len
);
113 up_write(&mm
->mmap_sem
);
123 static int get_reg_offset(struct insn
*insn
, struct pt_regs
*regs
,
128 static const int regoff
[] = {
129 offsetof(struct pt_regs
, ax
),
130 offsetof(struct pt_regs
, cx
),
131 offsetof(struct pt_regs
, dx
),
132 offsetof(struct pt_regs
, bx
),
133 offsetof(struct pt_regs
, sp
),
134 offsetof(struct pt_regs
, bp
),
135 offsetof(struct pt_regs
, si
),
136 offsetof(struct pt_regs
, di
),
138 offsetof(struct pt_regs
, r8
),
139 offsetof(struct pt_regs
, r9
),
140 offsetof(struct pt_regs
, r10
),
141 offsetof(struct pt_regs
, r11
),
142 offsetof(struct pt_regs
, r12
),
143 offsetof(struct pt_regs
, r13
),
144 offsetof(struct pt_regs
, r14
),
145 offsetof(struct pt_regs
, r15
),
148 int nr_registers
= ARRAY_SIZE(regoff
);
150 * Don't possibly decode a 32-bit instructions as
151 * reading a 64-bit-only register.
153 if (IS_ENABLED(CONFIG_X86_64
) && !insn
->x86_64
)
158 regno
= X86_MODRM_RM(insn
->modrm
.value
);
159 if (X86_REX_B(insn
->rex_prefix
.value
) == 1)
164 regno
= X86_SIB_INDEX(insn
->sib
.value
);
165 if (X86_REX_X(insn
->rex_prefix
.value
) == 1)
170 regno
= X86_SIB_BASE(insn
->sib
.value
);
171 if (X86_REX_B(insn
->rex_prefix
.value
) == 1)
176 pr_err("invalid register type");
181 if (regno
> nr_registers
) {
182 WARN_ONCE(1, "decoded an instruction with an invalid register");
185 return regoff
[regno
];
189 * return the address being referenced be instruction
190 * for rm=3 returning the content of the rm reg
191 * for rm!=3 calculates the address using SIB and Disp
193 static void __user
*mpx_get_addr_ref(struct insn
*insn
, struct pt_regs
*regs
)
195 unsigned long addr
, base
, indx
;
196 int addr_offset
, base_offset
, indx_offset
;
199 insn_get_modrm(insn
);
201 sib
= insn
->sib
.value
;
203 if (X86_MODRM_MOD(insn
->modrm
.value
) == 3) {
204 addr_offset
= get_reg_offset(insn
, regs
, REG_TYPE_RM
);
207 addr
= regs_get_register(regs
, addr_offset
);
209 if (insn
->sib
.nbytes
) {
210 base_offset
= get_reg_offset(insn
, regs
, REG_TYPE_BASE
);
214 indx_offset
= get_reg_offset(insn
, regs
, REG_TYPE_INDEX
);
218 base
= regs_get_register(regs
, base_offset
);
219 indx
= regs_get_register(regs
, indx_offset
);
220 addr
= base
+ indx
* (1 << X86_SIB_SCALE(sib
));
222 addr_offset
= get_reg_offset(insn
, regs
, REG_TYPE_RM
);
225 addr
= regs_get_register(regs
, addr_offset
);
227 addr
+= insn
->displacement
.value
;
229 return (void __user
*)addr
;
231 return (void __user
*)-1;
234 static int mpx_insn_decode(struct insn
*insn
,
235 struct pt_regs
*regs
)
237 unsigned char buf
[MAX_INSN_SIZE
];
238 int x86_64
= !test_thread_flag(TIF_IA32
);
242 not_copied
= copy_from_user(buf
, (void __user
*)regs
->ip
, sizeof(buf
));
243 nr_copied
= sizeof(buf
) - not_copied
;
245 * The decoder _should_ fail nicely if we pass it a short buffer.
246 * But, let's not depend on that implementation detail. If we
247 * did not get anything, just error out now.
251 insn_init(insn
, buf
, nr_copied
, x86_64
);
252 insn_get_length(insn
);
254 * copy_from_user() tries to get as many bytes as we could see in
255 * the largest possible instruction. If the instruction we are
256 * after is shorter than that _and_ we attempt to copy from
257 * something unreadable, we might get a short read. This is OK
258 * as long as the read did not stop in the middle of the
259 * instruction. Check to see if we got a partial instruction.
261 if (nr_copied
< insn
->length
)
264 insn_get_opcode(insn
);
266 * We only _really_ need to decode bndcl/bndcn/bndcu
267 * Error out on anything else.
269 if (insn
->opcode
.bytes
[0] != 0x0f)
271 if ((insn
->opcode
.bytes
[1] != 0x1a) &&
272 (insn
->opcode
.bytes
[1] != 0x1b))
281 * If a bounds overflow occurs then a #BR is generated. This
282 * function decodes MPX instructions to get violation address
283 * and set this address into extended struct siginfo.
285 * Note that this is not a super precise way of doing this.
286 * Userspace could have, by the time we get here, written
287 * anything it wants in to the instructions. We can not
288 * trust anything about it. They might not be valid
289 * instructions or might encode invalid registers, etc...
291 * The caller is expected to kfree() the returned siginfo_t.
293 siginfo_t
*mpx_generate_siginfo(struct pt_regs
*regs
)
295 const struct bndreg
*bndregs
, *bndreg
;
296 siginfo_t
*info
= NULL
;
301 err
= mpx_insn_decode(&insn
, regs
);
306 * We know at this point that we are only dealing with
309 insn_get_modrm(&insn
);
310 bndregno
= X86_MODRM_REG(insn
.modrm
.value
);
315 /* get bndregs field from current task's xsave area */
316 bndregs
= get_xsave_field_ptr(XSTATE_BNDREGS
);
321 /* now go select the individual register in the set of 4 */
322 bndreg
= &bndregs
[bndregno
];
324 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
330 * The registers are always 64-bit, but the upper 32
331 * bits are ignored in 32-bit mode. Also, note that the
332 * upper bounds are architecturally represented in 1's
335 * The 'unsigned long' cast is because the compiler
336 * complains when casting from integers to different-size
339 info
->si_lower
= (void __user
*)(unsigned long)bndreg
->lower_bound
;
340 info
->si_upper
= (void __user
*)(unsigned long)~bndreg
->upper_bound
;
341 info
->si_addr_lsb
= 0;
342 info
->si_signo
= SIGSEGV
;
344 info
->si_code
= SEGV_BNDERR
;
345 info
->si_addr
= mpx_get_addr_ref(&insn
, regs
);
347 * We were not able to extract an address from the instruction,
348 * probably because there was something invalid in it.
350 if (info
->si_addr
== (void *)-1) {
354 trace_mpx_bounds_register_exception(info
->si_addr
, bndreg
);
357 /* info might be NULL, but kfree() handles that */
362 static __user
void *mpx_get_bounds_dir(void)
364 const struct bndcsr
*bndcsr
;
366 if (!cpu_feature_enabled(X86_FEATURE_MPX
))
367 return MPX_INVALID_BOUNDS_DIR
;
370 * The bounds directory pointer is stored in a register
371 * only accessible if we first do an xsave.
373 bndcsr
= get_xsave_field_ptr(XSTATE_BNDCSR
);
375 return MPX_INVALID_BOUNDS_DIR
;
378 * Make sure the register looks valid by checking the
381 if (!(bndcsr
->bndcfgu
& MPX_BNDCFG_ENABLE_FLAG
))
382 return MPX_INVALID_BOUNDS_DIR
;
385 * Lastly, mask off the low bits used for configuration
386 * flags, and return the address of the bounds table.
388 return (void __user
*)(unsigned long)
389 (bndcsr
->bndcfgu
& MPX_BNDCFG_ADDR_MASK
);
392 int mpx_enable_management(void)
394 void __user
*bd_base
= MPX_INVALID_BOUNDS_DIR
;
395 struct mm_struct
*mm
= current
->mm
;
399 * runtime in the userspace will be responsible for allocation of
400 * the bounds directory. Then, it will save the base of the bounds
401 * directory into XSAVE/XRSTOR Save Area and enable MPX through
402 * XRSTOR instruction.
404 * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is
405 * expected to be relatively expensive. Storing the bounds
406 * directory here means that we do not have to do xsave in the
407 * unmap path; we can just use mm->bd_addr instead.
409 bd_base
= mpx_get_bounds_dir();
410 down_write(&mm
->mmap_sem
);
411 mm
->bd_addr
= bd_base
;
412 if (mm
->bd_addr
== MPX_INVALID_BOUNDS_DIR
)
415 up_write(&mm
->mmap_sem
);
419 int mpx_disable_management(void)
421 struct mm_struct
*mm
= current
->mm
;
423 if (!cpu_feature_enabled(X86_FEATURE_MPX
))
426 down_write(&mm
->mmap_sem
);
427 mm
->bd_addr
= MPX_INVALID_BOUNDS_DIR
;
428 up_write(&mm
->mmap_sem
);
432 static int mpx_cmpxchg_bd_entry(struct mm_struct
*mm
,
433 unsigned long *curval
,
434 unsigned long __user
*addr
,
435 unsigned long old_val
, unsigned long new_val
)
439 * user_atomic_cmpxchg_inatomic() actually uses sizeof()
440 * the pointer that we pass to it to figure out how much
441 * data to cmpxchg. We have to be careful here not to
442 * pass a pointer to a 64-bit data type when we only want
445 if (is_64bit_mm(mm
)) {
446 ret
= user_atomic_cmpxchg_inatomic(curval
,
447 addr
, old_val
, new_val
);
449 u32
uninitialized_var(curval_32
);
450 u32 old_val_32
= old_val
;
451 u32 new_val_32
= new_val
;
452 u32 __user
*addr_32
= (u32 __user
*)addr
;
454 ret
= user_atomic_cmpxchg_inatomic(&curval_32
,
455 addr_32
, old_val_32
, new_val_32
);
462 * With 32-bit mode, a bounds directory is 4MB, and the size of each
463 * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB,
464 * and the size of each bounds table is 4MB.
466 static int allocate_bt(struct mm_struct
*mm
, long __user
*bd_entry
)
468 unsigned long expected_old_val
= 0;
469 unsigned long actual_old_val
= 0;
470 unsigned long bt_addr
;
471 unsigned long bd_new_entry
;
475 * Carve the virtual space out of userspace for the new
478 bt_addr
= mpx_mmap(mpx_bt_size_bytes(mm
));
479 if (IS_ERR((void *)bt_addr
))
480 return PTR_ERR((void *)bt_addr
);
482 * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
484 bd_new_entry
= bt_addr
| MPX_BD_ENTRY_VALID_FLAG
;
487 * Go poke the address of the new bounds table in to the
488 * bounds directory entry out in userspace memory. Note:
489 * we may race with another CPU instantiating the same table.
490 * In that case the cmpxchg will see an unexpected
493 * This can fault, but that's OK because we do not hold
494 * mmap_sem at this point, unlike some of the other part
495 * of the MPX code that have to pagefault_disable().
497 ret
= mpx_cmpxchg_bd_entry(mm
, &actual_old_val
, bd_entry
,
498 expected_old_val
, bd_new_entry
);
503 * The user_atomic_cmpxchg_inatomic() will only return nonzero
504 * for faults, *not* if the cmpxchg itself fails. Now we must
505 * verify that the cmpxchg itself completed successfully.
508 * We expected an empty 'expected_old_val', but instead found
509 * an apparently valid entry. Assume we raced with another
510 * thread to instantiate this table and desclare succecss.
512 if (actual_old_val
& MPX_BD_ENTRY_VALID_FLAG
) {
517 * We found a non-empty bd_entry but it did not have the
518 * VALID_FLAG set. Return an error which will result in
519 * a SEGV since this probably means that somebody scribbled
520 * some invalid data in to a bounds table.
522 if (expected_old_val
!= actual_old_val
) {
526 trace_mpx_new_bounds_table(bt_addr
);
529 vm_munmap(bt_addr
, mpx_bt_size_bytes(mm
));
534 * When a BNDSTX instruction attempts to save bounds to a bounds
535 * table, it will first attempt to look up the table in the
536 * first-level bounds directory. If it does not find a table in
537 * the directory, a #BR is generated and we get here in order to
538 * allocate a new table.
540 * With 32-bit mode, the size of BD is 4MB, and the size of each
541 * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
542 * and the size of each bound table is 4MB.
544 static int do_mpx_bt_fault(void)
546 unsigned long bd_entry
, bd_base
;
547 const struct bndcsr
*bndcsr
;
548 struct mm_struct
*mm
= current
->mm
;
550 bndcsr
= get_xsave_field_ptr(XSTATE_BNDCSR
);
554 * Mask off the preserve and enable bits
556 bd_base
= bndcsr
->bndcfgu
& MPX_BNDCFG_ADDR_MASK
;
558 * The hardware provides the address of the missing or invalid
559 * entry via BNDSTATUS, so we don't have to go look it up.
561 bd_entry
= bndcsr
->bndstatus
& MPX_BNDSTA_ADDR_MASK
;
563 * Make sure the directory entry is within where we think
566 if ((bd_entry
< bd_base
) ||
567 (bd_entry
>= bd_base
+ mpx_bd_size_bytes(mm
)))
570 return allocate_bt(mm
, (long __user
*)bd_entry
);
573 int mpx_handle_bd_fault(void)
576 * Userspace never asked us to manage the bounds tables,
579 if (!kernel_managing_mpx_tables(current
->mm
))
582 if (do_mpx_bt_fault()) {
583 force_sig(SIGSEGV
, current
);
585 * The force_sig() is essentially "handling" this
586 * exception, so we do not pass up the error
587 * from do_mpx_bt_fault().
594 * A thin wrapper around get_user_pages(). Returns 0 if the
595 * fault was resolved or -errno if not.
597 static int mpx_resolve_fault(long __user
*addr
, int write
)
603 gup_ret
= get_user_pages(current
, current
->mm
, (unsigned long)addr
,
604 nr_pages
, write
, force
, NULL
, NULL
);
606 * get_user_pages() returns number of pages gotten.
607 * 0 means we failed to fault in and get anything,
608 * probably because 'addr' is bad.
612 /* Other error, return it */
615 /* must have gup'd a page and gup_ret>0, success */
619 static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct
*mm
,
620 unsigned long bd_entry
)
622 unsigned long bt_addr
= bd_entry
;
625 * Bit 0 in a bt_entry is always the valid bit.
627 bt_addr
&= ~MPX_BD_ENTRY_VALID_FLAG
;
629 * Tables are naturally aligned at 8-byte boundaries
630 * on 64-bit and 4-byte boundaries on 32-bit. The
631 * documentation makes it appear that the low bits
632 * are ignored by the hardware, so we do the same.
638 bt_addr
&= ~(align_to_bytes
-1);
643 * Get the base of bounds tables pointed by specific bounds
646 static int get_bt_addr(struct mm_struct
*mm
,
647 long __user
*bd_entry_ptr
,
648 unsigned long *bt_addr_result
)
652 unsigned long bd_entry
;
653 unsigned long bt_addr
;
655 if (!access_ok(VERIFY_READ
, (bd_entry_ptr
), sizeof(*bd_entry_ptr
)))
662 ret
= get_user(bd_entry
, bd_entry_ptr
);
667 ret
= mpx_resolve_fault(bd_entry_ptr
, need_write
);
669 * If we could not resolve the fault, consider it
670 * userspace's fault and error out.
676 valid_bit
= bd_entry
& MPX_BD_ENTRY_VALID_FLAG
;
677 bt_addr
= mpx_bd_entry_to_bt_addr(mm
, bd_entry
);
680 * When the kernel is managing bounds tables, a bounds directory
681 * entry will either have a valid address (plus the valid bit)
682 * *OR* be completely empty. If we see a !valid entry *and* some
683 * data in the address field, we know something is wrong. This
684 * -EINVAL return will cause a SIGSEGV.
686 if (!valid_bit
&& bt_addr
)
689 * Do we have an completely zeroed bt entry? That is OK. It
690 * just means there was no bounds table for this memory. Make
691 * sure to distinguish this from -EINVAL, which will cause
697 *bt_addr_result
= bt_addr
;
701 static inline int bt_entry_size_bytes(struct mm_struct
*mm
)
704 return MPX_BT_ENTRY_BYTES_64
;
706 return MPX_BT_ENTRY_BYTES_32
;
710 * Take a virtual address and turns it in to the offset in bytes
711 * inside of the bounds table where the bounds table entry
712 * controlling 'addr' can be found.
714 static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct
*mm
,
717 unsigned long bt_table_nr_entries
;
718 unsigned long offset
= addr
;
720 if (is_64bit_mm(mm
)) {
721 /* Bottom 3 bits are ignored on 64-bit */
723 bt_table_nr_entries
= MPX_BT_NR_ENTRIES_64
;
725 /* Bottom 2 bits are ignored on 32-bit */
727 bt_table_nr_entries
= MPX_BT_NR_ENTRIES_32
;
730 * We know the size of the table in to which we are
731 * indexing, and we have eliminated all the low bits
732 * which are ignored for indexing.
734 * Mask out all the high bits which we do not need
735 * to index in to the table. Note that the tables
736 * are always powers of two so this gives us a proper
739 offset
&= (bt_table_nr_entries
-1);
741 * We now have an entry offset in terms of *entries* in
742 * the table. We need to scale it back up to bytes.
744 offset
*= bt_entry_size_bytes(mm
);
749 * How much virtual address space does a single bounds
750 * directory entry cover?
752 * Note, we need a long long because 4GB doesn't fit in
753 * to a long on 32-bit.
755 static inline unsigned long bd_entry_virt_space(struct mm_struct
*mm
)
757 unsigned long long virt_space
= (1ULL << boot_cpu_data
.x86_virt_bits
);
759 return virt_space
/ MPX_BD_NR_ENTRIES_64
;
761 return virt_space
/ MPX_BD_NR_ENTRIES_32
;
765 * Free the backing physical pages of bounds table 'bt_addr'.
766 * Assume start...end is within that bounds table.
768 static noinline
int zap_bt_entries_mapping(struct mm_struct
*mm
,
769 unsigned long bt_addr
,
770 unsigned long start_mapping
, unsigned long end_mapping
)
772 struct vm_area_struct
*vma
;
773 unsigned long addr
, len
;
778 * if we 'end' on a boundary, the offset will be 0 which
779 * is not what we want. Back it up a byte to get the
780 * last bt entry. Then once we have the entry itself,
781 * move 'end' back up by the table entry size.
783 start
= bt_addr
+ mpx_get_bt_entry_offset_bytes(mm
, start_mapping
);
784 end
= bt_addr
+ mpx_get_bt_entry_offset_bytes(mm
, end_mapping
- 1);
786 * Move end back up by one entry. Among other things
787 * this ensures that it remains page-aligned and does
788 * not screw up zap_page_range()
790 end
+= bt_entry_size_bytes(mm
);
793 * Find the first overlapping vma. If vma->vm_start > start, there
794 * will be a hole in the bounds table. This -EINVAL return will
797 vma
= find_vma(mm
, start
);
798 if (!vma
|| vma
->vm_start
> start
)
802 * A NUMA policy on a VM_MPX VMA could cause this bounds table to
803 * be split. So we need to look across the entire 'start -> end'
804 * range of this bounds table, find all of the VM_MPX VMAs, and
808 while (vma
&& vma
->vm_start
< end
) {
810 * We followed a bounds directory entry down
811 * here. If we find a non-MPX VMA, that's bad,
812 * so stop immediately and return an error. This
813 * probably results in a SIGSEGV.
815 if (!is_mpx_vma(vma
))
818 len
= min(vma
->vm_end
, end
) - addr
;
819 zap_page_range(vma
, addr
, len
, NULL
);
820 trace_mpx_unmap_zap(addr
, addr
+len
);
823 addr
= vma
->vm_start
;
828 static unsigned long mpx_get_bd_entry_offset(struct mm_struct
*mm
,
832 * There are several ways to derive the bd offsets. We
833 * use the following approach here:
834 * 1. We know the size of the virtual address space
835 * 2. We know the number of entries in a bounds table
836 * 3. We know that each entry covers a fixed amount of
837 * virtual address space.
838 * So, we can just divide the virtual address by the
839 * virtual space used by one entry to determine which
840 * entry "controls" the given virtual address.
842 if (is_64bit_mm(mm
)) {
843 int bd_entry_size
= 8; /* 64-bit pointer */
845 * Take the 64-bit addressing hole in to account.
847 addr
&= ((1UL << boot_cpu_data
.x86_virt_bits
) - 1);
848 return (addr
/ bd_entry_virt_space(mm
)) * bd_entry_size
;
850 int bd_entry_size
= 4; /* 32-bit pointer */
852 * 32-bit has no hole so this case needs no mask
854 return (addr
/ bd_entry_virt_space(mm
)) * bd_entry_size
;
857 * The two return calls above are exact copies. If we
858 * pull out a single copy and put it in here, gcc won't
859 * realize that we're doing a power-of-2 divide and use
860 * shifts. It uses a real divide. If we put them up
861 * there, it manages to figure it out (gcc 4.8.3).
865 static int unmap_entire_bt(struct mm_struct
*mm
,
866 long __user
*bd_entry
, unsigned long bt_addr
)
868 unsigned long expected_old_val
= bt_addr
| MPX_BD_ENTRY_VALID_FLAG
;
869 unsigned long uninitialized_var(actual_old_val
);
874 unsigned long cleared_bd_entry
= 0;
877 ret
= mpx_cmpxchg_bd_entry(mm
, &actual_old_val
,
878 bd_entry
, expected_old_val
, cleared_bd_entry
);
883 ret
= mpx_resolve_fault(bd_entry
, need_write
);
885 * If we could not resolve the fault, consider it
886 * userspace's fault and error out.
892 * The cmpxchg was performed, check the results.
894 if (actual_old_val
!= expected_old_val
) {
896 * Someone else raced with us to unmap the table.
897 * That is OK, since we were both trying to do
898 * the same thing. Declare success.
903 * Something messed with the bounds directory
904 * entry. We hold mmap_sem for read or write
905 * here, so it could not be a _new_ bounds table
906 * that someone just allocated. Something is
907 * wrong, so pass up the error and SIGSEGV.
912 * Note, we are likely being called under do_munmap() already. To
913 * avoid recursion, do_munmap() will check whether it comes
914 * from one bounds table through VM_MPX flag.
916 return do_munmap(mm
, bt_addr
, mpx_bt_size_bytes(mm
));
919 static int try_unmap_single_bt(struct mm_struct
*mm
,
920 unsigned long start
, unsigned long end
)
922 struct vm_area_struct
*next
;
923 struct vm_area_struct
*prev
;
925 * "bta" == Bounds Table Area: the area controlled by the
926 * bounds table that we are unmapping.
928 unsigned long bta_start_vaddr
= start
& ~(bd_entry_virt_space(mm
)-1);
929 unsigned long bta_end_vaddr
= bta_start_vaddr
+ bd_entry_virt_space(mm
);
930 unsigned long uninitialized_var(bt_addr
);
931 void __user
*bde_vaddr
;
934 * We already unlinked the VMAs from the mm's rbtree so 'start'
935 * is guaranteed to be in a hole. This gets us the first VMA
936 * before the hole in to 'prev' and the next VMA after the hole
939 next
= find_vma_prev(mm
, start
, &prev
);
941 * Do not count other MPX bounds table VMAs as neighbors.
942 * Although theoretically possible, we do not allow bounds
943 * tables for bounds tables so our heads do not explode.
944 * If we count them as neighbors here, we may end up with
945 * lots of tables even though we have no actual table
948 while (next
&& is_mpx_vma(next
))
949 next
= next
->vm_next
;
950 while (prev
&& is_mpx_vma(prev
))
951 prev
= prev
->vm_prev
;
953 * We know 'start' and 'end' lie within an area controlled
954 * by a single bounds table. See if there are any other
955 * VMAs controlled by that bounds table. If there are not
956 * then we can "expand" the are we are unmapping to possibly
957 * cover the entire table.
959 next
= find_vma_prev(mm
, start
, &prev
);
960 if ((!prev
|| prev
->vm_end
<= bta_start_vaddr
) &&
961 (!next
|| next
->vm_start
>= bta_end_vaddr
)) {
963 * No neighbor VMAs controlled by same bounds
964 * table. Try to unmap the whole thing
966 start
= bta_start_vaddr
;
970 bde_vaddr
= mm
->bd_addr
+ mpx_get_bd_entry_offset(mm
, start
);
971 ret
= get_bt_addr(mm
, bde_vaddr
, &bt_addr
);
973 * No bounds table there, so nothing to unmap.
975 if (ret
== -ENOENT
) {
982 * We are unmapping an entire table. Either because the
983 * unmap that started this whole process was large enough
984 * to cover an entire table, or that the unmap was small
985 * but was the area covered by a bounds table.
987 if ((start
== bta_start_vaddr
) &&
988 (end
== bta_end_vaddr
))
989 return unmap_entire_bt(mm
, bde_vaddr
, bt_addr
);
990 return zap_bt_entries_mapping(mm
, bt_addr
, start
, end
);
993 static int mpx_unmap_tables(struct mm_struct
*mm
,
994 unsigned long start
, unsigned long end
)
996 unsigned long one_unmap_start
;
997 trace_mpx_unmap_search(start
, end
);
999 one_unmap_start
= start
;
1000 while (one_unmap_start
< end
) {
1002 unsigned long next_unmap_start
= ALIGN(one_unmap_start
+1,
1003 bd_entry_virt_space(mm
));
1004 unsigned long one_unmap_end
= end
;
1006 * if the end is beyond the current bounds table,
1007 * move it back so we only deal with a single one
1010 if (one_unmap_end
> next_unmap_start
)
1011 one_unmap_end
= next_unmap_start
;
1012 ret
= try_unmap_single_bt(mm
, one_unmap_start
, one_unmap_end
);
1016 one_unmap_start
= next_unmap_start
;
1022 * Free unused bounds tables covered in a virtual address region being
1023 * munmap()ed. Assume end > start.
1025 * This function will be called by do_munmap(), and the VMAs covering
1026 * the virtual address region start...end have already been split if
1027 * necessary, and the 'vma' is the first vma in this range (start -> end).
1029 void mpx_notify_unmap(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1030 unsigned long start
, unsigned long end
)
1035 * Refuse to do anything unless userspace has asked
1036 * the kernel to help manage the bounds tables,
1038 if (!kernel_managing_mpx_tables(current
->mm
))
1041 * This will look across the entire 'start -> end' range,
1042 * and find all of the non-VM_MPX VMAs.
1044 * To avoid recursion, if a VM_MPX vma is found in the range
1045 * (start->end), we will not continue follow-up work. This
1046 * recursion represents having bounds tables for bounds tables,
1047 * which should not occur normally. Being strict about it here
1048 * helps ensure that we do not have an exploitable stack overflow.
1051 if (vma
->vm_flags
& VM_MPX
)
1054 } while (vma
&& vma
->vm_start
< end
);
1056 ret
= mpx_unmap_tables(mm
, start
, end
);
1058 force_sig(SIGSEGV
, current
);