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