mpx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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))
  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))
  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))
  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 mpx_bndreg_state *bndregs;
  215. const struct mpx_bndreg *bndreg;
  216. siginfo_t *info = NULL;
  217. struct insn insn;
  218. uint8_t bndregno;
  219. int err;
  220. err = mpx_insn_decode(&insn, regs);
  221. if (err)
  222. goto err_out;
  223. /*
  224. * We know at this point that we are only dealing with
  225. * MPX instructions.
  226. */
  227. insn_get_modrm(&insn);
  228. bndregno = X86_MODRM_REG(insn.modrm.value);
  229. if (bndregno > 3) {
  230. err = -EINVAL;
  231. goto err_out;
  232. }
  233. /* get bndregs field from current task's xsave area */
  234. bndregs = get_xsave_field_ptr(XFEATURE_MASK_BNDREGS);
  235. if (!bndregs) {
  236. err = -EINVAL;
  237. goto err_out;
  238. }
  239. /* now go select the individual register in the set of 4 */
  240. bndreg = &bndregs->bndreg[bndregno];
  241. info = kzalloc(sizeof(*info), GFP_KERNEL);
  242. if (!info) {
  243. err = -ENOMEM;
  244. goto err_out;
  245. }
  246. /*
  247. * The registers are always 64-bit, but the upper 32
  248. * bits are ignored in 32-bit mode. Also, note that the
  249. * upper bounds are architecturally represented in 1's
  250. * complement form.
  251. *
  252. * The 'unsigned long' cast is because the compiler
  253. * complains when casting from integers to different-size
  254. * pointers.
  255. */
  256. info->si_lower = (void __user *)(unsigned long)bndreg->lower_bound;
  257. info->si_upper = (void __user *)(unsigned long)~bndreg->upper_bound;
  258. info->si_addr_lsb = 0;
  259. info->si_signo = SIGSEGV;
  260. info->si_errno = 0;
  261. info->si_code = SEGV_BNDERR;
  262. info->si_addr = mpx_get_addr_ref(&insn, regs);
  263. /*
  264. * We were not able to extract an address from the instruction,
  265. * probably because there was something invalid in it.
  266. */
  267. if (info->si_addr == (void __user *)-1) {
  268. err = -EINVAL;
  269. goto err_out;
  270. }
  271. trace_mpx_bounds_register_exception(info->si_addr, bndreg);
  272. return info;
  273. err_out:
  274. /* info might be NULL, but kfree() handles that */
  275. kfree(info);
  276. return ERR_PTR(err);
  277. }
  278. static __user void *mpx_get_bounds_dir(void)
  279. {
  280. const struct mpx_bndcsr *bndcsr;
  281. if (!cpu_feature_enabled(X86_FEATURE_MPX))
  282. return MPX_INVALID_BOUNDS_DIR;
  283. /*
  284. * The bounds directory pointer is stored in a register
  285. * only accessible if we first do an xsave.
  286. */
  287. bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
  288. if (!bndcsr)
  289. return MPX_INVALID_BOUNDS_DIR;
  290. /*
  291. * Make sure the register looks valid by checking the
  292. * enable bit.
  293. */
  294. if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
  295. return MPX_INVALID_BOUNDS_DIR;
  296. /*
  297. * Lastly, mask off the low bits used for configuration
  298. * flags, and return the address of the bounds table.
  299. */
  300. return (void __user *)(unsigned long)
  301. (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
  302. }
  303. int mpx_enable_management(void)
  304. {
  305. void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
  306. struct mm_struct *mm = current->mm;
  307. int ret = 0;
  308. /*
  309. * runtime in the userspace will be responsible for allocation of
  310. * the bounds directory. Then, it will save the base of the bounds
  311. * directory into XSAVE/XRSTOR Save Area and enable MPX through
  312. * XRSTOR instruction.
  313. *
  314. * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is
  315. * expected to be relatively expensive. Storing the bounds
  316. * directory here means that we do not have to do xsave in the
  317. * unmap path; we can just use mm->bd_addr instead.
  318. */
  319. bd_base = mpx_get_bounds_dir();
  320. down_write(&mm->mmap_sem);
  321. mm->bd_addr = bd_base;
  322. if (mm->bd_addr == MPX_INVALID_BOUNDS_DIR)
  323. ret = -ENXIO;
  324. up_write(&mm->mmap_sem);
  325. return ret;
  326. }
  327. int mpx_disable_management(void)
  328. {
  329. struct mm_struct *mm = current->mm;
  330. if (!cpu_feature_enabled(X86_FEATURE_MPX))
  331. return -ENXIO;
  332. down_write(&mm->mmap_sem);
  333. mm->bd_addr = MPX_INVALID_BOUNDS_DIR;
  334. up_write(&mm->mmap_sem);
  335. return 0;
  336. }
  337. static int mpx_cmpxchg_bd_entry(struct mm_struct *mm,
  338. unsigned long *curval,
  339. unsigned long __user *addr,
  340. unsigned long old_val, unsigned long new_val)
  341. {
  342. int ret;
  343. /*
  344. * user_atomic_cmpxchg_inatomic() actually uses sizeof()
  345. * the pointer that we pass to it to figure out how much
  346. * data to cmpxchg. We have to be careful here not to
  347. * pass a pointer to a 64-bit data type when we only want
  348. * a 32-bit copy.
  349. */
  350. if (is_64bit_mm(mm)) {
  351. ret = user_atomic_cmpxchg_inatomic(curval,
  352. addr, old_val, new_val);
  353. } else {
  354. u32 uninitialized_var(curval_32);
  355. u32 old_val_32 = old_val;
  356. u32 new_val_32 = new_val;
  357. u32 __user *addr_32 = (u32 __user *)addr;
  358. ret = user_atomic_cmpxchg_inatomic(&curval_32,
  359. addr_32, old_val_32, new_val_32);
  360. *curval = curval_32;
  361. }
  362. return ret;
  363. }
  364. /*
  365. * With 32-bit mode, a bounds directory is 4MB, and the size of each
  366. * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB,
  367. * and the size of each bounds table is 4MB.
  368. */
  369. static int allocate_bt(struct mm_struct *mm, long __user *bd_entry)
  370. {
  371. unsigned long expected_old_val = 0;
  372. unsigned long actual_old_val = 0;
  373. unsigned long bt_addr;
  374. unsigned long bd_new_entry;
  375. int ret = 0;
  376. /*
  377. * Carve the virtual space out of userspace for the new
  378. * bounds table:
  379. */
  380. bt_addr = mpx_mmap(mpx_bt_size_bytes(mm));
  381. if (IS_ERR((void *)bt_addr))
  382. return PTR_ERR((void *)bt_addr);
  383. /*
  384. * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
  385. */
  386. bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
  387. /*
  388. * Go poke the address of the new bounds table in to the
  389. * bounds directory entry out in userspace memory. Note:
  390. * we may race with another CPU instantiating the same table.
  391. * In that case the cmpxchg will see an unexpected
  392. * 'actual_old_val'.
  393. *
  394. * This can fault, but that's OK because we do not hold
  395. * mmap_sem at this point, unlike some of the other part
  396. * of the MPX code that have to pagefault_disable().
  397. */
  398. ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry,
  399. expected_old_val, bd_new_entry);
  400. if (ret)
  401. goto out_unmap;
  402. /*
  403. * The user_atomic_cmpxchg_inatomic() will only return nonzero
  404. * for faults, *not* if the cmpxchg itself fails. Now we must
  405. * verify that the cmpxchg itself completed successfully.
  406. */
  407. /*
  408. * We expected an empty 'expected_old_val', but instead found
  409. * an apparently valid entry. Assume we raced with another
  410. * thread to instantiate this table and desclare succecss.
  411. */
  412. if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
  413. ret = 0;
  414. goto out_unmap;
  415. }
  416. /*
  417. * We found a non-empty bd_entry but it did not have the
  418. * VALID_FLAG set. Return an error which will result in
  419. * a SEGV since this probably means that somebody scribbled
  420. * some invalid data in to a bounds table.
  421. */
  422. if (expected_old_val != actual_old_val) {
  423. ret = -EINVAL;
  424. goto out_unmap;
  425. }
  426. trace_mpx_new_bounds_table(bt_addr);
  427. return 0;
  428. out_unmap:
  429. vm_munmap(bt_addr, mpx_bt_size_bytes(mm));
  430. return ret;
  431. }
  432. /*
  433. * When a BNDSTX instruction attempts to save bounds to a bounds
  434. * table, it will first attempt to look up the table in the
  435. * first-level bounds directory. If it does not find a table in
  436. * the directory, a #BR is generated and we get here in order to
  437. * allocate a new table.
  438. *
  439. * With 32-bit mode, the size of BD is 4MB, and the size of each
  440. * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
  441. * and the size of each bound table is 4MB.
  442. */
  443. static int do_mpx_bt_fault(void)
  444. {
  445. unsigned long bd_entry, bd_base;
  446. const struct mpx_bndcsr *bndcsr;
  447. struct mm_struct *mm = current->mm;
  448. bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
  449. if (!bndcsr)
  450. return -EINVAL;
  451. /*
  452. * Mask off the preserve and enable bits
  453. */
  454. bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
  455. /*
  456. * The hardware provides the address of the missing or invalid
  457. * entry via BNDSTATUS, so we don't have to go look it up.
  458. */
  459. bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
  460. /*
  461. * Make sure the directory entry is within where we think
  462. * the directory is.
  463. */
  464. if ((bd_entry < bd_base) ||
  465. (bd_entry >= bd_base + mpx_bd_size_bytes(mm)))
  466. return -EINVAL;
  467. return allocate_bt(mm, (long __user *)bd_entry);
  468. }
  469. int mpx_handle_bd_fault(void)
  470. {
  471. /*
  472. * Userspace never asked us to manage the bounds tables,
  473. * so refuse to help.
  474. */
  475. if (!kernel_managing_mpx_tables(current->mm))
  476. return -EINVAL;
  477. return do_mpx_bt_fault();
  478. }
  479. /*
  480. * A thin wrapper around get_user_pages(). Returns 0 if the
  481. * fault was resolved or -errno if not.
  482. */
  483. static int mpx_resolve_fault(long __user *addr, int write)
  484. {
  485. long gup_ret;
  486. int nr_pages = 1;
  487. gup_ret = get_user_pages((unsigned long)addr, nr_pages,
  488. write ? FOLL_WRITE : 0, NULL, NULL);
  489. /*
  490. * get_user_pages() returns number of pages gotten.
  491. * 0 means we failed to fault in and get anything,
  492. * probably because 'addr' is bad.
  493. */
  494. if (!gup_ret)
  495. return -EFAULT;
  496. /* Other error, return it */
  497. if (gup_ret < 0)
  498. return gup_ret;
  499. /* must have gup'd a page and gup_ret>0, success */
  500. return 0;
  501. }
  502. static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm,
  503. unsigned long bd_entry)
  504. {
  505. unsigned long bt_addr = bd_entry;
  506. int align_to_bytes;
  507. /*
  508. * Bit 0 in a bt_entry is always the valid bit.
  509. */
  510. bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG;
  511. /*
  512. * Tables are naturally aligned at 8-byte boundaries
  513. * on 64-bit and 4-byte boundaries on 32-bit. The
  514. * documentation makes it appear that the low bits
  515. * are ignored by the hardware, so we do the same.
  516. */
  517. if (is_64bit_mm(mm))
  518. align_to_bytes = 8;
  519. else
  520. align_to_bytes = 4;
  521. bt_addr &= ~(align_to_bytes-1);
  522. return bt_addr;
  523. }
  524. /*
  525. * We only want to do a 4-byte get_user() on 32-bit. Otherwise,
  526. * we might run off the end of the bounds table if we are on
  527. * a 64-bit kernel and try to get 8 bytes.
  528. */
  529. int get_user_bd_entry(struct mm_struct *mm, unsigned long *bd_entry_ret,
  530. long __user *bd_entry_ptr)
  531. {
  532. u32 bd_entry_32;
  533. int ret;
  534. if (is_64bit_mm(mm))
  535. return get_user(*bd_entry_ret, bd_entry_ptr);
  536. /*
  537. * Note that get_user() uses the type of the *pointer* to
  538. * establish the size of the get, not the destination.
  539. */
  540. ret = get_user(bd_entry_32, (u32 __user *)bd_entry_ptr);
  541. *bd_entry_ret = bd_entry_32;
  542. return ret;
  543. }
  544. /*
  545. * Get the base of bounds tables pointed by specific bounds
  546. * directory entry.
  547. */
  548. static int get_bt_addr(struct mm_struct *mm,
  549. long __user *bd_entry_ptr,
  550. unsigned long *bt_addr_result)
  551. {
  552. int ret;
  553. int valid_bit;
  554. unsigned long bd_entry;
  555. unsigned long bt_addr;
  556. if (!access_ok(VERIFY_READ, (bd_entry_ptr), sizeof(*bd_entry_ptr)))
  557. return -EFAULT;
  558. while (1) {
  559. int need_write = 0;
  560. pagefault_disable();
  561. ret = get_user_bd_entry(mm, &bd_entry, bd_entry_ptr);
  562. pagefault_enable();
  563. if (!ret)
  564. break;
  565. if (ret == -EFAULT)
  566. ret = mpx_resolve_fault(bd_entry_ptr, need_write);
  567. /*
  568. * If we could not resolve the fault, consider it
  569. * userspace's fault and error out.
  570. */
  571. if (ret)
  572. return ret;
  573. }
  574. valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG;
  575. bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry);
  576. /*
  577. * When the kernel is managing bounds tables, a bounds directory
  578. * entry will either have a valid address (plus the valid bit)
  579. * *OR* be completely empty. If we see a !valid entry *and* some
  580. * data in the address field, we know something is wrong. This
  581. * -EINVAL return will cause a SIGSEGV.
  582. */
  583. if (!valid_bit && bt_addr)
  584. return -EINVAL;
  585. /*
  586. * Do we have an completely zeroed bt entry? That is OK. It
  587. * just means there was no bounds table for this memory. Make
  588. * sure to distinguish this from -EINVAL, which will cause
  589. * a SEGV.
  590. */
  591. if (!valid_bit)
  592. return -ENOENT;
  593. *bt_addr_result = bt_addr;
  594. return 0;
  595. }
  596. static inline int bt_entry_size_bytes(struct mm_struct *mm)
  597. {
  598. if (is_64bit_mm(mm))
  599. return MPX_BT_ENTRY_BYTES_64;
  600. else
  601. return MPX_BT_ENTRY_BYTES_32;
  602. }
  603. /*
  604. * Take a virtual address and turns it in to the offset in bytes
  605. * inside of the bounds table where the bounds table entry
  606. * controlling 'addr' can be found.
  607. */
  608. static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm,
  609. unsigned long addr)
  610. {
  611. unsigned long bt_table_nr_entries;
  612. unsigned long offset = addr;
  613. if (is_64bit_mm(mm)) {
  614. /* Bottom 3 bits are ignored on 64-bit */
  615. offset >>= 3;
  616. bt_table_nr_entries = MPX_BT_NR_ENTRIES_64;
  617. } else {
  618. /* Bottom 2 bits are ignored on 32-bit */
  619. offset >>= 2;
  620. bt_table_nr_entries = MPX_BT_NR_ENTRIES_32;
  621. }
  622. /*
  623. * We know the size of the table in to which we are
  624. * indexing, and we have eliminated all the low bits
  625. * which are ignored for indexing.
  626. *
  627. * Mask out all the high bits which we do not need
  628. * to index in to the table. Note that the tables
  629. * are always powers of two so this gives us a proper
  630. * mask.
  631. */
  632. offset &= (bt_table_nr_entries-1);
  633. /*
  634. * We now have an entry offset in terms of *entries* in
  635. * the table. We need to scale it back up to bytes.
  636. */
  637. offset *= bt_entry_size_bytes(mm);
  638. return offset;
  639. }
  640. /*
  641. * How much virtual address space does a single bounds
  642. * directory entry cover?
  643. *
  644. * Note, we need a long long because 4GB doesn't fit in
  645. * to a long on 32-bit.
  646. */
  647. static inline unsigned long bd_entry_virt_space(struct mm_struct *mm)
  648. {
  649. unsigned long long virt_space;
  650. unsigned long long GB = (1ULL << 30);
  651. /*
  652. * This covers 32-bit emulation as well as 32-bit kernels
  653. * running on 64-bit hardware.
  654. */
  655. if (!is_64bit_mm(mm))
  656. return (4ULL * GB) / MPX_BD_NR_ENTRIES_32;
  657. /*
  658. * 'x86_virt_bits' returns what the hardware is capable
  659. * of, and returns the full >32-bit address space when
  660. * running 32-bit kernels on 64-bit hardware.
  661. */
  662. virt_space = (1ULL << boot_cpu_data.x86_virt_bits);
  663. return virt_space / MPX_BD_NR_ENTRIES_64;
  664. }
  665. /*
  666. * Free the backing physical pages of bounds table 'bt_addr'.
  667. * Assume start...end is within that bounds table.
  668. */
  669. static noinline int zap_bt_entries_mapping(struct mm_struct *mm,
  670. unsigned long bt_addr,
  671. unsigned long start_mapping, unsigned long end_mapping)
  672. {
  673. struct vm_area_struct *vma;
  674. unsigned long addr, len;
  675. unsigned long start;
  676. unsigned long end;
  677. /*
  678. * if we 'end' on a boundary, the offset will be 0 which
  679. * is not what we want. Back it up a byte to get the
  680. * last bt entry. Then once we have the entry itself,
  681. * move 'end' back up by the table entry size.
  682. */
  683. start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping);
  684. end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1);
  685. /*
  686. * Move end back up by one entry. Among other things
  687. * this ensures that it remains page-aligned and does
  688. * not screw up zap_page_range()
  689. */
  690. end += bt_entry_size_bytes(mm);
  691. /*
  692. * Find the first overlapping vma. If vma->vm_start > start, there
  693. * will be a hole in the bounds table. This -EINVAL return will
  694. * cause a SIGSEGV.
  695. */
  696. vma = find_vma(mm, start);
  697. if (!vma || vma->vm_start > start)
  698. return -EINVAL;
  699. /*
  700. * A NUMA policy on a VM_MPX VMA could cause this bounds table to
  701. * be split. So we need to look across the entire 'start -> end'
  702. * range of this bounds table, find all of the VM_MPX VMAs, and
  703. * zap only those.
  704. */
  705. addr = start;
  706. while (vma && vma->vm_start < end) {
  707. /*
  708. * We followed a bounds directory entry down
  709. * here. If we find a non-MPX VMA, that's bad,
  710. * so stop immediately and return an error. This
  711. * probably results in a SIGSEGV.
  712. */
  713. if (!(vma->vm_flags & VM_MPX))
  714. return -EINVAL;
  715. len = min(vma->vm_end, end) - addr;
  716. zap_page_range(vma, addr, len, NULL);
  717. trace_mpx_unmap_zap(addr, addr+len);
  718. vma = vma->vm_next;
  719. addr = vma->vm_start;
  720. }
  721. return 0;
  722. }
  723. static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm,
  724. unsigned long addr)
  725. {
  726. /*
  727. * There are several ways to derive the bd offsets. We
  728. * use the following approach here:
  729. * 1. We know the size of the virtual address space
  730. * 2. We know the number of entries in a bounds table
  731. * 3. We know that each entry covers a fixed amount of
  732. * virtual address space.
  733. * So, we can just divide the virtual address by the
  734. * virtual space used by one entry to determine which
  735. * entry "controls" the given virtual address.
  736. */
  737. if (is_64bit_mm(mm)) {
  738. int bd_entry_size = 8; /* 64-bit pointer */
  739. /*
  740. * Take the 64-bit addressing hole in to account.
  741. */
  742. addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1);
  743. return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
  744. } else {
  745. int bd_entry_size = 4; /* 32-bit pointer */
  746. /*
  747. * 32-bit has no hole so this case needs no mask
  748. */
  749. return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
  750. }
  751. /*
  752. * The two return calls above are exact copies. If we
  753. * pull out a single copy and put it in here, gcc won't
  754. * realize that we're doing a power-of-2 divide and use
  755. * shifts. It uses a real divide. If we put them up
  756. * there, it manages to figure it out (gcc 4.8.3).
  757. */
  758. }
  759. static int unmap_entire_bt(struct mm_struct *mm,
  760. long __user *bd_entry, unsigned long bt_addr)
  761. {
  762. unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
  763. unsigned long uninitialized_var(actual_old_val);
  764. int ret;
  765. while (1) {
  766. int need_write = 1;
  767. unsigned long cleared_bd_entry = 0;
  768. pagefault_disable();
  769. ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val,
  770. bd_entry, expected_old_val, cleared_bd_entry);
  771. pagefault_enable();
  772. if (!ret)
  773. break;
  774. if (ret == -EFAULT)
  775. ret = mpx_resolve_fault(bd_entry, need_write);
  776. /*
  777. * If we could not resolve the fault, consider it
  778. * userspace's fault and error out.
  779. */
  780. if (ret)
  781. return ret;
  782. }
  783. /*
  784. * The cmpxchg was performed, check the results.
  785. */
  786. if (actual_old_val != expected_old_val) {
  787. /*
  788. * Someone else raced with us to unmap the table.
  789. * That is OK, since we were both trying to do
  790. * the same thing. Declare success.
  791. */
  792. if (!actual_old_val)
  793. return 0;
  794. /*
  795. * Something messed with the bounds directory
  796. * entry. We hold mmap_sem for read or write
  797. * here, so it could not be a _new_ bounds table
  798. * that someone just allocated. Something is
  799. * wrong, so pass up the error and SIGSEGV.
  800. */
  801. return -EINVAL;
  802. }
  803. /*
  804. * Note, we are likely being called under do_munmap() already. To
  805. * avoid recursion, do_munmap() will check whether it comes
  806. * from one bounds table through VM_MPX flag.
  807. */
  808. return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm));
  809. }
  810. static int try_unmap_single_bt(struct mm_struct *mm,
  811. unsigned long start, unsigned long end)
  812. {
  813. struct vm_area_struct *next;
  814. struct vm_area_struct *prev;
  815. /*
  816. * "bta" == Bounds Table Area: the area controlled by the
  817. * bounds table that we are unmapping.
  818. */
  819. unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1);
  820. unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm);
  821. unsigned long uninitialized_var(bt_addr);
  822. void __user *bde_vaddr;
  823. int ret;
  824. /*
  825. * We already unlinked the VMAs from the mm's rbtree so 'start'
  826. * is guaranteed to be in a hole. This gets us the first VMA
  827. * before the hole in to 'prev' and the next VMA after the hole
  828. * in to 'next'.
  829. */
  830. next = find_vma_prev(mm, start, &prev);
  831. /*
  832. * Do not count other MPX bounds table VMAs as neighbors.
  833. * Although theoretically possible, we do not allow bounds
  834. * tables for bounds tables so our heads do not explode.
  835. * If we count them as neighbors here, we may end up with
  836. * lots of tables even though we have no actual table
  837. * entries in use.
  838. */
  839. while (next && (next->vm_flags & VM_MPX))
  840. next = next->vm_next;
  841. while (prev && (prev->vm_flags & VM_MPX))
  842. prev = prev->vm_prev;
  843. /*
  844. * We know 'start' and 'end' lie within an area controlled
  845. * by a single bounds table. See if there are any other
  846. * VMAs controlled by that bounds table. If there are not
  847. * then we can "expand" the are we are unmapping to possibly
  848. * cover the entire table.
  849. */
  850. next = find_vma_prev(mm, start, &prev);
  851. if ((!prev || prev->vm_end <= bta_start_vaddr) &&
  852. (!next || next->vm_start >= bta_end_vaddr)) {
  853. /*
  854. * No neighbor VMAs controlled by same bounds
  855. * table. Try to unmap the whole thing
  856. */
  857. start = bta_start_vaddr;
  858. end = bta_end_vaddr;
  859. }
  860. bde_vaddr = mm->bd_addr + mpx_get_bd_entry_offset(mm, start);
  861. ret = get_bt_addr(mm, bde_vaddr, &bt_addr);
  862. /*
  863. * No bounds table there, so nothing to unmap.
  864. */
  865. if (ret == -ENOENT) {
  866. ret = 0;
  867. return 0;
  868. }
  869. if (ret)
  870. return ret;
  871. /*
  872. * We are unmapping an entire table. Either because the
  873. * unmap that started this whole process was large enough
  874. * to cover an entire table, or that the unmap was small
  875. * but was the area covered by a bounds table.
  876. */
  877. if ((start == bta_start_vaddr) &&
  878. (end == bta_end_vaddr))
  879. return unmap_entire_bt(mm, bde_vaddr, bt_addr);
  880. return zap_bt_entries_mapping(mm, bt_addr, start, end);
  881. }
  882. static int mpx_unmap_tables(struct mm_struct *mm,
  883. unsigned long start, unsigned long end)
  884. {
  885. unsigned long one_unmap_start;
  886. trace_mpx_unmap_search(start, end);
  887. one_unmap_start = start;
  888. while (one_unmap_start < end) {
  889. int ret;
  890. unsigned long next_unmap_start = ALIGN(one_unmap_start+1,
  891. bd_entry_virt_space(mm));
  892. unsigned long one_unmap_end = end;
  893. /*
  894. * if the end is beyond the current bounds table,
  895. * move it back so we only deal with a single one
  896. * at a time
  897. */
  898. if (one_unmap_end > next_unmap_start)
  899. one_unmap_end = next_unmap_start;
  900. ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end);
  901. if (ret)
  902. return ret;
  903. one_unmap_start = next_unmap_start;
  904. }
  905. return 0;
  906. }
  907. /*
  908. * Free unused bounds tables covered in a virtual address region being
  909. * munmap()ed. Assume end > start.
  910. *
  911. * This function will be called by do_munmap(), and the VMAs covering
  912. * the virtual address region start...end have already been split if
  913. * necessary, and the 'vma' is the first vma in this range (start -> end).
  914. */
  915. void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
  916. unsigned long start, unsigned long end)
  917. {
  918. int ret;
  919. /*
  920. * Refuse to do anything unless userspace has asked
  921. * the kernel to help manage the bounds tables,
  922. */
  923. if (!kernel_managing_mpx_tables(current->mm))
  924. return;
  925. /*
  926. * This will look across the entire 'start -> end' range,
  927. * and find all of the non-VM_MPX VMAs.
  928. *
  929. * To avoid recursion, if a VM_MPX vma is found in the range
  930. * (start->end), we will not continue follow-up work. This
  931. * recursion represents having bounds tables for bounds tables,
  932. * which should not occur normally. Being strict about it here
  933. * helps ensure that we do not have an exploitable stack overflow.
  934. */
  935. do {
  936. if (vma->vm_flags & VM_MPX)
  937. return;
  938. vma = vma->vm_next;
  939. } while (vma && vma->vm_start < end);
  940. ret = mpx_unmap_tables(mm, start, end);
  941. if (ret)
  942. force_sig(SIGSEGV, current);
  943. }