vsyscall_64.c 7.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 *  Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
 *  Copyright 2003 Andi Kleen, SuSE Labs.
 *
5 6
 *  [ NOTE: this mechanism is now deprecated in favor of the vDSO. ]
 *
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16
 *  Thanks to hpa@transmeta.com for some useful hint.
 *  Special thanks to Ingo Molnar for his early experience with
 *  a different vsyscall implementation for Linux/IA32 and for the name.
 *
 *  vsyscall 1 is located at -10Mbyte, vsyscall 2 is located
 *  at virtual address -10Mbyte+1024bytes etc... There are at max 4
 *  vsyscalls. One vsyscall can reserve more than 1 slot to avoid
 *  jumping out of line if necessary. We cannot add more with this
 *  mechanism because older kernels won't return -ENOSYS.
 *
17 18
 *  Note: the concept clashes with user mode linux.  UML users should
 *  use the vDSO.
L
Linus Torvalds 已提交
19 20
 */

21
/* Disable profiling for userspace code: */
22
#define DISABLE_BRANCH_PROFILING
23

L
Linus Torvalds 已提交
24 25 26 27 28 29 30
#include <linux/time.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/seqlock.h>
#include <linux/jiffies.h>
#include <linux/sysctl.h>
31
#include <linux/clocksource.h>
32
#include <linux/getcpu.h>
33 34 35
#include <linux/cpu.h>
#include <linux/smp.h>
#include <linux/notifier.h>
36 37
#include <linux/syscalls.h>
#include <linux/ratelimit.h>
L
Linus Torvalds 已提交
38 39 40

#include <asm/vsyscall.h>
#include <asm/pgtable.h>
41
#include <asm/compat.h>
L
Linus Torvalds 已提交
42
#include <asm/page.h>
43
#include <asm/unistd.h>
L
Linus Torvalds 已提交
44 45 46
#include <asm/fixmap.h>
#include <asm/errno.h>
#include <asm/io.h>
47 48 49
#include <asm/segment.h>
#include <asm/desc.h>
#include <asm/topology.h>
50
#include <asm/vgtod.h>
51
#include <asm/traps.h>
L
Linus Torvalds 已提交
52

53 54
DEFINE_VVAR(int, vgetcpu_mode);
DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data) =
L
Linus Torvalds 已提交
55
{
56
	.lock = __SEQLOCK_UNLOCKED(__vsyscall_gtod_data.lock),
57
};
L
Linus Torvalds 已提交
58

59 60 61 62 63 64 65 66 67 68
void update_vsyscall_tz(void)
{
	unsigned long flags;

	write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
	/* sys_tz has changed */
	vsyscall_gtod_data.sys_tz = sys_tz;
	write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
}

69 70
void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
			struct clocksource *clock, u32 mult)
L
Linus Torvalds 已提交
71
{
72
	unsigned long flags;
L
Linus Torvalds 已提交
73

74
	write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
75

76
	/* copy vsyscall data */
77 78 79 80 81 82 83 84 85 86
	vsyscall_gtod_data.clock.vread		= clock->vread;
	vsyscall_gtod_data.clock.cycle_last	= clock->cycle_last;
	vsyscall_gtod_data.clock.mask		= clock->mask;
	vsyscall_gtod_data.clock.mult		= mult;
	vsyscall_gtod_data.clock.shift		= clock->shift;
	vsyscall_gtod_data.wall_time_sec	= wall_time->tv_sec;
	vsyscall_gtod_data.wall_time_nsec	= wall_time->tv_nsec;
	vsyscall_gtod_data.wall_to_monotonic	= *wtm;
	vsyscall_gtod_data.wall_time_coarse	= __current_kernel_time();

87
	write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
L
Linus Torvalds 已提交
88 89
}

90 91
static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
			      const char *message)
L
Linus Torvalds 已提交
92
{
93 94
	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
	struct task_struct *tsk;
L
Linus Torvalds 已提交
95

96 97
	if (!show_unhandled_signals || !__ratelimit(&rs))
		return;
L
Linus Torvalds 已提交
98

99
	tsk = current;
100

101
	printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
102
	       level, tsk->comm, task_pid_nr(tsk),
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	       message, regs->ip - 2, regs->cs,
	       regs->sp, regs->ax, regs->si, regs->di);
}

static int addr_to_vsyscall_nr(unsigned long addr)
{
	int nr;

	if ((addr & ~0xC00UL) != VSYSCALL_START)
		return -EINVAL;

	nr = (addr & 0xC00UL) >> 10;
	if (nr >= 3)
		return -EINVAL;

	return nr;
L
Linus Torvalds 已提交
119 120
}

121
void dotraplinkage do_emulate_vsyscall(struct pt_regs *regs, long error_code)
L
Linus Torvalds 已提交
122
{
123 124 125 126 127 128 129
	struct task_struct *tsk;
	unsigned long caller;
	int vsyscall_nr;
	long ret;

	local_irq_enable();

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	/*
	 * Real 64-bit user mode code has cs == __USER_CS.  Anything else
	 * is bogus.
	 */
	if (regs->cs != __USER_CS) {
		/*
		 * If we trapped from kernel mode, we might as well OOPS now
		 * instead of returning to some random address and OOPSing
		 * then.
		 */
		BUG_ON(!user_mode(regs));

		/* Compat mode and non-compat 32-bit CS should both segfault. */
		warn_bad_vsyscall(KERN_WARNING, regs,
				  "illegal int 0xcc from 32-bit mode");
		goto sigsegv;
	}

148 149 150 151
	/*
	 * x86-ism here: regs->ip points to the instruction after the int 0xcc,
	 * and int 0xcc is two bytes long.
	 */
152 153 154 155
	vsyscall_nr = addr_to_vsyscall_nr(regs->ip - 2);
	if (vsyscall_nr < 0) {
		warn_bad_vsyscall(KERN_WARNING, regs,
				  "illegal int 0xcc (exploit attempt?)");
156 157
		goto sigsegv;
	}
J
john stultz 已提交
158

159 160 161 162
	if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
		warn_bad_vsyscall(KERN_WARNING, regs, "int 0xcc with bad stack (exploit attempt?)");
		goto sigsegv;
	}
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	tsk = current;
	if (seccomp_mode(&tsk->seccomp))
		do_exit(SIGKILL);

	switch (vsyscall_nr) {
	case 0:
		ret = sys_gettimeofday(
			(struct timeval __user *)regs->di,
			(struct timezone __user *)regs->si);
		break;

	case 1:
		ret = sys_time((time_t __user *)regs->di);
		break;

	case 2:
		ret = sys_getcpu((unsigned __user *)regs->di,
				 (unsigned __user *)regs->si,
				 0);
		break;
	}
185

186 187 188 189 190 191 192 193 194 195 196 197
	if (ret == -EFAULT) {
		/*
		 * Bad news -- userspace fed a bad pointer to a vsyscall.
		 *
		 * With a real vsyscall, that would have caused SIGSEGV.
		 * To make writing reliable exploits using the emulated
		 * vsyscalls harder, generate SIGSEGV here as well.
		 */
		warn_bad_vsyscall(KERN_INFO, regs,
				  "vsyscall fault (exploit attempt?)");
		goto sigsegv;
	}
198

199
	regs->ax = ret;
L
Linus Torvalds 已提交
200

201 202 203
	/* Emulate a ret instruction. */
	regs->ip = caller;
	regs->sp += 8;
204

205 206 207 208 209 210
	local_irq_disable();
	return;

sigsegv:
	regs->ip -= 2;  /* The faulting instruction should be the int 0xcc. */
	force_sig(SIGSEGV, current);
211
	local_irq_disable();
L
Linus Torvalds 已提交
212 213
}

214 215 216 217
/*
 * Assume __initcall executes before all user space. Hopefully kmod
 * doesn't violate that. We'll find out if it does.
 */
218
static void __cpuinit vsyscall_set_cpu(int cpu)
219
{
220
	unsigned long d;
221 222
	unsigned long node = 0;
#ifdef CONFIG_NUMA
M
Mike Travis 已提交
223
	node = cpu_to_node(cpu);
224
#endif
225
	if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
226
		write_rdtscp_aux((node << 12) | cpu);
227

228 229 230 231
	/*
	 * Store cpu number in limit so that it can be loaded quickly
	 * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
	 */
232 233 234 235
	d = 0x0f40000000000ULL;
	d |= cpu;
	d |= (node & 0xf) << 12;
	d |= (node >> 4) << 48;
236

237
	write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
238 239
}

240 241 242 243 244 245 246 247 248 249
static void __cpuinit cpu_vsyscall_init(void *arg)
{
	/* preemption should be already off */
	vsyscall_set_cpu(raw_smp_processor_id());
}

static int __cpuinit
cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
{
	long cpu = (long)arg;
250

251
	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
252
		smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
253

254 255 256
	return NOTIFY_DONE;
}

I
Ingo Molnar 已提交
257
void __init map_vsyscall(void)
L
Linus Torvalds 已提交
258 259 260
{
	extern char __vsyscall_0;
	unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
261 262
	extern char __vvar_page;
	unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
L
Linus Torvalds 已提交
263

264
	/* Note that VSYSCALL_MAPPED_PAGES must agree with the code below. */
L
Linus Torvalds 已提交
265
	__set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
266
	__set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
267
	BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) != (unsigned long)VVAR_ADDRESS);
L
Linus Torvalds 已提交
268 269 270 271
}

static int __init vsyscall_init(void)
{
272 273
	BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));

274
	on_each_cpu(cpu_vsyscall_init, NULL, 1);
275 276
	/* notifier priority > KVM */
	hotcpu_notifier(cpu_vsyscall_notifier, 30);
277

L
Linus Torvalds 已提交
278 279 280
	return 0;
}
__initcall(vsyscall_init);