mpx.c 27 KB

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