Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE |
3 | * Copyright 2003 Andi Kleen, SuSE Labs. | |
4 | * | |
5cec93c2 AL |
5 | * [ NOTE: this mechanism is now deprecated in favor of the vDSO. ] |
6 | * | |
1da177e4 LT |
7 | * Thanks to hpa@transmeta.com for some useful hint. |
8 | * Special thanks to Ingo Molnar for his early experience with | |
9 | * a different vsyscall implementation for Linux/IA32 and for the name. | |
10 | * | |
11 | * vsyscall 1 is located at -10Mbyte, vsyscall 2 is located | |
12 | * at virtual address -10Mbyte+1024bytes etc... There are at max 4 | |
13 | * vsyscalls. One vsyscall can reserve more than 1 slot to avoid | |
14 | * jumping out of line if necessary. We cannot add more with this | |
15 | * mechanism because older kernels won't return -ENOSYS. | |
1da177e4 | 16 | * |
5cec93c2 AL |
17 | * Note: the concept clashes with user mode linux. UML users should |
18 | * use the vDSO. | |
1da177e4 LT |
19 | */ |
20 | ||
c767a54b JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
1da177e4 LT |
23 | #include <linux/time.h> |
24 | #include <linux/init.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/timer.h> | |
27 | #include <linux/seqlock.h> | |
28 | #include <linux/jiffies.h> | |
29 | #include <linux/sysctl.h> | |
29574022 | 30 | #include <linux/topology.h> |
189374ae | 31 | #include <linux/timekeeper_internal.h> |
c08c8205 | 32 | #include <linux/getcpu.h> |
8c131af1 AK |
33 | #include <linux/cpu.h> |
34 | #include <linux/smp.h> | |
35 | #include <linux/notifier.h> | |
5cec93c2 AL |
36 | #include <linux/syscalls.h> |
37 | #include <linux/ratelimit.h> | |
1da177e4 LT |
38 | |
39 | #include <asm/vsyscall.h> | |
40 | #include <asm/pgtable.h> | |
c9712944 | 41 | #include <asm/compat.h> |
1da177e4 | 42 | #include <asm/page.h> |
7460ed28 | 43 | #include <asm/unistd.h> |
1da177e4 LT |
44 | #include <asm/fixmap.h> |
45 | #include <asm/errno.h> | |
46 | #include <asm/io.h> | |
c08c8205 VP |
47 | #include <asm/segment.h> |
48 | #include <asm/desc.h> | |
49 | #include <asm/topology.h> | |
2aae950b | 50 | #include <asm/vgtod.h> |
5cec93c2 | 51 | #include <asm/traps.h> |
1da177e4 | 52 | |
c149a665 AL |
53 | #define CREATE_TRACE_POINTS |
54 | #include "vsyscall_trace.h" | |
55 | ||
8c49d9a7 | 56 | DEFINE_VVAR(int, vgetcpu_mode); |
2ab51657 | 57 | DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data); |
1da177e4 | 58 | |
2e57ae05 | 59 | static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE; |
3ae36655 AL |
60 | |
61 | static int __init vsyscall_setup(char *str) | |
62 | { | |
63 | if (str) { | |
64 | if (!strcmp("emulate", str)) | |
65 | vsyscall_mode = EMULATE; | |
66 | else if (!strcmp("native", str)) | |
67 | vsyscall_mode = NATIVE; | |
68 | else if (!strcmp("none", str)) | |
69 | vsyscall_mode = NONE; | |
70 | else | |
71 | return -EINVAL; | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | return -EINVAL; | |
77 | } | |
78 | early_param("vsyscall", vsyscall_setup); | |
79 | ||
2c622148 TB |
80 | void update_vsyscall_tz(void) |
81 | { | |
2c622148 | 82 | vsyscall_gtod_data.sys_tz = sys_tz; |
2c622148 TB |
83 | } |
84 | ||
70639421 | 85 | void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm, |
7615856e | 86 | struct clocksource *clock, u32 mult) |
1da177e4 | 87 | { |
91ec87d5 | 88 | struct timespec monotonic; |
5cec93c2 | 89 | |
68fe7b23 TG |
90 | write_seqcount_begin(&vsyscall_gtod_data.seq); |
91 | ||
7460ed28 | 92 | /* copy vsyscall data */ |
98d0ac38 | 93 | vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode; |
5cec93c2 AL |
94 | vsyscall_gtod_data.clock.cycle_last = clock->cycle_last; |
95 | vsyscall_gtod_data.clock.mask = clock->mask; | |
96 | vsyscall_gtod_data.clock.mult = mult; | |
97 | vsyscall_gtod_data.clock.shift = clock->shift; | |
91ec87d5 | 98 | |
5cec93c2 AL |
99 | vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec; |
100 | vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec; | |
91ec87d5 AL |
101 | |
102 | monotonic = timespec_add(*wall_time, *wtm); | |
103 | vsyscall_gtod_data.monotonic_time_sec = monotonic.tv_sec; | |
104 | vsyscall_gtod_data.monotonic_time_nsec = monotonic.tv_nsec; | |
105 | ||
5cec93c2 | 106 | vsyscall_gtod_data.wall_time_coarse = __current_kernel_time(); |
91ec87d5 AL |
107 | vsyscall_gtod_data.monotonic_time_coarse = |
108 | timespec_add(vsyscall_gtod_data.wall_time_coarse, *wtm); | |
5cec93c2 | 109 | |
2ab51657 | 110 | write_seqcount_end(&vsyscall_gtod_data.seq); |
1da177e4 LT |
111 | } |
112 | ||
5cec93c2 AL |
113 | static void warn_bad_vsyscall(const char *level, struct pt_regs *regs, |
114 | const char *message) | |
1da177e4 | 115 | { |
c767a54b | 116 | if (!show_unhandled_signals) |
5cec93c2 | 117 | return; |
1da177e4 | 118 | |
c767a54b JP |
119 | pr_notice_ratelimited("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n", |
120 | level, current->comm, task_pid_nr(current), | |
121 | message, regs->ip, regs->cs, | |
122 | regs->sp, regs->ax, regs->si, regs->di); | |
c9712944 AL |
123 | } |
124 | ||
125 | static int addr_to_vsyscall_nr(unsigned long addr) | |
126 | { | |
127 | int nr; | |
128 | ||
129 | if ((addr & ~0xC00UL) != VSYSCALL_START) | |
130 | return -EINVAL; | |
131 | ||
132 | nr = (addr & 0xC00UL) >> 10; | |
133 | if (nr >= 3) | |
134 | return -EINVAL; | |
135 | ||
136 | return nr; | |
1da177e4 LT |
137 | } |
138 | ||
09d31442 | 139 | #ifdef CONFIG_SECCOMP |
5651721e WD |
140 | static int vsyscall_seccomp(struct task_struct *tsk, int syscall_nr) |
141 | { | |
142 | if (!seccomp_mode(&tsk->seccomp)) | |
143 | return 0; | |
144 | task_pt_regs(tsk)->orig_ax = syscall_nr; | |
145 | task_pt_regs(tsk)->ax = syscall_nr; | |
146 | return __secure_computing(syscall_nr); | |
147 | } | |
09d31442 WD |
148 | #else |
149 | #define vsyscall_seccomp(_tsk, _nr) 0 | |
150 | #endif | |
5651721e | 151 | |
4fc34901 AL |
152 | static bool write_ok_or_segv(unsigned long ptr, size_t size) |
153 | { | |
154 | /* | |
155 | * XXX: if access_ok, get_user, and put_user handled | |
156 | * sig_on_uaccess_error, this could go away. | |
157 | */ | |
158 | ||
159 | if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) { | |
160 | siginfo_t info; | |
161 | struct thread_struct *thread = ¤t->thread; | |
162 | ||
163 | thread->error_code = 6; /* user fault, no page, write */ | |
164 | thread->cr2 = ptr; | |
51e7dc70 | 165 | thread->trap_nr = X86_TRAP_PF; |
4fc34901 AL |
166 | |
167 | memset(&info, 0, sizeof(info)); | |
168 | info.si_signo = SIGSEGV; | |
169 | info.si_errno = 0; | |
170 | info.si_code = SEGV_MAPERR; | |
171 | info.si_addr = (void __user *)ptr; | |
172 | ||
173 | force_sig_info(SIGSEGV, &info, current); | |
174 | return false; | |
175 | } else { | |
176 | return true; | |
177 | } | |
178 | } | |
179 | ||
3ae36655 | 180 | bool emulate_vsyscall(struct pt_regs *regs, unsigned long address) |
1da177e4 | 181 | { |
5cec93c2 AL |
182 | struct task_struct *tsk; |
183 | unsigned long caller; | |
184 | int vsyscall_nr; | |
4fc34901 | 185 | int prev_sig_on_uaccess_error; |
5cec93c2 | 186 | long ret; |
5651721e | 187 | int skip; |
5cec93c2 | 188 | |
3ae36655 AL |
189 | /* |
190 | * No point in checking CS -- the only way to get here is a user mode | |
191 | * trap to a high address, which means that we're in 64-bit user code. | |
192 | */ | |
5cec93c2 | 193 | |
3ae36655 | 194 | WARN_ON_ONCE(address != regs->ip); |
c9712944 | 195 | |
3ae36655 AL |
196 | if (vsyscall_mode == NONE) { |
197 | warn_bad_vsyscall(KERN_INFO, regs, | |
198 | "vsyscall attempted with vsyscall=none"); | |
199 | return false; | |
c9712944 AL |
200 | } |
201 | ||
3ae36655 | 202 | vsyscall_nr = addr_to_vsyscall_nr(address); |
c149a665 AL |
203 | |
204 | trace_emulate_vsyscall(vsyscall_nr); | |
205 | ||
c9712944 AL |
206 | if (vsyscall_nr < 0) { |
207 | warn_bad_vsyscall(KERN_WARNING, regs, | |
3ae36655 | 208 | "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround"); |
5cec93c2 AL |
209 | goto sigsegv; |
210 | } | |
d0aff6e6 | 211 | |
5cec93c2 | 212 | if (get_user(caller, (unsigned long __user *)regs->sp) != 0) { |
3ae36655 AL |
213 | warn_bad_vsyscall(KERN_WARNING, regs, |
214 | "vsyscall with bad stack (exploit attempt?)"); | |
5cec93c2 AL |
215 | goto sigsegv; |
216 | } | |
8c73626a | 217 | |
5cec93c2 | 218 | tsk = current; |
4fc34901 AL |
219 | /* |
220 | * With a real vsyscall, page faults cause SIGSEGV. We want to | |
221 | * preserve that behavior to make writing exploits harder. | |
222 | */ | |
223 | prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error; | |
224 | current_thread_info()->sig_on_uaccess_error = 1; | |
225 | ||
226 | /* | |
46ed99d1 | 227 | * NULL is a valid user pointer (in the access_ok sense) on 32-bit and |
4fc34901 | 228 | * 64-bit, so we don't need to special-case it here. For all the |
46ed99d1 | 229 | * vsyscalls, NULL means "don't write anything" not "write it at |
4fc34901 AL |
230 | * address 0". |
231 | */ | |
232 | ret = -EFAULT; | |
5651721e | 233 | skip = 0; |
5cec93c2 AL |
234 | switch (vsyscall_nr) { |
235 | case 0: | |
5651721e WD |
236 | skip = vsyscall_seccomp(tsk, __NR_gettimeofday); |
237 | if (skip) | |
238 | break; | |
239 | ||
4fc34901 AL |
240 | if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) || |
241 | !write_ok_or_segv(regs->si, sizeof(struct timezone))) | |
242 | break; | |
243 | ||
5cec93c2 AL |
244 | ret = sys_gettimeofday( |
245 | (struct timeval __user *)regs->di, | |
246 | (struct timezone __user *)regs->si); | |
247 | break; | |
248 | ||
249 | case 1: | |
5651721e WD |
250 | skip = vsyscall_seccomp(tsk, __NR_time); |
251 | if (skip) | |
252 | break; | |
253 | ||
4fc34901 AL |
254 | if (!write_ok_or_segv(regs->di, sizeof(time_t))) |
255 | break; | |
256 | ||
5cec93c2 AL |
257 | ret = sys_time((time_t __user *)regs->di); |
258 | break; | |
259 | ||
260 | case 2: | |
5651721e WD |
261 | skip = vsyscall_seccomp(tsk, __NR_getcpu); |
262 | if (skip) | |
263 | break; | |
264 | ||
4fc34901 AL |
265 | if (!write_ok_or_segv(regs->di, sizeof(unsigned)) || |
266 | !write_ok_or_segv(regs->si, sizeof(unsigned))) | |
267 | break; | |
268 | ||
5cec93c2 AL |
269 | ret = sys_getcpu((unsigned __user *)regs->di, |
270 | (unsigned __user *)regs->si, | |
46ed99d1 | 271 | NULL); |
5cec93c2 | 272 | break; |
5cec93c2 | 273 | } |
8c73626a | 274 | |
4fc34901 AL |
275 | current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error; |
276 | ||
5651721e WD |
277 | if (skip) { |
278 | if ((long)regs->ax <= 0L) /* seccomp errno emulation */ | |
279 | goto do_ret; | |
280 | goto done; /* seccomp trace/trap */ | |
281 | } | |
282 | ||
5cec93c2 | 283 | if (ret == -EFAULT) { |
4fc34901 | 284 | /* Bad news -- userspace fed a bad pointer to a vsyscall. */ |
5cec93c2 AL |
285 | warn_bad_vsyscall(KERN_INFO, regs, |
286 | "vsyscall fault (exploit attempt?)"); | |
4fc34901 AL |
287 | |
288 | /* | |
289 | * If we failed to generate a signal for any reason, | |
290 | * generate one here. (This should be impossible.) | |
291 | */ | |
292 | if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) && | |
293 | !sigismember(&tsk->pending.signal, SIGSEGV))) | |
294 | goto sigsegv; | |
295 | ||
296 | return true; /* Don't emulate the ret. */ | |
5cec93c2 | 297 | } |
8c73626a | 298 | |
5cec93c2 | 299 | regs->ax = ret; |
1da177e4 | 300 | |
5651721e | 301 | do_ret: |
5cec93c2 AL |
302 | /* Emulate a ret instruction. */ |
303 | regs->ip = caller; | |
304 | regs->sp += 8; | |
5651721e | 305 | done: |
3ae36655 | 306 | return true; |
5cec93c2 AL |
307 | |
308 | sigsegv: | |
5cec93c2 | 309 | force_sig(SIGSEGV, current); |
3ae36655 | 310 | return true; |
1da177e4 LT |
311 | } |
312 | ||
5cec93c2 AL |
313 | /* |
314 | * Assume __initcall executes before all user space. Hopefully kmod | |
315 | * doesn't violate that. We'll find out if it does. | |
316 | */ | |
8c131af1 | 317 | static void __cpuinit vsyscall_set_cpu(int cpu) |
c08c8205 | 318 | { |
fc8b8a60 | 319 | unsigned long d; |
c08c8205 VP |
320 | unsigned long node = 0; |
321 | #ifdef CONFIG_NUMA | |
98c9e27a | 322 | node = cpu_to_node(cpu); |
c08c8205 | 323 | #endif |
92cb7612 | 324 | if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP)) |
8c131af1 | 325 | write_rdtscp_aux((node << 12) | cpu); |
c08c8205 | 326 | |
5cec93c2 AL |
327 | /* |
328 | * Store cpu number in limit so that it can be loaded quickly | |
329 | * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node) | |
330 | */ | |
fc8b8a60 JF |
331 | d = 0x0f40000000000ULL; |
332 | d |= cpu; | |
333 | d |= (node & 0xf) << 12; | |
334 | d |= (node >> 4) << 48; | |
5cec93c2 | 335 | |
fc8b8a60 | 336 | write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S); |
c08c8205 VP |
337 | } |
338 | ||
8c131af1 AK |
339 | static void __cpuinit cpu_vsyscall_init(void *arg) |
340 | { | |
341 | /* preemption should be already off */ | |
342 | vsyscall_set_cpu(raw_smp_processor_id()); | |
343 | } | |
344 | ||
345 | static int __cpuinit | |
346 | cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg) | |
347 | { | |
348 | long cpu = (long)arg; | |
5cec93c2 | 349 | |
8bb78442 | 350 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) |
8691e5a8 | 351 | smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1); |
5cec93c2 | 352 | |
8c131af1 AK |
353 | return NOTIFY_DONE; |
354 | } | |
355 | ||
e4026440 | 356 | void __init map_vsyscall(void) |
1da177e4 | 357 | { |
3ae36655 AL |
358 | extern char __vsyscall_page; |
359 | unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page); | |
9fd67b4e AL |
360 | extern char __vvar_page; |
361 | unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page); | |
1da177e4 | 362 | |
3ae36655 AL |
363 | __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall, |
364 | vsyscall_mode == NATIVE | |
365 | ? PAGE_KERNEL_VSYSCALL | |
366 | : PAGE_KERNEL_VVAR); | |
367 | BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) != | |
368 | (unsigned long)VSYSCALL_START); | |
369 | ||
9fd67b4e | 370 | __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR); |
3ae36655 AL |
371 | BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) != |
372 | (unsigned long)VVAR_ADDRESS); | |
1da177e4 LT |
373 | } |
374 | ||
375 | static int __init vsyscall_init(void) | |
376 | { | |
5cec93c2 AL |
377 | BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)); |
378 | ||
15c8b6c1 | 379 | on_each_cpu(cpu_vsyscall_init, NULL, 1); |
be43f83d SY |
380 | /* notifier priority > KVM */ |
381 | hotcpu_notifier(cpu_vsyscall_notifier, 30); | |
5cec93c2 | 382 | |
1da177e4 LT |
383 | return 0; |
384 | } | |
1da177e4 | 385 | __initcall(vsyscall_init); |