x86/mpx: Introduce new 'directory entry' to 'addr' helper function
[deliverable/linux.git] / arch / x86 / mm / mpx.c
CommitLineData
57319d80
QR
1/*
2 * mpx.c - Memory Protection eXtensions
3 *
4 * Copyright (c) 2014, Intel Corporation.
5 * Qiaowei Ren <qiaowei.ren@intel.com>
6 * Dave Hansen <dave.hansen@intel.com>
7 */
8#include <linux/kernel.h>
fcc7ffd6 9#include <linux/slab.h>
57319d80
QR
10#include <linux/syscalls.h>
11#include <linux/sched/sysctl.h>
12
fe3d197f 13#include <asm/insn.h>
57319d80 14#include <asm/mman.h>
1de4fa14 15#include <asm/mmu_context.h>
57319d80 16#include <asm/mpx.h>
fe3d197f 17#include <asm/processor.h>
78f7f1e5 18#include <asm/fpu/internal.h>
57319d80 19
e7126cf5
DH
20#define CREATE_TRACE_POINTS
21#include <asm/trace/mpx.h>
22
57319d80
QR
23static const char *mpx_mapping_name(struct vm_area_struct *vma)
24{
25 return "[mpx]";
26}
27
28static struct vm_operations_struct mpx_vma_ops = {
29 .name = mpx_mapping_name,
30};
31
1de4fa14
DH
32static int is_mpx_vma(struct vm_area_struct *vma)
33{
34 return (vma->vm_ops == &mpx_vma_ops);
35}
36
57319d80
QR
37/*
38 * This is really a simplified "vm_mmap". it only handles MPX
39 * bounds tables (the bounds directory is user-allocated).
40 *
41 * Later on, we use the vma->vm_ops to uniquely identify these
42 * VMAs.
43 */
44static unsigned long mpx_mmap(unsigned long len)
45{
46 unsigned long ret;
47 unsigned long addr, pgoff;
48 struct mm_struct *mm = current->mm;
49 vm_flags_t vm_flags;
50 struct vm_area_struct *vma;
51
eb099e5b
DH
52 /* Only bounds table can be allocated here */
53 if (len != MPX_BT_SIZE_BYTES)
57319d80
QR
54 return -EINVAL;
55
56 down_write(&mm->mmap_sem);
57
58 /* Too many mappings? */
59 if (mm->map_count > sysctl_max_map_count) {
60 ret = -ENOMEM;
61 goto out;
62 }
63
64 /* Obtain the address to map to. we verify (or select) it and ensure
65 * that it represents a valid section of the address space.
66 */
67 addr = get_unmapped_area(NULL, 0, len, 0, MAP_ANONYMOUS | MAP_PRIVATE);
68 if (addr & ~PAGE_MASK) {
69 ret = addr;
70 goto out;
71 }
72
73 vm_flags = VM_READ | VM_WRITE | VM_MPX |
74 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
75
76 /* Set pgoff according to addr for anon_vma */
77 pgoff = addr >> PAGE_SHIFT;
78
79 ret = mmap_region(NULL, addr, len, vm_flags, pgoff);
80 if (IS_ERR_VALUE(ret))
81 goto out;
82
83 vma = find_vma(mm, ret);
84 if (!vma) {
85 ret = -ENOMEM;
86 goto out;
87 }
88 vma->vm_ops = &mpx_vma_ops;
89
90 if (vm_flags & VM_LOCKED) {
91 up_write(&mm->mmap_sem);
92 mm_populate(ret, len);
93 return ret;
94 }
95
96out:
97 up_write(&mm->mmap_sem);
98 return ret;
99}
fcc7ffd6
DH
100
101enum reg_type {
102 REG_TYPE_RM = 0,
103 REG_TYPE_INDEX,
104 REG_TYPE_BASE,
105};
106
68c009c4
DH
107static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
108 enum reg_type type)
fcc7ffd6
DH
109{
110 int regno = 0;
111
112 static const int regoff[] = {
113 offsetof(struct pt_regs, ax),
114 offsetof(struct pt_regs, cx),
115 offsetof(struct pt_regs, dx),
116 offsetof(struct pt_regs, bx),
117 offsetof(struct pt_regs, sp),
118 offsetof(struct pt_regs, bp),
119 offsetof(struct pt_regs, si),
120 offsetof(struct pt_regs, di),
121#ifdef CONFIG_X86_64
122 offsetof(struct pt_regs, r8),
123 offsetof(struct pt_regs, r9),
124 offsetof(struct pt_regs, r10),
125 offsetof(struct pt_regs, r11),
126 offsetof(struct pt_regs, r12),
127 offsetof(struct pt_regs, r13),
128 offsetof(struct pt_regs, r14),
129 offsetof(struct pt_regs, r15),
130#endif
131 };
132 int nr_registers = ARRAY_SIZE(regoff);
133 /*
134 * Don't possibly decode a 32-bit instructions as
135 * reading a 64-bit-only register.
136 */
137 if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
138 nr_registers -= 8;
139
140 switch (type) {
141 case REG_TYPE_RM:
142 regno = X86_MODRM_RM(insn->modrm.value);
143 if (X86_REX_B(insn->rex_prefix.value) == 1)
144 regno += 8;
145 break;
146
147 case REG_TYPE_INDEX:
148 regno = X86_SIB_INDEX(insn->sib.value);
149 if (X86_REX_X(insn->rex_prefix.value) == 1)
150 regno += 8;
151 break;
152
153 case REG_TYPE_BASE:
154 regno = X86_SIB_BASE(insn->sib.value);
155 if (X86_REX_B(insn->rex_prefix.value) == 1)
156 regno += 8;
157 break;
158
159 default:
160 pr_err("invalid register type");
161 BUG();
162 break;
163 }
164
165 if (regno > nr_registers) {
166 WARN_ONCE(1, "decoded an instruction with an invalid register");
167 return -EINVAL;
168 }
169 return regoff[regno];
170}
171
172/*
173 * return the address being referenced be instruction
174 * for rm=3 returning the content of the rm reg
175 * for rm!=3 calculates the address using SIB and Disp
176 */
177static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)
178{
68c009c4
DH
179 unsigned long addr, base, indx;
180 int addr_offset, base_offset, indx_offset;
fcc7ffd6
DH
181 insn_byte_t sib;
182
183 insn_get_modrm(insn);
184 insn_get_sib(insn);
185 sib = insn->sib.value;
186
187 if (X86_MODRM_MOD(insn->modrm.value) == 3) {
188 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
189 if (addr_offset < 0)
190 goto out_err;
191 addr = regs_get_register(regs, addr_offset);
192 } else {
193 if (insn->sib.nbytes) {
194 base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
195 if (base_offset < 0)
196 goto out_err;
197
198 indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
199 if (indx_offset < 0)
200 goto out_err;
201
202 base = regs_get_register(regs, base_offset);
203 indx = regs_get_register(regs, indx_offset);
204 addr = base + indx * (1 << X86_SIB_SCALE(sib));
205 } else {
206 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
207 if (addr_offset < 0)
208 goto out_err;
209 addr = regs_get_register(regs, addr_offset);
210 }
211 addr += insn->displacement.value;
212 }
213 return (void __user *)addr;
214out_err:
215 return (void __user *)-1;
216}
217
218static int mpx_insn_decode(struct insn *insn,
219 struct pt_regs *regs)
220{
221 unsigned char buf[MAX_INSN_SIZE];
222 int x86_64 = !test_thread_flag(TIF_IA32);
223 int not_copied;
224 int nr_copied;
225
226 not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf));
227 nr_copied = sizeof(buf) - not_copied;
228 /*
229 * The decoder _should_ fail nicely if we pass it a short buffer.
230 * But, let's not depend on that implementation detail. If we
231 * did not get anything, just error out now.
232 */
233 if (!nr_copied)
234 return -EFAULT;
235 insn_init(insn, buf, nr_copied, x86_64);
236 insn_get_length(insn);
237 /*
238 * copy_from_user() tries to get as many bytes as we could see in
239 * the largest possible instruction. If the instruction we are
240 * after is shorter than that _and_ we attempt to copy from
241 * something unreadable, we might get a short read. This is OK
242 * as long as the read did not stop in the middle of the
243 * instruction. Check to see if we got a partial instruction.
244 */
245 if (nr_copied < insn->length)
246 return -EFAULT;
247
248 insn_get_opcode(insn);
249 /*
250 * We only _really_ need to decode bndcl/bndcn/bndcu
251 * Error out on anything else.
252 */
253 if (insn->opcode.bytes[0] != 0x0f)
254 goto bad_opcode;
255 if ((insn->opcode.bytes[1] != 0x1a) &&
256 (insn->opcode.bytes[1] != 0x1b))
257 goto bad_opcode;
258
259 return 0;
260bad_opcode:
261 return -EINVAL;
262}
263
264/*
265 * If a bounds overflow occurs then a #BR is generated. This
266 * function decodes MPX instructions to get violation address
267 * and set this address into extended struct siginfo.
268 *
269 * Note that this is not a super precise way of doing this.
270 * Userspace could have, by the time we get here, written
271 * anything it wants in to the instructions. We can not
272 * trust anything about it. They might not be valid
273 * instructions or might encode invalid registers, etc...
274 *
275 * The caller is expected to kfree() the returned siginfo_t.
276 */
46a6e0cf 277siginfo_t *mpx_generate_siginfo(struct pt_regs *regs)
fcc7ffd6 278{
a84eeaa9 279 const struct bndreg *bndregs, *bndreg;
fe3d197f 280 siginfo_t *info = NULL;
fcc7ffd6
DH
281 struct insn insn;
282 uint8_t bndregno;
283 int err;
fcc7ffd6
DH
284
285 err = mpx_insn_decode(&insn, regs);
286 if (err)
287 goto err_out;
288
289 /*
290 * We know at this point that we are only dealing with
291 * MPX instructions.
292 */
293 insn_get_modrm(&insn);
294 bndregno = X86_MODRM_REG(insn.modrm.value);
295 if (bndregno > 3) {
296 err = -EINVAL;
297 goto err_out;
298 }
a84eeaa9
DH
299 /* get bndregs field from current task's xsave area */
300 bndregs = get_xsave_field_ptr(XSTATE_BNDREGS);
fe3d197f
DH
301 if (!bndregs) {
302 err = -EINVAL;
303 goto err_out;
304 }
305 /* now go select the individual register in the set of 4 */
306 bndreg = &bndregs[bndregno];
307
fcc7ffd6
DH
308 info = kzalloc(sizeof(*info), GFP_KERNEL);
309 if (!info) {
310 err = -ENOMEM;
311 goto err_out;
312 }
313 /*
314 * The registers are always 64-bit, but the upper 32
315 * bits are ignored in 32-bit mode. Also, note that the
316 * upper bounds are architecturally represented in 1's
317 * complement form.
318 *
319 * The 'unsigned long' cast is because the compiler
320 * complains when casting from integers to different-size
321 * pointers.
322 */
fe3d197f
DH
323 info->si_lower = (void __user *)(unsigned long)bndreg->lower_bound;
324 info->si_upper = (void __user *)(unsigned long)~bndreg->upper_bound;
fcc7ffd6
DH
325 info->si_addr_lsb = 0;
326 info->si_signo = SIGSEGV;
327 info->si_errno = 0;
328 info->si_code = SEGV_BNDERR;
329 info->si_addr = mpx_get_addr_ref(&insn, regs);
330 /*
331 * We were not able to extract an address from the instruction,
332 * probably because there was something invalid in it.
333 */
334 if (info->si_addr == (void *)-1) {
335 err = -EINVAL;
336 goto err_out;
337 }
97efebf1 338 trace_mpx_bounds_register_exception(info->si_addr, bndreg);
fcc7ffd6
DH
339 return info;
340err_out:
fe3d197f
DH
341 /* info might be NULL, but kfree() handles that */
342 kfree(info);
fcc7ffd6
DH
343 return ERR_PTR(err);
344}
fe3d197f 345
46a6e0cf 346static __user void *mpx_get_bounds_dir(void)
fe3d197f 347{
a84eeaa9 348 const struct bndcsr *bndcsr;
fe3d197f
DH
349
350 if (!cpu_feature_enabled(X86_FEATURE_MPX))
351 return MPX_INVALID_BOUNDS_DIR;
352
814564a0
DH
353 /*
354 * 32-bit binaries on 64-bit kernels are currently
355 * unsupported.
356 */
357 if (IS_ENABLED(CONFIG_X86_64) && test_thread_flag(TIF_IA32))
358 return MPX_INVALID_BOUNDS_DIR;
fe3d197f
DH
359 /*
360 * The bounds directory pointer is stored in a register
361 * only accessible if we first do an xsave.
362 */
a84eeaa9 363 bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR);
fe3d197f
DH
364 if (!bndcsr)
365 return MPX_INVALID_BOUNDS_DIR;
366
367 /*
368 * Make sure the register looks valid by checking the
369 * enable bit.
370 */
371 if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
372 return MPX_INVALID_BOUNDS_DIR;
373
374 /*
375 * Lastly, mask off the low bits used for configuration
376 * flags, and return the address of the bounds table.
377 */
378 return (void __user *)(unsigned long)
379 (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
380}
381
46a6e0cf 382int mpx_enable_management(void)
fe3d197f
DH
383{
384 void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
46a6e0cf 385 struct mm_struct *mm = current->mm;
fe3d197f
DH
386 int ret = 0;
387
388 /*
389 * runtime in the userspace will be responsible for allocation of
390 * the bounds directory. Then, it will save the base of the bounds
391 * directory into XSAVE/XRSTOR Save Area and enable MPX through
392 * XRSTOR instruction.
393 *
a84eeaa9
DH
394 * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is
395 * expected to be relatively expensive. Storing the bounds
396 * directory here means that we do not have to do xsave in the
397 * unmap path; we can just use mm->bd_addr instead.
fe3d197f 398 */
46a6e0cf 399 bd_base = mpx_get_bounds_dir();
fe3d197f
DH
400 down_write(&mm->mmap_sem);
401 mm->bd_addr = bd_base;
402 if (mm->bd_addr == MPX_INVALID_BOUNDS_DIR)
403 ret = -ENXIO;
404
405 up_write(&mm->mmap_sem);
406 return ret;
407}
408
46a6e0cf 409int mpx_disable_management(void)
fe3d197f
DH
410{
411 struct mm_struct *mm = current->mm;
412
413 if (!cpu_feature_enabled(X86_FEATURE_MPX))
414 return -ENXIO;
415
416 down_write(&mm->mmap_sem);
417 mm->bd_addr = MPX_INVALID_BOUNDS_DIR;
418 up_write(&mm->mmap_sem);
419 return 0;
420}
421
422/*
423 * With 32-bit mode, MPX_BT_SIZE_BYTES is 4MB, and the size of each
424 * bounds table is 16KB. With 64-bit mode, MPX_BT_SIZE_BYTES is 2GB,
425 * and the size of each bounds table is 4MB.
426 */
427static int allocate_bt(long __user *bd_entry)
428{
429 unsigned long expected_old_val = 0;
430 unsigned long actual_old_val = 0;
431 unsigned long bt_addr;
a1149fc8 432 unsigned long bd_new_entry;
fe3d197f
DH
433 int ret = 0;
434
435 /*
436 * Carve the virtual space out of userspace for the new
437 * bounds table:
438 */
439 bt_addr = mpx_mmap(MPX_BT_SIZE_BYTES);
440 if (IS_ERR((void *)bt_addr))
441 return PTR_ERR((void *)bt_addr);
442 /*
443 * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
444 */
a1149fc8 445 bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
fe3d197f
DH
446
447 /*
448 * Go poke the address of the new bounds table in to the
449 * bounds directory entry out in userspace memory. Note:
450 * we may race with another CPU instantiating the same table.
451 * In that case the cmpxchg will see an unexpected
452 * 'actual_old_val'.
453 *
454 * This can fault, but that's OK because we do not hold
455 * mmap_sem at this point, unlike some of the other part
456 * of the MPX code that have to pagefault_disable().
457 */
458 ret = user_atomic_cmpxchg_inatomic(&actual_old_val, bd_entry,
a1149fc8 459 expected_old_val, bd_new_entry);
fe3d197f
DH
460 if (ret)
461 goto out_unmap;
462
463 /*
464 * The user_atomic_cmpxchg_inatomic() will only return nonzero
465 * for faults, *not* if the cmpxchg itself fails. Now we must
466 * verify that the cmpxchg itself completed successfully.
467 */
468 /*
469 * We expected an empty 'expected_old_val', but instead found
470 * an apparently valid entry. Assume we raced with another
471 * thread to instantiate this table and desclare succecss.
472 */
473 if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
474 ret = 0;
475 goto out_unmap;
476 }
477 /*
478 * We found a non-empty bd_entry but it did not have the
479 * VALID_FLAG set. Return an error which will result in
480 * a SEGV since this probably means that somebody scribbled
481 * some invalid data in to a bounds table.
482 */
483 if (expected_old_val != actual_old_val) {
484 ret = -EINVAL;
485 goto out_unmap;
486 }
cd4996dc 487 trace_mpx_new_bounds_table(bt_addr);
fe3d197f
DH
488 return 0;
489out_unmap:
a1149fc8 490 vm_munmap(bt_addr, MPX_BT_SIZE_BYTES);
fe3d197f
DH
491 return ret;
492}
493
494/*
495 * When a BNDSTX instruction attempts to save bounds to a bounds
496 * table, it will first attempt to look up the table in the
497 * first-level bounds directory. If it does not find a table in
498 * the directory, a #BR is generated and we get here in order to
499 * allocate a new table.
500 *
501 * With 32-bit mode, the size of BD is 4MB, and the size of each
502 * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
503 * and the size of each bound table is 4MB.
504 */
46a6e0cf 505static int do_mpx_bt_fault(void)
fe3d197f
DH
506{
507 unsigned long bd_entry, bd_base;
a84eeaa9 508 const struct bndcsr *bndcsr;
fe3d197f 509
a84eeaa9 510 bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR);
fe3d197f
DH
511 if (!bndcsr)
512 return -EINVAL;
513 /*
514 * Mask off the preserve and enable bits
515 */
516 bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
517 /*
518 * The hardware provides the address of the missing or invalid
519 * entry via BNDSTATUS, so we don't have to go look it up.
520 */
521 bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
522 /*
523 * Make sure the directory entry is within where we think
524 * the directory is.
525 */
526 if ((bd_entry < bd_base) ||
527 (bd_entry >= bd_base + MPX_BD_SIZE_BYTES))
528 return -EINVAL;
529
530 return allocate_bt((long __user *)bd_entry);
531}
532
46a6e0cf 533int mpx_handle_bd_fault(void)
fe3d197f
DH
534{
535 /*
536 * Userspace never asked us to manage the bounds tables,
537 * so refuse to help.
538 */
539 if (!kernel_managing_mpx_tables(current->mm))
540 return -EINVAL;
541
46a6e0cf 542 if (do_mpx_bt_fault()) {
fe3d197f
DH
543 force_sig(SIGSEGV, current);
544 /*
545 * The force_sig() is essentially "handling" this
546 * exception, so we do not pass up the error
547 * from do_mpx_bt_fault().
548 */
549 }
550 return 0;
551}
1de4fa14
DH
552
553/*
554 * A thin wrapper around get_user_pages(). Returns 0 if the
555 * fault was resolved or -errno if not.
556 */
557static int mpx_resolve_fault(long __user *addr, int write)
558{
559 long gup_ret;
560 int nr_pages = 1;
561 int force = 0;
562
563 gup_ret = get_user_pages(current, current->mm, (unsigned long)addr,
564 nr_pages, write, force, NULL, NULL);
565 /*
566 * get_user_pages() returns number of pages gotten.
567 * 0 means we failed to fault in and get anything,
568 * probably because 'addr' is bad.
569 */
570 if (!gup_ret)
571 return -EFAULT;
572 /* Other error, return it */
573 if (gup_ret < 0)
574 return gup_ret;
575 /* must have gup'd a page and gup_ret>0, success */
576 return 0;
577}
578
54587653
DH
579static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm,
580 unsigned long bd_entry)
581{
582 unsigned long bt_addr = bd_entry;
583 int align_to_bytes;
584 /*
585 * Bit 0 in a bt_entry is always the valid bit.
586 */
587 bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG;
588 /*
589 * Tables are naturally aligned at 8-byte boundaries
590 * on 64-bit and 4-byte boundaries on 32-bit. The
591 * documentation makes it appear that the low bits
592 * are ignored by the hardware, so we do the same.
593 */
594 if (is_64bit_mm(mm))
595 align_to_bytes = 8;
596 else
597 align_to_bytes = 4;
598 bt_addr &= ~(align_to_bytes-1);
599 return bt_addr;
600}
601
1de4fa14
DH
602/*
603 * Get the base of bounds tables pointed by specific bounds
604 * directory entry.
605 */
606static int get_bt_addr(struct mm_struct *mm,
54587653
DH
607 long __user *bd_entry_ptr,
608 unsigned long *bt_addr_result)
1de4fa14
DH
609{
610 int ret;
611 int valid_bit;
54587653
DH
612 unsigned long bd_entry;
613 unsigned long bt_addr;
1de4fa14 614
54587653 615 if (!access_ok(VERIFY_READ, (bd_entry_ptr), sizeof(*bd_entry_ptr)))
1de4fa14
DH
616 return -EFAULT;
617
618 while (1) {
619 int need_write = 0;
620
621 pagefault_disable();
54587653 622 ret = get_user(bd_entry, bd_entry_ptr);
1de4fa14
DH
623 pagefault_enable();
624 if (!ret)
625 break;
626 if (ret == -EFAULT)
54587653 627 ret = mpx_resolve_fault(bd_entry_ptr, need_write);
1de4fa14
DH
628 /*
629 * If we could not resolve the fault, consider it
630 * userspace's fault and error out.
631 */
632 if (ret)
633 return ret;
634 }
635
54587653
DH
636 valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG;
637 bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry);
1de4fa14
DH
638
639 /*
640 * When the kernel is managing bounds tables, a bounds directory
641 * entry will either have a valid address (plus the valid bit)
642 * *OR* be completely empty. If we see a !valid entry *and* some
643 * data in the address field, we know something is wrong. This
644 * -EINVAL return will cause a SIGSEGV.
645 */
54587653 646 if (!valid_bit && bt_addr)
1de4fa14
DH
647 return -EINVAL;
648 /*
649 * Do we have an completely zeroed bt entry? That is OK. It
650 * just means there was no bounds table for this memory. Make
651 * sure to distinguish this from -EINVAL, which will cause
652 * a SEGV.
653 */
654 if (!valid_bit)
655 return -ENOENT;
656
54587653 657 *bt_addr_result = bt_addr;
1de4fa14
DH
658 return 0;
659}
660
661/*
662 * Free the backing physical pages of bounds table 'bt_addr'.
663 * Assume start...end is within that bounds table.
664 */
665static int zap_bt_entries(struct mm_struct *mm,
666 unsigned long bt_addr,
667 unsigned long start, unsigned long end)
668{
669 struct vm_area_struct *vma;
670 unsigned long addr, len;
671
672 /*
673 * Find the first overlapping vma. If vma->vm_start > start, there
674 * will be a hole in the bounds table. This -EINVAL return will
675 * cause a SIGSEGV.
676 */
677 vma = find_vma(mm, start);
678 if (!vma || vma->vm_start > start)
679 return -EINVAL;
680
681 /*
682 * A NUMA policy on a VM_MPX VMA could cause this bouds table to
683 * be split. So we need to look across the entire 'start -> end'
684 * range of this bounds table, find all of the VM_MPX VMAs, and
685 * zap only those.
686 */
687 addr = start;
688 while (vma && vma->vm_start < end) {
689 /*
690 * We followed a bounds directory entry down
691 * here. If we find a non-MPX VMA, that's bad,
692 * so stop immediately and return an error. This
693 * probably results in a SIGSEGV.
694 */
695 if (!is_mpx_vma(vma))
696 return -EINVAL;
697
698 len = min(vma->vm_end, end) - addr;
699 zap_page_range(vma, addr, len, NULL);
2a1dcb1f 700 trace_mpx_unmap_zap(addr, addr+len);
1de4fa14
DH
701
702 vma = vma->vm_next;
703 addr = vma->vm_start;
704 }
705
706 return 0;
707}
708
709static int unmap_single_bt(struct mm_struct *mm,
710 long __user *bd_entry, unsigned long bt_addr)
711{
712 unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
713 unsigned long actual_old_val = 0;
714 int ret;
715
716 while (1) {
717 int need_write = 1;
718
719 pagefault_disable();
720 ret = user_atomic_cmpxchg_inatomic(&actual_old_val, bd_entry,
721 expected_old_val, 0);
722 pagefault_enable();
723 if (!ret)
724 break;
725 if (ret == -EFAULT)
726 ret = mpx_resolve_fault(bd_entry, need_write);
727 /*
728 * If we could not resolve the fault, consider it
729 * userspace's fault and error out.
730 */
731 if (ret)
732 return ret;
733 }
734 /*
735 * The cmpxchg was performed, check the results.
736 */
737 if (actual_old_val != expected_old_val) {
738 /*
739 * Someone else raced with us to unmap the table.
740 * There was no bounds table pointed to by the
741 * directory, so declare success. Somebody freed
742 * it.
743 */
744 if (!actual_old_val)
745 return 0;
746 /*
747 * Something messed with the bounds directory
748 * entry. We hold mmap_sem for read or write
749 * here, so it could not be a _new_ bounds table
750 * that someone just allocated. Something is
751 * wrong, so pass up the error and SIGSEGV.
752 */
753 return -EINVAL;
754 }
755
756 /*
757 * Note, we are likely being called under do_munmap() already. To
758 * avoid recursion, do_munmap() will check whether it comes
759 * from one bounds table through VM_MPX flag.
760 */
761 return do_munmap(mm, bt_addr, MPX_BT_SIZE_BYTES);
762}
763
764/*
765 * If the bounds table pointed by bounds directory 'bd_entry' is
766 * not shared, unmap this whole bounds table. Otherwise, only free
767 * those backing physical pages of bounds table entries covered
768 * in this virtual address region start...end.
769 */
770static int unmap_shared_bt(struct mm_struct *mm,
771 long __user *bd_entry, unsigned long start,
772 unsigned long end, bool prev_shared, bool next_shared)
773{
774 unsigned long bt_addr;
775 int ret;
776
777 ret = get_bt_addr(mm, bd_entry, &bt_addr);
778 /*
779 * We could see an "error" ret for not-present bounds
780 * tables (not really an error), or actual errors, but
781 * stop unmapping either way.
782 */
783 if (ret)
784 return ret;
785
786 if (prev_shared && next_shared)
787 ret = zap_bt_entries(mm, bt_addr,
788 bt_addr+MPX_GET_BT_ENTRY_OFFSET(start),
789 bt_addr+MPX_GET_BT_ENTRY_OFFSET(end));
790 else if (prev_shared)
791 ret = zap_bt_entries(mm, bt_addr,
792 bt_addr+MPX_GET_BT_ENTRY_OFFSET(start),
793 bt_addr+MPX_BT_SIZE_BYTES);
794 else if (next_shared)
795 ret = zap_bt_entries(mm, bt_addr, bt_addr,
796 bt_addr+MPX_GET_BT_ENTRY_OFFSET(end));
797 else
798 ret = unmap_single_bt(mm, bd_entry, bt_addr);
799
800 return ret;
801}
802
803/*
804 * A virtual address region being munmap()ed might share bounds table
805 * with adjacent VMAs. We only need to free the backing physical
806 * memory of these shared bounds tables entries covered in this virtual
807 * address region.
808 */
809static int unmap_edge_bts(struct mm_struct *mm,
810 unsigned long start, unsigned long end)
811{
812 int ret;
813 long __user *bde_start, *bde_end;
814 struct vm_area_struct *prev, *next;
815 bool prev_shared = false, next_shared = false;
816
817 bde_start = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(start);
818 bde_end = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(end-1);
819
820 /*
821 * Check whether bde_start and bde_end are shared with adjacent
822 * VMAs.
823 *
824 * We already unliked the VMAs from the mm's rbtree so 'start'
825 * is guaranteed to be in a hole. This gets us the first VMA
826 * before the hole in to 'prev' and the next VMA after the hole
827 * in to 'next'.
828 */
829 next = find_vma_prev(mm, start, &prev);
830 if (prev && (mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(prev->vm_end-1))
831 == bde_start)
832 prev_shared = true;
833 if (next && (mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(next->vm_start))
834 == bde_end)
835 next_shared = true;
836
837 /*
838 * This virtual address region being munmap()ed is only
839 * covered by one bounds table.
840 *
841 * In this case, if this table is also shared with adjacent
842 * VMAs, only part of the backing physical memory of the bounds
843 * table need be freeed. Otherwise the whole bounds table need
844 * be unmapped.
845 */
846 if (bde_start == bde_end) {
847 return unmap_shared_bt(mm, bde_start, start, end,
848 prev_shared, next_shared);
849 }
850
851 /*
852 * If more than one bounds tables are covered in this virtual
853 * address region being munmap()ed, we need to separately check
854 * whether bde_start and bde_end are shared with adjacent VMAs.
855 */
856 ret = unmap_shared_bt(mm, bde_start, start, end, prev_shared, false);
857 if (ret)
858 return ret;
859 ret = unmap_shared_bt(mm, bde_end, start, end, false, next_shared);
860 if (ret)
861 return ret;
862
863 return 0;
864}
865
866static int mpx_unmap_tables(struct mm_struct *mm,
867 unsigned long start, unsigned long end)
868{
869 int ret;
870 long __user *bd_entry, *bde_start, *bde_end;
871 unsigned long bt_addr;
872
2a1dcb1f 873 trace_mpx_unmap_search(start, end);
1de4fa14
DH
874 /*
875 * "Edge" bounds tables are those which are being used by the region
876 * (start -> end), but that may be shared with adjacent areas. If they
877 * turn out to be completely unshared, they will be freed. If they are
878 * shared, we will free the backing store (like an MADV_DONTNEED) for
879 * areas used by this region.
880 */
881 ret = unmap_edge_bts(mm, start, end);
882 switch (ret) {
883 /* non-present tables are OK */
884 case 0:
885 case -ENOENT:
886 /* Success, or no tables to unmap */
887 break;
888 case -EINVAL:
889 case -EFAULT:
890 default:
891 return ret;
892 }
893
894 /*
895 * Only unmap the bounds table that are
896 * 1. fully covered
897 * 2. not at the edges of the mapping, even if full aligned
898 */
899 bde_start = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(start);
900 bde_end = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(end-1);
901 for (bd_entry = bde_start + 1; bd_entry < bde_end; bd_entry++) {
902 ret = get_bt_addr(mm, bd_entry, &bt_addr);
903 switch (ret) {
904 case 0:
905 break;
906 case -ENOENT:
907 /* No table here, try the next one */
908 continue;
909 case -EINVAL:
910 case -EFAULT:
911 default:
912 /*
913 * Note: we are being strict here.
914 * Any time we run in to an issue
915 * unmapping tables, we stop and
916 * SIGSEGV.
917 */
918 return ret;
919 }
920
921 ret = unmap_single_bt(mm, bd_entry, bt_addr);
922 if (ret)
923 return ret;
924 }
925
926 return 0;
927}
928
929/*
930 * Free unused bounds tables covered in a virtual address region being
931 * munmap()ed. Assume end > start.
932 *
933 * This function will be called by do_munmap(), and the VMAs covering
934 * the virtual address region start...end have already been split if
935 * necessary, and the 'vma' is the first vma in this range (start -> end).
936 */
937void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
938 unsigned long start, unsigned long end)
939{
940 int ret;
941
942 /*
943 * Refuse to do anything unless userspace has asked
944 * the kernel to help manage the bounds tables,
945 */
946 if (!kernel_managing_mpx_tables(current->mm))
947 return;
948 /*
949 * This will look across the entire 'start -> end' range,
950 * and find all of the non-VM_MPX VMAs.
951 *
952 * To avoid recursion, if a VM_MPX vma is found in the range
953 * (start->end), we will not continue follow-up work. This
954 * recursion represents having bounds tables for bounds tables,
955 * which should not occur normally. Being strict about it here
956 * helps ensure that we do not have an exploitable stack overflow.
957 */
958 do {
959 if (vma->vm_flags & VM_MPX)
960 return;
961 vma = vma->vm_next;
962 } while (vma && vma->vm_start < end);
963
964 ret = mpx_unmap_tables(mm, start, end);
965 if (ret)
966 force_sig(SIGSEGV, current);
967}
This page took 0.120101 seconds and 5 git commands to generate.