mpx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. * 32-bit binaries on 64-bit kernels are currently
  314. * unsupported.
  315. */
  316. if (IS_ENABLED(CONFIG_X86_64) && test_thread_flag(TIF_IA32))
  317. return MPX_INVALID_BOUNDS_DIR;
  318. /*
  319. * The bounds directory pointer is stored in a register
  320. * only accessible if we first do an xsave.
  321. */
  322. fpu_save_init(&tsk->thread.fpu);
  323. bndcsr = get_xsave_addr(&tsk->thread.fpu.state->xsave, XSTATE_BNDCSR);
  324. if (!bndcsr)
  325. return MPX_INVALID_BOUNDS_DIR;
  326. /*
  327. * Make sure the register looks valid by checking the
  328. * enable bit.
  329. */
  330. if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
  331. return MPX_INVALID_BOUNDS_DIR;
  332. /*
  333. * Lastly, mask off the low bits used for configuration
  334. * flags, and return the address of the bounds table.
  335. */
  336. return (void __user *)(unsigned long)
  337. (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
  338. }
  339. int mpx_enable_management(struct task_struct *tsk)
  340. {
  341. void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
  342. struct mm_struct *mm = tsk->mm;
  343. int ret = 0;
  344. /*
  345. * runtime in the userspace will be responsible for allocation of
  346. * the bounds directory. Then, it will save the base of the bounds
  347. * directory into XSAVE/XRSTOR Save Area and enable MPX through
  348. * XRSTOR instruction.
  349. *
  350. * fpu_xsave() is expected to be very expensive. Storing the bounds
  351. * directory here means that we do not have to do xsave in the unmap
  352. * path; we can just use mm->bd_addr instead.
  353. */
  354. bd_base = task_get_bounds_dir(tsk);
  355. down_write(&mm->mmap_sem);
  356. mm->bd_addr = bd_base;
  357. if (mm->bd_addr == MPX_INVALID_BOUNDS_DIR)
  358. ret = -ENXIO;
  359. up_write(&mm->mmap_sem);
  360. return ret;
  361. }
  362. int mpx_disable_management(struct task_struct *tsk)
  363. {
  364. struct mm_struct *mm = current->mm;
  365. if (!cpu_feature_enabled(X86_FEATURE_MPX))
  366. return -ENXIO;
  367. down_write(&mm->mmap_sem);
  368. mm->bd_addr = MPX_INVALID_BOUNDS_DIR;
  369. up_write(&mm->mmap_sem);
  370. return 0;
  371. }
  372. /*
  373. * With 32-bit mode, MPX_BT_SIZE_BYTES is 4MB, and the size of each
  374. * bounds table is 16KB. With 64-bit mode, MPX_BT_SIZE_BYTES is 2GB,
  375. * and the size of each bounds table is 4MB.
  376. */
  377. static int allocate_bt(long __user *bd_entry)
  378. {
  379. unsigned long expected_old_val = 0;
  380. unsigned long actual_old_val = 0;
  381. unsigned long bt_addr;
  382. int ret = 0;
  383. /*
  384. * Carve the virtual space out of userspace for the new
  385. * bounds table:
  386. */
  387. bt_addr = mpx_mmap(MPX_BT_SIZE_BYTES);
  388. if (IS_ERR((void *)bt_addr))
  389. return PTR_ERR((void *)bt_addr);
  390. /*
  391. * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
  392. */
  393. bt_addr = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
  394. /*
  395. * Go poke the address of the new bounds table in to the
  396. * bounds directory entry out in userspace memory. Note:
  397. * we may race with another CPU instantiating the same table.
  398. * In that case the cmpxchg will see an unexpected
  399. * 'actual_old_val'.
  400. *
  401. * This can fault, but that's OK because we do not hold
  402. * mmap_sem at this point, unlike some of the other part
  403. * of the MPX code that have to pagefault_disable().
  404. */
  405. ret = user_atomic_cmpxchg_inatomic(&actual_old_val, bd_entry,
  406. expected_old_val, bt_addr);
  407. if (ret)
  408. goto out_unmap;
  409. /*
  410. * The user_atomic_cmpxchg_inatomic() will only return nonzero
  411. * for faults, *not* if the cmpxchg itself fails. Now we must
  412. * verify that the cmpxchg itself completed successfully.
  413. */
  414. /*
  415. * We expected an empty 'expected_old_val', but instead found
  416. * an apparently valid entry. Assume we raced with another
  417. * thread to instantiate this table and desclare succecss.
  418. */
  419. if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
  420. ret = 0;
  421. goto out_unmap;
  422. }
  423. /*
  424. * We found a non-empty bd_entry but it did not have the
  425. * VALID_FLAG set. Return an error which will result in
  426. * a SEGV since this probably means that somebody scribbled
  427. * some invalid data in to a bounds table.
  428. */
  429. if (expected_old_val != actual_old_val) {
  430. ret = -EINVAL;
  431. goto out_unmap;
  432. }
  433. return 0;
  434. out_unmap:
  435. vm_munmap(bt_addr & MPX_BT_ADDR_MASK, MPX_BT_SIZE_BYTES);
  436. return ret;
  437. }
  438. /*
  439. * When a BNDSTX instruction attempts to save bounds to a bounds
  440. * table, it will first attempt to look up the table in the
  441. * first-level bounds directory. If it does not find a table in
  442. * the directory, a #BR is generated and we get here in order to
  443. * allocate a new table.
  444. *
  445. * With 32-bit mode, the size of BD is 4MB, and the size of each
  446. * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
  447. * and the size of each bound table is 4MB.
  448. */
  449. static int do_mpx_bt_fault(struct xsave_struct *xsave_buf)
  450. {
  451. unsigned long bd_entry, bd_base;
  452. struct bndcsr *bndcsr;
  453. bndcsr = get_xsave_addr(xsave_buf, XSTATE_BNDCSR);
  454. if (!bndcsr)
  455. return -EINVAL;
  456. /*
  457. * Mask off the preserve and enable bits
  458. */
  459. bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
  460. /*
  461. * The hardware provides the address of the missing or invalid
  462. * entry via BNDSTATUS, so we don't have to go look it up.
  463. */
  464. bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
  465. /*
  466. * Make sure the directory entry is within where we think
  467. * the directory is.
  468. */
  469. if ((bd_entry < bd_base) ||
  470. (bd_entry >= bd_base + MPX_BD_SIZE_BYTES))
  471. return -EINVAL;
  472. return allocate_bt((long __user *)bd_entry);
  473. }
  474. int mpx_handle_bd_fault(struct xsave_struct *xsave_buf)
  475. {
  476. /*
  477. * Userspace never asked us to manage the bounds tables,
  478. * so refuse to help.
  479. */
  480. if (!kernel_managing_mpx_tables(current->mm))
  481. return -EINVAL;
  482. if (do_mpx_bt_fault(xsave_buf)) {
  483. force_sig(SIGSEGV, current);
  484. /*
  485. * The force_sig() is essentially "handling" this
  486. * exception, so we do not pass up the error
  487. * from do_mpx_bt_fault().
  488. */
  489. }
  490. return 0;
  491. }
  492. /*
  493. * A thin wrapper around get_user_pages(). Returns 0 if the
  494. * fault was resolved or -errno if not.
  495. */
  496. static int mpx_resolve_fault(long __user *addr, int write)
  497. {
  498. long gup_ret;
  499. int nr_pages = 1;
  500. int force = 0;
  501. gup_ret = get_user_pages(current, current->mm, (unsigned long)addr,
  502. nr_pages, write, force, NULL, NULL);
  503. /*
  504. * get_user_pages() returns number of pages gotten.
  505. * 0 means we failed to fault in and get anything,
  506. * probably because 'addr' is bad.
  507. */
  508. if (!gup_ret)
  509. return -EFAULT;
  510. /* Other error, return it */
  511. if (gup_ret < 0)
  512. return gup_ret;
  513. /* must have gup'd a page and gup_ret>0, success */
  514. return 0;
  515. }
  516. /*
  517. * Get the base of bounds tables pointed by specific bounds
  518. * directory entry.
  519. */
  520. static int get_bt_addr(struct mm_struct *mm,
  521. long __user *bd_entry, unsigned long *bt_addr)
  522. {
  523. int ret;
  524. int valid_bit;
  525. if (!access_ok(VERIFY_READ, (bd_entry), sizeof(*bd_entry)))
  526. return -EFAULT;
  527. while (1) {
  528. int need_write = 0;
  529. pagefault_disable();
  530. ret = get_user(*bt_addr, bd_entry);
  531. pagefault_enable();
  532. if (!ret)
  533. break;
  534. if (ret == -EFAULT)
  535. ret = mpx_resolve_fault(bd_entry, need_write);
  536. /*
  537. * If we could not resolve the fault, consider it
  538. * userspace's fault and error out.
  539. */
  540. if (ret)
  541. return ret;
  542. }
  543. valid_bit = *bt_addr & MPX_BD_ENTRY_VALID_FLAG;
  544. *bt_addr &= MPX_BT_ADDR_MASK;
  545. /*
  546. * When the kernel is managing bounds tables, a bounds directory
  547. * entry will either have a valid address (plus the valid bit)
  548. * *OR* be completely empty. If we see a !valid entry *and* some
  549. * data in the address field, we know something is wrong. This
  550. * -EINVAL return will cause a SIGSEGV.
  551. */
  552. if (!valid_bit && *bt_addr)
  553. return -EINVAL;
  554. /*
  555. * Do we have an completely zeroed bt entry? That is OK. It
  556. * just means there was no bounds table for this memory. Make
  557. * sure to distinguish this from -EINVAL, which will cause
  558. * a SEGV.
  559. */
  560. if (!valid_bit)
  561. return -ENOENT;
  562. return 0;
  563. }
  564. /*
  565. * Free the backing physical pages of bounds table 'bt_addr'.
  566. * Assume start...end is within that bounds table.
  567. */
  568. static int zap_bt_entries(struct mm_struct *mm,
  569. unsigned long bt_addr,
  570. unsigned long start, unsigned long end)
  571. {
  572. struct vm_area_struct *vma;
  573. unsigned long addr, len;
  574. /*
  575. * Find the first overlapping vma. If vma->vm_start > start, there
  576. * will be a hole in the bounds table. This -EINVAL return will
  577. * cause a SIGSEGV.
  578. */
  579. vma = find_vma(mm, start);
  580. if (!vma || vma->vm_start > start)
  581. return -EINVAL;
  582. /*
  583. * A NUMA policy on a VM_MPX VMA could cause this bouds table to
  584. * be split. So we need to look across the entire 'start -> end'
  585. * range of this bounds table, find all of the VM_MPX VMAs, and
  586. * zap only those.
  587. */
  588. addr = start;
  589. while (vma && vma->vm_start < end) {
  590. /*
  591. * We followed a bounds directory entry down
  592. * here. If we find a non-MPX VMA, that's bad,
  593. * so stop immediately and return an error. This
  594. * probably results in a SIGSEGV.
  595. */
  596. if (!is_mpx_vma(vma))
  597. return -EINVAL;
  598. len = min(vma->vm_end, end) - addr;
  599. zap_page_range(vma, addr, len, NULL);
  600. vma = vma->vm_next;
  601. addr = vma->vm_start;
  602. }
  603. return 0;
  604. }
  605. static int unmap_single_bt(struct mm_struct *mm,
  606. long __user *bd_entry, unsigned long bt_addr)
  607. {
  608. unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
  609. unsigned long actual_old_val = 0;
  610. int ret;
  611. while (1) {
  612. int need_write = 1;
  613. pagefault_disable();
  614. ret = user_atomic_cmpxchg_inatomic(&actual_old_val, bd_entry,
  615. expected_old_val, 0);
  616. pagefault_enable();
  617. if (!ret)
  618. break;
  619. if (ret == -EFAULT)
  620. ret = mpx_resolve_fault(bd_entry, need_write);
  621. /*
  622. * If we could not resolve the fault, consider it
  623. * userspace's fault and error out.
  624. */
  625. if (ret)
  626. return ret;
  627. }
  628. /*
  629. * The cmpxchg was performed, check the results.
  630. */
  631. if (actual_old_val != expected_old_val) {
  632. /*
  633. * Someone else raced with us to unmap the table.
  634. * There was no bounds table pointed to by the
  635. * directory, so declare success. Somebody freed
  636. * it.
  637. */
  638. if (!actual_old_val)
  639. return 0;
  640. /*
  641. * Something messed with the bounds directory
  642. * entry. We hold mmap_sem for read or write
  643. * here, so it could not be a _new_ bounds table
  644. * that someone just allocated. Something is
  645. * wrong, so pass up the error and SIGSEGV.
  646. */
  647. return -EINVAL;
  648. }
  649. /*
  650. * Note, we are likely being called under do_munmap() already. To
  651. * avoid recursion, do_munmap() will check whether it comes
  652. * from one bounds table through VM_MPX flag.
  653. */
  654. return do_munmap(mm, bt_addr, MPX_BT_SIZE_BYTES);
  655. }
  656. /*
  657. * If the bounds table pointed by bounds directory 'bd_entry' is
  658. * not shared, unmap this whole bounds table. Otherwise, only free
  659. * those backing physical pages of bounds table entries covered
  660. * in this virtual address region start...end.
  661. */
  662. static int unmap_shared_bt(struct mm_struct *mm,
  663. long __user *bd_entry, unsigned long start,
  664. unsigned long end, bool prev_shared, bool next_shared)
  665. {
  666. unsigned long bt_addr;
  667. int ret;
  668. ret = get_bt_addr(mm, bd_entry, &bt_addr);
  669. /*
  670. * We could see an "error" ret for not-present bounds
  671. * tables (not really an error), or actual errors, but
  672. * stop unmapping either way.
  673. */
  674. if (ret)
  675. return ret;
  676. if (prev_shared && next_shared)
  677. ret = zap_bt_entries(mm, bt_addr,
  678. bt_addr+MPX_GET_BT_ENTRY_OFFSET(start),
  679. bt_addr+MPX_GET_BT_ENTRY_OFFSET(end));
  680. else if (prev_shared)
  681. ret = zap_bt_entries(mm, bt_addr,
  682. bt_addr+MPX_GET_BT_ENTRY_OFFSET(start),
  683. bt_addr+MPX_BT_SIZE_BYTES);
  684. else if (next_shared)
  685. ret = zap_bt_entries(mm, bt_addr, bt_addr,
  686. bt_addr+MPX_GET_BT_ENTRY_OFFSET(end));
  687. else
  688. ret = unmap_single_bt(mm, bd_entry, bt_addr);
  689. return ret;
  690. }
  691. /*
  692. * A virtual address region being munmap()ed might share bounds table
  693. * with adjacent VMAs. We only need to free the backing physical
  694. * memory of these shared bounds tables entries covered in this virtual
  695. * address region.
  696. */
  697. static int unmap_edge_bts(struct mm_struct *mm,
  698. unsigned long start, unsigned long end)
  699. {
  700. int ret;
  701. long __user *bde_start, *bde_end;
  702. struct vm_area_struct *prev, *next;
  703. bool prev_shared = false, next_shared = false;
  704. bde_start = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(start);
  705. bde_end = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(end-1);
  706. /*
  707. * Check whether bde_start and bde_end are shared with adjacent
  708. * VMAs.
  709. *
  710. * We already unliked the VMAs from the mm's rbtree so 'start'
  711. * is guaranteed to be in a hole. This gets us the first VMA
  712. * before the hole in to 'prev' and the next VMA after the hole
  713. * in to 'next'.
  714. */
  715. next = find_vma_prev(mm, start, &prev);
  716. if (prev && (mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(prev->vm_end-1))
  717. == bde_start)
  718. prev_shared = true;
  719. if (next && (mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(next->vm_start))
  720. == bde_end)
  721. next_shared = true;
  722. /*
  723. * This virtual address region being munmap()ed is only
  724. * covered by one bounds table.
  725. *
  726. * In this case, if this table is also shared with adjacent
  727. * VMAs, only part of the backing physical memory of the bounds
  728. * table need be freeed. Otherwise the whole bounds table need
  729. * be unmapped.
  730. */
  731. if (bde_start == bde_end) {
  732. return unmap_shared_bt(mm, bde_start, start, end,
  733. prev_shared, next_shared);
  734. }
  735. /*
  736. * If more than one bounds tables are covered in this virtual
  737. * address region being munmap()ed, we need to separately check
  738. * whether bde_start and bde_end are shared with adjacent VMAs.
  739. */
  740. ret = unmap_shared_bt(mm, bde_start, start, end, prev_shared, false);
  741. if (ret)
  742. return ret;
  743. ret = unmap_shared_bt(mm, bde_end, start, end, false, next_shared);
  744. if (ret)
  745. return ret;
  746. return 0;
  747. }
  748. static int mpx_unmap_tables(struct mm_struct *mm,
  749. unsigned long start, unsigned long end)
  750. {
  751. int ret;
  752. long __user *bd_entry, *bde_start, *bde_end;
  753. unsigned long bt_addr;
  754. /*
  755. * "Edge" bounds tables are those which are being used by the region
  756. * (start -> end), but that may be shared with adjacent areas. If they
  757. * turn out to be completely unshared, they will be freed. If they are
  758. * shared, we will free the backing store (like an MADV_DONTNEED) for
  759. * areas used by this region.
  760. */
  761. ret = unmap_edge_bts(mm, start, end);
  762. switch (ret) {
  763. /* non-present tables are OK */
  764. case 0:
  765. case -ENOENT:
  766. /* Success, or no tables to unmap */
  767. break;
  768. case -EINVAL:
  769. case -EFAULT:
  770. default:
  771. return ret;
  772. }
  773. /*
  774. * Only unmap the bounds table that are
  775. * 1. fully covered
  776. * 2. not at the edges of the mapping, even if full aligned
  777. */
  778. bde_start = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(start);
  779. bde_end = mm->bd_addr + MPX_GET_BD_ENTRY_OFFSET(end-1);
  780. for (bd_entry = bde_start + 1; bd_entry < bde_end; bd_entry++) {
  781. ret = get_bt_addr(mm, bd_entry, &bt_addr);
  782. switch (ret) {
  783. case 0:
  784. break;
  785. case -ENOENT:
  786. /* No table here, try the next one */
  787. continue;
  788. case -EINVAL:
  789. case -EFAULT:
  790. default:
  791. /*
  792. * Note: we are being strict here.
  793. * Any time we run in to an issue
  794. * unmapping tables, we stop and
  795. * SIGSEGV.
  796. */
  797. return ret;
  798. }
  799. ret = unmap_single_bt(mm, bd_entry, bt_addr);
  800. if (ret)
  801. return ret;
  802. }
  803. return 0;
  804. }
  805. /*
  806. * Free unused bounds tables covered in a virtual address region being
  807. * munmap()ed. Assume end > start.
  808. *
  809. * This function will be called by do_munmap(), and the VMAs covering
  810. * the virtual address region start...end have already been split if
  811. * necessary, and the 'vma' is the first vma in this range (start -> end).
  812. */
  813. void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
  814. unsigned long start, unsigned long end)
  815. {
  816. int ret;
  817. /*
  818. * Refuse to do anything unless userspace has asked
  819. * the kernel to help manage the bounds tables,
  820. */
  821. if (!kernel_managing_mpx_tables(current->mm))
  822. return;
  823. /*
  824. * This will look across the entire 'start -> end' range,
  825. * and find all of the non-VM_MPX VMAs.
  826. *
  827. * To avoid recursion, if a VM_MPX vma is found in the range
  828. * (start->end), we will not continue follow-up work. This
  829. * recursion represents having bounds tables for bounds tables,
  830. * which should not occur normally. Being strict about it here
  831. * helps ensure that we do not have an exploitable stack overflow.
  832. */
  833. do {
  834. if (vma->vm_flags & VM_MPX)
  835. return;
  836. vma = vma->vm_next;
  837. } while (vma && vma->vm_start < end);
  838. ret = mpx_unmap_tables(mm, start, end);
  839. if (ret)
  840. force_sig(SIGSEGV, current);
  841. }