vsyscall_64.c 7.2 KB

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