xref: /linux/arch/x86/entry/vdso/vma.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
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/vdso_datastore.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/tlb.h>
24 #include <asm/page.h>
25 #include <asm/desc.h>
26 #include <asm/cpufeature.h>
27 #include <asm/vdso/vsyscall.h>
28 #include <clocksource/hyperv_timer.h>
29 
30 static_assert(VDSO_NR_PAGES + VDSO_NR_VCLOCK_PAGES == __VDSO_PAGES);
31 
32 unsigned int vclocks_used __read_mostly;
33 
34 #if defined(CONFIG_X86_64)
35 unsigned int __read_mostly vdso64_enabled = 1;
36 #endif
37 
38 int __init init_vdso_image(const struct vdso_image *image)
39 {
40 	BUILD_BUG_ON(VDSO_CLOCKMODE_MAX >= 32);
41 	BUG_ON(image->size % PAGE_SIZE != 0);
42 
43 	apply_alternatives((struct alt_instr *)(image->data + image->alt),
44 			   (struct alt_instr *)(image->data + image->alt +
45 						image->alt_len));
46 
47 	return 0;
48 }
49 
50 struct linux_binprm;
51 
52 static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
53 		      struct vm_area_struct *vma, struct vm_fault *vmf)
54 {
55 	const struct vdso_image *image = vma->vm_mm->context.vdso_image;
56 
57 	if (!image || (vmf->pgoff << PAGE_SHIFT) >= image->size)
58 		return VM_FAULT_SIGBUS;
59 
60 	vmf->page = virt_to_page(image->data + (vmf->pgoff << PAGE_SHIFT));
61 	get_page(vmf->page);
62 	return 0;
63 }
64 
65 static void vdso_fix_landing(const struct vdso_image *image,
66 		struct vm_area_struct *new_vma)
67 {
68 	struct pt_regs *regs = current_pt_regs();
69 	unsigned long ipoffset = regs->ip -
70 		(unsigned long)current->mm->context.vdso;
71 
72 	if (ipoffset < image->size)
73 		regs->ip = new_vma->vm_start + ipoffset;
74 }
75 
76 static int vdso_mremap(const struct vm_special_mapping *sm,
77 		struct vm_area_struct *new_vma)
78 {
79 	const struct vdso_image *image = current->mm->context.vdso_image;
80 
81 	vdso_fix_landing(image, new_vma);
82 	current->mm->context.vdso = (void __user *)new_vma->vm_start;
83 
84 	return 0;
85 }
86 
87 static vm_fault_t vvar_vclock_fault(const struct vm_special_mapping *sm,
88 				    struct vm_area_struct *vma, struct vm_fault *vmf)
89 {
90 	switch (vmf->pgoff) {
91 #ifdef CONFIG_PARAVIRT_CLOCK
92 	case VDSO_PAGE_PVCLOCK_OFFSET:
93 	{
94 		struct pvclock_vsyscall_time_info *pvti =
95 			pvclock_get_pvti_cpu0_va();
96 
97 		if (pvti && vclock_was_used(VDSO_CLOCKMODE_PVCLOCK))
98 			return vmf_insert_pfn_prot(vma, vmf->address,
99 					__pa(pvti) >> PAGE_SHIFT,
100 					pgprot_decrypted(vma->vm_page_prot));
101 		break;
102 	}
103 #endif /* CONFIG_PARAVIRT_CLOCK */
104 #ifdef CONFIG_HYPERV_TIMER
105 	case VDSO_PAGE_HVCLOCK_OFFSET:
106 	{
107 		unsigned long pfn = hv_get_tsc_pfn();
108 		if (pfn && vclock_was_used(VDSO_CLOCKMODE_HVCLOCK))
109 			return vmf_insert_pfn(vma, vmf->address, pfn);
110 		break;
111 	}
112 #endif /* CONFIG_HYPERV_TIMER */
113 	}
114 
115 	return VM_FAULT_SIGBUS;
116 }
117 
118 static const struct vm_special_mapping vdso_mapping = {
119 	.name = "[vdso]",
120 	.fault = vdso_fault,
121 	.mremap = vdso_mremap,
122 };
123 static const struct vm_special_mapping vvar_vclock_mapping = {
124 	.name = "[vvar_vclock]",
125 	.fault = vvar_vclock_fault,
126 };
127 
128 /*
129  * Add vdso and vvar mappings to current process.
130  * @image          - blob to map
131  * @addr           - request a specific address (zero to map at free addr)
132  */
133 static int map_vdso(const struct vdso_image *image, unsigned long addr)
134 {
135 	struct mm_struct *mm = current->mm;
136 	struct vm_area_struct *vma;
137 	unsigned long text_start;
138 	int ret = 0;
139 
140 	if (mmap_write_lock_killable(mm))
141 		return -EINTR;
142 
143 	addr = get_unmapped_area(NULL, addr,
144 				 image->size + __VDSO_PAGES * PAGE_SIZE, 0, 0);
145 	if (IS_ERR_VALUE(addr)) {
146 		ret = addr;
147 		goto up_fail;
148 	}
149 
150 	text_start = addr + __VDSO_PAGES * PAGE_SIZE;
151 
152 	/*
153 	 * MAYWRITE to allow gdb to COW and set breakpoints
154 	 */
155 	vma = _install_special_mapping(mm,
156 				       text_start,
157 				       image->size,
158 				       VM_READ|VM_EXEC|
159 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
160 				       VM_SEALED_SYSMAP,
161 				       &vdso_mapping);
162 
163 	if (IS_ERR(vma)) {
164 		ret = PTR_ERR(vma);
165 		goto up_fail;
166 	}
167 
168 	vma = vdso_install_vvar_mapping(mm, addr);
169 	if (IS_ERR(vma)) {
170 		ret = PTR_ERR(vma);
171 		do_munmap(mm, text_start, image->size, NULL);
172 		goto up_fail;
173 	}
174 
175 	vma = _install_special_mapping(mm,
176 				       VDSO_VCLOCK_PAGES_START(addr),
177 				       VDSO_NR_VCLOCK_PAGES * PAGE_SIZE,
178 				       VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
179 				       VM_PFNMAP|VM_SEALED_SYSMAP,
180 				       &vvar_vclock_mapping);
181 
182 	if (IS_ERR(vma)) {
183 		ret = PTR_ERR(vma);
184 		do_munmap(mm, text_start, image->size, NULL);
185 		do_munmap(mm, addr, image->size, NULL);
186 		goto up_fail;
187 	}
188 
189 	current->mm->context.vdso = (void __user *)text_start;
190 	current->mm->context.vdso_image = image;
191 
192 up_fail:
193 	mmap_write_unlock(mm);
194 	return ret;
195 }
196 
197 int map_vdso_once(const struct vdso_image *image, unsigned long addr)
198 {
199 	struct mm_struct *mm = current->mm;
200 	struct vm_area_struct *vma;
201 	VMA_ITERATOR(vmi, mm, 0);
202 
203 	mmap_write_lock(mm);
204 	/*
205 	 * Check if we have already mapped vdso blob - fail to prevent
206 	 * abusing from userspace install_special_mapping, which may
207 	 * not do accounting and rlimit right.
208 	 * We could search vma near context.vdso, but it's a slowpath,
209 	 * so let's explicitly check all VMAs to be completely sure.
210 	 */
211 	for_each_vma(vmi, vma) {
212 		if (vma_is_special_mapping(vma, &vdso_mapping) ||
213 				vma_is_special_mapping(vma, &vdso_vvar_mapping) ||
214 				vma_is_special_mapping(vma, &vvar_vclock_mapping)) {
215 			mmap_write_unlock(mm);
216 			return -EEXIST;
217 		}
218 	}
219 	mmap_write_unlock(mm);
220 
221 	return map_vdso(image, addr);
222 }
223 
224 static int load_vdso32(void)
225 {
226 	if (vdso32_enabled != 1)  /* Other values all mean "disabled" */
227 		return 0;
228 
229 	return map_vdso(&vdso32_image, 0);
230 }
231 
232 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
233 {
234 	if (IS_ENABLED(CONFIG_X86_64)) {
235 		if (!vdso64_enabled)
236 			return 0;
237 
238 		return map_vdso(&vdso64_image, 0);
239 	}
240 
241 	return load_vdso32();
242 }
243 
244 #ifdef CONFIG_COMPAT
245 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
246 				       int uses_interp, bool x32)
247 {
248 	if (IS_ENABLED(CONFIG_X86_X32_ABI) && x32) {
249 		if (!vdso64_enabled)
250 			return 0;
251 		return map_vdso(&vdsox32_image, 0);
252 	}
253 
254 	if (IS_ENABLED(CONFIG_IA32_EMULATION))
255 		return load_vdso32();
256 
257 	return 0;
258 }
259 #endif
260 
261 bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
262 {
263 	const struct vdso_image *image = current->mm->context.vdso_image;
264 	unsigned long vdso = (unsigned long) current->mm->context.vdso;
265 
266 	if (in_ia32_syscall() && image == &vdso32_image) {
267 		if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad ||
268 		    regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad)
269 			return true;
270 	}
271 	return false;
272 }
273 
274 #ifdef CONFIG_X86_64
275 static __init int vdso_setup(char *s)
276 {
277 	vdso64_enabled = simple_strtoul(s, NULL, 0);
278 	return 1;
279 }
280 __setup("vdso=", vdso_setup);
281 #endif /* CONFIG_X86_64 */
282