vma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2007 Andi Kleen, SUSE Labs.
  4. *
  5. * This contains most of the x86 vDSO kernel-side code.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/err.h>
  9. #include <linux/sched.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/random.h>
  14. #include <linux/elf.h>
  15. #include <linux/cpu.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/time_namespace.h>
  18. #include <asm/pvclock.h>
  19. #include <asm/vgtod.h>
  20. #include <asm/proto.h>
  21. #include <asm/vdso.h>
  22. #include <asm/vvar.h>
  23. #include <asm/tlb.h>
  24. #include <asm/page.h>
  25. #include <asm/desc.h>
  26. #include <asm/cpufeature.h>
  27. #include <clocksource/hyperv_timer.h>
  28. #undef _ASM_X86_VVAR_H
  29. #define EMIT_VVAR(name, offset) \
  30. const size_t name ## _offset = offset;
  31. #include <asm/vvar.h>
  32. struct vdso_data *arch_get_vdso_data(void *vvar_page)
  33. {
  34. return (struct vdso_data *)(vvar_page + _vdso_data_offset);
  35. }
  36. #undef EMIT_VVAR
  37. unsigned int vclocks_used __read_mostly;
  38. #if defined(CONFIG_X86_64)
  39. unsigned int __read_mostly vdso64_enabled = 1;
  40. #endif
  41. void __init init_vdso_image(const struct vdso_image *image)
  42. {
  43. BUG_ON(image->size % PAGE_SIZE != 0);
  44. apply_alternatives((struct alt_instr *)(image->data + image->alt),
  45. (struct alt_instr *)(image->data + image->alt +
  46. image->alt_len));
  47. }
  48. static const struct vm_special_mapping vvar_mapping;
  49. struct linux_binprm;
  50. static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
  51. struct vm_area_struct *vma, struct vm_fault *vmf)
  52. {
  53. const struct vdso_image *image = vma->vm_mm->context.vdso_image;
  54. if (!image || (vmf->pgoff << PAGE_SHIFT) >= image->size)
  55. return VM_FAULT_SIGBUS;
  56. vmf->page = virt_to_page(image->data + (vmf->pgoff << PAGE_SHIFT));
  57. get_page(vmf->page);
  58. return 0;
  59. }
  60. static void vdso_fix_landing(const struct vdso_image *image,
  61. struct vm_area_struct *new_vma)
  62. {
  63. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  64. if (in_ia32_syscall() && image == &vdso_image_32) {
  65. struct pt_regs *regs = current_pt_regs();
  66. unsigned long vdso_land = image->sym_int80_landing_pad;
  67. unsigned long old_land_addr = vdso_land +
  68. (unsigned long)current->mm->context.vdso;
  69. /* Fixing userspace landing - look at do_fast_syscall_32 */
  70. if (regs->ip == old_land_addr)
  71. regs->ip = new_vma->vm_start + vdso_land;
  72. }
  73. #endif
  74. }
  75. static int vdso_mremap(const struct vm_special_mapping *sm,
  76. struct vm_area_struct *new_vma)
  77. {
  78. const struct vdso_image *image = current->mm->context.vdso_image;
  79. vdso_fix_landing(image, new_vma);
  80. current->mm->context.vdso = (void __user *)new_vma->vm_start;
  81. return 0;
  82. }
  83. #ifdef CONFIG_TIME_NS
  84. static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
  85. {
  86. if (likely(vma->vm_mm == current->mm))
  87. return current->nsproxy->time_ns->vvar_page;
  88. /*
  89. * VM_PFNMAP | VM_IO protect .fault() handler from being called
  90. * through interfaces like /proc/$pid/mem or
  91. * process_vm_{readv,writev}() as long as there's no .access()
  92. * in special_mapping_vmops().
  93. * For more details check_vma_flags() and __access_remote_vm()
  94. */
  95. WARN(1, "vvar_page accessed remotely");
  96. return NULL;
  97. }
  98. /*
  99. * The vvar page layout depends on whether a task belongs to the root or
  100. * non-root time namespace. Whenever a task changes its namespace, the VVAR
  101. * page tables are cleared and then they will re-faulted with a
  102. * corresponding layout.
  103. * See also the comment near timens_setup_vdso_data() for details.
  104. */
  105. int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
  106. {
  107. struct mm_struct *mm = task->mm;
  108. struct vm_area_struct *vma;
  109. VMA_ITERATOR(vmi, mm, 0);
  110. mmap_read_lock(mm);
  111. for_each_vma(vmi, vma) {
  112. unsigned long size = vma->vm_end - vma->vm_start;
  113. if (vma_is_special_mapping(vma, &vvar_mapping))
  114. zap_page_range(vma, vma->vm_start, size);
  115. }
  116. mmap_read_unlock(mm);
  117. return 0;
  118. }
  119. #else
  120. static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
  121. {
  122. return NULL;
  123. }
  124. #endif
  125. static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
  126. struct vm_area_struct *vma, struct vm_fault *vmf)
  127. {
  128. const struct vdso_image *image = vma->vm_mm->context.vdso_image;
  129. unsigned long pfn;
  130. long sym_offset;
  131. if (!image)
  132. return VM_FAULT_SIGBUS;
  133. sym_offset = (long)(vmf->pgoff << PAGE_SHIFT) +
  134. image->sym_vvar_start;
  135. /*
  136. * Sanity check: a symbol offset of zero means that the page
  137. * does not exist for this vdso image, not that the page is at
  138. * offset zero relative to the text mapping. This should be
  139. * impossible here, because sym_offset should only be zero for
  140. * the page past the end of the vvar mapping.
  141. */
  142. if (sym_offset == 0)
  143. return VM_FAULT_SIGBUS;
  144. if (sym_offset == image->sym_vvar_page) {
  145. struct page *timens_page = find_timens_vvar_page(vma);
  146. pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
  147. /*
  148. * If a task belongs to a time namespace then a namespace
  149. * specific VVAR is mapped with the sym_vvar_page offset and
  150. * the real VVAR page is mapped with the sym_timens_page
  151. * offset.
  152. * See also the comment near timens_setup_vdso_data().
  153. */
  154. if (timens_page) {
  155. unsigned long addr;
  156. vm_fault_t err;
  157. /*
  158. * Optimization: inside time namespace pre-fault
  159. * VVAR page too. As on timens page there are only
  160. * offsets for clocks on VVAR, it'll be faulted
  161. * shortly by VDSO code.
  162. */
  163. addr = vmf->address + (image->sym_timens_page - sym_offset);
  164. err = vmf_insert_pfn(vma, addr, pfn);
  165. if (unlikely(err & VM_FAULT_ERROR))
  166. return err;
  167. pfn = page_to_pfn(timens_page);
  168. }
  169. return vmf_insert_pfn(vma, vmf->address, pfn);
  170. } else if (sym_offset == image->sym_pvclock_page) {
  171. struct pvclock_vsyscall_time_info *pvti =
  172. pvclock_get_pvti_cpu0_va();
  173. if (pvti && vclock_was_used(VDSO_CLOCKMODE_PVCLOCK)) {
  174. return vmf_insert_pfn_prot(vma, vmf->address,
  175. __pa(pvti) >> PAGE_SHIFT,
  176. pgprot_decrypted(vma->vm_page_prot));
  177. }
  178. } else if (sym_offset == image->sym_hvclock_page) {
  179. struct ms_hyperv_tsc_page *tsc_pg = hv_get_tsc_page();
  180. if (tsc_pg && vclock_was_used(VDSO_CLOCKMODE_HVCLOCK))
  181. return vmf_insert_pfn(vma, vmf->address,
  182. virt_to_phys(tsc_pg) >> PAGE_SHIFT);
  183. } else if (sym_offset == image->sym_timens_page) {
  184. struct page *timens_page = find_timens_vvar_page(vma);
  185. if (!timens_page)
  186. return VM_FAULT_SIGBUS;
  187. pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
  188. return vmf_insert_pfn(vma, vmf->address, pfn);
  189. }
  190. return VM_FAULT_SIGBUS;
  191. }
  192. static const struct vm_special_mapping vdso_mapping = {
  193. .name = "[vdso]",
  194. .fault = vdso_fault,
  195. .mremap = vdso_mremap,
  196. };
  197. static const struct vm_special_mapping vvar_mapping = {
  198. .name = "[vvar]",
  199. .fault = vvar_fault,
  200. };
  201. /*
  202. * Add vdso and vvar mappings to current process.
  203. * @image - blob to map
  204. * @addr - request a specific address (zero to map at free addr)
  205. */
  206. static int map_vdso(const struct vdso_image *image, unsigned long addr)
  207. {
  208. struct mm_struct *mm = current->mm;
  209. struct vm_area_struct *vma;
  210. unsigned long text_start;
  211. int ret = 0;
  212. if (mmap_write_lock_killable(mm))
  213. return -EINTR;
  214. addr = get_unmapped_area(NULL, addr,
  215. image->size - image->sym_vvar_start, 0, 0);
  216. if (IS_ERR_VALUE(addr)) {
  217. ret = addr;
  218. goto up_fail;
  219. }
  220. text_start = addr - image->sym_vvar_start;
  221. /*
  222. * MAYWRITE to allow gdb to COW and set breakpoints
  223. */
  224. vma = _install_special_mapping(mm,
  225. text_start,
  226. image->size,
  227. VM_READ|VM_EXEC|
  228. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  229. &vdso_mapping);
  230. if (IS_ERR(vma)) {
  231. ret = PTR_ERR(vma);
  232. goto up_fail;
  233. }
  234. vma = _install_special_mapping(mm,
  235. addr,
  236. -image->sym_vvar_start,
  237. VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
  238. VM_PFNMAP,
  239. &vvar_mapping);
  240. if (IS_ERR(vma)) {
  241. ret = PTR_ERR(vma);
  242. do_munmap(mm, text_start, image->size, NULL);
  243. } else {
  244. current->mm->context.vdso = (void __user *)text_start;
  245. current->mm->context.vdso_image = image;
  246. }
  247. up_fail:
  248. mmap_write_unlock(mm);
  249. return ret;
  250. }
  251. #ifdef CONFIG_X86_64
  252. /*
  253. * Put the vdso above the (randomized) stack with another randomized
  254. * offset. This way there is no hole in the middle of address space.
  255. * To save memory make sure it is still in the same PTE as the stack
  256. * top. This doesn't give that many random bits.
  257. *
  258. * Note that this algorithm is imperfect: the distribution of the vdso
  259. * start address within a PMD is biased toward the end.
  260. *
  261. * Only used for the 64-bit and x32 vdsos.
  262. */
  263. static unsigned long vdso_addr(unsigned long start, unsigned len)
  264. {
  265. unsigned long addr, end;
  266. unsigned offset;
  267. /*
  268. * Round up the start address. It can start out unaligned as a result
  269. * of stack start randomization.
  270. */
  271. start = PAGE_ALIGN(start);
  272. /* Round the lowest possible end address up to a PMD boundary. */
  273. end = (start + len + PMD_SIZE - 1) & PMD_MASK;
  274. if (end >= DEFAULT_MAP_WINDOW)
  275. end = DEFAULT_MAP_WINDOW;
  276. end -= len;
  277. if (end > start) {
  278. offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
  279. addr = start + (offset << PAGE_SHIFT);
  280. } else {
  281. addr = start;
  282. }
  283. /*
  284. * Forcibly align the final address in case we have a hardware
  285. * issue that requires alignment for performance reasons.
  286. */
  287. addr = align_vdso_addr(addr);
  288. return addr;
  289. }
  290. static int map_vdso_randomized(const struct vdso_image *image)
  291. {
  292. unsigned long addr = vdso_addr(current->mm->start_stack, image->size-image->sym_vvar_start);
  293. return map_vdso(image, addr);
  294. }
  295. #endif
  296. int map_vdso_once(const struct vdso_image *image, unsigned long addr)
  297. {
  298. struct mm_struct *mm = current->mm;
  299. struct vm_area_struct *vma;
  300. VMA_ITERATOR(vmi, mm, 0);
  301. mmap_write_lock(mm);
  302. /*
  303. * Check if we have already mapped vdso blob - fail to prevent
  304. * abusing from userspace install_special_mapping, which may
  305. * not do accounting and rlimit right.
  306. * We could search vma near context.vdso, but it's a slowpath,
  307. * so let's explicitly check all VMAs to be completely sure.
  308. */
  309. for_each_vma(vmi, vma) {
  310. if (vma_is_special_mapping(vma, &vdso_mapping) ||
  311. vma_is_special_mapping(vma, &vvar_mapping)) {
  312. mmap_write_unlock(mm);
  313. return -EEXIST;
  314. }
  315. }
  316. mmap_write_unlock(mm);
  317. return map_vdso(image, addr);
  318. }
  319. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  320. static int load_vdso32(void)
  321. {
  322. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  323. return 0;
  324. return map_vdso(&vdso_image_32, 0);
  325. }
  326. #endif
  327. #ifdef CONFIG_X86_64
  328. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  329. {
  330. if (!vdso64_enabled)
  331. return 0;
  332. return map_vdso_randomized(&vdso_image_64);
  333. }
  334. #ifdef CONFIG_COMPAT
  335. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  336. int uses_interp, bool x32)
  337. {
  338. #ifdef CONFIG_X86_X32_ABI
  339. if (x32) {
  340. if (!vdso64_enabled)
  341. return 0;
  342. return map_vdso_randomized(&vdso_image_x32);
  343. }
  344. #endif
  345. #ifdef CONFIG_IA32_EMULATION
  346. return load_vdso32();
  347. #else
  348. return 0;
  349. #endif
  350. }
  351. #endif
  352. #else
  353. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  354. {
  355. return load_vdso32();
  356. }
  357. #endif
  358. bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
  359. {
  360. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  361. const struct vdso_image *image = current->mm->context.vdso_image;
  362. unsigned long vdso = (unsigned long) current->mm->context.vdso;
  363. if (in_ia32_syscall() && image == &vdso_image_32) {
  364. if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad ||
  365. regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad)
  366. return true;
  367. }
  368. #endif
  369. return false;
  370. }
  371. #ifdef CONFIG_X86_64
  372. static __init int vdso_setup(char *s)
  373. {
  374. vdso64_enabled = simple_strtoul(s, NULL, 0);
  375. return 1;
  376. }
  377. __setup("vdso=", vdso_setup);
  378. static int __init init_vdso(void)
  379. {
  380. BUILD_BUG_ON(VDSO_CLOCKMODE_MAX >= 32);
  381. init_vdso_image(&vdso_image_64);
  382. #ifdef CONFIG_X86_X32_ABI
  383. init_vdso_image(&vdso_image_x32);
  384. #endif
  385. return 0;
  386. }
  387. subsys_initcall(init_vdso);
  388. #endif /* CONFIG_X86_64 */