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