ldt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
  4. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  5. * Copyright (C) 2002 Andi Kleen
  6. *
  7. * This handles calls from both 32bit and 64bit mode.
  8. *
  9. * Lock order:
  10. * contex.ldt_usr_sem
  11. * mmap_sem
  12. * context.lock
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/gfp.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/ldt.h>
  25. #include <asm/tlb.h>
  26. #include <asm/desc.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/syscalls.h>
  29. static void refresh_ldt_segments(void)
  30. {
  31. #ifdef CONFIG_X86_64
  32. unsigned short sel;
  33. /*
  34. * Make sure that the cached DS and ES descriptors match the updated
  35. * LDT.
  36. */
  37. savesegment(ds, sel);
  38. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
  39. loadsegment(ds, sel);
  40. savesegment(es, sel);
  41. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
  42. loadsegment(es, sel);
  43. #endif
  44. }
  45. /* context.lock is held by the task which issued the smp function call */
  46. static void flush_ldt(void *__mm)
  47. {
  48. struct mm_struct *mm = __mm;
  49. if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
  50. return;
  51. load_mm_ldt(mm);
  52. refresh_ldt_segments();
  53. }
  54. /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
  55. static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
  56. {
  57. struct ldt_struct *new_ldt;
  58. unsigned int alloc_size;
  59. if (num_entries > LDT_ENTRIES)
  60. return NULL;
  61. new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
  62. if (!new_ldt)
  63. return NULL;
  64. BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
  65. alloc_size = num_entries * LDT_ENTRY_SIZE;
  66. /*
  67. * Xen is very picky: it requires a page-aligned LDT that has no
  68. * trailing nonzero bytes in any page that contains LDT descriptors.
  69. * Keep it simple: zero the whole allocation and never allocate less
  70. * than PAGE_SIZE.
  71. */
  72. if (alloc_size > PAGE_SIZE)
  73. new_ldt->entries = vzalloc(alloc_size);
  74. else
  75. new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
  76. if (!new_ldt->entries) {
  77. kfree(new_ldt);
  78. return NULL;
  79. }
  80. /* The new LDT isn't aliased for PTI yet. */
  81. new_ldt->slot = -1;
  82. new_ldt->nr_entries = num_entries;
  83. return new_ldt;
  84. }
  85. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  86. static void do_sanity_check(struct mm_struct *mm,
  87. bool had_kernel_mapping,
  88. bool had_user_mapping)
  89. {
  90. if (mm->context.ldt) {
  91. /*
  92. * We already had an LDT. The top-level entry should already
  93. * have been allocated and synchronized with the usermode
  94. * tables.
  95. */
  96. WARN_ON(!had_kernel_mapping);
  97. if (static_cpu_has(X86_FEATURE_PTI))
  98. WARN_ON(!had_user_mapping);
  99. } else {
  100. /*
  101. * This is the first time we're mapping an LDT for this process.
  102. * Sync the pgd to the usermode tables.
  103. */
  104. WARN_ON(had_kernel_mapping);
  105. if (static_cpu_has(X86_FEATURE_PTI))
  106. WARN_ON(had_user_mapping);
  107. }
  108. }
  109. #ifdef CONFIG_X86_PAE
  110. static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va)
  111. {
  112. p4d_t *p4d;
  113. pud_t *pud;
  114. if (pgd->pgd == 0)
  115. return NULL;
  116. p4d = p4d_offset(pgd, va);
  117. if (p4d_none(*p4d))
  118. return NULL;
  119. pud = pud_offset(p4d, va);
  120. if (pud_none(*pud))
  121. return NULL;
  122. return pmd_offset(pud, va);
  123. }
  124. static void map_ldt_struct_to_user(struct mm_struct *mm)
  125. {
  126. pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
  127. pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
  128. pmd_t *k_pmd, *u_pmd;
  129. k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
  130. u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
  131. if (static_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
  132. set_pmd(u_pmd, *k_pmd);
  133. }
  134. static void sanity_check_ldt_mapping(struct mm_struct *mm)
  135. {
  136. pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
  137. pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
  138. bool had_kernel, had_user;
  139. pmd_t *k_pmd, *u_pmd;
  140. k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
  141. u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
  142. had_kernel = (k_pmd->pmd != 0);
  143. had_user = (u_pmd->pmd != 0);
  144. do_sanity_check(mm, had_kernel, had_user);
  145. }
  146. #else /* !CONFIG_X86_PAE */
  147. static void map_ldt_struct_to_user(struct mm_struct *mm)
  148. {
  149. pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
  150. if (static_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
  151. set_pgd(kernel_to_user_pgdp(pgd), *pgd);
  152. }
  153. static void sanity_check_ldt_mapping(struct mm_struct *mm)
  154. {
  155. pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
  156. bool had_kernel = (pgd->pgd != 0);
  157. bool had_user = (kernel_to_user_pgdp(pgd)->pgd != 0);
  158. do_sanity_check(mm, had_kernel, had_user);
  159. }
  160. #endif /* CONFIG_X86_PAE */
  161. /*
  162. * If PTI is enabled, this maps the LDT into the kernelmode and
  163. * usermode tables for the given mm.
  164. */
  165. static int
  166. map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
  167. {
  168. unsigned long va;
  169. bool is_vmalloc;
  170. spinlock_t *ptl;
  171. int i, nr_pages;
  172. if (!static_cpu_has(X86_FEATURE_PTI))
  173. return 0;
  174. /*
  175. * Any given ldt_struct should have map_ldt_struct() called at most
  176. * once.
  177. */
  178. WARN_ON(ldt->slot != -1);
  179. /* Check if the current mappings are sane */
  180. sanity_check_ldt_mapping(mm);
  181. is_vmalloc = is_vmalloc_addr(ldt->entries);
  182. nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
  183. for (i = 0; i < nr_pages; i++) {
  184. unsigned long offset = i << PAGE_SHIFT;
  185. const void *src = (char *)ldt->entries + offset;
  186. unsigned long pfn;
  187. pgprot_t pte_prot;
  188. pte_t pte, *ptep;
  189. va = (unsigned long)ldt_slot_va(slot) + offset;
  190. pfn = is_vmalloc ? vmalloc_to_pfn(src) :
  191. page_to_pfn(virt_to_page(src));
  192. /*
  193. * Treat the PTI LDT range as a *userspace* range.
  194. * get_locked_pte() will allocate all needed pagetables
  195. * and account for them in this mm.
  196. */
  197. ptep = get_locked_pte(mm, va, &ptl);
  198. if (!ptep)
  199. return -ENOMEM;
  200. /*
  201. * Map it RO so the easy to find address is not a primary
  202. * target via some kernel interface which misses a
  203. * permission check.
  204. */
  205. pte_prot = __pgprot(__PAGE_KERNEL_RO & ~_PAGE_GLOBAL);
  206. /* Filter out unsuppored __PAGE_KERNEL* bits: */
  207. pgprot_val(pte_prot) &= __supported_pte_mask;
  208. pte = pfn_pte(pfn, pte_prot);
  209. set_pte_at(mm, va, ptep, pte);
  210. pte_unmap_unlock(ptep, ptl);
  211. }
  212. /* Propagate LDT mapping to the user page-table */
  213. map_ldt_struct_to_user(mm);
  214. ldt->slot = slot;
  215. return 0;
  216. }
  217. static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
  218. {
  219. unsigned long va;
  220. int i, nr_pages;
  221. if (!ldt)
  222. return;
  223. /* LDT map/unmap is only required for PTI */
  224. if (!static_cpu_has(X86_FEATURE_PTI))
  225. return;
  226. nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
  227. for (i = 0; i < nr_pages; i++) {
  228. unsigned long offset = i << PAGE_SHIFT;
  229. spinlock_t *ptl;
  230. pte_t *ptep;
  231. va = (unsigned long)ldt_slot_va(ldt->slot) + offset;
  232. ptep = get_locked_pte(mm, va, &ptl);
  233. pte_clear(mm, va, ptep);
  234. pte_unmap_unlock(ptep, ptl);
  235. }
  236. va = (unsigned long)ldt_slot_va(ldt->slot);
  237. flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, 0);
  238. }
  239. #else /* !CONFIG_PAGE_TABLE_ISOLATION */
  240. static int
  241. map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
  242. {
  243. return 0;
  244. }
  245. static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
  246. {
  247. }
  248. #endif /* CONFIG_PAGE_TABLE_ISOLATION */
  249. static void free_ldt_pgtables(struct mm_struct *mm)
  250. {
  251. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  252. struct mmu_gather tlb;
  253. unsigned long start = LDT_BASE_ADDR;
  254. unsigned long end = LDT_END_ADDR;
  255. if (!static_cpu_has(X86_FEATURE_PTI))
  256. return;
  257. tlb_gather_mmu(&tlb, mm, start, end);
  258. free_pgd_range(&tlb, start, end, start, end);
  259. tlb_finish_mmu(&tlb, start, end);
  260. #endif
  261. }
  262. /* After calling this, the LDT is immutable. */
  263. static void finalize_ldt_struct(struct ldt_struct *ldt)
  264. {
  265. paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
  266. }
  267. static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
  268. {
  269. mutex_lock(&mm->context.lock);
  270. /* Synchronizes with READ_ONCE in load_mm_ldt. */
  271. smp_store_release(&mm->context.ldt, ldt);
  272. /* Activate the LDT for all CPUs using currents mm. */
  273. on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);
  274. mutex_unlock(&mm->context.lock);
  275. }
  276. static void free_ldt_struct(struct ldt_struct *ldt)
  277. {
  278. if (likely(!ldt))
  279. return;
  280. paravirt_free_ldt(ldt->entries, ldt->nr_entries);
  281. if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
  282. vfree_atomic(ldt->entries);
  283. else
  284. free_page((unsigned long)ldt->entries);
  285. kfree(ldt);
  286. }
  287. /*
  288. * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
  289. * the new task is not running, so nothing can be installed.
  290. */
  291. int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
  292. {
  293. struct ldt_struct *new_ldt;
  294. int retval = 0;
  295. if (!old_mm)
  296. return 0;
  297. mutex_lock(&old_mm->context.lock);
  298. if (!old_mm->context.ldt)
  299. goto out_unlock;
  300. new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
  301. if (!new_ldt) {
  302. retval = -ENOMEM;
  303. goto out_unlock;
  304. }
  305. memcpy(new_ldt->entries, old_mm->context.ldt->entries,
  306. new_ldt->nr_entries * LDT_ENTRY_SIZE);
  307. finalize_ldt_struct(new_ldt);
  308. retval = map_ldt_struct(mm, new_ldt, 0);
  309. if (retval) {
  310. free_ldt_pgtables(mm);
  311. free_ldt_struct(new_ldt);
  312. goto out_unlock;
  313. }
  314. mm->context.ldt = new_ldt;
  315. out_unlock:
  316. mutex_unlock(&old_mm->context.lock);
  317. return retval;
  318. }
  319. /*
  320. * No need to lock the MM as we are the last user
  321. *
  322. * 64bit: Don't touch the LDT register - we're already in the next thread.
  323. */
  324. void destroy_context_ldt(struct mm_struct *mm)
  325. {
  326. free_ldt_struct(mm->context.ldt);
  327. mm->context.ldt = NULL;
  328. }
  329. void ldt_arch_exit_mmap(struct mm_struct *mm)
  330. {
  331. free_ldt_pgtables(mm);
  332. }
  333. static int read_ldt(void __user *ptr, unsigned long bytecount)
  334. {
  335. struct mm_struct *mm = current->mm;
  336. unsigned long entries_size;
  337. int retval;
  338. down_read(&mm->context.ldt_usr_sem);
  339. if (!mm->context.ldt) {
  340. retval = 0;
  341. goto out_unlock;
  342. }
  343. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  344. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  345. entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
  346. if (entries_size > bytecount)
  347. entries_size = bytecount;
  348. if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
  349. retval = -EFAULT;
  350. goto out_unlock;
  351. }
  352. if (entries_size != bytecount) {
  353. /* Zero-fill the rest and pretend we read bytecount bytes. */
  354. if (clear_user(ptr + entries_size, bytecount - entries_size)) {
  355. retval = -EFAULT;
  356. goto out_unlock;
  357. }
  358. }
  359. retval = bytecount;
  360. out_unlock:
  361. up_read(&mm->context.ldt_usr_sem);
  362. return retval;
  363. }
  364. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  365. {
  366. /* CHECKME: Can we use _one_ random number ? */
  367. #ifdef CONFIG_X86_32
  368. unsigned long size = 5 * sizeof(struct desc_struct);
  369. #else
  370. unsigned long size = 128;
  371. #endif
  372. if (bytecount > size)
  373. bytecount = size;
  374. if (clear_user(ptr, bytecount))
  375. return -EFAULT;
  376. return bytecount;
  377. }
  378. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  379. {
  380. struct mm_struct *mm = current->mm;
  381. struct ldt_struct *new_ldt, *old_ldt;
  382. unsigned int old_nr_entries, new_nr_entries;
  383. struct user_desc ldt_info;
  384. struct desc_struct ldt;
  385. int error;
  386. error = -EINVAL;
  387. if (bytecount != sizeof(ldt_info))
  388. goto out;
  389. error = -EFAULT;
  390. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  391. goto out;
  392. error = -EINVAL;
  393. if (ldt_info.entry_number >= LDT_ENTRIES)
  394. goto out;
  395. if (ldt_info.contents == 3) {
  396. if (oldmode)
  397. goto out;
  398. if (ldt_info.seg_not_present == 0)
  399. goto out;
  400. }
  401. if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
  402. LDT_empty(&ldt_info)) {
  403. /* The user wants to clear the entry. */
  404. memset(&ldt, 0, sizeof(ldt));
  405. } else {
  406. if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
  407. error = -EINVAL;
  408. goto out;
  409. }
  410. fill_ldt(&ldt, &ldt_info);
  411. if (oldmode)
  412. ldt.avl = 0;
  413. }
  414. if (down_write_killable(&mm->context.ldt_usr_sem))
  415. return -EINTR;
  416. old_ldt = mm->context.ldt;
  417. old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
  418. new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
  419. error = -ENOMEM;
  420. new_ldt = alloc_ldt_struct(new_nr_entries);
  421. if (!new_ldt)
  422. goto out_unlock;
  423. if (old_ldt)
  424. memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
  425. new_ldt->entries[ldt_info.entry_number] = ldt;
  426. finalize_ldt_struct(new_ldt);
  427. /*
  428. * If we are using PTI, map the new LDT into the userspace pagetables.
  429. * If there is already an LDT, use the other slot so that other CPUs
  430. * will continue to use the old LDT until install_ldt() switches
  431. * them over to the new LDT.
  432. */
  433. error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
  434. if (error) {
  435. /*
  436. * This only can fail for the first LDT setup. If an LDT is
  437. * already installed then the PTE page is already
  438. * populated. Mop up a half populated page table.
  439. */
  440. if (!WARN_ON_ONCE(old_ldt))
  441. free_ldt_pgtables(mm);
  442. free_ldt_struct(new_ldt);
  443. goto out_unlock;
  444. }
  445. install_ldt(mm, new_ldt);
  446. unmap_ldt_struct(mm, old_ldt);
  447. free_ldt_struct(old_ldt);
  448. error = 0;
  449. out_unlock:
  450. up_write(&mm->context.ldt_usr_sem);
  451. out:
  452. return error;
  453. }
  454. SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
  455. unsigned long , bytecount)
  456. {
  457. int ret = -ENOSYS;
  458. switch (func) {
  459. case 0:
  460. ret = read_ldt(ptr, bytecount);
  461. break;
  462. case 1:
  463. ret = write_ldt(ptr, bytecount, 1);
  464. break;
  465. case 2:
  466. ret = read_default_ldt(ptr, bytecount);
  467. break;
  468. case 0x11:
  469. ret = write_ldt(ptr, bytecount, 0);
  470. break;
  471. }
  472. /*
  473. * The SYSCALL_DEFINE() macros give us an 'unsigned long'
  474. * return type, but tht ABI for sys_modify_ldt() expects
  475. * 'int'. This cast gives us an int-sized value in %rax
  476. * for the return code. The 'unsigned' is necessary so
  477. * the compiler does not try to sign-extend the negative
  478. * return codes into the high half of the register when
  479. * taking the value from int->long.
  480. */
  481. return (unsigned int)ret;
  482. }