vdso.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VDSO implementations.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. *
  7. * Author: Will Deacon <will.deacon@arm.com>
  8. */
  9. #include <linux/cache.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/elf.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/gfp.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/sched.h>
  18. #include <linux/signal.h>
  19. #include <linux/slab.h>
  20. #include <linux/time_namespace.h>
  21. #include <linux/timekeeper_internal.h>
  22. #include <linux/vmalloc.h>
  23. #include <vdso/datapage.h>
  24. #include <vdso/helpers.h>
  25. #include <vdso/vsyscall.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/signal32.h>
  28. #include <asm/vdso.h>
  29. enum vdso_abi {
  30. VDSO_ABI_AA64,
  31. VDSO_ABI_AA32,
  32. };
  33. struct vdso_abi_info {
  34. const char *name;
  35. const char *vdso_code_start;
  36. const char *vdso_code_end;
  37. unsigned long vdso_pages;
  38. /* Data Mapping */
  39. struct vm_special_mapping *dm;
  40. /* Code Mapping */
  41. struct vm_special_mapping *cm;
  42. };
  43. static struct vdso_abi_info vdso_info[] __ro_after_init = {
  44. [VDSO_ABI_AA64] = {
  45. .name = "vdso",
  46. .vdso_code_start = vdso_start,
  47. .vdso_code_end = vdso_end,
  48. },
  49. #ifdef CONFIG_COMPAT_VDSO
  50. [VDSO_ABI_AA32] = {
  51. .name = "vdso32",
  52. .vdso_code_start = vdso32_start,
  53. .vdso_code_end = vdso32_end,
  54. },
  55. #endif /* CONFIG_COMPAT_VDSO */
  56. };
  57. /*
  58. * The vDSO data page.
  59. */
  60. static union vdso_data_store vdso_data_store __page_aligned_data;
  61. struct vdso_data *vdso_data = vdso_data_store.data;
  62. static int vdso_mremap(const struct vm_special_mapping *sm,
  63. struct vm_area_struct *new_vma)
  64. {
  65. current->mm->context.vdso = (void *)new_vma->vm_start;
  66. return 0;
  67. }
  68. static int __init __vdso_init(enum vdso_abi abi)
  69. {
  70. int i;
  71. struct page **vdso_pagelist;
  72. unsigned long pfn;
  73. if (memcmp(vdso_info[abi].vdso_code_start, "\177ELF", 4)) {
  74. pr_err("vDSO is not a valid ELF object!\n");
  75. return -EINVAL;
  76. }
  77. vdso_info[abi].vdso_pages = (
  78. vdso_info[abi].vdso_code_end -
  79. vdso_info[abi].vdso_code_start) >>
  80. PAGE_SHIFT;
  81. vdso_pagelist = kcalloc(vdso_info[abi].vdso_pages,
  82. sizeof(struct page *),
  83. GFP_KERNEL);
  84. if (vdso_pagelist == NULL)
  85. return -ENOMEM;
  86. /* Grab the vDSO code pages. */
  87. pfn = sym_to_pfn(vdso_info[abi].vdso_code_start);
  88. for (i = 0; i < vdso_info[abi].vdso_pages; i++)
  89. vdso_pagelist[i] = pfn_to_page(pfn + i);
  90. vdso_info[abi].cm->pages = vdso_pagelist;
  91. return 0;
  92. }
  93. #ifdef CONFIG_TIME_NS
  94. struct vdso_data *arch_get_vdso_data(void *vvar_page)
  95. {
  96. return (struct vdso_data *)(vvar_page);
  97. }
  98. /*
  99. * The vvar mapping contains data for a specific time namespace, so when a task
  100. * changes namespace we must unmap its vvar data for the old namespace.
  101. * Subsequent faults will map in data for the new namespace.
  102. *
  103. * For more details see timens_setup_vdso_data().
  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. if (vma_is_special_mapping(vma, vdso_info[VDSO_ABI_AA64].dm))
  113. zap_vma_pages(vma);
  114. #ifdef CONFIG_COMPAT_VDSO
  115. if (vma_is_special_mapping(vma, vdso_info[VDSO_ABI_AA32].dm))
  116. zap_vma_pages(vma);
  117. #endif
  118. }
  119. mmap_read_unlock(mm);
  120. return 0;
  121. }
  122. #endif
  123. static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
  124. struct vm_area_struct *vma, struct vm_fault *vmf)
  125. {
  126. struct page *timens_page = find_timens_vvar_page(vma);
  127. unsigned long pfn;
  128. switch (vmf->pgoff) {
  129. case VVAR_DATA_PAGE_OFFSET:
  130. if (timens_page)
  131. pfn = page_to_pfn(timens_page);
  132. else
  133. pfn = sym_to_pfn(vdso_data);
  134. break;
  135. #ifdef CONFIG_TIME_NS
  136. case VVAR_TIMENS_PAGE_OFFSET:
  137. /*
  138. * If a task belongs to a time namespace then a namespace
  139. * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
  140. * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
  141. * offset.
  142. * See also the comment near timens_setup_vdso_data().
  143. */
  144. if (!timens_page)
  145. return VM_FAULT_SIGBUS;
  146. pfn = sym_to_pfn(vdso_data);
  147. break;
  148. #endif /* CONFIG_TIME_NS */
  149. default:
  150. return VM_FAULT_SIGBUS;
  151. }
  152. return vmf_insert_pfn(vma, vmf->address, pfn);
  153. }
  154. static int __setup_additional_pages(enum vdso_abi abi,
  155. struct mm_struct *mm,
  156. struct linux_binprm *bprm,
  157. int uses_interp)
  158. {
  159. unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
  160. unsigned long gp_flags = 0;
  161. void *ret;
  162. BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
  163. vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT;
  164. /* Be sure to map the data page */
  165. vdso_mapping_len = vdso_text_len + VVAR_NR_PAGES * PAGE_SIZE;
  166. vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
  167. if (IS_ERR_VALUE(vdso_base)) {
  168. ret = ERR_PTR(vdso_base);
  169. goto up_fail;
  170. }
  171. ret = _install_special_mapping(mm, vdso_base, VVAR_NR_PAGES * PAGE_SIZE,
  172. VM_READ|VM_MAYREAD|VM_PFNMAP,
  173. vdso_info[abi].dm);
  174. if (IS_ERR(ret))
  175. goto up_fail;
  176. if (system_supports_bti_kernel())
  177. gp_flags = VM_ARM64_BTI;
  178. vdso_base += VVAR_NR_PAGES * PAGE_SIZE;
  179. mm->context.vdso = (void *)vdso_base;
  180. ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
  181. VM_READ|VM_EXEC|gp_flags|
  182. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  183. vdso_info[abi].cm);
  184. if (IS_ERR(ret))
  185. goto up_fail;
  186. return 0;
  187. up_fail:
  188. mm->context.vdso = NULL;
  189. return PTR_ERR(ret);
  190. }
  191. #ifdef CONFIG_COMPAT
  192. /*
  193. * Create and map the vectors page for AArch32 tasks.
  194. */
  195. enum aarch32_map {
  196. AA32_MAP_VECTORS, /* kuser helpers */
  197. AA32_MAP_SIGPAGE,
  198. AA32_MAP_VVAR,
  199. AA32_MAP_VDSO,
  200. };
  201. static struct page *aarch32_vectors_page __ro_after_init;
  202. static struct page *aarch32_sig_page __ro_after_init;
  203. static int aarch32_sigpage_mremap(const struct vm_special_mapping *sm,
  204. struct vm_area_struct *new_vma)
  205. {
  206. current->mm->context.sigpage = (void *)new_vma->vm_start;
  207. return 0;
  208. }
  209. static struct vm_special_mapping aarch32_vdso_maps[] = {
  210. [AA32_MAP_VECTORS] = {
  211. .name = "[vectors]", /* ABI */
  212. .pages = &aarch32_vectors_page,
  213. },
  214. [AA32_MAP_SIGPAGE] = {
  215. .name = "[sigpage]", /* ABI */
  216. .pages = &aarch32_sig_page,
  217. .mremap = aarch32_sigpage_mremap,
  218. },
  219. [AA32_MAP_VVAR] = {
  220. .name = "[vvar]",
  221. .fault = vvar_fault,
  222. },
  223. [AA32_MAP_VDSO] = {
  224. .name = "[vdso]",
  225. .mremap = vdso_mremap,
  226. },
  227. };
  228. static int aarch32_alloc_kuser_vdso_page(void)
  229. {
  230. extern char __kuser_helper_start[], __kuser_helper_end[];
  231. int kuser_sz = __kuser_helper_end - __kuser_helper_start;
  232. unsigned long vdso_page;
  233. if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
  234. return 0;
  235. vdso_page = get_zeroed_page(GFP_KERNEL);
  236. if (!vdso_page)
  237. return -ENOMEM;
  238. memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start,
  239. kuser_sz);
  240. aarch32_vectors_page = virt_to_page((void *)vdso_page);
  241. return 0;
  242. }
  243. #define COMPAT_SIGPAGE_POISON_WORD 0xe7fddef1
  244. static int aarch32_alloc_sigpage(void)
  245. {
  246. extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
  247. int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
  248. __le32 poison = cpu_to_le32(COMPAT_SIGPAGE_POISON_WORD);
  249. void *sigpage;
  250. sigpage = (void *)__get_free_page(GFP_KERNEL);
  251. if (!sigpage)
  252. return -ENOMEM;
  253. memset32(sigpage, (__force u32)poison, PAGE_SIZE / sizeof(poison));
  254. memcpy(sigpage, __aarch32_sigret_code_start, sigret_sz);
  255. aarch32_sig_page = virt_to_page(sigpage);
  256. return 0;
  257. }
  258. static int __init __aarch32_alloc_vdso_pages(void)
  259. {
  260. if (!IS_ENABLED(CONFIG_COMPAT_VDSO))
  261. return 0;
  262. vdso_info[VDSO_ABI_AA32].dm = &aarch32_vdso_maps[AA32_MAP_VVAR];
  263. vdso_info[VDSO_ABI_AA32].cm = &aarch32_vdso_maps[AA32_MAP_VDSO];
  264. return __vdso_init(VDSO_ABI_AA32);
  265. }
  266. static int __init aarch32_alloc_vdso_pages(void)
  267. {
  268. int ret;
  269. ret = __aarch32_alloc_vdso_pages();
  270. if (ret)
  271. return ret;
  272. ret = aarch32_alloc_sigpage();
  273. if (ret)
  274. return ret;
  275. return aarch32_alloc_kuser_vdso_page();
  276. }
  277. arch_initcall(aarch32_alloc_vdso_pages);
  278. static int aarch32_kuser_helpers_setup(struct mm_struct *mm)
  279. {
  280. void *ret;
  281. if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
  282. return 0;
  283. /*
  284. * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's
  285. * not safe to CoW the page containing the CPU exception vectors.
  286. */
  287. ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE,
  288. VM_READ | VM_EXEC |
  289. VM_MAYREAD | VM_MAYEXEC,
  290. &aarch32_vdso_maps[AA32_MAP_VECTORS]);
  291. return PTR_ERR_OR_ZERO(ret);
  292. }
  293. static int aarch32_sigreturn_setup(struct mm_struct *mm)
  294. {
  295. unsigned long addr;
  296. void *ret;
  297. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  298. if (IS_ERR_VALUE(addr)) {
  299. ret = ERR_PTR(addr);
  300. goto out;
  301. }
  302. /*
  303. * VM_MAYWRITE is required to allow gdb to Copy-on-Write and
  304. * set breakpoints.
  305. */
  306. ret = _install_special_mapping(mm, addr, PAGE_SIZE,
  307. VM_READ | VM_EXEC | VM_MAYREAD |
  308. VM_MAYWRITE | VM_MAYEXEC,
  309. &aarch32_vdso_maps[AA32_MAP_SIGPAGE]);
  310. if (IS_ERR(ret))
  311. goto out;
  312. mm->context.sigpage = (void *)addr;
  313. out:
  314. return PTR_ERR_OR_ZERO(ret);
  315. }
  316. int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  317. {
  318. struct mm_struct *mm = current->mm;
  319. int ret;
  320. if (mmap_write_lock_killable(mm))
  321. return -EINTR;
  322. ret = aarch32_kuser_helpers_setup(mm);
  323. if (ret)
  324. goto out;
  325. if (IS_ENABLED(CONFIG_COMPAT_VDSO)) {
  326. ret = __setup_additional_pages(VDSO_ABI_AA32, mm, bprm,
  327. uses_interp);
  328. if (ret)
  329. goto out;
  330. }
  331. ret = aarch32_sigreturn_setup(mm);
  332. out:
  333. mmap_write_unlock(mm);
  334. return ret;
  335. }
  336. #endif /* CONFIG_COMPAT */
  337. enum aarch64_map {
  338. AA64_MAP_VVAR,
  339. AA64_MAP_VDSO,
  340. };
  341. static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = {
  342. [AA64_MAP_VVAR] = {
  343. .name = "[vvar]",
  344. .fault = vvar_fault,
  345. },
  346. [AA64_MAP_VDSO] = {
  347. .name = "[vdso]",
  348. .mremap = vdso_mremap,
  349. },
  350. };
  351. static int __init vdso_init(void)
  352. {
  353. vdso_info[VDSO_ABI_AA64].dm = &aarch64_vdso_maps[AA64_MAP_VVAR];
  354. vdso_info[VDSO_ABI_AA64].cm = &aarch64_vdso_maps[AA64_MAP_VDSO];
  355. return __vdso_init(VDSO_ABI_AA64);
  356. }
  357. arch_initcall(vdso_init);
  358. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  359. {
  360. struct mm_struct *mm = current->mm;
  361. int ret;
  362. if (mmap_write_lock_killable(mm))
  363. return -EINTR;
  364. ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
  365. mmap_write_unlock(mm);
  366. return ret;
  367. }