blob: cdc95a707cd14ab4a0700e320521f17172ae1986 [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
21#include <linux/time.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/timer.h>
25#include <linux/seqlock.h>
26#include <linux/jiffies.h>
27#include <linux/sysctl.h>
Paul Gortmaker29574022011-05-26 12:33:18 -040028#include <linux/topology.h>
john stultz7460ed22007-02-16 01:28:21 -080029#include <linux/clocksource.h>
Vojtech Pavlikc08c8202006-09-26 10:52:28 +020030#include <linux/getcpu.h>
Andi Kleen8c131af2006-11-14 16:57:46 +010031#include <linux/cpu.h>
32#include <linux/smp.h>
33#include <linux/notifier.h>
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040034#include <linux/syscalls.h>
35#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/vsyscall.h>
38#include <asm/pgtable.h>
Andy Lutomirskic9712942011-07-13 09:24:09 -040039#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/page.h>
john stultz7460ed22007-02-16 01:28:21 -080041#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/fixmap.h>
43#include <asm/errno.h>
44#include <asm/io.h>
Vojtech Pavlikc08c8202006-09-26 10:52:28 +020045#include <asm/segment.h>
46#include <asm/desc.h>
47#include <asm/topology.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020048#include <asm/vgtod.h>
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040049#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Lutomirskic149a662011-08-03 09:31:54 -040051#define CREATE_TRACE_POINTS
52#include "vsyscall_trace.h"
53
Andy Lutomirski8c49d9a2011-05-23 09:31:24 -040054DEFINE_VVAR(int, vgetcpu_mode);
Thomas Gleixner2ab51652012-02-28 19:46:04 +000055DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Andy Lutomirski2e57ae02011-11-07 16:33:41 -080057static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
Andy Lutomirski3ae36652011-08-10 11:15:32 -040058
59static int __init vsyscall_setup(char *str)
60{
61 if (str) {
62 if (!strcmp("emulate", str))
63 vsyscall_mode = EMULATE;
64 else if (!strcmp("native", str))
65 vsyscall_mode = NATIVE;
66 else if (!strcmp("none", str))
67 vsyscall_mode = NONE;
68 else
69 return -EINVAL;
70
71 return 0;
72 }
73
74 return -EINVAL;
75}
76early_param("vsyscall", vsyscall_setup);
77
Tony Breeds2c622142007-10-18 03:04:57 -070078void update_vsyscall_tz(void)
79{
Tony Breeds2c622142007-10-18 03:04:57 -070080 vsyscall_gtod_data.sys_tz = sys_tz;
Tony Breeds2c622142007-10-18 03:04:57 -070081}
82
John Stultz76158562010-07-13 17:56:23 -070083void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
84 struct clocksource *clock, u32 mult)
john stultz7460ed22007-02-16 01:28:21 -080085{
Thomas Gleixner2ab51652012-02-28 19:46:04 +000086 write_seqcount_begin(&vsyscall_gtod_data.seq);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040087
john stultz7460ed22007-02-16 01:28:21 -080088 /* copy vsyscall data */
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040089 vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -040090 vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
91 vsyscall_gtod_data.clock.mask = clock->mask;
92 vsyscall_gtod_data.clock.mult = mult;
93 vsyscall_gtod_data.clock.shift = clock->shift;
94 vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
95 vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
96 vsyscall_gtod_data.wall_to_monotonic = *wtm;
97 vsyscall_gtod_data.wall_time_coarse = __current_kernel_time();
98
Thomas Gleixner2ab51652012-02-28 19:46:04 +000099 write_seqcount_end(&vsyscall_gtod_data.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400102static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
103 const char *message)
104{
105 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
106 struct task_struct *tsk;
107
108 if (!show_unhandled_signals || !__ratelimit(&rs))
109 return;
110
111 tsk = current;
112
Andy Lutomirskic9712942011-07-13 09:24:09 -0400113 printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400114 level, tsk->comm, task_pid_nr(tsk),
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400115 message, regs->ip, regs->cs,
Andy Lutomirskic9712942011-07-13 09:24:09 -0400116 regs->sp, regs->ax, regs->si, regs->di);
117}
118
119static int addr_to_vsyscall_nr(unsigned long addr)
120{
121 int nr;
122
123 if ((addr & ~0xC00UL) != VSYSCALL_START)
124 return -EINVAL;
125
126 nr = (addr & 0xC00UL) >> 10;
127 if (nr >= 3)
128 return -EINVAL;
129
130 return nr;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400131}
132
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800133static bool write_ok_or_segv(unsigned long ptr, size_t size)
134{
135 /*
136 * XXX: if access_ok, get_user, and put_user handled
137 * sig_on_uaccess_error, this could go away.
138 */
139
140 if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
141 siginfo_t info;
142 struct thread_struct *thread = &current->thread;
143
144 thread->error_code = 6; /* user fault, no page, write */
145 thread->cr2 = ptr;
146 thread->trap_no = 14;
147
148 memset(&info, 0, sizeof(info));
149 info.si_signo = SIGSEGV;
150 info.si_errno = 0;
151 info.si_code = SEGV_MAPERR;
152 info.si_addr = (void __user *)ptr;
153
154 force_sig_info(SIGSEGV, &info, current);
155 return false;
156 } else {
157 return true;
158 }
159}
160
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400161bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400162{
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400163 struct task_struct *tsk;
164 unsigned long caller;
165 int vsyscall_nr;
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800166 int prev_sig_on_uaccess_error;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400167 long ret;
168
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400169 /*
170 * No point in checking CS -- the only way to get here is a user mode
171 * trap to a high address, which means that we're in 64-bit user code.
172 */
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400173
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400174 WARN_ON_ONCE(address != regs->ip);
Andy Lutomirskic9712942011-07-13 09:24:09 -0400175
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400176 if (vsyscall_mode == NONE) {
177 warn_bad_vsyscall(KERN_INFO, regs,
178 "vsyscall attempted with vsyscall=none");
179 return false;
Andy Lutomirskic9712942011-07-13 09:24:09 -0400180 }
181
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400182 vsyscall_nr = addr_to_vsyscall_nr(address);
Andy Lutomirskic149a662011-08-03 09:31:54 -0400183
184 trace_emulate_vsyscall(vsyscall_nr);
185
Andy Lutomirskic9712942011-07-13 09:24:09 -0400186 if (vsyscall_nr < 0) {
187 warn_bad_vsyscall(KERN_WARNING, regs,
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400188 "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 -0400189 goto sigsegv;
190 }
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400191
192 if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400193 warn_bad_vsyscall(KERN_WARNING, regs,
194 "vsyscall with bad stack (exploit attempt?)");
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400195 goto sigsegv;
196 }
197
198 tsk = current;
199 if (seccomp_mode(&tsk->seccomp))
200 do_exit(SIGKILL);
201
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800202 /*
203 * With a real vsyscall, page faults cause SIGSEGV. We want to
204 * preserve that behavior to make writing exploits harder.
205 */
206 prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
207 current_thread_info()->sig_on_uaccess_error = 1;
208
209 /*
210 * 0 is a valid user pointer (in the access_ok sense) on 32-bit and
211 * 64-bit, so we don't need to special-case it here. For all the
212 * vsyscalls, 0 means "don't write anything" not "write it at
213 * address 0".
214 */
215 ret = -EFAULT;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400216 switch (vsyscall_nr) {
217 case 0:
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800218 if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
219 !write_ok_or_segv(regs->si, sizeof(struct timezone)))
220 break;
221
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400222 ret = sys_gettimeofday(
223 (struct timeval __user *)regs->di,
224 (struct timezone __user *)regs->si);
225 break;
226
227 case 1:
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800228 if (!write_ok_or_segv(regs->di, sizeof(time_t)))
229 break;
230
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400231 ret = sys_time((time_t __user *)regs->di);
232 break;
233
234 case 2:
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800235 if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
236 !write_ok_or_segv(regs->si, sizeof(unsigned)))
237 break;
238
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400239 ret = sys_getcpu((unsigned __user *)regs->di,
240 (unsigned __user *)regs->si,
241 0);
242 break;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400243 }
244
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800245 current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
246
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400247 if (ret == -EFAULT) {
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800248 /* Bad news -- userspace fed a bad pointer to a vsyscall. */
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400249 warn_bad_vsyscall(KERN_INFO, regs,
250 "vsyscall fault (exploit attempt?)");
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800251
252 /*
253 * If we failed to generate a signal for any reason,
254 * generate one here. (This should be impossible.)
255 */
256 if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
257 !sigismember(&tsk->pending.signal, SIGSEGV)))
258 goto sigsegv;
259
260 return true; /* Don't emulate the ret. */
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400261 }
262
263 regs->ax = ret;
264
265 /* Emulate a ret instruction. */
266 regs->ip = caller;
267 regs->sp += 8;
268
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400269 return true;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400270
271sigsegv:
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400272 force_sig(SIGSEGV, current);
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400273 return true;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400274}
275
276/*
277 * Assume __initcall executes before all user space. Hopefully kmod
278 * doesn't violate that. We'll find out if it does.
john stultz7460ed22007-02-16 01:28:21 -0800279 */
Andi Kleen8c131af2006-11-14 16:57:46 +0100280static void __cpuinit vsyscall_set_cpu(int cpu)
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200281{
Jeremy Fitzhardingefc8b8a62008-06-25 00:19:01 -0400282 unsigned long d;
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200283 unsigned long node = 0;
284#ifdef CONFIG_NUMA
Mike Travis98c9e272007-10-17 18:04:39 +0200285 node = cpu_to_node(cpu);
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200286#endif
Mike Travis92cb7612007-10-19 20:35:04 +0200287 if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
Andi Kleen8c131af2006-11-14 16:57:46 +0100288 write_rdtscp_aux((node << 12) | cpu);
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200289
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400290 /*
291 * Store cpu number in limit so that it can be loaded quickly
292 * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
293 */
Jeremy Fitzhardingefc8b8a62008-06-25 00:19:01 -0400294 d = 0x0f40000000000ULL;
295 d |= cpu;
296 d |= (node & 0xf) << 12;
297 d |= (node >> 4) << 48;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400298
Jeremy Fitzhardingefc8b8a62008-06-25 00:19:01 -0400299 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200300}
301
Andi Kleen8c131af2006-11-14 16:57:46 +0100302static void __cpuinit cpu_vsyscall_init(void *arg)
303{
304 /* preemption should be already off */
305 vsyscall_set_cpu(raw_smp_processor_id());
306}
307
308static int __cpuinit
309cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
310{
311 long cpu = (long)arg;
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400312
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700313 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
Jens Axboe8691e5a2008-06-06 11:18:06 +0200314 smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400315
Andi Kleen8c131af2006-11-14 16:57:46 +0100316 return NOTIFY_DONE;
317}
318
Ingo Molnare4026442008-01-30 13:32:39 +0100319void __init map_vsyscall(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400321 extern char __vsyscall_page;
322 unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
Andy Lutomirski9fd67b42011-06-05 13:50:19 -0400323 extern char __vvar_page;
324 unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400326 __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
327 vsyscall_mode == NATIVE
328 ? PAGE_KERNEL_VSYSCALL
329 : PAGE_KERNEL_VVAR);
330 BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
331 (unsigned long)VSYSCALL_START);
332
Andy Lutomirski9fd67b42011-06-05 13:50:19 -0400333 __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400334 BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
335 (unsigned long)VVAR_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338static int __init vsyscall_init(void)
339{
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400340 BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
341
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200342 on_each_cpu(cpu_vsyscall_init, NULL, 1);
Sheng Yangbe43f832009-12-18 16:48:45 +0800343 /* notifier priority > KVM */
344 hotcpu_notifier(cpu_vsyscall_notifier, 30);
Andy Lutomirski5cec93c2011-06-05 13:50:24 -0400345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348__initcall(vsyscall_init);