vma.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2007 Andi Kleen, SUSE Labs.
  3. * Subject to the GPL, v.2
  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 <asm/pvclock.h>
  18. #include <asm/vgtod.h>
  19. #include <asm/proto.h>
  20. #include <asm/vdso.h>
  21. #include <asm/vvar.h>
  22. #include <asm/page.h>
  23. #include <asm/desc.h>
  24. #include <asm/cpufeature.h>
  25. #include <asm/mshyperv.h>
  26. #if defined(CONFIG_X86_64)
  27. unsigned int __read_mostly vdso64_enabled = 1;
  28. #endif
  29. void __init init_vdso_image(const struct vdso_image *image)
  30. {
  31. BUG_ON(image->size % PAGE_SIZE != 0);
  32. apply_alternatives((struct alt_instr *)(image->data + image->alt),
  33. (struct alt_instr *)(image->data + image->alt +
  34. image->alt_len));
  35. }
  36. struct linux_binprm;
  37. static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
  38. struct vm_area_struct *vma, struct vm_fault *vmf)
  39. {
  40. const struct vdso_image *image = vma->vm_mm->context.vdso_image;
  41. if (!image || (vmf->pgoff << PAGE_SHIFT) >= image->size)
  42. return VM_FAULT_SIGBUS;
  43. vmf->page = virt_to_page(image->data + (vmf->pgoff << PAGE_SHIFT));
  44. get_page(vmf->page);
  45. return 0;
  46. }
  47. static void vdso_fix_landing(const struct vdso_image *image,
  48. struct vm_area_struct *new_vma)
  49. {
  50. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  51. if (in_ia32_syscall() && image == &vdso_image_32) {
  52. struct pt_regs *regs = current_pt_regs();
  53. unsigned long vdso_land = image->sym_int80_landing_pad;
  54. unsigned long old_land_addr = vdso_land +
  55. (unsigned long)current->mm->context.vdso;
  56. /* Fixing userspace landing - look at do_fast_syscall_32 */
  57. if (regs->ip == old_land_addr)
  58. regs->ip = new_vma->vm_start + vdso_land;
  59. }
  60. #endif
  61. }
  62. static int vdso_mremap(const struct vm_special_mapping *sm,
  63. struct vm_area_struct *new_vma)
  64. {
  65. unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
  66. const struct vdso_image *image = current->mm->context.vdso_image;
  67. if (image->size != new_size)
  68. return -EINVAL;
  69. vdso_fix_landing(image, new_vma);
  70. current->mm->context.vdso = (void __user *)new_vma->vm_start;
  71. return 0;
  72. }
  73. static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
  74. struct vm_area_struct *vma, struct vm_fault *vmf)
  75. {
  76. const struct vdso_image *image = vma->vm_mm->context.vdso_image;
  77. long sym_offset;
  78. if (!image)
  79. return VM_FAULT_SIGBUS;
  80. sym_offset = (long)(vmf->pgoff << PAGE_SHIFT) +
  81. image->sym_vvar_start;
  82. /*
  83. * Sanity check: a symbol offset of zero means that the page
  84. * does not exist for this vdso image, not that the page is at
  85. * offset zero relative to the text mapping. This should be
  86. * impossible here, because sym_offset should only be zero for
  87. * the page past the end of the vvar mapping.
  88. */
  89. if (sym_offset == 0)
  90. return VM_FAULT_SIGBUS;
  91. if (sym_offset == image->sym_vvar_page) {
  92. return vmf_insert_pfn(vma, vmf->address,
  93. __pa_symbol(&__vvar_page) >> PAGE_SHIFT);
  94. } else if (sym_offset == image->sym_pvclock_page) {
  95. struct pvclock_vsyscall_time_info *pvti =
  96. pvclock_get_pvti_cpu0_va();
  97. if (pvti && vclock_was_used(VCLOCK_PVCLOCK)) {
  98. return vmf_insert_pfn_prot(vma, vmf->address,
  99. __pa(pvti) >> PAGE_SHIFT,
  100. pgprot_decrypted(vma->vm_page_prot));
  101. }
  102. } else if (sym_offset == image->sym_hvclock_page) {
  103. struct ms_hyperv_tsc_page *tsc_pg = hv_get_tsc_page();
  104. if (tsc_pg && vclock_was_used(VCLOCK_HVCLOCK))
  105. return vmf_insert_pfn(vma, vmf->address,
  106. vmalloc_to_pfn(tsc_pg));
  107. }
  108. return VM_FAULT_SIGBUS;
  109. }
  110. static const struct vm_special_mapping vdso_mapping = {
  111. .name = "[vdso]",
  112. .fault = vdso_fault,
  113. .mremap = vdso_mremap,
  114. };
  115. static const struct vm_special_mapping vvar_mapping = {
  116. .name = "[vvar]",
  117. .fault = vvar_fault,
  118. };
  119. /*
  120. * Add vdso and vvar mappings to current process.
  121. * @image - blob to map
  122. * @addr - request a specific address (zero to map at free addr)
  123. */
  124. static int map_vdso(const struct vdso_image *image, unsigned long addr)
  125. {
  126. struct mm_struct *mm = current->mm;
  127. struct vm_area_struct *vma;
  128. unsigned long text_start;
  129. int ret = 0;
  130. if (down_write_killable(&mm->mmap_sem))
  131. return -EINTR;
  132. addr = get_unmapped_area(NULL, addr,
  133. image->size - image->sym_vvar_start, 0, 0);
  134. if (IS_ERR_VALUE(addr)) {
  135. ret = addr;
  136. goto up_fail;
  137. }
  138. text_start = addr - image->sym_vvar_start;
  139. /*
  140. * MAYWRITE to allow gdb to COW and set breakpoints
  141. */
  142. vma = _install_special_mapping(mm,
  143. text_start,
  144. image->size,
  145. VM_READ|VM_EXEC|
  146. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  147. &vdso_mapping);
  148. if (IS_ERR(vma)) {
  149. ret = PTR_ERR(vma);
  150. goto up_fail;
  151. }
  152. vma = _install_special_mapping(mm,
  153. addr,
  154. -image->sym_vvar_start,
  155. VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
  156. VM_PFNMAP,
  157. &vvar_mapping);
  158. if (IS_ERR(vma)) {
  159. ret = PTR_ERR(vma);
  160. do_munmap(mm, text_start, image->size, NULL);
  161. } else {
  162. current->mm->context.vdso = (void __user *)text_start;
  163. current->mm->context.vdso_image = image;
  164. }
  165. up_fail:
  166. up_write(&mm->mmap_sem);
  167. return ret;
  168. }
  169. #ifdef CONFIG_X86_64
  170. /*
  171. * Put the vdso above the (randomized) stack with another randomized
  172. * offset. This way there is no hole in the middle of address space.
  173. * To save memory make sure it is still in the same PTE as the stack
  174. * top. This doesn't give that many random bits.
  175. *
  176. * Note that this algorithm is imperfect: the distribution of the vdso
  177. * start address within a PMD is biased toward the end.
  178. *
  179. * Only used for the 64-bit and x32 vdsos.
  180. */
  181. static unsigned long vdso_addr(unsigned long start, unsigned len)
  182. {
  183. unsigned long addr, end;
  184. unsigned offset;
  185. /*
  186. * Round up the start address. It can start out unaligned as a result
  187. * of stack start randomization.
  188. */
  189. start = PAGE_ALIGN(start);
  190. /* Round the lowest possible end address up to a PMD boundary. */
  191. end = (start + len + PMD_SIZE - 1) & PMD_MASK;
  192. if (end >= TASK_SIZE_MAX)
  193. end = TASK_SIZE_MAX;
  194. end -= len;
  195. if (end > start) {
  196. offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
  197. addr = start + (offset << PAGE_SHIFT);
  198. } else {
  199. addr = start;
  200. }
  201. /*
  202. * Forcibly align the final address in case we have a hardware
  203. * issue that requires alignment for performance reasons.
  204. */
  205. addr = align_vdso_addr(addr);
  206. return addr;
  207. }
  208. static int map_vdso_randomized(const struct vdso_image *image)
  209. {
  210. unsigned long addr = vdso_addr(current->mm->start_stack, image->size-image->sym_vvar_start);
  211. return map_vdso(image, addr);
  212. }
  213. #endif
  214. int map_vdso_once(const struct vdso_image *image, unsigned long addr)
  215. {
  216. struct mm_struct *mm = current->mm;
  217. struct vm_area_struct *vma;
  218. down_write(&mm->mmap_sem);
  219. /*
  220. * Check if we have already mapped vdso blob - fail to prevent
  221. * abusing from userspace install_speciall_mapping, which may
  222. * not do accounting and rlimit right.
  223. * We could search vma near context.vdso, but it's a slowpath,
  224. * so let's explicitely check all VMAs to be completely sure.
  225. */
  226. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  227. if (vma_is_special_mapping(vma, &vdso_mapping) ||
  228. vma_is_special_mapping(vma, &vvar_mapping)) {
  229. up_write(&mm->mmap_sem);
  230. return -EEXIST;
  231. }
  232. }
  233. up_write(&mm->mmap_sem);
  234. return map_vdso(image, addr);
  235. }
  236. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  237. static int load_vdso32(void)
  238. {
  239. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  240. return 0;
  241. return map_vdso(&vdso_image_32, 0);
  242. }
  243. #endif
  244. #ifdef CONFIG_X86_64
  245. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  246. {
  247. if (!vdso64_enabled)
  248. return 0;
  249. return map_vdso_randomized(&vdso_image_64);
  250. }
  251. #ifdef CONFIG_COMPAT
  252. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  253. int uses_interp)
  254. {
  255. #ifdef CONFIG_X86_X32_ABI
  256. if (test_thread_flag(TIF_X32)) {
  257. if (!vdso64_enabled)
  258. return 0;
  259. return map_vdso_randomized(&vdso_image_x32);
  260. }
  261. #endif
  262. #ifdef CONFIG_IA32_EMULATION
  263. return load_vdso32();
  264. #else
  265. return 0;
  266. #endif
  267. }
  268. #endif
  269. #else
  270. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  271. {
  272. return load_vdso32();
  273. }
  274. #endif
  275. #ifdef CONFIG_X86_64
  276. static __init int vdso_setup(char *s)
  277. {
  278. vdso64_enabled = simple_strtoul(s, NULL, 0);
  279. return 0;
  280. }
  281. __setup("vdso=", vdso_setup);
  282. static int __init init_vdso(void)
  283. {
  284. init_vdso_image(&vdso_image_64);
  285. #ifdef CONFIG_X86_X32_ABI
  286. init_vdso_image(&vdso_image_x32);
  287. #endif
  288. return 0;
  289. }
  290. subsys_initcall(init_vdso);
  291. #endif /* CONFIG_X86_64 */