2 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
3 * Copyright 2003 Andi Kleen, SuSE Labs.
5 * [ NOTE: this mechanism is now deprecated in favor of the vDSO. ]
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.
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.
17 * Note: the concept clashes with user mode linux. UML users should
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 #include <linux/topology.h>
31 #include <linux/timekeeper_internal.h>
32 #include <linux/getcpu.h>
33 #include <linux/cpu.h>
34 #include <linux/smp.h>
35 #include <linux/notifier.h>
36 #include <linux/syscalls.h>
37 #include <linux/ratelimit.h>
39 #include <asm/vsyscall.h>
40 #include <asm/pgtable.h>
41 #include <asm/compat.h>
43 #include <asm/unistd.h>
44 #include <asm/fixmap.h>
45 #include <asm/errno.h>
47 #include <asm/segment.h>
49 #include <asm/topology.h>
50 #include <asm/traps.h>
52 #define CREATE_TRACE_POINTS
53 #include "vsyscall_trace.h"
55 DEFINE_VVAR(int, vgetcpu_mode
);
57 static enum { EMULATE
, NATIVE
, NONE
} vsyscall_mode
= EMULATE
;
59 static int __init
vsyscall_setup(char *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
))
76 early_param("vsyscall", vsyscall_setup
);
78 static void warn_bad_vsyscall(const char *level
, struct pt_regs
*regs
,
81 if (!show_unhandled_signals
)
84 printk_ratelimited("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
85 level
, current
->comm
, task_pid_nr(current
),
86 message
, regs
->ip
, regs
->cs
,
87 regs
->sp
, regs
->ax
, regs
->si
, regs
->di
);
90 static int addr_to_vsyscall_nr(unsigned long addr
)
94 if ((addr
& ~0xC00UL
) != VSYSCALL_ADDR
)
97 nr
= (addr
& 0xC00UL
) >> 10;
104 static bool write_ok_or_segv(unsigned long ptr
, size_t size
)
107 * XXX: if access_ok, get_user, and put_user handled
108 * sig_on_uaccess_error, this could go away.
111 if (!access_ok(VERIFY_WRITE
, (void __user
*)ptr
, size
)) {
113 struct thread_struct
*thread
= ¤t
->thread
;
115 thread
->error_code
= 6; /* user fault, no page, write */
117 thread
->trap_nr
= X86_TRAP_PF
;
119 memset(&info
, 0, sizeof(info
));
120 info
.si_signo
= SIGSEGV
;
122 info
.si_code
= SEGV_MAPERR
;
123 info
.si_addr
= (void __user
*)ptr
;
125 force_sig_info(SIGSEGV
, &info
, current
);
132 bool emulate_vsyscall(struct pt_regs
*regs
, unsigned long address
)
134 struct task_struct
*tsk
;
135 unsigned long caller
;
136 int vsyscall_nr
, syscall_nr
, tmp
;
137 int prev_sig_on_uaccess_error
;
141 * No point in checking CS -- the only way to get here is a user mode
142 * trap to a high address, which means that we're in 64-bit user code.
145 WARN_ON_ONCE(address
!= regs
->ip
);
147 if (vsyscall_mode
== NONE
) {
148 warn_bad_vsyscall(KERN_INFO
, regs
,
149 "vsyscall attempted with vsyscall=none");
153 vsyscall_nr
= addr_to_vsyscall_nr(address
);
155 trace_emulate_vsyscall(vsyscall_nr
);
157 if (vsyscall_nr
< 0) {
158 warn_bad_vsyscall(KERN_WARNING
, regs
,
159 "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
163 if (get_user(caller
, (unsigned long __user
*)regs
->sp
) != 0) {
164 warn_bad_vsyscall(KERN_WARNING
, regs
,
165 "vsyscall with bad stack (exploit attempt?)");
172 * Check for access_ok violations and find the syscall nr.
174 * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
175 * 64-bit, so we don't need to special-case it here. For all the
176 * vsyscalls, NULL means "don't write anything" not "write it at
179 switch (vsyscall_nr
) {
181 if (!write_ok_or_segv(regs
->di
, sizeof(struct timeval
)) ||
182 !write_ok_or_segv(regs
->si
, sizeof(struct timezone
))) {
187 syscall_nr
= __NR_gettimeofday
;
191 if (!write_ok_or_segv(regs
->di
, sizeof(time_t))) {
196 syscall_nr
= __NR_time
;
200 if (!write_ok_or_segv(regs
->di
, sizeof(unsigned)) ||
201 !write_ok_or_segv(regs
->si
, sizeof(unsigned))) {
206 syscall_nr
= __NR_getcpu
;
211 * Handle seccomp. regs->ip must be the original value.
212 * See seccomp_send_sigsys and Documentation/prctl/seccomp_filter.txt.
214 * We could optimize the seccomp disabled case, but performance
215 * here doesn't matter.
217 regs
->orig_ax
= syscall_nr
;
219 tmp
= secure_computing();
220 if ((!tmp
&& regs
->orig_ax
!= syscall_nr
) || regs
->ip
!= address
) {
221 warn_bad_vsyscall(KERN_DEBUG
, regs
,
222 "seccomp tried to change syscall nr or ip");
226 goto do_ret
; /* skip requested */
229 * With a real vsyscall, page faults cause SIGSEGV. We want to
230 * preserve that behavior to make writing exploits harder.
232 prev_sig_on_uaccess_error
= current_thread_info()->sig_on_uaccess_error
;
233 current_thread_info()->sig_on_uaccess_error
= 1;
236 switch (vsyscall_nr
) {
238 ret
= sys_gettimeofday(
239 (struct timeval __user
*)regs
->di
,
240 (struct timezone __user
*)regs
->si
);
244 ret
= sys_time((time_t __user
*)regs
->di
);
248 ret
= sys_getcpu((unsigned __user
*)regs
->di
,
249 (unsigned __user
*)regs
->si
,
254 current_thread_info()->sig_on_uaccess_error
= prev_sig_on_uaccess_error
;
257 if (ret
== -EFAULT
) {
258 /* Bad news -- userspace fed a bad pointer to a vsyscall. */
259 warn_bad_vsyscall(KERN_INFO
, regs
,
260 "vsyscall fault (exploit attempt?)");
263 * If we failed to generate a signal for any reason,
264 * generate one here. (This should be impossible.)
266 if (WARN_ON_ONCE(!sigismember(&tsk
->pending
.signal
, SIGBUS
) &&
267 !sigismember(&tsk
->pending
.signal
, SIGSEGV
)))
270 return true; /* Don't emulate the ret. */
276 /* Emulate a ret instruction. */
282 force_sig(SIGSEGV
, current
);
287 * Assume __initcall executes before all user space. Hopefully kmod
288 * doesn't violate that. We'll find out if it does.
290 static void vsyscall_set_cpu(int cpu
)
293 unsigned long node
= 0;
295 node
= cpu_to_node(cpu
);
297 if (cpu_has(&cpu_data(cpu
), X86_FEATURE_RDTSCP
))
298 write_rdtscp_aux((node
<< 12) | cpu
);
301 * Store cpu number in limit so that it can be loaded quickly
302 * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
304 d
= 0x0f40000000000ULL
;
306 d
|= (node
& 0xf) << 12;
307 d
|= (node
>> 4) << 48;
309 write_gdt_entry(get_cpu_gdt_table(cpu
), GDT_ENTRY_PER_CPU
, &d
, DESCTYPE_S
);
312 static void cpu_vsyscall_init(void *arg
)
314 /* preemption should be already off */
315 vsyscall_set_cpu(raw_smp_processor_id());
319 cpu_vsyscall_notifier(struct notifier_block
*n
, unsigned long action
, void *arg
)
321 long cpu
= (long)arg
;
323 if (action
== CPU_ONLINE
|| action
== CPU_ONLINE_FROZEN
)
324 smp_call_function_single(cpu
, cpu_vsyscall_init
, NULL
, 1);
329 void __init
map_vsyscall(void)
331 extern char __vsyscall_page
;
332 unsigned long physaddr_vsyscall
= __pa_symbol(&__vsyscall_page
);
334 __set_fixmap(VSYSCALL_PAGE
, physaddr_vsyscall
,
335 vsyscall_mode
== NATIVE
336 ? PAGE_KERNEL_VSYSCALL
338 BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE
) !=
339 (unsigned long)VSYSCALL_ADDR
);
342 static int __init
vsyscall_init(void)
344 cpu_notifier_register_begin();
346 on_each_cpu(cpu_vsyscall_init
, NULL
, 1);
347 /* notifier priority > KVM */
348 __hotcpu_notifier(cpu_vsyscall_notifier
, 30);
350 cpu_notifier_register_done();
354 __initcall(vsyscall_init
);