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 inline unsigned long mpx_bd_size_bytes(struct mm_struct *mm)
26 return MPX_BD_SIZE_BYTES_64;
28 return MPX_BD_SIZE_BYTES_32;
31 static inline unsigned long mpx_bt_size_bytes(struct mm_struct *mm)
34 return MPX_BT_SIZE_BYTES_64;
36 return MPX_BT_SIZE_BYTES_32;
40 * This is really a simplified "vm_mmap". it only handles MPX
41 * bounds tables (the bounds directory is user-allocated).
43 static unsigned long mpx_mmap(unsigned long len)
45 struct mm_struct *mm = current->mm;
46 unsigned long addr, populate;
48 /* Only bounds table can be allocated here */
49 if (len != mpx_bt_size_bytes(mm))
52 down_write(&mm->mmap_sem);
53 addr = do_mmap(NULL, 0, len, PROT_READ | PROT_WRITE,
54 MAP_ANONYMOUS | MAP_PRIVATE, VM_MPX, 0, &populate);
55 up_write(&mm->mmap_sem);
57 mm_populate(addr, populate);
68 static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
73 static const int regoff[] = {
74 offsetof(struct pt_regs, ax),
75 offsetof(struct pt_regs, cx),
76 offsetof(struct pt_regs, dx),
77 offsetof(struct pt_regs, bx),
78 offsetof(struct pt_regs, sp),
79 offsetof(struct pt_regs, bp),
80 offsetof(struct pt_regs, si),
81 offsetof(struct pt_regs, di),
83 offsetof(struct pt_regs, r8),
84 offsetof(struct pt_regs, r9),
85 offsetof(struct pt_regs, r10),
86 offsetof(struct pt_regs, r11),
87 offsetof(struct pt_regs, r12),
88 offsetof(struct pt_regs, r13),
89 offsetof(struct pt_regs, r14),
90 offsetof(struct pt_regs, r15),
93 int nr_registers = ARRAY_SIZE(regoff);
95 * Don't possibly decode a 32-bit instructions as
96 * reading a 64-bit-only register.
98 if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
103 regno = X86_MODRM_RM(insn->modrm.value);
104 if (X86_REX_B(insn->rex_prefix.value) == 1)
109 regno = X86_SIB_INDEX(insn->sib.value);
110 if (X86_REX_X(insn->rex_prefix.value) == 1)
115 regno = X86_SIB_BASE(insn->sib.value);
116 if (X86_REX_B(insn->rex_prefix.value) == 1)
121 pr_err("invalid register type");
126 if (regno > nr_registers) {
127 WARN_ONCE(1, "decoded an instruction with an invalid register");
130 return regoff[regno];
134 * return the address being referenced be instruction
135 * for rm=3 returning the content of the rm reg
136 * for rm!=3 calculates the address using SIB and Disp
138 static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)
140 unsigned long addr, base, indx;
141 int addr_offset, base_offset, indx_offset;
144 insn_get_modrm(insn);
146 sib = insn->sib.value;
148 if (X86_MODRM_MOD(insn->modrm.value) == 3) {
149 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
152 addr = regs_get_register(regs, addr_offset);
154 if (insn->sib.nbytes) {
155 base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
159 indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
163 base = regs_get_register(regs, base_offset);
164 indx = regs_get_register(regs, indx_offset);
165 addr = base + indx * (1 << X86_SIB_SCALE(sib));
167 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
170 addr = regs_get_register(regs, addr_offset);
172 addr += insn->displacement.value;
174 return (void __user *)addr;
176 return (void __user *)-1;
179 static int mpx_insn_decode(struct insn *insn,
180 struct pt_regs *regs)
182 unsigned char buf[MAX_INSN_SIZE];
183 int x86_64 = !test_thread_flag(TIF_IA32);
187 not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf));
188 nr_copied = sizeof(buf) - not_copied;
190 * The decoder _should_ fail nicely if we pass it a short buffer.
191 * But, let's not depend on that implementation detail. If we
192 * did not get anything, just error out now.
196 insn_init(insn, buf, nr_copied, x86_64);
197 insn_get_length(insn);
199 * copy_from_user() tries to get as many bytes as we could see in
200 * the largest possible instruction. If the instruction we are
201 * after is shorter than that _and_ we attempt to copy from
202 * something unreadable, we might get a short read. This is OK
203 * as long as the read did not stop in the middle of the
204 * instruction. Check to see if we got a partial instruction.
206 if (nr_copied < insn->length)
209 insn_get_opcode(insn);
211 * We only _really_ need to decode bndcl/bndcn/bndcu
212 * Error out on anything else.
214 if (insn->opcode.bytes[0] != 0x0f)
216 if ((insn->opcode.bytes[1] != 0x1a) &&
217 (insn->opcode.bytes[1] != 0x1b))
226 * If a bounds overflow occurs then a #BR is generated. This
227 * function decodes MPX instructions to get violation address
228 * and set this address into extended struct siginfo.
230 * Note that this is not a super precise way of doing this.
231 * Userspace could have, by the time we get here, written
232 * anything it wants in to the instructions. We can not
233 * trust anything about it. They might not be valid
234 * instructions or might encode invalid registers, etc...
236 * The caller is expected to kfree() the returned siginfo_t.
238 siginfo_t *mpx_generate_siginfo(struct pt_regs *regs)
240 const struct bndreg *bndregs, *bndreg;
241 siginfo_t *info = NULL;
246 err = mpx_insn_decode(&insn, regs);
251 * We know at this point that we are only dealing with
254 insn_get_modrm(&insn);
255 bndregno = X86_MODRM_REG(insn.modrm.value);
260 /* get bndregs field from current task's xsave area */
261 bndregs = get_xsave_field_ptr(XSTATE_BNDREGS);
266 /* now go select the individual register in the set of 4 */
267 bndreg = &bndregs[bndregno];
269 info = kzalloc(sizeof(*info), GFP_KERNEL);
275 * The registers are always 64-bit, but the upper 32
276 * bits are ignored in 32-bit mode. Also, note that the
277 * upper bounds are architecturally represented in 1's
280 * The 'unsigned long' cast is because the compiler
281 * complains when casting from integers to different-size
284 info->si_lower = (void __user *)(unsigned long)bndreg->lower_bound;
285 info->si_upper = (void __user *)(unsigned long)~bndreg->upper_bound;
286 info->si_addr_lsb = 0;
287 info->si_signo = SIGSEGV;
289 info->si_code = SEGV_BNDERR;
290 info->si_addr = mpx_get_addr_ref(&insn, regs);
292 * We were not able to extract an address from the instruction,
293 * probably because there was something invalid in it.
295 if (info->si_addr == (void *)-1) {
299 trace_mpx_bounds_register_exception(info->si_addr, bndreg);
302 /* info might be NULL, but kfree() handles that */
307 static __user void *mpx_get_bounds_dir(void)
309 const struct bndcsr *bndcsr;
311 if (!cpu_feature_enabled(X86_FEATURE_MPX))
312 return MPX_INVALID_BOUNDS_DIR;
315 * The bounds directory pointer is stored in a register
316 * only accessible if we first do an xsave.
318 bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR);
320 return MPX_INVALID_BOUNDS_DIR;
323 * Make sure the register looks valid by checking the
326 if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
327 return MPX_INVALID_BOUNDS_DIR;
330 * Lastly, mask off the low bits used for configuration
331 * flags, and return the address of the bounds table.
333 return (void __user *)(unsigned long)
334 (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
337 int mpx_enable_management(void)
339 void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
340 struct mm_struct *mm = current->mm;
344 * runtime in the userspace will be responsible for allocation of
345 * the bounds directory. Then, it will save the base of the bounds
346 * directory into XSAVE/XRSTOR Save Area and enable MPX through
347 * XRSTOR instruction.
349 * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is
350 * expected to be relatively expensive. Storing the bounds
351 * directory here means that we do not have to do xsave in the
352 * unmap path; we can just use mm->bd_addr instead.
354 bd_base = mpx_get_bounds_dir();
355 down_write(&mm->mmap_sem);
356 mm->bd_addr = bd_base;
357 if (mm->bd_addr == MPX_INVALID_BOUNDS_DIR)
360 up_write(&mm->mmap_sem);
364 int mpx_disable_management(void)
366 struct mm_struct *mm = current->mm;
368 if (!cpu_feature_enabled(X86_FEATURE_MPX))
371 down_write(&mm->mmap_sem);
372 mm->bd_addr = MPX_INVALID_BOUNDS_DIR;
373 up_write(&mm->mmap_sem);
377 static int mpx_cmpxchg_bd_entry(struct mm_struct *mm,
378 unsigned long *curval,
379 unsigned long __user *addr,
380 unsigned long old_val, unsigned long new_val)
384 * user_atomic_cmpxchg_inatomic() actually uses sizeof()
385 * the pointer that we pass to it to figure out how much
386 * data to cmpxchg. We have to be careful here not to
387 * pass a pointer to a 64-bit data type when we only want
390 if (is_64bit_mm(mm)) {
391 ret = user_atomic_cmpxchg_inatomic(curval,
392 addr, old_val, new_val);
394 u32 uninitialized_var(curval_32);
395 u32 old_val_32 = old_val;
396 u32 new_val_32 = new_val;
397 u32 __user *addr_32 = (u32 __user *)addr;
399 ret = user_atomic_cmpxchg_inatomic(&curval_32,
400 addr_32, old_val_32, new_val_32);
407 * With 32-bit mode, a bounds directory is 4MB, and the size of each
408 * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB,
409 * and the size of each bounds table is 4MB.
411 static int allocate_bt(struct mm_struct *mm, long __user *bd_entry)
413 unsigned long expected_old_val = 0;
414 unsigned long actual_old_val = 0;
415 unsigned long bt_addr;
416 unsigned long bd_new_entry;
420 * Carve the virtual space out of userspace for the new
423 bt_addr = mpx_mmap(mpx_bt_size_bytes(mm));
424 if (IS_ERR((void *)bt_addr))
425 return PTR_ERR((void *)bt_addr);
427 * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
429 bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
432 * Go poke the address of the new bounds table in to the
433 * bounds directory entry out in userspace memory. Note:
434 * we may race with another CPU instantiating the same table.
435 * In that case the cmpxchg will see an unexpected
438 * This can fault, but that's OK because we do not hold
439 * mmap_sem at this point, unlike some of the other part
440 * of the MPX code that have to pagefault_disable().
442 ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry,
443 expected_old_val, bd_new_entry);
448 * The user_atomic_cmpxchg_inatomic() will only return nonzero
449 * for faults, *not* if the cmpxchg itself fails. Now we must
450 * verify that the cmpxchg itself completed successfully.
453 * We expected an empty 'expected_old_val', but instead found
454 * an apparently valid entry. Assume we raced with another
455 * thread to instantiate this table and desclare succecss.
457 if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
462 * We found a non-empty bd_entry but it did not have the
463 * VALID_FLAG set. Return an error which will result in
464 * a SEGV since this probably means that somebody scribbled
465 * some invalid data in to a bounds table.
467 if (expected_old_val != actual_old_val) {
471 trace_mpx_new_bounds_table(bt_addr);
474 vm_munmap(bt_addr, mpx_bt_size_bytes(mm));
479 * When a BNDSTX instruction attempts to save bounds to a bounds
480 * table, it will first attempt to look up the table in the
481 * first-level bounds directory. If it does not find a table in
482 * the directory, a #BR is generated and we get here in order to
483 * allocate a new table.
485 * With 32-bit mode, the size of BD is 4MB, and the size of each
486 * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
487 * and the size of each bound table is 4MB.
489 static int do_mpx_bt_fault(void)
491 unsigned long bd_entry, bd_base;
492 const struct bndcsr *bndcsr;
493 struct mm_struct *mm = current->mm;
495 bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR);
499 * Mask off the preserve and enable bits
501 bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
503 * The hardware provides the address of the missing or invalid
504 * entry via BNDSTATUS, so we don't have to go look it up.
506 bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
508 * Make sure the directory entry is within where we think
511 if ((bd_entry < bd_base) ||
512 (bd_entry >= bd_base + mpx_bd_size_bytes(mm)))
515 return allocate_bt(mm, (long __user *)bd_entry);
518 int mpx_handle_bd_fault(void)
521 * Userspace never asked us to manage the bounds tables,
524 if (!kernel_managing_mpx_tables(current->mm))
527 if (do_mpx_bt_fault()) {
528 force_sig(SIGSEGV, current);
530 * The force_sig() is essentially "handling" this
531 * exception, so we do not pass up the error
532 * from do_mpx_bt_fault().
539 * A thin wrapper around get_user_pages(). Returns 0 if the
540 * fault was resolved or -errno if not.
542 static int mpx_resolve_fault(long __user *addr, int write)
548 gup_ret = get_user_pages(current, current->mm, (unsigned long)addr,
549 nr_pages, write, force, NULL, NULL);
551 * get_user_pages() returns number of pages gotten.
552 * 0 means we failed to fault in and get anything,
553 * probably because 'addr' is bad.
557 /* Other error, return it */
560 /* must have gup'd a page and gup_ret>0, success */
564 static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm,
565 unsigned long bd_entry)
567 unsigned long bt_addr = bd_entry;
570 * Bit 0 in a bt_entry is always the valid bit.
572 bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG;
574 * Tables are naturally aligned at 8-byte boundaries
575 * on 64-bit and 4-byte boundaries on 32-bit. The
576 * documentation makes it appear that the low bits
577 * are ignored by the hardware, so we do the same.
583 bt_addr &= ~(align_to_bytes-1);
588 * Get the base of bounds tables pointed by specific bounds
591 static int get_bt_addr(struct mm_struct *mm,
592 long __user *bd_entry_ptr,
593 unsigned long *bt_addr_result)
597 unsigned long bd_entry;
598 unsigned long bt_addr;
600 if (!access_ok(VERIFY_READ, (bd_entry_ptr), sizeof(*bd_entry_ptr)))
607 ret = get_user(bd_entry, bd_entry_ptr);
612 ret = mpx_resolve_fault(bd_entry_ptr, need_write);
614 * If we could not resolve the fault, consider it
615 * userspace's fault and error out.
621 valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG;
622 bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry);
625 * When the kernel is managing bounds tables, a bounds directory
626 * entry will either have a valid address (plus the valid bit)
627 * *OR* be completely empty. If we see a !valid entry *and* some
628 * data in the address field, we know something is wrong. This
629 * -EINVAL return will cause a SIGSEGV.
631 if (!valid_bit && bt_addr)
634 * Do we have an completely zeroed bt entry? That is OK. It
635 * just means there was no bounds table for this memory. Make
636 * sure to distinguish this from -EINVAL, which will cause
642 *bt_addr_result = bt_addr;
646 static inline int bt_entry_size_bytes(struct mm_struct *mm)
649 return MPX_BT_ENTRY_BYTES_64;
651 return MPX_BT_ENTRY_BYTES_32;
655 * Take a virtual address and turns it in to the offset in bytes
656 * inside of the bounds table where the bounds table entry
657 * controlling 'addr' can be found.
659 static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm,
662 unsigned long bt_table_nr_entries;
663 unsigned long offset = addr;
665 if (is_64bit_mm(mm)) {
666 /* Bottom 3 bits are ignored on 64-bit */
668 bt_table_nr_entries = MPX_BT_NR_ENTRIES_64;
670 /* Bottom 2 bits are ignored on 32-bit */
672 bt_table_nr_entries = MPX_BT_NR_ENTRIES_32;
675 * We know the size of the table in to which we are
676 * indexing, and we have eliminated all the low bits
677 * which are ignored for indexing.
679 * Mask out all the high bits which we do not need
680 * to index in to the table. Note that the tables
681 * are always powers of two so this gives us a proper
684 offset &= (bt_table_nr_entries-1);
686 * We now have an entry offset in terms of *entries* in
687 * the table. We need to scale it back up to bytes.
689 offset *= bt_entry_size_bytes(mm);
694 * How much virtual address space does a single bounds
695 * directory entry cover?
697 * Note, we need a long long because 4GB doesn't fit in
698 * to a long on 32-bit.
700 static inline unsigned long bd_entry_virt_space(struct mm_struct *mm)
702 unsigned long long virt_space = (1ULL << boot_cpu_data.x86_virt_bits);
704 return virt_space / MPX_BD_NR_ENTRIES_64;
706 return virt_space / MPX_BD_NR_ENTRIES_32;
710 * Free the backing physical pages of bounds table 'bt_addr'.
711 * Assume start...end is within that bounds table.
713 static noinline int zap_bt_entries_mapping(struct mm_struct *mm,
714 unsigned long bt_addr,
715 unsigned long start_mapping, unsigned long end_mapping)
717 struct vm_area_struct *vma;
718 unsigned long addr, len;
723 * if we 'end' on a boundary, the offset will be 0 which
724 * is not what we want. Back it up a byte to get the
725 * last bt entry. Then once we have the entry itself,
726 * move 'end' back up by the table entry size.
728 start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping);
729 end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1);
731 * Move end back up by one entry. Among other things
732 * this ensures that it remains page-aligned and does
733 * not screw up zap_page_range()
735 end += bt_entry_size_bytes(mm);
738 * Find the first overlapping vma. If vma->vm_start > start, there
739 * will be a hole in the bounds table. This -EINVAL return will
742 vma = find_vma(mm, start);
743 if (!vma || vma->vm_start > start)
747 * A NUMA policy on a VM_MPX VMA could cause this bounds table to
748 * be split. So we need to look across the entire 'start -> end'
749 * range of this bounds table, find all of the VM_MPX VMAs, and
753 while (vma && vma->vm_start < end) {
755 * We followed a bounds directory entry down
756 * here. If we find a non-MPX VMA, that's bad,
757 * so stop immediately and return an error. This
758 * probably results in a SIGSEGV.
760 if (!(vma->vm_flags & VM_MPX))
763 len = min(vma->vm_end, end) - addr;
764 zap_page_range(vma, addr, len, NULL);
765 trace_mpx_unmap_zap(addr, addr+len);
768 addr = vma->vm_start;
773 static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm,
777 * There are several ways to derive the bd offsets. We
778 * use the following approach here:
779 * 1. We know the size of the virtual address space
780 * 2. We know the number of entries in a bounds table
781 * 3. We know that each entry covers a fixed amount of
782 * virtual address space.
783 * So, we can just divide the virtual address by the
784 * virtual space used by one entry to determine which
785 * entry "controls" the given virtual address.
787 if (is_64bit_mm(mm)) {
788 int bd_entry_size = 8; /* 64-bit pointer */
790 * Take the 64-bit addressing hole in to account.
792 addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1);
793 return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
795 int bd_entry_size = 4; /* 32-bit pointer */
797 * 32-bit has no hole so this case needs no mask
799 return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
802 * The two return calls above are exact copies. If we
803 * pull out a single copy and put it in here, gcc won't
804 * realize that we're doing a power-of-2 divide and use
805 * shifts. It uses a real divide. If we put them up
806 * there, it manages to figure it out (gcc 4.8.3).
810 static int unmap_entire_bt(struct mm_struct *mm,
811 long __user *bd_entry, unsigned long bt_addr)
813 unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
814 unsigned long uninitialized_var(actual_old_val);
819 unsigned long cleared_bd_entry = 0;
822 ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val,
823 bd_entry, expected_old_val, cleared_bd_entry);
828 ret = mpx_resolve_fault(bd_entry, need_write);
830 * If we could not resolve the fault, consider it
831 * userspace's fault and error out.
837 * The cmpxchg was performed, check the results.
839 if (actual_old_val != expected_old_val) {
841 * Someone else raced with us to unmap the table.
842 * That is OK, since we were both trying to do
843 * the same thing. Declare success.
848 * Something messed with the bounds directory
849 * entry. We hold mmap_sem for read or write
850 * here, so it could not be a _new_ bounds table
851 * that someone just allocated. Something is
852 * wrong, so pass up the error and SIGSEGV.
857 * Note, we are likely being called under do_munmap() already. To
858 * avoid recursion, do_munmap() will check whether it comes
859 * from one bounds table through VM_MPX flag.
861 return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm));
864 static int try_unmap_single_bt(struct mm_struct *mm,
865 unsigned long start, unsigned long end)
867 struct vm_area_struct *next;
868 struct vm_area_struct *prev;
870 * "bta" == Bounds Table Area: the area controlled by the
871 * bounds table that we are unmapping.
873 unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1);
874 unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm);
875 unsigned long uninitialized_var(bt_addr);
876 void __user *bde_vaddr;
879 * We already unlinked the VMAs from the mm's rbtree so 'start'
880 * is guaranteed to be in a hole. This gets us the first VMA
881 * before the hole in to 'prev' and the next VMA after the hole
884 next = find_vma_prev(mm, start, &prev);
886 * Do not count other MPX bounds table VMAs as neighbors.
887 * Although theoretically possible, we do not allow bounds
888 * tables for bounds tables so our heads do not explode.
889 * If we count them as neighbors here, we may end up with
890 * lots of tables even though we have no actual table
893 while (next && (next->vm_flags & VM_MPX))
894 next = next->vm_next;
895 while (prev && (prev->vm_flags & VM_MPX))
896 prev = prev->vm_prev;
898 * We know 'start' and 'end' lie within an area controlled
899 * by a single bounds table. See if there are any other
900 * VMAs controlled by that bounds table. If there are not
901 * then we can "expand" the are we are unmapping to possibly
902 * cover the entire table.
904 next = find_vma_prev(mm, start, &prev);
905 if ((!prev || prev->vm_end <= bta_start_vaddr) &&
906 (!next || next->vm_start >= bta_end_vaddr)) {
908 * No neighbor VMAs controlled by same bounds
909 * table. Try to unmap the whole thing
911 start = bta_start_vaddr;
915 bde_vaddr = mm->bd_addr + mpx_get_bd_entry_offset(mm, start);
916 ret = get_bt_addr(mm, bde_vaddr, &bt_addr);
918 * No bounds table there, so nothing to unmap.
920 if (ret == -ENOENT) {
927 * We are unmapping an entire table. Either because the
928 * unmap that started this whole process was large enough
929 * to cover an entire table, or that the unmap was small
930 * but was the area covered by a bounds table.
932 if ((start == bta_start_vaddr) &&
933 (end == bta_end_vaddr))
934 return unmap_entire_bt(mm, bde_vaddr, bt_addr);
935 return zap_bt_entries_mapping(mm, bt_addr, start, end);
938 static int mpx_unmap_tables(struct mm_struct *mm,
939 unsigned long start, unsigned long end)
941 unsigned long one_unmap_start;
942 trace_mpx_unmap_search(start, end);
944 one_unmap_start = start;
945 while (one_unmap_start < end) {
947 unsigned long next_unmap_start = ALIGN(one_unmap_start+1,
948 bd_entry_virt_space(mm));
949 unsigned long one_unmap_end = end;
951 * if the end is beyond the current bounds table,
952 * move it back so we only deal with a single one
955 if (one_unmap_end > next_unmap_start)
956 one_unmap_end = next_unmap_start;
957 ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end);
961 one_unmap_start = next_unmap_start;
967 * Free unused bounds tables covered in a virtual address region being
968 * munmap()ed. Assume end > start.
970 * This function will be called by do_munmap(), and the VMAs covering
971 * the virtual address region start...end have already been split if
972 * necessary, and the 'vma' is the first vma in this range (start -> end).
974 void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
975 unsigned long start, unsigned long end)
980 * Refuse to do anything unless userspace has asked
981 * the kernel to help manage the bounds tables,
983 if (!kernel_managing_mpx_tables(current->mm))
986 * This will look across the entire 'start -> end' range,
987 * and find all of the non-VM_MPX VMAs.
989 * To avoid recursion, if a VM_MPX vma is found in the range
990 * (start->end), we will not continue follow-up work. This
991 * recursion represents having bounds tables for bounds tables,
992 * which should not occur normally. Being strict about it here
993 * helps ensure that we do not have an exploitable stack overflow.
996 if (vma->vm_flags & VM_MPX)
999 } while (vma && vma->vm_start < end);
1001 ret = mpx_unmap_tables(mm, start, end);
1003 force_sig(SIGSEGV, current);