xref: /linux-6.15/arch/arm64/kernel/vdso.c (revision 1b6867d2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VDSO implementations.
4  *
5  * Copyright (C) 2012 ARM Limited
6  *
7  * Author: Will Deacon <[email protected]>
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/timekeeper_internal.h>
22 #include <linux/vmalloc.h>
23 #include <vdso/datapage.h>
24 #include <vdso/helpers.h>
25 #include <vdso/vsyscall.h>
26 
27 #include <asm/cacheflush.h>
28 #include <asm/signal32.h>
29 #include <asm/vdso.h>
30 
31 extern char vdso_start[], vdso_end[];
32 #ifdef CONFIG_COMPAT_VDSO
33 extern char vdso32_start[], vdso32_end[];
34 #endif /* CONFIG_COMPAT_VDSO */
35 
36 enum vdso_abi {
37 	VDSO_ABI_AA64,
38 #ifdef CONFIG_COMPAT_VDSO
39 	VDSO_ABI_AA32,
40 #endif /* CONFIG_COMPAT_VDSO */
41 };
42 
43 struct vdso_abi_info {
44 	const char *name;
45 	const char *vdso_code_start;
46 	const char *vdso_code_end;
47 	unsigned long vdso_pages;
48 	/* Data Mapping */
49 	struct vm_special_mapping *dm;
50 	/* Code Mapping */
51 	struct vm_special_mapping *cm;
52 };
53 
54 static struct vdso_abi_info vdso_info[] __ro_after_init = {
55 	[VDSO_ABI_AA64] = {
56 		.name = "vdso",
57 		.vdso_code_start = vdso_start,
58 		.vdso_code_end = vdso_end,
59 	},
60 #ifdef CONFIG_COMPAT_VDSO
61 	[VDSO_ABI_AA32] = {
62 		.name = "vdso32",
63 		.vdso_code_start = vdso32_start,
64 		.vdso_code_end = vdso32_end,
65 	},
66 #endif /* CONFIG_COMPAT_VDSO */
67 };
68 
69 /*
70  * The vDSO data page.
71  */
72 static union {
73 	struct vdso_data	data[CS_BASES];
74 	u8			page[PAGE_SIZE];
75 } vdso_data_store __page_aligned_data;
76 struct vdso_data *vdso_data = vdso_data_store.data;
77 
78 static int __vdso_remap(enum vdso_abi abi,
79 			const struct vm_special_mapping *sm,
80 			struct vm_area_struct *new_vma)
81 {
82 	unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
83 	unsigned long vdso_size = vdso_info[abi].vdso_code_end -
84 				  vdso_info[abi].vdso_code_start;
85 
86 	if (vdso_size != new_size)
87 		return -EINVAL;
88 
89 	current->mm->context.vdso = (void *)new_vma->vm_start;
90 
91 	return 0;
92 }
93 
94 static int __vdso_init(enum vdso_abi abi)
95 {
96 	int i;
97 	struct page **vdso_pagelist;
98 	unsigned long pfn;
99 
100 	if (memcmp(vdso_info[abi].vdso_code_start, "\177ELF", 4)) {
101 		pr_err("vDSO is not a valid ELF object!\n");
102 		return -EINVAL;
103 	}
104 
105 	vdso_info[abi].vdso_pages = (
106 			vdso_info[abi].vdso_code_end -
107 			vdso_info[abi].vdso_code_start) >>
108 			PAGE_SHIFT;
109 
110 	vdso_pagelist = kcalloc(vdso_info[abi].vdso_pages,
111 				sizeof(struct page *),
112 				GFP_KERNEL);
113 	if (vdso_pagelist == NULL)
114 		return -ENOMEM;
115 
116 	/* Grab the vDSO code pages. */
117 	pfn = sym_to_pfn(vdso_info[abi].vdso_code_start);
118 
119 	for (i = 0; i < vdso_info[abi].vdso_pages; i++)
120 		vdso_pagelist[i] = pfn_to_page(pfn + i);
121 
122 	vdso_info[abi].cm->pages = vdso_pagelist;
123 
124 	return 0;
125 }
126 
127 #ifdef CONFIG_TIME_NS
128 /*
129  * The vvar mapping contains data for a specific time namespace, so when a task
130  * changes namespace we must unmap its vvar data for the old namespace.
131  * Subsequent faults will map in data for the new namespace.
132  *
133  * For more details see timens_setup_vdso_data().
134  */
135 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
136 {
137 	struct mm_struct *mm = task->mm;
138 	struct vm_area_struct *vma;
139 
140 	mmap_read_lock(mm);
141 
142 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
143 		unsigned long size = vma->vm_end - vma->vm_start;
144 
145 		if (vma_is_special_mapping(vma, vdso_info[VDSO_ABI_AA64].dm))
146 			zap_page_range(vma, vma->vm_start, size);
147 #ifdef CONFIG_COMPAT_VDSO
148 		if (vma_is_special_mapping(vma, vdso_info[VDSO_ABI_AA32].dm))
149 			zap_page_range(vma, vma->vm_start, size);
150 #endif
151 	}
152 
153 	mmap_read_unlock(mm);
154 	return 0;
155 }
156 #endif
157 
158 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
159 			     struct vm_area_struct *vma, struct vm_fault *vmf)
160 {
161 	if (vmf->pgoff == 0)
162 		return vmf_insert_pfn(vma, vmf->address,
163 				sym_to_pfn(vdso_data));
164 	return VM_FAULT_SIGBUS;
165 }
166 
167 static int __setup_additional_pages(enum vdso_abi abi,
168 				    struct mm_struct *mm,
169 				    struct linux_binprm *bprm,
170 				    int uses_interp)
171 {
172 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
173 	unsigned long gp_flags = 0;
174 	void *ret;
175 
176 	vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT;
177 	/* Be sure to map the data page */
178 	vdso_mapping_len = vdso_text_len + PAGE_SIZE;
179 
180 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
181 	if (IS_ERR_VALUE(vdso_base)) {
182 		ret = ERR_PTR(vdso_base);
183 		goto up_fail;
184 	}
185 
186 	ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
187 				       VM_READ|VM_MAYREAD|VM_PFNMAP,
188 				       vdso_info[abi].dm);
189 	if (IS_ERR(ret))
190 		goto up_fail;
191 
192 	if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && system_supports_bti())
193 		gp_flags = VM_ARM64_BTI;
194 
195 	vdso_base += PAGE_SIZE;
196 	mm->context.vdso = (void *)vdso_base;
197 	ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
198 				       VM_READ|VM_EXEC|gp_flags|
199 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
200 				       vdso_info[abi].cm);
201 	if (IS_ERR(ret))
202 		goto up_fail;
203 
204 	return 0;
205 
206 up_fail:
207 	mm->context.vdso = NULL;
208 	return PTR_ERR(ret);
209 }
210 
211 #ifdef CONFIG_COMPAT
212 /*
213  * Create and map the vectors page for AArch32 tasks.
214  */
215 #ifdef CONFIG_COMPAT_VDSO
216 static int aarch32_vdso_mremap(const struct vm_special_mapping *sm,
217 		struct vm_area_struct *new_vma)
218 {
219 	return __vdso_remap(VDSO_ABI_AA32, sm, new_vma);
220 }
221 #endif /* CONFIG_COMPAT_VDSO */
222 
223 enum aarch32_map {
224 	AA32_MAP_VECTORS, /* kuser helpers */
225 #ifdef CONFIG_COMPAT_VDSO
226 	AA32_MAP_VVAR,
227 	AA32_MAP_VDSO,
228 #endif
229 	AA32_MAP_SIGPAGE
230 };
231 
232 static struct page *aarch32_vectors_page __ro_after_init;
233 static struct page *aarch32_sig_page __ro_after_init;
234 
235 static struct vm_special_mapping aarch32_vdso_maps[] = {
236 	[AA32_MAP_VECTORS] = {
237 		.name	= "[vectors]", /* ABI */
238 		.pages	= &aarch32_vectors_page,
239 	},
240 #ifdef CONFIG_COMPAT_VDSO
241 	[AA32_MAP_VVAR] = {
242 		.name = "[vvar]",
243 		.fault = vvar_fault,
244 	},
245 	[AA32_MAP_VDSO] = {
246 		.name = "[vdso]",
247 		.mremap = aarch32_vdso_mremap,
248 	},
249 #endif /* CONFIG_COMPAT_VDSO */
250 	[AA32_MAP_SIGPAGE] = {
251 		.name	= "[sigpage]", /* ABI */
252 		.pages	= &aarch32_sig_page,
253 	},
254 };
255 
256 static int aarch32_alloc_kuser_vdso_page(void)
257 {
258 	extern char __kuser_helper_start[], __kuser_helper_end[];
259 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
260 	unsigned long vdso_page;
261 
262 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
263 		return 0;
264 
265 	vdso_page = get_zeroed_page(GFP_ATOMIC);
266 	if (!vdso_page)
267 		return -ENOMEM;
268 
269 	memcpy((void *)(vdso_page + 0x1000 - kuser_sz), __kuser_helper_start,
270 	       kuser_sz);
271 	aarch32_vectors_page = virt_to_page(vdso_page);
272 	flush_dcache_page(aarch32_vectors_page);
273 	return 0;
274 }
275 
276 static int aarch32_alloc_sigpage(void)
277 {
278 	extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
279 	int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
280 	unsigned long sigpage;
281 
282 	sigpage = get_zeroed_page(GFP_ATOMIC);
283 	if (!sigpage)
284 		return -ENOMEM;
285 
286 	memcpy((void *)sigpage, __aarch32_sigret_code_start, sigret_sz);
287 	aarch32_sig_page = virt_to_page(sigpage);
288 	flush_dcache_page(aarch32_sig_page);
289 	return 0;
290 }
291 
292 #ifdef CONFIG_COMPAT_VDSO
293 static int __aarch32_alloc_vdso_pages(void)
294 {
295 	vdso_info[VDSO_ABI_AA32].dm = &aarch32_vdso_maps[AA32_MAP_VVAR];
296 	vdso_info[VDSO_ABI_AA32].cm = &aarch32_vdso_maps[AA32_MAP_VDSO];
297 
298 	return __vdso_init(VDSO_ABI_AA32);
299 }
300 #endif /* CONFIG_COMPAT_VDSO */
301 
302 static int __init aarch32_alloc_vdso_pages(void)
303 {
304 	int ret;
305 
306 #ifdef CONFIG_COMPAT_VDSO
307 	ret = __aarch32_alloc_vdso_pages();
308 	if (ret)
309 		return ret;
310 #endif
311 
312 	ret = aarch32_alloc_sigpage();
313 	if (ret)
314 		return ret;
315 
316 	return aarch32_alloc_kuser_vdso_page();
317 }
318 arch_initcall(aarch32_alloc_vdso_pages);
319 
320 static int aarch32_kuser_helpers_setup(struct mm_struct *mm)
321 {
322 	void *ret;
323 
324 	if (!IS_ENABLED(CONFIG_KUSER_HELPERS))
325 		return 0;
326 
327 	/*
328 	 * Avoid VM_MAYWRITE for compatibility with arch/arm/, where it's
329 	 * not safe to CoW the page containing the CPU exception vectors.
330 	 */
331 	ret = _install_special_mapping(mm, AARCH32_VECTORS_BASE, PAGE_SIZE,
332 				       VM_READ | VM_EXEC |
333 				       VM_MAYREAD | VM_MAYEXEC,
334 				       &aarch32_vdso_maps[AA32_MAP_VECTORS]);
335 
336 	return PTR_ERR_OR_ZERO(ret);
337 }
338 
339 static int aarch32_sigreturn_setup(struct mm_struct *mm)
340 {
341 	unsigned long addr;
342 	void *ret;
343 
344 	addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
345 	if (IS_ERR_VALUE(addr)) {
346 		ret = ERR_PTR(addr);
347 		goto out;
348 	}
349 
350 	/*
351 	 * VM_MAYWRITE is required to allow gdb to Copy-on-Write and
352 	 * set breakpoints.
353 	 */
354 	ret = _install_special_mapping(mm, addr, PAGE_SIZE,
355 				       VM_READ | VM_EXEC | VM_MAYREAD |
356 				       VM_MAYWRITE | VM_MAYEXEC,
357 				       &aarch32_vdso_maps[AA32_MAP_SIGPAGE]);
358 	if (IS_ERR(ret))
359 		goto out;
360 
361 	mm->context.sigpage = (void *)addr;
362 
363 out:
364 	return PTR_ERR_OR_ZERO(ret);
365 }
366 
367 int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
368 {
369 	struct mm_struct *mm = current->mm;
370 	int ret;
371 
372 	if (mmap_write_lock_killable(mm))
373 		return -EINTR;
374 
375 	ret = aarch32_kuser_helpers_setup(mm);
376 	if (ret)
377 		goto out;
378 
379 #ifdef CONFIG_COMPAT_VDSO
380 	ret = __setup_additional_pages(VDSO_ABI_AA32,
381 				       mm,
382 				       bprm,
383 				       uses_interp);
384 	if (ret)
385 		goto out;
386 #endif /* CONFIG_COMPAT_VDSO */
387 
388 	ret = aarch32_sigreturn_setup(mm);
389 out:
390 	mmap_write_unlock(mm);
391 	return ret;
392 }
393 #endif /* CONFIG_COMPAT */
394 
395 static int vdso_mremap(const struct vm_special_mapping *sm,
396 		struct vm_area_struct *new_vma)
397 {
398 	return __vdso_remap(VDSO_ABI_AA64, sm, new_vma);
399 }
400 
401 enum aarch64_map {
402 	AA64_MAP_VVAR,
403 	AA64_MAP_VDSO,
404 };
405 
406 static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = {
407 	[AA64_MAP_VVAR] = {
408 		.name	= "[vvar]",
409 		.fault = vvar_fault,
410 	},
411 	[AA64_MAP_VDSO] = {
412 		.name	= "[vdso]",
413 		.mremap = vdso_mremap,
414 	},
415 };
416 
417 static int __init vdso_init(void)
418 {
419 	vdso_info[VDSO_ABI_AA64].dm = &aarch64_vdso_maps[AA64_MAP_VVAR];
420 	vdso_info[VDSO_ABI_AA64].cm = &aarch64_vdso_maps[AA64_MAP_VDSO];
421 
422 	return __vdso_init(VDSO_ABI_AA64);
423 }
424 arch_initcall(vdso_init);
425 
426 int arch_setup_additional_pages(struct linux_binprm *bprm,
427 				int uses_interp)
428 {
429 	struct mm_struct *mm = current->mm;
430 	int ret;
431 
432 	if (mmap_write_lock_killable(mm))
433 		return -EINTR;
434 
435 	ret = __setup_additional_pages(VDSO_ABI_AA64,
436 				       mm,
437 				       bprm,
438 				       uses_interp);
439 
440 	mmap_write_unlock(mm);
441 
442 	return ret;
443 }
444