xref: /linux/arch/arm64/kernel/vdso.c (revision d6df494a01351b1b757b7f1f559bcf696d03667d)
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 
10 #include <linux/cache.h>
11 #include <linux/clocksource.h>
12 #include <linux/elf.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/gfp.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/sched.h>
19 #include <linux/signal.h>
20 #include <linux/slab.h>
21 #include <linux/vdso_datastore.h>
22 #include <linux/vmalloc.h>
23 #include <vdso/datapage.h>
24 #include <vdso/helpers.h>
25 
26 #include <asm/cacheflush.h>
27 #include <asm/signal32.h>
28 #include <asm/vdso.h>
29 
30 enum vdso_abi {
31 	VDSO_ABI_AA64,
32 	VDSO_ABI_AA32,
33 };
34 
35 struct vdso_abi_info {
36 	const char *name;
37 	const char *vdso_code_start;
38 	const char *vdso_code_end;
39 	unsigned long vdso_pages;
40 	/* Code Mapping */
41 	struct vm_special_mapping *cm;
42 };
43 
44 static struct vdso_abi_info vdso_info[] __ro_after_init = {
45 	[VDSO_ABI_AA64] = {
46 		.name = "vdso",
47 		.vdso_code_start = vdso_start,
48 		.vdso_code_end = vdso_end,
49 	},
50 #ifdef CONFIG_COMPAT_VDSO
51 	[VDSO_ABI_AA32] = {
52 		.name = "vdso32",
53 		.vdso_code_start = vdso32_start,
54 		.vdso_code_end = vdso32_end,
55 	},
56 #endif /* CONFIG_COMPAT_VDSO */
57 };
58 
59 static int vdso_mremap(const struct vm_special_mapping *sm,
60 		struct vm_area_struct *new_vma)
61 {
62 	current->mm->context.vdso = (void *)new_vma->vm_start;
63 
64 	return 0;
65 }
66 
67 static int __init __vdso_init(enum vdso_abi abi)
68 {
69 	int i;
70 	struct page **vdso_pagelist;
71 	unsigned long pfn;
72 
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 
78 	vdso_info[abi].vdso_pages = (
79 			vdso_info[abi].vdso_code_end -
80 			vdso_info[abi].vdso_code_start) >>
81 			PAGE_SHIFT;
82 
83 	vdso_pagelist = kzalloc_objs(struct page *, vdso_info[abi].vdso_pages);
84 	if (vdso_pagelist == NULL)
85 		return -ENOMEM;
86 
87 	/* Grab the vDSO code pages. */
88 	pfn = sym_to_pfn(vdso_info[abi].vdso_code_start);
89 
90 	for (i = 0; i < vdso_info[abi].vdso_pages; i++)
91 		vdso_pagelist[i] = pfn_to_page(pfn + i);
92 
93 	vdso_info[abi].cm->pages = vdso_pagelist;
94 
95 	return 0;
96 }
97 
98 static int __setup_additional_pages(enum vdso_abi abi,
99 				    struct mm_struct *mm,
100 				    struct linux_binprm *bprm,
101 				    int uses_interp)
102 {
103 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
104 	unsigned long gp_flags = 0;
105 	void *ret;
106 
107 	BUILD_BUG_ON(VDSO_NR_PAGES != __VDSO_PAGES);
108 
109 	vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT;
110 	/* Be sure to map the data page */
111 	vdso_mapping_len = vdso_text_len + VDSO_NR_PAGES * PAGE_SIZE;
112 
113 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
114 	if (IS_ERR_VALUE(vdso_base)) {
115 		ret = ERR_PTR(vdso_base);
116 		goto up_fail;
117 	}
118 
119 	ret = vdso_install_vvar_mapping(mm, vdso_base);
120 	if (IS_ERR(ret))
121 		goto up_fail;
122 
123 	if (system_supports_bti_kernel())
124 		gp_flags = VM_ARM64_BTI;
125 
126 	vdso_base += VDSO_NR_PAGES * PAGE_SIZE;
127 	mm->context.vdso = (void *)vdso_base;
128 	ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
129 				       VM_READ|VM_EXEC|gp_flags|
130 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
131 				       VM_SEALED_SYSMAP,
132 				       vdso_info[abi].cm);
133 	if (IS_ERR(ret))
134 		goto up_fail;
135 
136 	return 0;
137 
138 up_fail:
139 	mm->context.vdso = NULL;
140 	return PTR_ERR(ret);
141 }
142 
143 #ifdef CONFIG_COMPAT
144 /*
145  * Create and map the vectors page for AArch32 tasks.
146  */
147 enum aarch32_map {
148 	AA32_MAP_VECTORS, /* kuser helpers */
149 	AA32_MAP_SIGPAGE,
150 	AA32_MAP_VDSO,
151 };
152 
153 static struct page *aarch32_vectors_page __ro_after_init;
154 static struct page *aarch32_sig_page __ro_after_init;
155 
156 static int aarch32_sigpage_mremap(const struct vm_special_mapping *sm,
157 				  struct vm_area_struct *new_vma)
158 {
159 	current->mm->context.sigpage = (void *)new_vma->vm_start;
160 
161 	return 0;
162 }
163 
164 static struct vm_special_mapping aarch32_vdso_maps[] = {
165 	[AA32_MAP_VECTORS] = {
166 		.name	= "[vectors]", /* ABI */
167 		.pages	= &aarch32_vectors_page,
168 	},
169 	[AA32_MAP_SIGPAGE] = {
170 		.name	= "[sigpage]", /* ABI */
171 		.pages	= &aarch32_sig_page,
172 		.mremap	= aarch32_sigpage_mremap,
173 	},
174 	[AA32_MAP_VDSO] = {
175 		.name = "[vdso]",
176 		.mremap = vdso_mremap,
177 	},
178 };
179 
180 static int aarch32_alloc_kuser_vdso_page(void)
181 {
182 	extern char __kuser_helper_start[], __kuser_helper_end[];
183 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
184 	unsigned long vdso_page;
185 
186 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
187 		return 0;
188 
189 	vdso_page = get_zeroed_page(GFP_KERNEL);
190 	if (!vdso_page)
191 		return -ENOMEM;
192 
193 	memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start,
194 	       kuser_sz);
195 	aarch32_vectors_page = virt_to_page((void *)vdso_page);
196 	return 0;
197 }
198 
199 #define COMPAT_SIGPAGE_POISON_WORD	0xe7fddef1
200 static int aarch32_alloc_sigpage(void)
201 {
202 	extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
203 	int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
204 	__le32 poison = cpu_to_le32(COMPAT_SIGPAGE_POISON_WORD);
205 	void *sigpage;
206 
207 	sigpage = (void *)__get_free_page(GFP_KERNEL);
208 	if (!sigpage)
209 		return -ENOMEM;
210 
211 	memset32(sigpage, (__force u32)poison, PAGE_SIZE / sizeof(poison));
212 	memcpy(sigpage, __aarch32_sigret_code_start, sigret_sz);
213 	aarch32_sig_page = virt_to_page(sigpage);
214 	return 0;
215 }
216 
217 static int __init __aarch32_alloc_vdso_pages(void)
218 {
219 
220 	if (!IS_ENABLED(CONFIG_COMPAT_VDSO))
221 		return 0;
222 
223 	vdso_info[VDSO_ABI_AA32].cm = &aarch32_vdso_maps[AA32_MAP_VDSO];
224 
225 	return __vdso_init(VDSO_ABI_AA32);
226 }
227 
228 static int __init aarch32_alloc_vdso_pages(void)
229 {
230 	int ret;
231 
232 	ret = __aarch32_alloc_vdso_pages();
233 	if (ret)
234 		return ret;
235 
236 	ret = aarch32_alloc_sigpage();
237 	if (ret)
238 		return ret;
239 
240 	return aarch32_alloc_kuser_vdso_page();
241 }
242 arch_initcall(aarch32_alloc_vdso_pages);
243 
244 static int aarch32_kuser_helpers_setup(struct mm_struct *mm)
245 {
246 	void *ret;
247 
248 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
249 		return 0;
250 
251 	/*
252 	 * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's
253 	 * not safe to CoW the page containing the CPU exception vectors.
254 	 */
255 	ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE,
256 				       VM_READ | VM_EXEC |
257 				       VM_MAYREAD | VM_MAYEXEC |
258 				       VM_SEALED_SYSMAP,
259 				       &aarch32_vdso_maps[AA32_MAP_VECTORS]);
260 
261 	return PTR_ERR_OR_ZERO(ret);
262 }
263 
264 static int aarch32_sigreturn_setup(struct mm_struct *mm)
265 {
266 	unsigned long addr;
267 	void *ret;
268 
269 	addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
270 	if (IS_ERR_VALUE(addr)) {
271 		ret = ERR_PTR(addr);
272 		goto out;
273 	}
274 
275 	/*
276 	 * VM_MAYWRITE is required to allow gdb to Copy-on-Write and
277 	 * set breakpoints.
278 	 */
279 	ret = _install_special_mapping(mm, addr, PAGE_SIZE,
280 				       VM_READ | VM_EXEC | VM_MAYREAD |
281 				       VM_MAYWRITE | VM_MAYEXEC |
282 				       VM_SEALED_SYSMAP,
283 				       &aarch32_vdso_maps[AA32_MAP_SIGPAGE]);
284 	if (IS_ERR(ret))
285 		goto out;
286 
287 	mm->context.sigpage = (void *)addr;
288 
289 out:
290 	return PTR_ERR_OR_ZERO(ret);
291 }
292 
293 int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
294 {
295 	struct mm_struct *mm = current->mm;
296 	int ret;
297 
298 	if (mmap_write_lock_killable(mm))
299 		return -EINTR;
300 
301 	ret = aarch32_kuser_helpers_setup(mm);
302 	if (ret)
303 		goto out;
304 
305 	if (IS_ENABLED(CONFIG_COMPAT_VDSO)) {
306 		ret = __setup_additional_pages(VDSO_ABI_AA32, mm, bprm,
307 					       uses_interp);
308 		if (ret)
309 			goto out;
310 	}
311 
312 	ret = aarch32_sigreturn_setup(mm);
313 out:
314 	mmap_write_unlock(mm);
315 	return ret;
316 }
317 #endif /* CONFIG_COMPAT */
318 
319 static struct vm_special_mapping aarch64_vdso_map __ro_after_init = {
320 	.name	= "[vdso]",
321 	.mremap = vdso_mremap,
322 };
323 
324 static int __init vdso_init(void)
325 {
326 	vdso_info[VDSO_ABI_AA64].cm = &aarch64_vdso_map;
327 
328 	return __vdso_init(VDSO_ABI_AA64);
329 }
330 arch_initcall(vdso_init);
331 
332 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
333 {
334 	struct mm_struct *mm = current->mm;
335 	int ret;
336 
337 	if (mmap_write_lock_killable(mm))
338 		return -EINTR;
339 
340 	ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
341 	mmap_write_unlock(mm);
342 
343 	return ret;
344 }
345