mpx.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mpx.c - Memory Protection eXtensions
  4. *
  5. * Copyright (c) 2014, Intel Corporation.
  6. * Qiaowei Ren <qiaowei.ren@intel.com>
  7. * Dave Hansen <dave.hansen@intel.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/mm_types.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/sched/sysctl.h>
  14. #include <asm/insn.h>
  15. #include <asm/insn-eval.h>
  16. #include <asm/mman.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/mpx.h>
  19. #include <asm/processor.h>
  20. #include <asm/fpu/internal.h>
  21. #define CREATE_TRACE_POINTS
  22. #include <asm/trace/mpx.h>
  23. static inline unsigned long mpx_bd_size_bytes(struct mm_struct *mm)
  24. {
  25. if (is_64bit_mm(mm))
  26. return MPX_BD_SIZE_BYTES_64;
  27. else
  28. return MPX_BD_SIZE_BYTES_32;
  29. }
  30. static inline unsigned long mpx_bt_size_bytes(struct mm_struct *mm)
  31. {
  32. if (is_64bit_mm(mm))
  33. return MPX_BT_SIZE_BYTES_64;
  34. else
  35. return MPX_BT_SIZE_BYTES_32;
  36. }
  37. /*
  38. * This is really a simplified "vm_mmap". it only handles MPX
  39. * bounds tables (the bounds directory is user-allocated).
  40. */
  41. static unsigned long mpx_mmap(unsigned long len)
  42. {
  43. struct mm_struct *mm = current->mm;
  44. unsigned long addr, populate;
  45. /* Only bounds table can be allocated here */
  46. if (len != mpx_bt_size_bytes(mm))
  47. return -EINVAL;
  48. down_write(&mm->mmap_sem);
  49. addr = do_mmap(NULL, 0, len, PROT_READ | PROT_WRITE,
  50. MAP_ANONYMOUS | MAP_PRIVATE, VM_MPX, 0, &populate, NULL);
  51. up_write(&mm->mmap_sem);
  52. if (populate)
  53. mm_populate(addr, populate);
  54. return addr;
  55. }
  56. static int mpx_insn_decode(struct insn *insn,
  57. struct pt_regs *regs)
  58. {
  59. unsigned char buf[MAX_INSN_SIZE];
  60. int x86_64 = !test_thread_flag(TIF_IA32);
  61. int not_copied;
  62. int nr_copied;
  63. not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf));
  64. nr_copied = sizeof(buf) - not_copied;
  65. /*
  66. * The decoder _should_ fail nicely if we pass it a short buffer.
  67. * But, let's not depend on that implementation detail. If we
  68. * did not get anything, just error out now.
  69. */
  70. if (!nr_copied)
  71. return -EFAULT;
  72. insn_init(insn, buf, nr_copied, x86_64);
  73. insn_get_length(insn);
  74. /*
  75. * copy_from_user() tries to get as many bytes as we could see in
  76. * the largest possible instruction. If the instruction we are
  77. * after is shorter than that _and_ we attempt to copy from
  78. * something unreadable, we might get a short read. This is OK
  79. * as long as the read did not stop in the middle of the
  80. * instruction. Check to see if we got a partial instruction.
  81. */
  82. if (nr_copied < insn->length)
  83. return -EFAULT;
  84. insn_get_opcode(insn);
  85. /*
  86. * We only _really_ need to decode bndcl/bndcn/bndcu
  87. * Error out on anything else.
  88. */
  89. if (insn->opcode.bytes[0] != 0x0f)
  90. goto bad_opcode;
  91. if ((insn->opcode.bytes[1] != 0x1a) &&
  92. (insn->opcode.bytes[1] != 0x1b))
  93. goto bad_opcode;
  94. return 0;
  95. bad_opcode:
  96. return -EINVAL;
  97. }
  98. /*
  99. * If a bounds overflow occurs then a #BR is generated. This
  100. * function decodes MPX instructions to get violation address
  101. * and set this address into extended struct siginfo.
  102. *
  103. * Note that this is not a super precise way of doing this.
  104. * Userspace could have, by the time we get here, written
  105. * anything it wants in to the instructions. We can not
  106. * trust anything about it. They might not be valid
  107. * instructions or might encode invalid registers, etc...
  108. */
  109. int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs)
  110. {
  111. const struct mpx_bndreg_state *bndregs;
  112. const struct mpx_bndreg *bndreg;
  113. struct insn insn;
  114. uint8_t bndregno;
  115. int err;
  116. err = mpx_insn_decode(&insn, regs);
  117. if (err)
  118. goto err_out;
  119. /*
  120. * We know at this point that we are only dealing with
  121. * MPX instructions.
  122. */
  123. insn_get_modrm(&insn);
  124. bndregno = X86_MODRM_REG(insn.modrm.value);
  125. if (bndregno > 3) {
  126. err = -EINVAL;
  127. goto err_out;
  128. }
  129. /* get bndregs field from current task's xsave area */
  130. bndregs = get_xsave_field_ptr(XFEATURE_MASK_BNDREGS);
  131. if (!bndregs) {
  132. err = -EINVAL;
  133. goto err_out;
  134. }
  135. /* now go select the individual register in the set of 4 */
  136. bndreg = &bndregs->bndreg[bndregno];
  137. /*
  138. * The registers are always 64-bit, but the upper 32
  139. * bits are ignored in 32-bit mode. Also, note that the
  140. * upper bounds are architecturally represented in 1's
  141. * complement form.
  142. *
  143. * The 'unsigned long' cast is because the compiler
  144. * complains when casting from integers to different-size
  145. * pointers.
  146. */
  147. info->lower = (void __user *)(unsigned long)bndreg->lower_bound;
  148. info->upper = (void __user *)(unsigned long)~bndreg->upper_bound;
  149. info->addr = insn_get_addr_ref(&insn, regs);
  150. /*
  151. * We were not able to extract an address from the instruction,
  152. * probably because there was something invalid in it.
  153. */
  154. if (info->addr == (void __user *)-1) {
  155. err = -EINVAL;
  156. goto err_out;
  157. }
  158. trace_mpx_bounds_register_exception(info->addr, bndreg);
  159. return 0;
  160. err_out:
  161. /* info might be NULL, but kfree() handles that */
  162. return err;
  163. }
  164. static __user void *mpx_get_bounds_dir(void)
  165. {
  166. const struct mpx_bndcsr *bndcsr;
  167. if (!cpu_feature_enabled(X86_FEATURE_MPX))
  168. return MPX_INVALID_BOUNDS_DIR;
  169. /*
  170. * The bounds directory pointer is stored in a register
  171. * only accessible if we first do an xsave.
  172. */
  173. bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
  174. if (!bndcsr)
  175. return MPX_INVALID_BOUNDS_DIR;
  176. /*
  177. * Make sure the register looks valid by checking the
  178. * enable bit.
  179. */
  180. if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
  181. return MPX_INVALID_BOUNDS_DIR;
  182. /*
  183. * Lastly, mask off the low bits used for configuration
  184. * flags, and return the address of the bounds table.
  185. */
  186. return (void __user *)(unsigned long)
  187. (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
  188. }
  189. int mpx_enable_management(void)
  190. {
  191. void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
  192. struct mm_struct *mm = current->mm;
  193. int ret = 0;
  194. /*
  195. * runtime in the userspace will be responsible for allocation of
  196. * the bounds directory. Then, it will save the base of the bounds
  197. * directory into XSAVE/XRSTOR Save Area and enable MPX through
  198. * XRSTOR instruction.
  199. *
  200. * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is
  201. * expected to be relatively expensive. Storing the bounds
  202. * directory here means that we do not have to do xsave in the
  203. * unmap path; we can just use mm->context.bd_addr instead.
  204. */
  205. bd_base = mpx_get_bounds_dir();
  206. down_write(&mm->mmap_sem);
  207. /* MPX doesn't support addresses above 47 bits yet. */
  208. if (find_vma(mm, DEFAULT_MAP_WINDOW)) {
  209. pr_warn_once("%s (%d): MPX cannot handle addresses "
  210. "above 47-bits. Disabling.",
  211. current->comm, current->pid);
  212. ret = -ENXIO;
  213. goto out;
  214. }
  215. mm->context.bd_addr = bd_base;
  216. if (mm->context.bd_addr == MPX_INVALID_BOUNDS_DIR)
  217. ret = -ENXIO;
  218. out:
  219. up_write(&mm->mmap_sem);
  220. return ret;
  221. }
  222. int mpx_disable_management(void)
  223. {
  224. struct mm_struct *mm = current->mm;
  225. if (!cpu_feature_enabled(X86_FEATURE_MPX))
  226. return -ENXIO;
  227. down_write(&mm->mmap_sem);
  228. mm->context.bd_addr = MPX_INVALID_BOUNDS_DIR;
  229. up_write(&mm->mmap_sem);
  230. return 0;
  231. }
  232. static int mpx_cmpxchg_bd_entry(struct mm_struct *mm,
  233. unsigned long *curval,
  234. unsigned long __user *addr,
  235. unsigned long old_val, unsigned long new_val)
  236. {
  237. int ret;
  238. /*
  239. * user_atomic_cmpxchg_inatomic() actually uses sizeof()
  240. * the pointer that we pass to it to figure out how much
  241. * data to cmpxchg. We have to be careful here not to
  242. * pass a pointer to a 64-bit data type when we only want
  243. * a 32-bit copy.
  244. */
  245. if (is_64bit_mm(mm)) {
  246. ret = user_atomic_cmpxchg_inatomic(curval,
  247. addr, old_val, new_val);
  248. } else {
  249. u32 uninitialized_var(curval_32);
  250. u32 old_val_32 = old_val;
  251. u32 new_val_32 = new_val;
  252. u32 __user *addr_32 = (u32 __user *)addr;
  253. ret = user_atomic_cmpxchg_inatomic(&curval_32,
  254. addr_32, old_val_32, new_val_32);
  255. *curval = curval_32;
  256. }
  257. return ret;
  258. }
  259. /*
  260. * With 32-bit mode, a bounds directory is 4MB, and the size of each
  261. * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB,
  262. * and the size of each bounds table is 4MB.
  263. */
  264. static int allocate_bt(struct mm_struct *mm, long __user *bd_entry)
  265. {
  266. unsigned long expected_old_val = 0;
  267. unsigned long actual_old_val = 0;
  268. unsigned long bt_addr;
  269. unsigned long bd_new_entry;
  270. int ret = 0;
  271. /*
  272. * Carve the virtual space out of userspace for the new
  273. * bounds table:
  274. */
  275. bt_addr = mpx_mmap(mpx_bt_size_bytes(mm));
  276. if (IS_ERR((void *)bt_addr))
  277. return PTR_ERR((void *)bt_addr);
  278. /*
  279. * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
  280. */
  281. bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
  282. /*
  283. * Go poke the address of the new bounds table in to the
  284. * bounds directory entry out in userspace memory. Note:
  285. * we may race with another CPU instantiating the same table.
  286. * In that case the cmpxchg will see an unexpected
  287. * 'actual_old_val'.
  288. *
  289. * This can fault, but that's OK because we do not hold
  290. * mmap_sem at this point, unlike some of the other part
  291. * of the MPX code that have to pagefault_disable().
  292. */
  293. ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry,
  294. expected_old_val, bd_new_entry);
  295. if (ret)
  296. goto out_unmap;
  297. /*
  298. * The user_atomic_cmpxchg_inatomic() will only return nonzero
  299. * for faults, *not* if the cmpxchg itself fails. Now we must
  300. * verify that the cmpxchg itself completed successfully.
  301. */
  302. /*
  303. * We expected an empty 'expected_old_val', but instead found
  304. * an apparently valid entry. Assume we raced with another
  305. * thread to instantiate this table and desclare succecss.
  306. */
  307. if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
  308. ret = 0;
  309. goto out_unmap;
  310. }
  311. /*
  312. * We found a non-empty bd_entry but it did not have the
  313. * VALID_FLAG set. Return an error which will result in
  314. * a SEGV since this probably means that somebody scribbled
  315. * some invalid data in to a bounds table.
  316. */
  317. if (expected_old_val != actual_old_val) {
  318. ret = -EINVAL;
  319. goto out_unmap;
  320. }
  321. trace_mpx_new_bounds_table(bt_addr);
  322. return 0;
  323. out_unmap:
  324. vm_munmap(bt_addr, mpx_bt_size_bytes(mm));
  325. return ret;
  326. }
  327. /*
  328. * When a BNDSTX instruction attempts to save bounds to a bounds
  329. * table, it will first attempt to look up the table in the
  330. * first-level bounds directory. If it does not find a table in
  331. * the directory, a #BR is generated and we get here in order to
  332. * allocate a new table.
  333. *
  334. * With 32-bit mode, the size of BD is 4MB, and the size of each
  335. * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
  336. * and the size of each bound table is 4MB.
  337. */
  338. static int do_mpx_bt_fault(void)
  339. {
  340. unsigned long bd_entry, bd_base;
  341. const struct mpx_bndcsr *bndcsr;
  342. struct mm_struct *mm = current->mm;
  343. bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
  344. if (!bndcsr)
  345. return -EINVAL;
  346. /*
  347. * Mask off the preserve and enable bits
  348. */
  349. bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
  350. /*
  351. * The hardware provides the address of the missing or invalid
  352. * entry via BNDSTATUS, so we don't have to go look it up.
  353. */
  354. bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
  355. /*
  356. * Make sure the directory entry is within where we think
  357. * the directory is.
  358. */
  359. if ((bd_entry < bd_base) ||
  360. (bd_entry >= bd_base + mpx_bd_size_bytes(mm)))
  361. return -EINVAL;
  362. return allocate_bt(mm, (long __user *)bd_entry);
  363. }
  364. int mpx_handle_bd_fault(void)
  365. {
  366. /*
  367. * Userspace never asked us to manage the bounds tables,
  368. * so refuse to help.
  369. */
  370. if (!kernel_managing_mpx_tables(current->mm))
  371. return -EINVAL;
  372. return do_mpx_bt_fault();
  373. }
  374. /*
  375. * A thin wrapper around get_user_pages(). Returns 0 if the
  376. * fault was resolved or -errno if not.
  377. */
  378. static int mpx_resolve_fault(long __user *addr, int write)
  379. {
  380. long gup_ret;
  381. int nr_pages = 1;
  382. gup_ret = get_user_pages((unsigned long)addr, nr_pages,
  383. write ? FOLL_WRITE : 0, NULL, NULL);
  384. /*
  385. * get_user_pages() returns number of pages gotten.
  386. * 0 means we failed to fault in and get anything,
  387. * probably because 'addr' is bad.
  388. */
  389. if (!gup_ret)
  390. return -EFAULT;
  391. /* Other error, return it */
  392. if (gup_ret < 0)
  393. return gup_ret;
  394. /* must have gup'd a page and gup_ret>0, success */
  395. return 0;
  396. }
  397. static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm,
  398. unsigned long bd_entry)
  399. {
  400. unsigned long bt_addr = bd_entry;
  401. int align_to_bytes;
  402. /*
  403. * Bit 0 in a bt_entry is always the valid bit.
  404. */
  405. bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG;
  406. /*
  407. * Tables are naturally aligned at 8-byte boundaries
  408. * on 64-bit and 4-byte boundaries on 32-bit. The
  409. * documentation makes it appear that the low bits
  410. * are ignored by the hardware, so we do the same.
  411. */
  412. if (is_64bit_mm(mm))
  413. align_to_bytes = 8;
  414. else
  415. align_to_bytes = 4;
  416. bt_addr &= ~(align_to_bytes-1);
  417. return bt_addr;
  418. }
  419. /*
  420. * We only want to do a 4-byte get_user() on 32-bit. Otherwise,
  421. * we might run off the end of the bounds table if we are on
  422. * a 64-bit kernel and try to get 8 bytes.
  423. */
  424. static int get_user_bd_entry(struct mm_struct *mm, unsigned long *bd_entry_ret,
  425. long __user *bd_entry_ptr)
  426. {
  427. u32 bd_entry_32;
  428. int ret;
  429. if (is_64bit_mm(mm))
  430. return get_user(*bd_entry_ret, bd_entry_ptr);
  431. /*
  432. * Note that get_user() uses the type of the *pointer* to
  433. * establish the size of the get, not the destination.
  434. */
  435. ret = get_user(bd_entry_32, (u32 __user *)bd_entry_ptr);
  436. *bd_entry_ret = bd_entry_32;
  437. return ret;
  438. }
  439. /*
  440. * Get the base of bounds tables pointed by specific bounds
  441. * directory entry.
  442. */
  443. static int get_bt_addr(struct mm_struct *mm,
  444. long __user *bd_entry_ptr,
  445. unsigned long *bt_addr_result)
  446. {
  447. int ret;
  448. int valid_bit;
  449. unsigned long bd_entry;
  450. unsigned long bt_addr;
  451. if (!access_ok(VERIFY_READ, (bd_entry_ptr), sizeof(*bd_entry_ptr)))
  452. return -EFAULT;
  453. while (1) {
  454. int need_write = 0;
  455. pagefault_disable();
  456. ret = get_user_bd_entry(mm, &bd_entry, bd_entry_ptr);
  457. pagefault_enable();
  458. if (!ret)
  459. break;
  460. if (ret == -EFAULT)
  461. ret = mpx_resolve_fault(bd_entry_ptr, need_write);
  462. /*
  463. * If we could not resolve the fault, consider it
  464. * userspace's fault and error out.
  465. */
  466. if (ret)
  467. return ret;
  468. }
  469. valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG;
  470. bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry);
  471. /*
  472. * When the kernel is managing bounds tables, a bounds directory
  473. * entry will either have a valid address (plus the valid bit)
  474. * *OR* be completely empty. If we see a !valid entry *and* some
  475. * data in the address field, we know something is wrong. This
  476. * -EINVAL return will cause a SIGSEGV.
  477. */
  478. if (!valid_bit && bt_addr)
  479. return -EINVAL;
  480. /*
  481. * Do we have an completely zeroed bt entry? That is OK. It
  482. * just means there was no bounds table for this memory. Make
  483. * sure to distinguish this from -EINVAL, which will cause
  484. * a SEGV.
  485. */
  486. if (!valid_bit)
  487. return -ENOENT;
  488. *bt_addr_result = bt_addr;
  489. return 0;
  490. }
  491. static inline int bt_entry_size_bytes(struct mm_struct *mm)
  492. {
  493. if (is_64bit_mm(mm))
  494. return MPX_BT_ENTRY_BYTES_64;
  495. else
  496. return MPX_BT_ENTRY_BYTES_32;
  497. }
  498. /*
  499. * Take a virtual address and turns it in to the offset in bytes
  500. * inside of the bounds table where the bounds table entry
  501. * controlling 'addr' can be found.
  502. */
  503. static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm,
  504. unsigned long addr)
  505. {
  506. unsigned long bt_table_nr_entries;
  507. unsigned long offset = addr;
  508. if (is_64bit_mm(mm)) {
  509. /* Bottom 3 bits are ignored on 64-bit */
  510. offset >>= 3;
  511. bt_table_nr_entries = MPX_BT_NR_ENTRIES_64;
  512. } else {
  513. /* Bottom 2 bits are ignored on 32-bit */
  514. offset >>= 2;
  515. bt_table_nr_entries = MPX_BT_NR_ENTRIES_32;
  516. }
  517. /*
  518. * We know the size of the table in to which we are
  519. * indexing, and we have eliminated all the low bits
  520. * which are ignored for indexing.
  521. *
  522. * Mask out all the high bits which we do not need
  523. * to index in to the table. Note that the tables
  524. * are always powers of two so this gives us a proper
  525. * mask.
  526. */
  527. offset &= (bt_table_nr_entries-1);
  528. /*
  529. * We now have an entry offset in terms of *entries* in
  530. * the table. We need to scale it back up to bytes.
  531. */
  532. offset *= bt_entry_size_bytes(mm);
  533. return offset;
  534. }
  535. /*
  536. * How much virtual address space does a single bounds
  537. * directory entry cover?
  538. *
  539. * Note, we need a long long because 4GB doesn't fit in
  540. * to a long on 32-bit.
  541. */
  542. static inline unsigned long bd_entry_virt_space(struct mm_struct *mm)
  543. {
  544. unsigned long long virt_space;
  545. unsigned long long GB = (1ULL << 30);
  546. /*
  547. * This covers 32-bit emulation as well as 32-bit kernels
  548. * running on 64-bit hardware.
  549. */
  550. if (!is_64bit_mm(mm))
  551. return (4ULL * GB) / MPX_BD_NR_ENTRIES_32;
  552. /*
  553. * 'x86_virt_bits' returns what the hardware is capable
  554. * of, and returns the full >32-bit address space when
  555. * running 32-bit kernels on 64-bit hardware.
  556. */
  557. virt_space = (1ULL << boot_cpu_data.x86_virt_bits);
  558. return virt_space / MPX_BD_NR_ENTRIES_64;
  559. }
  560. /*
  561. * Free the backing physical pages of bounds table 'bt_addr'.
  562. * Assume start...end is within that bounds table.
  563. */
  564. static noinline int zap_bt_entries_mapping(struct mm_struct *mm,
  565. unsigned long bt_addr,
  566. unsigned long start_mapping, unsigned long end_mapping)
  567. {
  568. struct vm_area_struct *vma;
  569. unsigned long addr, len;
  570. unsigned long start;
  571. unsigned long end;
  572. /*
  573. * if we 'end' on a boundary, the offset will be 0 which
  574. * is not what we want. Back it up a byte to get the
  575. * last bt entry. Then once we have the entry itself,
  576. * move 'end' back up by the table entry size.
  577. */
  578. start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping);
  579. end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1);
  580. /*
  581. * Move end back up by one entry. Among other things
  582. * this ensures that it remains page-aligned and does
  583. * not screw up zap_page_range()
  584. */
  585. end += bt_entry_size_bytes(mm);
  586. /*
  587. * Find the first overlapping vma. If vma->vm_start > start, there
  588. * will be a hole in the bounds table. This -EINVAL return will
  589. * cause a SIGSEGV.
  590. */
  591. vma = find_vma(mm, start);
  592. if (!vma || vma->vm_start > start)
  593. return -EINVAL;
  594. /*
  595. * A NUMA policy on a VM_MPX VMA could cause this bounds table to
  596. * be split. So we need to look across the entire 'start -> end'
  597. * range of this bounds table, find all of the VM_MPX VMAs, and
  598. * zap only those.
  599. */
  600. addr = start;
  601. while (vma && vma->vm_start < end) {
  602. /*
  603. * We followed a bounds directory entry down
  604. * here. If we find a non-MPX VMA, that's bad,
  605. * so stop immediately and return an error. This
  606. * probably results in a SIGSEGV.
  607. */
  608. if (!(vma->vm_flags & VM_MPX))
  609. return -EINVAL;
  610. len = min(vma->vm_end, end) - addr;
  611. zap_page_range(vma, addr, len);
  612. trace_mpx_unmap_zap(addr, addr+len);
  613. vma = vma->vm_next;
  614. addr = vma->vm_start;
  615. }
  616. return 0;
  617. }
  618. static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm,
  619. unsigned long addr)
  620. {
  621. /*
  622. * There are several ways to derive the bd offsets. We
  623. * use the following approach here:
  624. * 1. We know the size of the virtual address space
  625. * 2. We know the number of entries in a bounds table
  626. * 3. We know that each entry covers a fixed amount of
  627. * virtual address space.
  628. * So, we can just divide the virtual address by the
  629. * virtual space used by one entry to determine which
  630. * entry "controls" the given virtual address.
  631. */
  632. if (is_64bit_mm(mm)) {
  633. int bd_entry_size = 8; /* 64-bit pointer */
  634. /*
  635. * Take the 64-bit addressing hole in to account.
  636. */
  637. addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1);
  638. return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
  639. } else {
  640. int bd_entry_size = 4; /* 32-bit pointer */
  641. /*
  642. * 32-bit has no hole so this case needs no mask
  643. */
  644. return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
  645. }
  646. /*
  647. * The two return calls above are exact copies. If we
  648. * pull out a single copy and put it in here, gcc won't
  649. * realize that we're doing a power-of-2 divide and use
  650. * shifts. It uses a real divide. If we put them up
  651. * there, it manages to figure it out (gcc 4.8.3).
  652. */
  653. }
  654. static int unmap_entire_bt(struct mm_struct *mm,
  655. long __user *bd_entry, unsigned long bt_addr)
  656. {
  657. unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
  658. unsigned long uninitialized_var(actual_old_val);
  659. int ret;
  660. while (1) {
  661. int need_write = 1;
  662. unsigned long cleared_bd_entry = 0;
  663. pagefault_disable();
  664. ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val,
  665. bd_entry, expected_old_val, cleared_bd_entry);
  666. pagefault_enable();
  667. if (!ret)
  668. break;
  669. if (ret == -EFAULT)
  670. ret = mpx_resolve_fault(bd_entry, need_write);
  671. /*
  672. * If we could not resolve the fault, consider it
  673. * userspace's fault and error out.
  674. */
  675. if (ret)
  676. return ret;
  677. }
  678. /*
  679. * The cmpxchg was performed, check the results.
  680. */
  681. if (actual_old_val != expected_old_val) {
  682. /*
  683. * Someone else raced with us to unmap the table.
  684. * That is OK, since we were both trying to do
  685. * the same thing. Declare success.
  686. */
  687. if (!actual_old_val)
  688. return 0;
  689. /*
  690. * Something messed with the bounds directory
  691. * entry. We hold mmap_sem for read or write
  692. * here, so it could not be a _new_ bounds table
  693. * that someone just allocated. Something is
  694. * wrong, so pass up the error and SIGSEGV.
  695. */
  696. return -EINVAL;
  697. }
  698. /*
  699. * Note, we are likely being called under do_munmap() already. To
  700. * avoid recursion, do_munmap() will check whether it comes
  701. * from one bounds table through VM_MPX flag.
  702. */
  703. return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm), NULL);
  704. }
  705. static int try_unmap_single_bt(struct mm_struct *mm,
  706. unsigned long start, unsigned long end)
  707. {
  708. struct vm_area_struct *next;
  709. struct vm_area_struct *prev;
  710. /*
  711. * "bta" == Bounds Table Area: the area controlled by the
  712. * bounds table that we are unmapping.
  713. */
  714. unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1);
  715. unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm);
  716. unsigned long uninitialized_var(bt_addr);
  717. void __user *bde_vaddr;
  718. int ret;
  719. /*
  720. * We already unlinked the VMAs from the mm's rbtree so 'start'
  721. * is guaranteed to be in a hole. This gets us the first VMA
  722. * before the hole in to 'prev' and the next VMA after the hole
  723. * in to 'next'.
  724. */
  725. next = find_vma_prev(mm, start, &prev);
  726. /*
  727. * Do not count other MPX bounds table VMAs as neighbors.
  728. * Although theoretically possible, we do not allow bounds
  729. * tables for bounds tables so our heads do not explode.
  730. * If we count them as neighbors here, we may end up with
  731. * lots of tables even though we have no actual table
  732. * entries in use.
  733. */
  734. while (next && (next->vm_flags & VM_MPX))
  735. next = next->vm_next;
  736. while (prev && (prev->vm_flags & VM_MPX))
  737. prev = prev->vm_prev;
  738. /*
  739. * We know 'start' and 'end' lie within an area controlled
  740. * by a single bounds table. See if there are any other
  741. * VMAs controlled by that bounds table. If there are not
  742. * then we can "expand" the are we are unmapping to possibly
  743. * cover the entire table.
  744. */
  745. next = find_vma_prev(mm, start, &prev);
  746. if ((!prev || prev->vm_end <= bta_start_vaddr) &&
  747. (!next || next->vm_start >= bta_end_vaddr)) {
  748. /*
  749. * No neighbor VMAs controlled by same bounds
  750. * table. Try to unmap the whole thing
  751. */
  752. start = bta_start_vaddr;
  753. end = bta_end_vaddr;
  754. }
  755. bde_vaddr = mm->context.bd_addr + mpx_get_bd_entry_offset(mm, start);
  756. ret = get_bt_addr(mm, bde_vaddr, &bt_addr);
  757. /*
  758. * No bounds table there, so nothing to unmap.
  759. */
  760. if (ret == -ENOENT) {
  761. ret = 0;
  762. return 0;
  763. }
  764. if (ret)
  765. return ret;
  766. /*
  767. * We are unmapping an entire table. Either because the
  768. * unmap that started this whole process was large enough
  769. * to cover an entire table, or that the unmap was small
  770. * but was the area covered by a bounds table.
  771. */
  772. if ((start == bta_start_vaddr) &&
  773. (end == bta_end_vaddr))
  774. return unmap_entire_bt(mm, bde_vaddr, bt_addr);
  775. return zap_bt_entries_mapping(mm, bt_addr, start, end);
  776. }
  777. static int mpx_unmap_tables(struct mm_struct *mm,
  778. unsigned long start, unsigned long end)
  779. {
  780. unsigned long one_unmap_start;
  781. trace_mpx_unmap_search(start, end);
  782. one_unmap_start = start;
  783. while (one_unmap_start < end) {
  784. int ret;
  785. unsigned long next_unmap_start = ALIGN(one_unmap_start+1,
  786. bd_entry_virt_space(mm));
  787. unsigned long one_unmap_end = end;
  788. /*
  789. * if the end is beyond the current bounds table,
  790. * move it back so we only deal with a single one
  791. * at a time
  792. */
  793. if (one_unmap_end > next_unmap_start)
  794. one_unmap_end = next_unmap_start;
  795. ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end);
  796. if (ret)
  797. return ret;
  798. one_unmap_start = next_unmap_start;
  799. }
  800. return 0;
  801. }
  802. /*
  803. * Free unused bounds tables covered in a virtual address region being
  804. * munmap()ed. Assume end > start.
  805. *
  806. * This function will be called by do_munmap(), and the VMAs covering
  807. * the virtual address region start...end have already been split if
  808. * necessary, and the 'vma' is the first vma in this range (start -> end).
  809. */
  810. void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
  811. unsigned long start, unsigned long end)
  812. {
  813. int ret;
  814. /*
  815. * Refuse to do anything unless userspace has asked
  816. * the kernel to help manage the bounds tables,
  817. */
  818. if (!kernel_managing_mpx_tables(current->mm))
  819. return;
  820. /*
  821. * This will look across the entire 'start -> end' range,
  822. * and find all of the non-VM_MPX VMAs.
  823. *
  824. * To avoid recursion, if a VM_MPX vma is found in the range
  825. * (start->end), we will not continue follow-up work. This
  826. * recursion represents having bounds tables for bounds tables,
  827. * which should not occur normally. Being strict about it here
  828. * helps ensure that we do not have an exploitable stack overflow.
  829. */
  830. do {
  831. if (vma->vm_flags & VM_MPX)
  832. return;
  833. vma = vma->vm_next;
  834. } while (vma && vma->vm_start < end);
  835. ret = mpx_unmap_tables(mm, start, end);
  836. if (ret)
  837. force_sig(SIGSEGV, current);
  838. }
  839. /* MPX cannot handle addresses above 47 bits yet. */
  840. unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len,
  841. unsigned long flags)
  842. {
  843. if (!kernel_managing_mpx_tables(current->mm))
  844. return addr;
  845. if (addr + len <= DEFAULT_MAP_WINDOW)
  846. return addr;
  847. if (flags & MAP_FIXED)
  848. return -ENOMEM;
  849. /*
  850. * Requested len is larger than the whole area we're allowed to map in.
  851. * Resetting hinting address wouldn't do much good -- fail early.
  852. */
  853. if (len > DEFAULT_MAP_WINDOW)
  854. return -ENOMEM;
  855. /* Look for unmap area within DEFAULT_MAP_WINDOW */
  856. return 0;
  857. }