seccomp: Make syscall skipping and nr changes more consistent
[deliverable/linux.git] / arch / x86 / kernel / vsyscall_64.c
CommitLineData
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>
7460ed28 31#include <linux/clocksource.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 56DEFINE_VVAR(int, vgetcpu_mode);
2ab51657 57DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data);
1da177e4 58
2e57ae05 59static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
3ae36655
AL
60
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
2c622148
TB
80void update_vsyscall_tz(void)
81{
2c622148 82 vsyscall_gtod_data.sys_tz = sys_tz;
2c622148
TB
83}
84
7615856e
JS
85void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
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
113static 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
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;
1da177e4
LT
137}
138
4fc34901
AL
139static 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;
51e7dc70 152 thread->trap_nr = X86_TRAP_PF;
4fc34901
AL
153
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
3ae36655 167bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
1da177e4 168{
5cec93c2
AL
169 struct task_struct *tsk;
170 unsigned long caller;
87b526d3 171 int vsyscall_nr, syscall_nr, tmp;
4fc34901 172 int prev_sig_on_uaccess_error;
5cec93c2
AL
173 long ret;
174
3ae36655
AL
175 /*
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 */
5cec93c2 179
3ae36655 180 WARN_ON_ONCE(address != regs->ip);
c9712944 181
3ae36655
AL
182 if (vsyscall_mode == NONE) {
183 warn_bad_vsyscall(KERN_INFO, regs,
184 "vsyscall attempted with vsyscall=none");
185 return false;
c9712944
AL
186 }
187
3ae36655 188 vsyscall_nr = addr_to_vsyscall_nr(address);
c149a665
AL
189
190 trace_emulate_vsyscall(vsyscall_nr);
191
c9712944
AL
192 if (vsyscall_nr < 0) {
193 warn_bad_vsyscall(KERN_WARNING, regs,
3ae36655 194 "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
5cec93c2
AL
195 goto sigsegv;
196 }
d0aff6e6 197
5cec93c2 198 if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
3ae36655
AL
199 warn_bad_vsyscall(KERN_WARNING, regs,
200 "vsyscall with bad stack (exploit attempt?)");
5cec93c2
AL
201 goto sigsegv;
202 }
8c73626a 203
5cec93c2 204 tsk = current;
4fc34901
AL
205
206 /*
87b526d3
AL
207 * Check for access_ok violations and find the syscall nr.
208 *
46ed99d1 209 * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
4fc34901 210 * 64-bit, so we don't need to special-case it here. For all the
46ed99d1 211 * vsyscalls, NULL means "don't write anything" not "write it at
4fc34901
AL
212 * address 0".
213 */
5cec93c2
AL
214 switch (vsyscall_nr) {
215 case 0:
4fc34901 216 if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
87b526d3
AL
217 !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
218 ret = -EFAULT;
219 goto check_fault;
220 }
4fc34901 221
87b526d3
AL
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
263 /*
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
270 ret = -EFAULT;
271 switch (vsyscall_nr) {
272 case 0:
5cec93c2
AL
273 ret = sys_gettimeofday(
274 (struct timeval __user *)regs->di,
275 (struct timezone __user *)regs->si);
276 break;
277
278 case 1:
5cec93c2
AL
279 ret = sys_time((time_t __user *)regs->di);
280 break;
281
282 case 2:
5cec93c2
AL
283 ret = sys_getcpu((unsigned __user *)regs->di,
284 (unsigned __user *)regs->si,
46ed99d1 285 NULL);
5cec93c2 286 break;
5cec93c2 287 }
8c73626a 288
4fc34901
AL
289 current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
290
87b526d3 291check_fault:
5cec93c2 292 if (ret == -EFAULT) {
4fc34901 293 /* Bad news -- userspace fed a bad pointer to a vsyscall. */
5cec93c2
AL
294 warn_bad_vsyscall(KERN_INFO, regs,
295 "vsyscall fault (exploit attempt?)");
4fc34901
AL
296
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. */
5cec93c2 306 }
8c73626a 307
5cec93c2 308 regs->ax = ret;
1da177e4 309
5651721e 310do_ret:
5cec93c2
AL
311 /* Emulate a ret instruction. */
312 regs->ip = caller;
313 regs->sp += 8;
3ae36655 314 return true;
5cec93c2
AL
315
316sigsegv:
5cec93c2 317 force_sig(SIGSEGV, current);
3ae36655 318 return true;
1da177e4
LT
319}
320
5cec93c2
AL
321/*
322 * Assume __initcall executes before all user space. Hopefully kmod
323 * doesn't violate that. We'll find out if it does.
324 */
8c131af1 325static void __cpuinit vsyscall_set_cpu(int cpu)
c08c8205 326{
fc8b8a60 327 unsigned long d;
c08c8205
VP
328 unsigned long node = 0;
329#ifdef CONFIG_NUMA
98c9e27a 330 node = cpu_to_node(cpu);
c08c8205 331#endif
92cb7612 332 if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
8c131af1 333 write_rdtscp_aux((node << 12) | cpu);
c08c8205 334
5cec93c2
AL
335 /*
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 */
fc8b8a60
JF
339 d = 0x0f40000000000ULL;
340 d |= cpu;
341 d |= (node & 0xf) << 12;
342 d |= (node >> 4) << 48;
5cec93c2 343
fc8b8a60 344 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
c08c8205
VP
345}
346
8c131af1
AK
347static 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;
5cec93c2 357
8bb78442 358 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
8691e5a8 359 smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
5cec93c2 360
8c131af1
AK
361 return NOTIFY_DONE;
362}
363
e4026440 364void __init map_vsyscall(void)
1da177e4 365{
3ae36655
AL
366 extern char __vsyscall_page;
367 unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
9fd67b4e
AL
368 extern char __vvar_page;
369 unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
1da177e4 370
3ae36655
AL
371 __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
9fd67b4e 378 __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
3ae36655
AL
379 BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
380 (unsigned long)VVAR_ADDRESS);
1da177e4
LT
381}
382
383static int __init vsyscall_init(void)
384{
5cec93c2
AL
385 BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
386
15c8b6c1 387 on_each_cpu(cpu_vsyscall_init, NULL, 1);
be43f83d
SY
388 /* notifier priority > KVM */
389 hotcpu_notifier(cpu_vsyscall_notifier, 30);
5cec93c2 390
1da177e4
LT
391 return 0;
392}
1da177e4 393__initcall(vsyscall_init);
This page took 0.732393 seconds and 5 git commands to generate.