vsyscall_64.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  3. * Copyright 2003 Andi Kleen, SuSE Labs.
  4. *
  5. * [ NOTE: this mechanism is now deprecated in favor of the vDSO. ]
  6. *
  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.
  16. *
  17. * Note: the concept clashes with user mode linux. UML users should
  18. * use the vDSO.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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>
  28. #include <linux/topology.h>
  29. #include <linux/timekeeper_internal.h>
  30. #include <linux/getcpu.h>
  31. #include <linux/cpu.h>
  32. #include <linux/smp.h>
  33. #include <linux/notifier.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/ratelimit.h>
  36. #include <asm/vsyscall.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/compat.h>
  39. #include <asm/page.h>
  40. #include <asm/unistd.h>
  41. #include <asm/fixmap.h>
  42. #include <asm/errno.h>
  43. #include <asm/io.h>
  44. #include <asm/segment.h>
  45. #include <asm/desc.h>
  46. #include <asm/topology.h>
  47. #include <asm/vgtod.h>
  48. #include <asm/traps.h>
  49. #define CREATE_TRACE_POINTS
  50. #include "vsyscall_trace.h"
  51. DEFINE_VVAR(int, vgetcpu_mode);
  52. DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data);
  53. static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
  54. static int __init vsyscall_setup(char *str)
  55. {
  56. if (str) {
  57. if (!strcmp("emulate", str))
  58. vsyscall_mode = EMULATE;
  59. else if (!strcmp("native", str))
  60. vsyscall_mode = NATIVE;
  61. else if (!strcmp("none", str))
  62. vsyscall_mode = NONE;
  63. else
  64. return -EINVAL;
  65. return 0;
  66. }
  67. return -EINVAL;
  68. }
  69. early_param("vsyscall", vsyscall_setup);
  70. void update_vsyscall_tz(void)
  71. {
  72. vsyscall_gtod_data.sys_tz = sys_tz;
  73. }
  74. void update_vsyscall(struct timekeeper *tk)
  75. {
  76. struct vsyscall_gtod_data *vdata = &vsyscall_gtod_data;
  77. write_seqcount_begin(&vdata->seq);
  78. /* copy vsyscall data */
  79. vdata->clock.vclock_mode = tk->clock->archdata.vclock_mode;
  80. vdata->clock.cycle_last = tk->clock->cycle_last;
  81. vdata->clock.mask = tk->clock->mask;
  82. vdata->clock.mult = tk->mult;
  83. vdata->clock.shift = tk->shift;
  84. vdata->wall_time_sec = tk->xtime_sec;
  85. vdata->wall_time_snsec = tk->xtime_nsec;
  86. vdata->monotonic_time_sec = tk->xtime_sec
  87. + tk->wall_to_monotonic.tv_sec;
  88. vdata->monotonic_time_snsec = tk->xtime_nsec
  89. + (tk->wall_to_monotonic.tv_nsec
  90. << tk->shift);
  91. while (vdata->monotonic_time_snsec >=
  92. (((u64)NSEC_PER_SEC) << tk->shift)) {
  93. vdata->monotonic_time_snsec -=
  94. ((u64)NSEC_PER_SEC) << tk->shift;
  95. vdata->monotonic_time_sec++;
  96. }
  97. vdata->wall_time_coarse.tv_sec = tk->xtime_sec;
  98. vdata->wall_time_coarse.tv_nsec = (long)(tk->xtime_nsec >> tk->shift);
  99. vdata->monotonic_time_coarse = timespec_add(vdata->wall_time_coarse,
  100. tk->wall_to_monotonic);
  101. write_seqcount_end(&vdata->seq);
  102. }
  103. static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
  104. const char *message)
  105. {
  106. if (!show_unhandled_signals)
  107. return;
  108. pr_notice_ratelimited("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
  109. level, current->comm, task_pid_nr(current),
  110. message, regs->ip, regs->cs,
  111. regs->sp, regs->ax, regs->si, regs->di);
  112. }
  113. static int addr_to_vsyscall_nr(unsigned long addr)
  114. {
  115. int nr;
  116. if ((addr & ~0xC00UL) != VSYSCALL_START)
  117. return -EINVAL;
  118. nr = (addr & 0xC00UL) >> 10;
  119. if (nr >= 3)
  120. return -EINVAL;
  121. return nr;
  122. }
  123. #ifdef CONFIG_SECCOMP
  124. static int vsyscall_seccomp(struct task_struct *tsk, int syscall_nr)
  125. {
  126. if (!seccomp_mode(&tsk->seccomp))
  127. return 0;
  128. task_pt_regs(tsk)->orig_ax = syscall_nr;
  129. task_pt_regs(tsk)->ax = syscall_nr;
  130. return __secure_computing(syscall_nr);
  131. }
  132. #else
  133. #define vsyscall_seccomp(_tsk, _nr) 0
  134. #endif
  135. static bool write_ok_or_segv(unsigned long ptr, size_t size)
  136. {
  137. /*
  138. * XXX: if access_ok, get_user, and put_user handled
  139. * sig_on_uaccess_error, this could go away.
  140. */
  141. if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
  142. siginfo_t info;
  143. struct thread_struct *thread = &current->thread;
  144. thread->error_code = 6; /* user fault, no page, write */
  145. thread->cr2 = ptr;
  146. thread->trap_nr = X86_TRAP_PF;
  147. memset(&info, 0, sizeof(info));
  148. info.si_signo = SIGSEGV;
  149. info.si_errno = 0;
  150. info.si_code = SEGV_MAPERR;
  151. info.si_addr = (void __user *)ptr;
  152. force_sig_info(SIGSEGV, &info, current);
  153. return false;
  154. } else {
  155. return true;
  156. }
  157. }
  158. bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
  159. {
  160. struct task_struct *tsk;
  161. unsigned long caller;
  162. int vsyscall_nr;
  163. int prev_sig_on_uaccess_error;
  164. long ret;
  165. int skip;
  166. /*
  167. * No point in checking CS -- the only way to get here is a user mode
  168. * trap to a high address, which means that we're in 64-bit user code.
  169. */
  170. WARN_ON_ONCE(address != regs->ip);
  171. if (vsyscall_mode == NONE) {
  172. warn_bad_vsyscall(KERN_INFO, regs,
  173. "vsyscall attempted with vsyscall=none");
  174. return false;
  175. }
  176. vsyscall_nr = addr_to_vsyscall_nr(address);
  177. trace_emulate_vsyscall(vsyscall_nr);
  178. if (vsyscall_nr < 0) {
  179. warn_bad_vsyscall(KERN_WARNING, regs,
  180. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  181. goto sigsegv;
  182. }
  183. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  184. warn_bad_vsyscall(KERN_WARNING, regs,
  185. "vsyscall with bad stack (exploit attempt?)");
  186. goto sigsegv;
  187. }
  188. tsk = current;
  189. /*
  190. * With a real vsyscall, page faults cause SIGSEGV. We want to
  191. * preserve that behavior to make writing exploits harder.
  192. */
  193. prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
  194. current_thread_info()->sig_on_uaccess_error = 1;
  195. /*
  196. * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
  197. * 64-bit, so we don't need to special-case it here. For all the
  198. * vsyscalls, NULL means "don't write anything" not "write it at
  199. * address 0".
  200. */
  201. ret = -EFAULT;
  202. skip = 0;
  203. switch (vsyscall_nr) {
  204. case 0:
  205. skip = vsyscall_seccomp(tsk, __NR_gettimeofday);
  206. if (skip)
  207. break;
  208. if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
  209. !write_ok_or_segv(regs->si, sizeof(struct timezone)))
  210. break;
  211. ret = sys_gettimeofday(
  212. (struct timeval __user *)regs->di,
  213. (struct timezone __user *)regs->si);
  214. break;
  215. case 1:
  216. skip = vsyscall_seccomp(tsk, __NR_time);
  217. if (skip)
  218. break;
  219. if (!write_ok_or_segv(regs->di, sizeof(time_t)))
  220. break;
  221. ret = sys_time((time_t __user *)regs->di);
  222. break;
  223. case 2:
  224. skip = vsyscall_seccomp(tsk, __NR_getcpu);
  225. if (skip)
  226. break;
  227. if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
  228. !write_ok_or_segv(regs->si, sizeof(unsigned)))
  229. break;
  230. ret = sys_getcpu((unsigned __user *)regs->di,
  231. (unsigned __user *)regs->si,
  232. NULL);
  233. break;
  234. }
  235. current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
  236. if (skip) {
  237. if ((long)regs->ax <= 0L) /* seccomp errno emulation */
  238. goto do_ret;
  239. goto done; /* seccomp trace/trap */
  240. }
  241. if (ret == -EFAULT) {
  242. /* Bad news -- userspace fed a bad pointer to a vsyscall. */
  243. warn_bad_vsyscall(KERN_INFO, regs,
  244. "vsyscall fault (exploit attempt?)");
  245. /*
  246. * If we failed to generate a signal for any reason,
  247. * generate one here. (This should be impossible.)
  248. */
  249. if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
  250. !sigismember(&tsk->pending.signal, SIGSEGV)))
  251. goto sigsegv;
  252. return true; /* Don't emulate the ret. */
  253. }
  254. regs->ax = ret;
  255. do_ret:
  256. /* Emulate a ret instruction. */
  257. regs->ip = caller;
  258. regs->sp += 8;
  259. done:
  260. return true;
  261. sigsegv:
  262. force_sig(SIGSEGV, current);
  263. return true;
  264. }
  265. /*
  266. * Assume __initcall executes before all user space. Hopefully kmod
  267. * doesn't violate that. We'll find out if it does.
  268. */
  269. static void __cpuinit vsyscall_set_cpu(int cpu)
  270. {
  271. unsigned long d;
  272. unsigned long node = 0;
  273. #ifdef CONFIG_NUMA
  274. node = cpu_to_node(cpu);
  275. #endif
  276. if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
  277. write_rdtscp_aux((node << 12) | cpu);
  278. /*
  279. * Store cpu number in limit so that it can be loaded quickly
  280. * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
  281. */
  282. d = 0x0f40000000000ULL;
  283. d |= cpu;
  284. d |= (node & 0xf) << 12;
  285. d |= (node >> 4) << 48;
  286. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  287. }
  288. static void __cpuinit cpu_vsyscall_init(void *arg)
  289. {
  290. /* preemption should be already off */
  291. vsyscall_set_cpu(raw_smp_processor_id());
  292. }
  293. static int __cpuinit
  294. cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
  295. {
  296. long cpu = (long)arg;
  297. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  298. smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
  299. return NOTIFY_DONE;
  300. }
  301. void __init map_vsyscall(void)
  302. {
  303. extern char __vsyscall_page;
  304. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  305. extern char __vvar_page;
  306. unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
  307. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
  308. vsyscall_mode == NATIVE
  309. ? PAGE_KERNEL_VSYSCALL
  310. : PAGE_KERNEL_VVAR);
  311. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
  312. (unsigned long)VSYSCALL_START);
  313. __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
  314. BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
  315. (unsigned long)VVAR_ADDRESS);
  316. }
  317. static int __init vsyscall_init(void)
  318. {
  319. BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
  320. on_each_cpu(cpu_vsyscall_init, NULL, 1);
  321. /* notifier priority > KVM */
  322. hotcpu_notifier(cpu_vsyscall_notifier, 30);
  323. return 0;
  324. }
  325. __initcall(vsyscall_init);