vma.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. DEFINE_VVAR(struct vdso_data, _vdso_data);
  38. DEFINE_VVAR_SINGLE(struct vdso_rng_data, _vdso_rng_data);
  39. unsigned int vclocks_used __read_mostly;
  40. #if defined(CONFIG_X86_64)
  41. unsigned int __read_mostly vdso64_enabled = 1;
  42. #endif
  43. int __init init_vdso_image(const struct vdso_image *image)
  44. {
  45. BUILD_BUG_ON(VDSO_CLOCKMODE_MAX >= 32);
  46. BUG_ON(image->size % PAGE_SIZE != 0);
  47. apply_alternatives((struct alt_instr *)(image->data + image->alt),
  48. (struct alt_instr *)(image->data + image->alt +
  49. image->alt_len));
  50. return 0;
  51. }
  52. static const struct vm_special_mapping vvar_mapping;
  53. struct linux_binprm;
  54. static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
  55. struct vm_area_struct *vma, struct vm_fault *vmf)
  56. {
  57. const struct vdso_image *image = vma->vm_mm->context.vdso_image;
  58. if (!image || (vmf->pgoff << PAGE_SHIFT) >= image->size)
  59. return VM_FAULT_SIGBUS;
  60. vmf->page = virt_to_page(image->data + (vmf->pgoff << PAGE_SHIFT));
  61. get_page(vmf->page);
  62. return 0;
  63. }
  64. static void vdso_fix_landing(const struct vdso_image *image,
  65. struct vm_area_struct *new_vma)
  66. {
  67. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  68. if (in_ia32_syscall() && image == &vdso_image_32) {
  69. struct pt_regs *regs = current_pt_regs();
  70. unsigned long vdso_land = image->sym_int80_landing_pad;
  71. unsigned long old_land_addr = vdso_land +
  72. (unsigned long)current->mm->context.vdso;
  73. /* Fixing userspace landing - look at do_fast_syscall_32 */
  74. if (regs->ip == old_land_addr)
  75. regs->ip = new_vma->vm_start + vdso_land;
  76. }
  77. #endif
  78. }
  79. static int vdso_mremap(const struct vm_special_mapping *sm,
  80. struct vm_area_struct *new_vma)
  81. {
  82. const struct vdso_image *image = current->mm->context.vdso_image;
  83. vdso_fix_landing(image, new_vma);
  84. current->mm->context.vdso = (void __user *)new_vma->vm_start;
  85. return 0;
  86. }
  87. #ifdef CONFIG_TIME_NS
  88. /*
  89. * The vvar page layout depends on whether a task belongs to the root or
  90. * non-root time namespace. Whenever a task changes its namespace, the VVAR
  91. * page tables are cleared and then they will re-faulted with a
  92. * corresponding layout.
  93. * See also the comment near timens_setup_vdso_data() for details.
  94. */
  95. int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
  96. {
  97. struct mm_struct *mm = task->mm;
  98. struct vm_area_struct *vma;
  99. VMA_ITERATOR(vmi, mm, 0);
  100. mmap_read_lock(mm);
  101. for_each_vma(vmi, vma) {
  102. if (vma_is_special_mapping(vma, &vvar_mapping))
  103. zap_vma_pages(vma);
  104. }
  105. mmap_read_unlock(mm);
  106. return 0;
  107. }
  108. #endif
  109. static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
  110. struct vm_area_struct *vma, struct vm_fault *vmf)
  111. {
  112. const struct vdso_image *image = vma->vm_mm->context.vdso_image;
  113. unsigned long pfn;
  114. long sym_offset;
  115. if (!image)
  116. return VM_FAULT_SIGBUS;
  117. sym_offset = (long)(vmf->pgoff << PAGE_SHIFT) +
  118. image->sym_vvar_start;
  119. /*
  120. * Sanity check: a symbol offset of zero means that the page
  121. * does not exist for this vdso image, not that the page is at
  122. * offset zero relative to the text mapping. This should be
  123. * impossible here, because sym_offset should only be zero for
  124. * the page past the end of the vvar mapping.
  125. */
  126. if (sym_offset == 0)
  127. return VM_FAULT_SIGBUS;
  128. if (sym_offset == image->sym_vvar_page) {
  129. struct page *timens_page = find_timens_vvar_page(vma);
  130. pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
  131. /*
  132. * If a task belongs to a time namespace then a namespace
  133. * specific VVAR is mapped with the sym_vvar_page offset and
  134. * the real VVAR page is mapped with the sym_timens_page
  135. * offset.
  136. * See also the comment near timens_setup_vdso_data().
  137. */
  138. if (timens_page) {
  139. unsigned long addr;
  140. vm_fault_t err;
  141. /*
  142. * Optimization: inside time namespace pre-fault
  143. * VVAR page too. As on timens page there are only
  144. * offsets for clocks on VVAR, it'll be faulted
  145. * shortly by VDSO code.
  146. */
  147. addr = vmf->address + (image->sym_timens_page - sym_offset);
  148. err = vmf_insert_pfn(vma, addr, pfn);
  149. if (unlikely(err & VM_FAULT_ERROR))
  150. return err;
  151. pfn = page_to_pfn(timens_page);
  152. }
  153. return vmf_insert_pfn(vma, vmf->address, pfn);
  154. } else if (sym_offset == image->sym_pvclock_page) {
  155. struct pvclock_vsyscall_time_info *pvti =
  156. pvclock_get_pvti_cpu0_va();
  157. if (pvti && vclock_was_used(VDSO_CLOCKMODE_PVCLOCK)) {
  158. return vmf_insert_pfn_prot(vma, vmf->address,
  159. __pa(pvti) >> PAGE_SHIFT,
  160. pgprot_decrypted(vma->vm_page_prot));
  161. }
  162. } else if (sym_offset == image->sym_hvclock_page) {
  163. pfn = hv_get_tsc_pfn();
  164. if (pfn && vclock_was_used(VDSO_CLOCKMODE_HVCLOCK))
  165. return vmf_insert_pfn(vma, vmf->address, pfn);
  166. } else if (sym_offset == image->sym_timens_page) {
  167. struct page *timens_page = find_timens_vvar_page(vma);
  168. if (!timens_page)
  169. return VM_FAULT_SIGBUS;
  170. pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
  171. return vmf_insert_pfn(vma, vmf->address, pfn);
  172. }
  173. return VM_FAULT_SIGBUS;
  174. }
  175. static const struct vm_special_mapping vdso_mapping = {
  176. .name = "[vdso]",
  177. .fault = vdso_fault,
  178. .mremap = vdso_mremap,
  179. };
  180. static const struct vm_special_mapping vvar_mapping = {
  181. .name = "[vvar]",
  182. .fault = vvar_fault,
  183. };
  184. /*
  185. * Add vdso and vvar mappings to current process.
  186. * @image - blob to map
  187. * @addr - request a specific address (zero to map at free addr)
  188. */
  189. static int map_vdso(const struct vdso_image *image, unsigned long addr)
  190. {
  191. struct mm_struct *mm = current->mm;
  192. struct vm_area_struct *vma;
  193. unsigned long text_start;
  194. int ret = 0;
  195. if (mmap_write_lock_killable(mm))
  196. return -EINTR;
  197. addr = get_unmapped_area(NULL, addr,
  198. image->size - image->sym_vvar_start, 0, 0);
  199. if (IS_ERR_VALUE(addr)) {
  200. ret = addr;
  201. goto up_fail;
  202. }
  203. text_start = addr - image->sym_vvar_start;
  204. /*
  205. * MAYWRITE to allow gdb to COW and set breakpoints
  206. */
  207. vma = _install_special_mapping(mm,
  208. text_start,
  209. image->size,
  210. VM_READ|VM_EXEC|
  211. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  212. &vdso_mapping);
  213. if (IS_ERR(vma)) {
  214. ret = PTR_ERR(vma);
  215. goto up_fail;
  216. }
  217. vma = _install_special_mapping(mm,
  218. addr,
  219. -image->sym_vvar_start,
  220. VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
  221. VM_PFNMAP,
  222. &vvar_mapping);
  223. if (IS_ERR(vma)) {
  224. ret = PTR_ERR(vma);
  225. do_munmap(mm, text_start, image->size, NULL);
  226. } else {
  227. current->mm->context.vdso = (void __user *)text_start;
  228. current->mm->context.vdso_image = image;
  229. }
  230. up_fail:
  231. mmap_write_unlock(mm);
  232. return ret;
  233. }
  234. int map_vdso_once(const struct vdso_image *image, unsigned long addr)
  235. {
  236. struct mm_struct *mm = current->mm;
  237. struct vm_area_struct *vma;
  238. VMA_ITERATOR(vmi, mm, 0);
  239. mmap_write_lock(mm);
  240. /*
  241. * Check if we have already mapped vdso blob - fail to prevent
  242. * abusing from userspace install_special_mapping, which may
  243. * not do accounting and rlimit right.
  244. * We could search vma near context.vdso, but it's a slowpath,
  245. * so let's explicitly check all VMAs to be completely sure.
  246. */
  247. for_each_vma(vmi, vma) {
  248. if (vma_is_special_mapping(vma, &vdso_mapping) ||
  249. vma_is_special_mapping(vma, &vvar_mapping)) {
  250. mmap_write_unlock(mm);
  251. return -EEXIST;
  252. }
  253. }
  254. mmap_write_unlock(mm);
  255. return map_vdso(image, addr);
  256. }
  257. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  258. static int load_vdso32(void)
  259. {
  260. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  261. return 0;
  262. return map_vdso(&vdso_image_32, 0);
  263. }
  264. #endif
  265. #ifdef CONFIG_X86_64
  266. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  267. {
  268. if (!vdso64_enabled)
  269. return 0;
  270. return map_vdso(&vdso_image_64, 0);
  271. }
  272. #ifdef CONFIG_COMPAT
  273. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  274. int uses_interp, bool x32)
  275. {
  276. #ifdef CONFIG_X86_X32_ABI
  277. if (x32) {
  278. if (!vdso64_enabled)
  279. return 0;
  280. return map_vdso(&vdso_image_x32, 0);
  281. }
  282. #endif
  283. #ifdef CONFIG_IA32_EMULATION
  284. return load_vdso32();
  285. #else
  286. return 0;
  287. #endif
  288. }
  289. #endif
  290. #else
  291. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  292. {
  293. return load_vdso32();
  294. }
  295. #endif
  296. bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
  297. {
  298. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  299. const struct vdso_image *image = current->mm->context.vdso_image;
  300. unsigned long vdso = (unsigned long) current->mm->context.vdso;
  301. if (in_ia32_syscall() && image == &vdso_image_32) {
  302. if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad ||
  303. regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad)
  304. return true;
  305. }
  306. #endif
  307. return false;
  308. }
  309. #ifdef CONFIG_X86_64
  310. static __init int vdso_setup(char *s)
  311. {
  312. vdso64_enabled = simple_strtoul(s, NULL, 0);
  313. return 1;
  314. }
  315. __setup("vdso=", vdso_setup);
  316. #endif /* CONFIG_X86_64 */