vsyscall_64.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /* Disable profiling for userspace code: */
  21. #define DISABLE_BRANCH_PROFILING
  22. #include <linux/time.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/timer.h>
  26. #include <linux/seqlock.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/clocksource.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. {
  54. .lock = __SEQLOCK_UNLOCKED(__vsyscall_gtod_data.lock),
  55. };
  56. void update_vsyscall_tz(void)
  57. {
  58. unsigned long flags;
  59. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  60. /* sys_tz has changed */
  61. vsyscall_gtod_data.sys_tz = sys_tz;
  62. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  63. }
  64. void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
  65. struct clocksource *clock, u32 mult)
  66. {
  67. unsigned long flags;
  68. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  69. /* copy vsyscall data */
  70. vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode;
  71. vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
  72. vsyscall_gtod_data.clock.mask = clock->mask;
  73. vsyscall_gtod_data.clock.mult = mult;
  74. vsyscall_gtod_data.clock.shift = clock->shift;
  75. vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
  76. vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
  77. vsyscall_gtod_data.wall_to_monotonic = *wtm;
  78. vsyscall_gtod_data.wall_time_coarse = __current_kernel_time();
  79. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  80. }
  81. static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
  82. const char *message)
  83. {
  84. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
  85. struct task_struct *tsk;
  86. if (!show_unhandled_signals || !__ratelimit(&rs))
  87. return;
  88. tsk = current;
  89. printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
  90. level, tsk->comm, task_pid_nr(tsk),
  91. message, regs->ip - 2, regs->cs,
  92. regs->sp, regs->ax, regs->si, regs->di);
  93. }
  94. static int addr_to_vsyscall_nr(unsigned long addr)
  95. {
  96. int nr;
  97. if ((addr & ~0xC00UL) != VSYSCALL_START)
  98. return -EINVAL;
  99. nr = (addr & 0xC00UL) >> 10;
  100. if (nr >= 3)
  101. return -EINVAL;
  102. return nr;
  103. }
  104. void dotraplinkage do_emulate_vsyscall(struct pt_regs *regs, long error_code)
  105. {
  106. struct task_struct *tsk;
  107. unsigned long caller;
  108. int vsyscall_nr;
  109. long ret;
  110. local_irq_enable();
  111. if (!user_64bit_mode(regs)) {
  112. /*
  113. * If we trapped from kernel mode, we might as well OOPS now
  114. * instead of returning to some random address and OOPSing
  115. * then.
  116. */
  117. BUG_ON(!user_mode(regs));
  118. /* Compat mode and non-compat 32-bit CS should both segfault. */
  119. warn_bad_vsyscall(KERN_WARNING, regs,
  120. "illegal int 0xcc from 32-bit mode");
  121. goto sigsegv;
  122. }
  123. /*
  124. * x86-ism here: regs->ip points to the instruction after the int 0xcc,
  125. * and int 0xcc is two bytes long.
  126. */
  127. vsyscall_nr = addr_to_vsyscall_nr(regs->ip - 2);
  128. trace_emulate_vsyscall(vsyscall_nr);
  129. if (vsyscall_nr < 0) {
  130. warn_bad_vsyscall(KERN_WARNING, regs,
  131. "illegal int 0xcc (exploit attempt?)");
  132. goto sigsegv;
  133. }
  134. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  135. warn_bad_vsyscall(KERN_WARNING, regs, "int 0xcc with bad stack (exploit attempt?)");
  136. goto sigsegv;
  137. }
  138. tsk = current;
  139. if (seccomp_mode(&tsk->seccomp))
  140. do_exit(SIGKILL);
  141. switch (vsyscall_nr) {
  142. case 0:
  143. ret = sys_gettimeofday(
  144. (struct timeval __user *)regs->di,
  145. (struct timezone __user *)regs->si);
  146. break;
  147. case 1:
  148. ret = sys_time((time_t __user *)regs->di);
  149. break;
  150. case 2:
  151. ret = sys_getcpu((unsigned __user *)regs->di,
  152. (unsigned __user *)regs->si,
  153. 0);
  154. break;
  155. }
  156. if (ret == -EFAULT) {
  157. /*
  158. * Bad news -- userspace fed a bad pointer to a vsyscall.
  159. *
  160. * With a real vsyscall, that would have caused SIGSEGV.
  161. * To make writing reliable exploits using the emulated
  162. * vsyscalls harder, generate SIGSEGV here as well.
  163. */
  164. warn_bad_vsyscall(KERN_INFO, regs,
  165. "vsyscall fault (exploit attempt?)");
  166. goto sigsegv;
  167. }
  168. regs->ax = ret;
  169. /* Emulate a ret instruction. */
  170. regs->ip = caller;
  171. regs->sp += 8;
  172. local_irq_disable();
  173. return;
  174. sigsegv:
  175. regs->ip -= 2; /* The faulting instruction should be the int 0xcc. */
  176. force_sig(SIGSEGV, current);
  177. local_irq_disable();
  178. }
  179. /*
  180. * Assume __initcall executes before all user space. Hopefully kmod
  181. * doesn't violate that. We'll find out if it does.
  182. */
  183. static void __cpuinit vsyscall_set_cpu(int cpu)
  184. {
  185. unsigned long d;
  186. unsigned long node = 0;
  187. #ifdef CONFIG_NUMA
  188. node = cpu_to_node(cpu);
  189. #endif
  190. if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
  191. write_rdtscp_aux((node << 12) | cpu);
  192. /*
  193. * Store cpu number in limit so that it can be loaded quickly
  194. * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
  195. */
  196. d = 0x0f40000000000ULL;
  197. d |= cpu;
  198. d |= (node & 0xf) << 12;
  199. d |= (node >> 4) << 48;
  200. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  201. }
  202. static void __cpuinit cpu_vsyscall_init(void *arg)
  203. {
  204. /* preemption should be already off */
  205. vsyscall_set_cpu(raw_smp_processor_id());
  206. }
  207. static int __cpuinit
  208. cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
  209. {
  210. long cpu = (long)arg;
  211. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  212. smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
  213. return NOTIFY_DONE;
  214. }
  215. void __init map_vsyscall(void)
  216. {
  217. extern char __vsyscall_0;
  218. unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
  219. extern char __vvar_page;
  220. unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
  221. /* Note that VSYSCALL_MAPPED_PAGES must agree with the code below. */
  222. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
  223. __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
  224. BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) != (unsigned long)VVAR_ADDRESS);
  225. }
  226. static int __init vsyscall_init(void)
  227. {
  228. BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
  229. on_each_cpu(cpu_vsyscall_init, NULL, 1);
  230. /* notifier priority > KVM */
  231. hotcpu_notifier(cpu_vsyscall_notifier, 30);
  232. return 0;
  233. }
  234. __initcall(vsyscall_init);