xref: /openbmc/linux/arch/x86/entry/vdso/vma.c (revision e9adcfec)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2007 Andi Kleen, SUSE Labs.
4  *
5  * This contains most of the x86 vDSO kernel-side code.
6  */
7 #include <linux/mm.h>
8 #include <linux/err.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/random.h>
14 #include <linux/elf.h>
15 #include <linux/cpu.h>
16 #include <linux/ptrace.h>
17 #include <linux/time_namespace.h>
18 
19 #include <asm/pvclock.h>
20 #include <asm/vgtod.h>
21 #include <asm/proto.h>
22 #include <asm/vdso.h>
23 #include <asm/vvar.h>
24 #include <asm/tlb.h>
25 #include <asm/page.h>
26 #include <asm/desc.h>
27 #include <asm/cpufeature.h>
28 #include <clocksource/hyperv_timer.h>
29 
30 #undef _ASM_X86_VVAR_H
31 #define EMIT_VVAR(name, offset)	\
32 	const size_t name ## _offset = offset;
33 #include <asm/vvar.h>
34 
35 struct vdso_data *arch_get_vdso_data(void *vvar_page)
36 {
37 	return (struct vdso_data *)(vvar_page + _vdso_data_offset);
38 }
39 #undef EMIT_VVAR
40 
41 unsigned int vclocks_used __read_mostly;
42 
43 #if defined(CONFIG_X86_64)
44 unsigned int __read_mostly vdso64_enabled = 1;
45 #endif
46 
47 void __init init_vdso_image(const struct vdso_image *image)
48 {
49 	BUG_ON(image->size % PAGE_SIZE != 0);
50 
51 	apply_alternatives((struct alt_instr *)(image->data + image->alt),
52 			   (struct alt_instr *)(image->data + image->alt +
53 						image->alt_len));
54 }
55 
56 static const struct vm_special_mapping vvar_mapping;
57 struct linux_binprm;
58 
59 static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
60 		      struct vm_area_struct *vma, struct vm_fault *vmf)
61 {
62 	const struct vdso_image *image = vma->vm_mm->context.vdso_image;
63 
64 	if (!image || (vmf->pgoff << PAGE_SHIFT) >= image->size)
65 		return VM_FAULT_SIGBUS;
66 
67 	vmf->page = virt_to_page(image->data + (vmf->pgoff << PAGE_SHIFT));
68 	get_page(vmf->page);
69 	return 0;
70 }
71 
72 static void vdso_fix_landing(const struct vdso_image *image,
73 		struct vm_area_struct *new_vma)
74 {
75 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
76 	if (in_ia32_syscall() && image == &vdso_image_32) {
77 		struct pt_regs *regs = current_pt_regs();
78 		unsigned long vdso_land = image->sym_int80_landing_pad;
79 		unsigned long old_land_addr = vdso_land +
80 			(unsigned long)current->mm->context.vdso;
81 
82 		/* Fixing userspace landing - look at do_fast_syscall_32 */
83 		if (regs->ip == old_land_addr)
84 			regs->ip = new_vma->vm_start + vdso_land;
85 	}
86 #endif
87 }
88 
89 static int vdso_mremap(const struct vm_special_mapping *sm,
90 		struct vm_area_struct *new_vma)
91 {
92 	const struct vdso_image *image = current->mm->context.vdso_image;
93 
94 	vdso_fix_landing(image, new_vma);
95 	current->mm->context.vdso = (void __user *)new_vma->vm_start;
96 
97 	return 0;
98 }
99 
100 #ifdef CONFIG_TIME_NS
101 /*
102  * The vvar page layout depends on whether a task belongs to the root or
103  * non-root time namespace. Whenever a task changes its namespace, the VVAR
104  * page tables are cleared and then they will re-faulted with a
105  * corresponding layout.
106  * See also the comment near timens_setup_vdso_data() for details.
107  */
108 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
109 {
110 	struct mm_struct *mm = task->mm;
111 	struct vm_area_struct *vma;
112 	VMA_ITERATOR(vmi, mm, 0);
113 
114 	mmap_read_lock(mm);
115 	for_each_vma(vmi, vma) {
116 		if (vma_is_special_mapping(vma, &vvar_mapping))
117 			zap_vma_pages(vma);
118 	}
119 	mmap_read_unlock(mm);
120 
121 	return 0;
122 }
123 #endif
124 
125 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
126 		      struct vm_area_struct *vma, struct vm_fault *vmf)
127 {
128 	const struct vdso_image *image = vma->vm_mm->context.vdso_image;
129 	unsigned long pfn;
130 	long sym_offset;
131 
132 	if (!image)
133 		return VM_FAULT_SIGBUS;
134 
135 	sym_offset = (long)(vmf->pgoff << PAGE_SHIFT) +
136 		image->sym_vvar_start;
137 
138 	/*
139 	 * Sanity check: a symbol offset of zero means that the page
140 	 * does not exist for this vdso image, not that the page is at
141 	 * offset zero relative to the text mapping.  This should be
142 	 * impossible here, because sym_offset should only be zero for
143 	 * the page past the end of the vvar mapping.
144 	 */
145 	if (sym_offset == 0)
146 		return VM_FAULT_SIGBUS;
147 
148 	if (sym_offset == image->sym_vvar_page) {
149 		struct page *timens_page = find_timens_vvar_page(vma);
150 
151 		pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
152 
153 		/*
154 		 * If a task belongs to a time namespace then a namespace
155 		 * specific VVAR is mapped with the sym_vvar_page offset and
156 		 * the real VVAR page is mapped with the sym_timens_page
157 		 * offset.
158 		 * See also the comment near timens_setup_vdso_data().
159 		 */
160 		if (timens_page) {
161 			unsigned long addr;
162 			vm_fault_t err;
163 
164 			/*
165 			 * Optimization: inside time namespace pre-fault
166 			 * VVAR page too. As on timens page there are only
167 			 * offsets for clocks on VVAR, it'll be faulted
168 			 * shortly by VDSO code.
169 			 */
170 			addr = vmf->address + (image->sym_timens_page - sym_offset);
171 			err = vmf_insert_pfn(vma, addr, pfn);
172 			if (unlikely(err & VM_FAULT_ERROR))
173 				return err;
174 
175 			pfn = page_to_pfn(timens_page);
176 		}
177 
178 		return vmf_insert_pfn(vma, vmf->address, pfn);
179 	} else if (sym_offset == image->sym_pvclock_page) {
180 		struct pvclock_vsyscall_time_info *pvti =
181 			pvclock_get_pvti_cpu0_va();
182 		if (pvti && vclock_was_used(VDSO_CLOCKMODE_PVCLOCK)) {
183 			return vmf_insert_pfn_prot(vma, vmf->address,
184 					__pa(pvti) >> PAGE_SHIFT,
185 					pgprot_decrypted(vma->vm_page_prot));
186 		}
187 	} else if (sym_offset == image->sym_hvclock_page) {
188 		pfn = hv_get_tsc_pfn();
189 
190 		if (pfn && vclock_was_used(VDSO_CLOCKMODE_HVCLOCK))
191 			return vmf_insert_pfn(vma, vmf->address, pfn);
192 	} else if (sym_offset == image->sym_timens_page) {
193 		struct page *timens_page = find_timens_vvar_page(vma);
194 
195 		if (!timens_page)
196 			return VM_FAULT_SIGBUS;
197 
198 		pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
199 		return vmf_insert_pfn(vma, vmf->address, pfn);
200 	}
201 
202 	return VM_FAULT_SIGBUS;
203 }
204 
205 static const struct vm_special_mapping vdso_mapping = {
206 	.name = "[vdso]",
207 	.fault = vdso_fault,
208 	.mremap = vdso_mremap,
209 };
210 static const struct vm_special_mapping vvar_mapping = {
211 	.name = "[vvar]",
212 	.fault = vvar_fault,
213 };
214 
215 /*
216  * Add vdso and vvar mappings to current process.
217  * @image          - blob to map
218  * @addr           - request a specific address (zero to map at free addr)
219  */
220 static int map_vdso(const struct vdso_image *image, unsigned long addr)
221 {
222 	struct mm_struct *mm = current->mm;
223 	struct vm_area_struct *vma;
224 	unsigned long text_start;
225 	int ret = 0;
226 
227 	if (mmap_write_lock_killable(mm))
228 		return -EINTR;
229 
230 	addr = get_unmapped_area(NULL, addr,
231 				 image->size - image->sym_vvar_start, 0, 0);
232 	if (IS_ERR_VALUE(addr)) {
233 		ret = addr;
234 		goto up_fail;
235 	}
236 
237 	text_start = addr - image->sym_vvar_start;
238 
239 	/*
240 	 * MAYWRITE to allow gdb to COW and set breakpoints
241 	 */
242 	vma = _install_special_mapping(mm,
243 				       text_start,
244 				       image->size,
245 				       VM_READ|VM_EXEC|
246 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
247 				       &vdso_mapping);
248 
249 	if (IS_ERR(vma)) {
250 		ret = PTR_ERR(vma);
251 		goto up_fail;
252 	}
253 
254 	vma = _install_special_mapping(mm,
255 				       addr,
256 				       -image->sym_vvar_start,
257 				       VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
258 				       VM_PFNMAP,
259 				       &vvar_mapping);
260 
261 	if (IS_ERR(vma)) {
262 		ret = PTR_ERR(vma);
263 		do_munmap(mm, text_start, image->size, NULL);
264 	} else {
265 		current->mm->context.vdso = (void __user *)text_start;
266 		current->mm->context.vdso_image = image;
267 	}
268 
269 up_fail:
270 	mmap_write_unlock(mm);
271 	return ret;
272 }
273 
274 #ifdef CONFIG_X86_64
275 /*
276  * Put the vdso above the (randomized) stack with another randomized
277  * offset.  This way there is no hole in the middle of address space.
278  * To save memory make sure it is still in the same PTE as the stack
279  * top.  This doesn't give that many random bits.
280  *
281  * Note that this algorithm is imperfect: the distribution of the vdso
282  * start address within a PMD is biased toward the end.
283  *
284  * Only used for the 64-bit and x32 vdsos.
285  */
286 static unsigned long vdso_addr(unsigned long start, unsigned len)
287 {
288 	unsigned long addr, end;
289 	unsigned offset;
290 
291 	/*
292 	 * Round up the start address.  It can start out unaligned as a result
293 	 * of stack start randomization.
294 	 */
295 	start = PAGE_ALIGN(start);
296 
297 	/* Round the lowest possible end address up to a PMD boundary. */
298 	end = (start + len + PMD_SIZE - 1) & PMD_MASK;
299 	if (end >= TASK_SIZE_MAX)
300 		end = TASK_SIZE_MAX;
301 	end -= len;
302 
303 	if (end > start) {
304 		offset = get_random_u32_below(((end - start) >> PAGE_SHIFT) + 1);
305 		addr = start + (offset << PAGE_SHIFT);
306 	} else {
307 		addr = start;
308 	}
309 
310 	/*
311 	 * Forcibly align the final address in case we have a hardware
312 	 * issue that requires alignment for performance reasons.
313 	 */
314 	addr = align_vdso_addr(addr);
315 
316 	return addr;
317 }
318 
319 static int map_vdso_randomized(const struct vdso_image *image)
320 {
321 	unsigned long addr = vdso_addr(current->mm->start_stack, image->size-image->sym_vvar_start);
322 
323 	return map_vdso(image, addr);
324 }
325 #endif
326 
327 int map_vdso_once(const struct vdso_image *image, unsigned long addr)
328 {
329 	struct mm_struct *mm = current->mm;
330 	struct vm_area_struct *vma;
331 	VMA_ITERATOR(vmi, mm, 0);
332 
333 	mmap_write_lock(mm);
334 	/*
335 	 * Check if we have already mapped vdso blob - fail to prevent
336 	 * abusing from userspace install_special_mapping, which may
337 	 * not do accounting and rlimit right.
338 	 * We could search vma near context.vdso, but it's a slowpath,
339 	 * so let's explicitly check all VMAs to be completely sure.
340 	 */
341 	for_each_vma(vmi, vma) {
342 		if (vma_is_special_mapping(vma, &vdso_mapping) ||
343 				vma_is_special_mapping(vma, &vvar_mapping)) {
344 			mmap_write_unlock(mm);
345 			return -EEXIST;
346 		}
347 	}
348 	mmap_write_unlock(mm);
349 
350 	return map_vdso(image, addr);
351 }
352 
353 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
354 static int load_vdso32(void)
355 {
356 	if (vdso32_enabled != 1)  /* Other values all mean "disabled" */
357 		return 0;
358 
359 	return map_vdso(&vdso_image_32, 0);
360 }
361 #endif
362 
363 #ifdef CONFIG_X86_64
364 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
365 {
366 	if (!vdso64_enabled)
367 		return 0;
368 
369 	return map_vdso_randomized(&vdso_image_64);
370 }
371 
372 #ifdef CONFIG_COMPAT
373 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
374 				       int uses_interp, bool x32)
375 {
376 #ifdef CONFIG_X86_X32_ABI
377 	if (x32) {
378 		if (!vdso64_enabled)
379 			return 0;
380 		return map_vdso_randomized(&vdso_image_x32);
381 	}
382 #endif
383 #ifdef CONFIG_IA32_EMULATION
384 	return load_vdso32();
385 #else
386 	return 0;
387 #endif
388 }
389 #endif
390 #else
391 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
392 {
393 	return load_vdso32();
394 }
395 #endif
396 
397 bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
398 {
399 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
400 	const struct vdso_image *image = current->mm->context.vdso_image;
401 	unsigned long vdso = (unsigned long) current->mm->context.vdso;
402 
403 	if (in_ia32_syscall() && image == &vdso_image_32) {
404 		if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad ||
405 		    regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad)
406 			return true;
407 	}
408 #endif
409 	return false;
410 }
411 
412 #ifdef CONFIG_X86_64
413 static __init int vdso_setup(char *s)
414 {
415 	vdso64_enabled = simple_strtoul(s, NULL, 0);
416 	return 1;
417 }
418 __setup("vdso=", vdso_setup);
419 
420 static int __init init_vdso(void)
421 {
422 	BUILD_BUG_ON(VDSO_CLOCKMODE_MAX >= 32);
423 
424 	init_vdso_image(&vdso_image_64);
425 
426 #ifdef CONFIG_X86_X32_ABI
427 	init_vdso_image(&vdso_image_x32);
428 #endif
429 
430 	return 0;
431 }
432 subsys_initcall(init_vdso);
433 #endif /* CONFIG_X86_64 */
434