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