blob: b2e58a248b3bf2dfa68be8cfcb1d57077d4f7514 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
3 * Copyright 2003 Andi Kleen, SuSE Labs.
4 *
Andy Lutomirski5cec93c2011-06-05 13:50:24 -04005 * [ NOTE: this mechanism is now deprecated in favor of the vDSO. ]
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040017 * Note: the concept clashes with user mode linux. UML users should
18 * use the vDSO.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Joe Perchesc767a542012-05-21 19:50:07 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Paul Gortmaker29574022011-05-26 12:33:18 -040030#include <linux/topology.h>
john stultz7460ed22007-02-16 01:28:21 -080031#include <linux/clocksource.h>
Vojtech Pavlikc08c8202006-09-26 10:52:28 +020032#include <linux/getcpu.h>
Andi Kleen8c131af2006-11-14 16:57:46 +010033#include <linux/cpu.h>
34#include <linux/smp.h>
35#include <linux/notifier.h>
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040036#include <linux/syscalls.h>
37#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/vsyscall.h>
40#include <asm/pgtable.h>
Andy Lutomirskic9712942011-07-13 09:24:09 -040041#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/page.h>
john stultz7460ed22007-02-16 01:28:21 -080043#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/fixmap.h>
45#include <asm/errno.h>
46#include <asm/io.h>
Vojtech Pavlikc08c8202006-09-26 10:52:28 +020047#include <asm/segment.h>
48#include <asm/desc.h>
49#include <asm/topology.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020050#include <asm/vgtod.h>
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040051#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Andy Lutomirskic149a662011-08-03 09:31:54 -040053#define CREATE_TRACE_POINTS
54#include "vsyscall_trace.h"
55
Andy Lutomirski8c49d9a2011-05-23 09:31:24 -040056DEFINE_VVAR(int, vgetcpu_mode);
Thomas Gleixner2ab51652012-02-28 19:46:04 +000057DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Andy Lutomirski2e57ae02011-11-07 16:33:41 -080059static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
Andy Lutomirski3ae36652011-08-10 11:15:32 -040060
61static 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}
78early_param("vsyscall", vsyscall_setup);
79
Tony Breeds2c622142007-10-18 03:04:57 -070080void update_vsyscall_tz(void)
81{
Tony Breeds2c622142007-10-18 03:04:57 -070082 vsyscall_gtod_data.sys_tz = sys_tz;
Tony Breeds2c622142007-10-18 03:04:57 -070083}
84
John Stultz76158562010-07-13 17:56:23 -070085void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
86 struct clocksource *clock, u32 mult)
john stultz7460ed22007-02-16 01:28:21 -080087{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -070088 struct timespec monotonic;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040089
Thomas Gleixner68fe7b22012-03-24 09:29:22 +010090 write_seqcount_begin(&vsyscall_gtod_data.seq);
91
john stultz7460ed22007-02-16 01:28:21 -080092 /* copy vsyscall data */
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040093 vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040094 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;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -070098
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040099 vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
100 vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700101
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
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400106 vsyscall_gtod_data.wall_time_coarse = __current_kernel_time();
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700107 vsyscall_gtod_data.monotonic_time_coarse =
108 timespec_add(vsyscall_gtod_data.wall_time_coarse, *wtm);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400109
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000110 write_seqcount_end(&vsyscall_gtod_data.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400113static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
114 const char *message)
115{
Joe Perchesc767a542012-05-21 19:50:07 -0700116 if (!show_unhandled_signals)
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400117 return;
118
Joe Perchesc767a542012-05-21 19:50:07 -0700119 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);
Andy Lutomirskic9712942011-07-13 09:24:09 -0400123}
124
125static 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;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400137}
138
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800139static bool write_ok_or_segv(unsigned long ptr, size_t size)
140{
141 /*
142 * XXX: if access_ok, get_user, and put_user handled
143 * sig_on_uaccess_error, this could go away.
144 */
145
146 if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
147 siginfo_t info;
148 struct thread_struct *thread = &current->thread;
149
150 thread->error_code = 6; /* user fault, no page, write */
151 thread->cr2 = ptr;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530152 thread->trap_nr = X86_TRAP_PF;
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800153
154 memset(&info, 0, sizeof(info));
155 info.si_signo = SIGSEGV;
156 info.si_errno = 0;
157 info.si_code = SEGV_MAPERR;
158 info.si_addr = (void __user *)ptr;
159
160 force_sig_info(SIGSEGV, &info, current);
161 return false;
162 } else {
163 return true;
164 }
165}
166
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400167bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400168{
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400169 struct task_struct *tsk;
170 unsigned long caller;
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700171 int vsyscall_nr, syscall_nr, tmp;
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800172 int prev_sig_on_uaccess_error;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400173 long ret;
174
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400175 /*
176 * No point in checking CS -- the only way to get here is a user mode
177 * trap to a high address, which means that we're in 64-bit user code.
178 */
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400179
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400180 WARN_ON_ONCE(address != regs->ip);
Andy Lutomirskic9712942011-07-13 09:24:09 -0400181
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400182 if (vsyscall_mode == NONE) {
183 warn_bad_vsyscall(KERN_INFO, regs,
184 "vsyscall attempted with vsyscall=none");
185 return false;
Andy Lutomirskic9712942011-07-13 09:24:09 -0400186 }
187
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400188 vsyscall_nr = addr_to_vsyscall_nr(address);
Andy Lutomirskic149a662011-08-03 09:31:54 -0400189
190 trace_emulate_vsyscall(vsyscall_nr);
191
Andy Lutomirskic9712942011-07-13 09:24:09 -0400192 if (vsyscall_nr < 0) {
193 warn_bad_vsyscall(KERN_WARNING, regs,
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400194 "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400195 goto sigsegv;
196 }
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400197
198 if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400199 warn_bad_vsyscall(KERN_WARNING, regs,
200 "vsyscall with bad stack (exploit attempt?)");
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400201 goto sigsegv;
202 }
203
204 tsk = current;
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700205
206 /*
207 * Check for access_ok violations and find the syscall nr.
208 *
209 * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
210 * 64-bit, so we don't need to special-case it here. For all the
211 * vsyscalls, NULL means "don't write anything" not "write it at
212 * address 0".
213 */
214 switch (vsyscall_nr) {
215 case 0:
216 if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
217 !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
218 ret = -EFAULT;
219 goto check_fault;
220 }
221
222 syscall_nr = __NR_gettimeofday;
223 break;
224
225 case 1:
226 if (!write_ok_or_segv(regs->di, sizeof(time_t))) {
227 ret = -EFAULT;
228 goto check_fault;
229 }
230
231 syscall_nr = __NR_time;
232 break;
233
234 case 2:
235 if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
236 !write_ok_or_segv(regs->si, sizeof(unsigned))) {
237 ret = -EFAULT;
238 goto check_fault;
239 }
240
241 syscall_nr = __NR_getcpu;
242 break;
243 }
244
245 /*
246 * Handle seccomp. regs->ip must be the original value.
247 * See seccomp_send_sigsys and Documentation/prctl/seccomp_filter.txt.
248 *
249 * We could optimize the seccomp disabled case, but performance
250 * here doesn't matter.
251 */
252 regs->orig_ax = syscall_nr;
253 regs->ax = -ENOSYS;
254 tmp = secure_computing(syscall_nr);
255 if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
256 warn_bad_vsyscall(KERN_DEBUG, regs,
257 "seccomp tried to change syscall nr or ip");
258 do_exit(SIGSYS);
259 }
260 if (tmp)
261 goto do_ret; /* skip requested */
262
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800263 /*
264 * With a real vsyscall, page faults cause SIGSEGV. We want to
265 * preserve that behavior to make writing exploits harder.
266 */
267 prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
268 current_thread_info()->sig_on_uaccess_error = 1;
269
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800270 ret = -EFAULT;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400271 switch (vsyscall_nr) {
272 case 0:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400273 ret = sys_gettimeofday(
274 (struct timeval __user *)regs->di,
275 (struct timezone __user *)regs->si);
276 break;
277
278 case 1:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400279 ret = sys_time((time_t __user *)regs->di);
280 break;
281
282 case 2:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400283 ret = sys_getcpu((unsigned __user *)regs->di,
284 (unsigned __user *)regs->si,
Emil Goode46ed99d2012-04-01 20:48:04 +0200285 NULL);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400286 break;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400287 }
288
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800289 current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
290
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700291check_fault:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400292 if (ret == -EFAULT) {
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800293 /* Bad news -- userspace fed a bad pointer to a vsyscall. */
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400294 warn_bad_vsyscall(KERN_INFO, regs,
295 "vsyscall fault (exploit attempt?)");
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800296
297 /*
298 * If we failed to generate a signal for any reason,
299 * generate one here. (This should be impossible.)
300 */
301 if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
302 !sigismember(&tsk->pending.signal, SIGSEGV)))
303 goto sigsegv;
304
305 return true; /* Don't emulate the ret. */
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400306 }
307
308 regs->ax = ret;
309
Will Drewry56517212012-07-13 12:06:35 -0500310do_ret:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400311 /* Emulate a ret instruction. */
312 regs->ip = caller;
313 regs->sp += 8;
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400314 return true;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400315
316sigsegv:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400317 force_sig(SIGSEGV, current);
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400318 return true;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400319}
320
321/*
322 * Assume __initcall executes before all user space. Hopefully kmod
323 * doesn't violate that. We'll find out if it does.
john stultz7460ed22007-02-16 01:28:21 -0800324 */
Andi Kleen8c131af2006-11-14 16:57:46 +0100325static void __cpuinit vsyscall_set_cpu(int cpu)
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200326{
Jeremy Fitzhardingefc8b8a62008-06-25 00:19:01 -0400327 unsigned long d;
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200328 unsigned long node = 0;
329#ifdef CONFIG_NUMA
Mike Travis98c9e272007-10-17 18:04:39 +0200330 node = cpu_to_node(cpu);
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200331#endif
Mike Travis92cb7612007-10-19 20:35:04 +0200332 if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
Andi Kleen8c131af2006-11-14 16:57:46 +0100333 write_rdtscp_aux((node << 12) | cpu);
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200334
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400335 /*
336 * Store cpu number in limit so that it can be loaded quickly
337 * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
338 */
Jeremy Fitzhardingefc8b8a62008-06-25 00:19:01 -0400339 d = 0x0f40000000000ULL;
340 d |= cpu;
341 d |= (node & 0xf) << 12;
342 d |= (node >> 4) << 48;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400343
Jeremy Fitzhardingefc8b8a62008-06-25 00:19:01 -0400344 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200345}
346
Andi Kleen8c131af2006-11-14 16:57:46 +0100347static void __cpuinit cpu_vsyscall_init(void *arg)
348{
349 /* preemption should be already off */
350 vsyscall_set_cpu(raw_smp_processor_id());
351}
352
353static int __cpuinit
354cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
355{
356 long cpu = (long)arg;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400357
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700358 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
Jens Axboe8691e5a2008-06-06 11:18:06 +0200359 smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400360
Andi Kleen8c131af2006-11-14 16:57:46 +0100361 return NOTIFY_DONE;
362}
363
Ingo Molnare4026442008-01-30 13:32:39 +0100364void __init map_vsyscall(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400366 extern char __vsyscall_page;
367 unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
Andy Lutomirski9fd67b42011-06-05 13:50:19 -0400368 extern char __vvar_page;
369 unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400371 __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
372 vsyscall_mode == NATIVE
373 ? PAGE_KERNEL_VSYSCALL
374 : PAGE_KERNEL_VVAR);
375 BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
376 (unsigned long)VSYSCALL_START);
377
Andy Lutomirski9fd67b42011-06-05 13:50:19 -0400378 __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400379 BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
380 (unsigned long)VVAR_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static int __init vsyscall_init(void)
384{
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400385 BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
386
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200387 on_each_cpu(cpu_vsyscall_init, NULL, 1);
Sheng Yangbe43f832009-12-18 16:48:45 +0800388 /* notifier priority > KVM */
389 hotcpu_notifier(cpu_vsyscall_notifier, 30);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393__initcall(vsyscall_init);