vdso.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
  4. * <benh@kernel.crashing.org>
  5. * Copyright (C) 2012 ARM Limited
  6. * Copyright (C) 2015 Regents of the University of California
  7. */
  8. #include <linux/elf.h>
  9. #include <linux/mm.h>
  10. #include <linux/slab.h>
  11. #include <linux/binfmts.h>
  12. #include <linux/err.h>
  13. #include <asm/page.h>
  14. #include <asm/vdso.h>
  15. #include <linux/time_namespace.h>
  16. #include <vdso/datapage.h>
  17. #include <vdso/vsyscall.h>
  18. enum vvar_pages {
  19. VVAR_DATA_PAGE_OFFSET,
  20. VVAR_TIMENS_PAGE_OFFSET,
  21. VVAR_NR_PAGES,
  22. };
  23. enum rv_vdso_map {
  24. RV_VDSO_MAP_VVAR,
  25. RV_VDSO_MAP_VDSO,
  26. };
  27. #define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT)
  28. static union vdso_data_store vdso_data_store __page_aligned_data;
  29. struct vdso_data *vdso_data = vdso_data_store.data;
  30. struct __vdso_info {
  31. const char *name;
  32. const char *vdso_code_start;
  33. const char *vdso_code_end;
  34. unsigned long vdso_pages;
  35. /* Data Mapping */
  36. struct vm_special_mapping *dm;
  37. /* Code Mapping */
  38. struct vm_special_mapping *cm;
  39. };
  40. static struct __vdso_info vdso_info;
  41. #ifdef CONFIG_COMPAT
  42. static struct __vdso_info compat_vdso_info;
  43. #endif
  44. static int vdso_mremap(const struct vm_special_mapping *sm,
  45. struct vm_area_struct *new_vma)
  46. {
  47. current->mm->context.vdso = (void *)new_vma->vm_start;
  48. return 0;
  49. }
  50. static void __init __vdso_init(struct __vdso_info *vdso_info)
  51. {
  52. unsigned int i;
  53. struct page **vdso_pagelist;
  54. unsigned long pfn;
  55. if (memcmp(vdso_info->vdso_code_start, "\177ELF", 4))
  56. panic("vDSO is not a valid ELF object!\n");
  57. vdso_info->vdso_pages = (
  58. vdso_info->vdso_code_end -
  59. vdso_info->vdso_code_start) >>
  60. PAGE_SHIFT;
  61. vdso_pagelist = kcalloc(vdso_info->vdso_pages,
  62. sizeof(struct page *),
  63. GFP_KERNEL);
  64. if (vdso_pagelist == NULL)
  65. panic("vDSO kcalloc failed!\n");
  66. /* Grab the vDSO code pages. */
  67. pfn = sym_to_pfn(vdso_info->vdso_code_start);
  68. for (i = 0; i < vdso_info->vdso_pages; i++)
  69. vdso_pagelist[i] = pfn_to_page(pfn + i);
  70. vdso_info->cm->pages = vdso_pagelist;
  71. }
  72. #ifdef CONFIG_TIME_NS
  73. struct vdso_data *arch_get_vdso_data(void *vvar_page)
  74. {
  75. return (struct vdso_data *)(vvar_page);
  76. }
  77. /*
  78. * The vvar mapping contains data for a specific time namespace, so when a task
  79. * changes namespace we must unmap its vvar data for the old namespace.
  80. * Subsequent faults will map in data for the new namespace.
  81. *
  82. * For more details see timens_setup_vdso_data().
  83. */
  84. int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
  85. {
  86. struct mm_struct *mm = task->mm;
  87. struct vm_area_struct *vma;
  88. VMA_ITERATOR(vmi, mm, 0);
  89. mmap_read_lock(mm);
  90. for_each_vma(vmi, vma) {
  91. if (vma_is_special_mapping(vma, vdso_info.dm))
  92. zap_vma_pages(vma);
  93. #ifdef CONFIG_COMPAT
  94. if (vma_is_special_mapping(vma, compat_vdso_info.dm))
  95. zap_vma_pages(vma);
  96. #endif
  97. }
  98. mmap_read_unlock(mm);
  99. return 0;
  100. }
  101. #endif
  102. static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
  103. struct vm_area_struct *vma, struct vm_fault *vmf)
  104. {
  105. struct page *timens_page = find_timens_vvar_page(vma);
  106. unsigned long pfn;
  107. switch (vmf->pgoff) {
  108. case VVAR_DATA_PAGE_OFFSET:
  109. if (timens_page)
  110. pfn = page_to_pfn(timens_page);
  111. else
  112. pfn = sym_to_pfn(vdso_data);
  113. break;
  114. #ifdef CONFIG_TIME_NS
  115. case VVAR_TIMENS_PAGE_OFFSET:
  116. /*
  117. * If a task belongs to a time namespace then a namespace
  118. * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
  119. * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
  120. * offset.
  121. * See also the comment near timens_setup_vdso_data().
  122. */
  123. if (!timens_page)
  124. return VM_FAULT_SIGBUS;
  125. pfn = sym_to_pfn(vdso_data);
  126. break;
  127. #endif /* CONFIG_TIME_NS */
  128. default:
  129. return VM_FAULT_SIGBUS;
  130. }
  131. return vmf_insert_pfn(vma, vmf->address, pfn);
  132. }
  133. static struct vm_special_mapping rv_vdso_maps[] __ro_after_init = {
  134. [RV_VDSO_MAP_VVAR] = {
  135. .name = "[vvar]",
  136. .fault = vvar_fault,
  137. },
  138. [RV_VDSO_MAP_VDSO] = {
  139. .name = "[vdso]",
  140. .mremap = vdso_mremap,
  141. },
  142. };
  143. static struct __vdso_info vdso_info __ro_after_init = {
  144. .name = "vdso",
  145. .vdso_code_start = vdso_start,
  146. .vdso_code_end = vdso_end,
  147. .dm = &rv_vdso_maps[RV_VDSO_MAP_VVAR],
  148. .cm = &rv_vdso_maps[RV_VDSO_MAP_VDSO],
  149. };
  150. #ifdef CONFIG_COMPAT
  151. static struct vm_special_mapping rv_compat_vdso_maps[] __ro_after_init = {
  152. [RV_VDSO_MAP_VVAR] = {
  153. .name = "[vvar]",
  154. .fault = vvar_fault,
  155. },
  156. [RV_VDSO_MAP_VDSO] = {
  157. .name = "[vdso]",
  158. .mremap = vdso_mremap,
  159. },
  160. };
  161. static struct __vdso_info compat_vdso_info __ro_after_init = {
  162. .name = "compat_vdso",
  163. .vdso_code_start = compat_vdso_start,
  164. .vdso_code_end = compat_vdso_end,
  165. .dm = &rv_compat_vdso_maps[RV_VDSO_MAP_VVAR],
  166. .cm = &rv_compat_vdso_maps[RV_VDSO_MAP_VDSO],
  167. };
  168. #endif
  169. static int __init vdso_init(void)
  170. {
  171. __vdso_init(&vdso_info);
  172. #ifdef CONFIG_COMPAT
  173. __vdso_init(&compat_vdso_info);
  174. #endif
  175. return 0;
  176. }
  177. arch_initcall(vdso_init);
  178. static int __setup_additional_pages(struct mm_struct *mm,
  179. struct linux_binprm *bprm,
  180. int uses_interp,
  181. struct __vdso_info *vdso_info)
  182. {
  183. unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
  184. void *ret;
  185. BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
  186. vdso_text_len = vdso_info->vdso_pages << PAGE_SHIFT;
  187. /* Be sure to map the data page */
  188. vdso_mapping_len = vdso_text_len + VVAR_SIZE;
  189. vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
  190. if (IS_ERR_VALUE(vdso_base)) {
  191. ret = ERR_PTR(vdso_base);
  192. goto up_fail;
  193. }
  194. ret = _install_special_mapping(mm, vdso_base, VVAR_SIZE,
  195. (VM_READ | VM_MAYREAD | VM_PFNMAP), vdso_info->dm);
  196. if (IS_ERR(ret))
  197. goto up_fail;
  198. vdso_base += VVAR_SIZE;
  199. mm->context.vdso = (void *)vdso_base;
  200. ret =
  201. _install_special_mapping(mm, vdso_base, vdso_text_len,
  202. (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
  203. vdso_info->cm);
  204. if (IS_ERR(ret))
  205. goto up_fail;
  206. return 0;
  207. up_fail:
  208. mm->context.vdso = NULL;
  209. return PTR_ERR(ret);
  210. }
  211. #ifdef CONFIG_COMPAT
  212. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  213. int uses_interp)
  214. {
  215. struct mm_struct *mm = current->mm;
  216. int ret;
  217. if (mmap_write_lock_killable(mm))
  218. return -EINTR;
  219. ret = __setup_additional_pages(mm, bprm, uses_interp,
  220. &compat_vdso_info);
  221. mmap_write_unlock(mm);
  222. return ret;
  223. }
  224. #endif
  225. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  226. {
  227. struct mm_struct *mm = current->mm;
  228. int ret;
  229. if (mmap_write_lock_killable(mm))
  230. return -EINTR;
  231. ret = __setup_additional_pages(mm, bprm, uses_interp, &vdso_info);
  232. mmap_write_unlock(mm);
  233. return ret;
  234. }