mpx.c 24 KB

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