vsyscall_64.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #include <linux/time.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/timer.h>
  24. #include <linux/seqlock.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/topology.h>
  28. #include <linux/clocksource.h>
  29. #include <linux/getcpu.h>
  30. #include <linux/cpu.h>
  31. #include <linux/smp.h>
  32. #include <linux/notifier.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/ratelimit.h>
  35. #include <asm/vsyscall.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/compat.h>
  38. #include <asm/page.h>
  39. #include <asm/unistd.h>
  40. #include <asm/fixmap.h>
  41. #include <asm/errno.h>
  42. #include <asm/io.h>
  43. #include <asm/segment.h>
  44. #include <asm/desc.h>
  45. #include <asm/topology.h>
  46. #include <asm/vgtod.h>
  47. #include <asm/traps.h>
  48. #define CREATE_TRACE_POINTS
  49. #include "vsyscall_trace.h"
  50. DEFINE_VVAR(int, vgetcpu_mode);
  51. DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data) =
  52. {
  53. .lock = __SEQLOCK_UNLOCKED(__vsyscall_gtod_data.lock),
  54. };
  55. static enum { EMULATE, NATIVE, NONE } vsyscall_mode = NATIVE;
  56. static int __init vsyscall_setup(char *str)
  57. {
  58. if (str) {
  59. if (!strcmp("emulate", str))
  60. vsyscall_mode = EMULATE;
  61. else if (!strcmp("native", str))
  62. vsyscall_mode = NATIVE;
  63. else if (!strcmp("none", str))
  64. vsyscall_mode = NONE;
  65. else
  66. return -EINVAL;
  67. return 0;
  68. }
  69. return -EINVAL;
  70. }
  71. early_param("vsyscall", vsyscall_setup);
  72. void update_vsyscall_tz(void)
  73. {
  74. unsigned long flags;
  75. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  76. /* sys_tz has changed */
  77. vsyscall_gtod_data.sys_tz = sys_tz;
  78. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  79. }
  80. void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
  81. struct clocksource *clock, u32 mult)
  82. {
  83. unsigned long flags;
  84. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  85. /* copy vsyscall data */
  86. vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode;
  87. vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
  88. vsyscall_gtod_data.clock.mask = clock->mask;
  89. vsyscall_gtod_data.clock.mult = mult;
  90. vsyscall_gtod_data.clock.shift = clock->shift;
  91. vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
  92. vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
  93. vsyscall_gtod_data.wall_to_monotonic = *wtm;
  94. vsyscall_gtod_data.wall_time_coarse = __current_kernel_time();
  95. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  96. }
  97. static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
  98. const char *message)
  99. {
  100. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
  101. struct task_struct *tsk;
  102. if (!show_unhandled_signals || !__ratelimit(&rs))
  103. return;
  104. tsk = current;
  105. printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
  106. level, tsk->comm, task_pid_nr(tsk),
  107. message, regs->ip, regs->cs,
  108. regs->sp, regs->ax, regs->si, regs->di);
  109. }
  110. static int addr_to_vsyscall_nr(unsigned long addr)
  111. {
  112. int nr;
  113. if ((addr & ~0xC00UL) != VSYSCALL_START)
  114. return -EINVAL;
  115. nr = (addr & 0xC00UL) >> 10;
  116. if (nr >= 3)
  117. return -EINVAL;
  118. return nr;
  119. }
  120. bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
  121. {
  122. struct task_struct *tsk;
  123. unsigned long caller;
  124. int vsyscall_nr;
  125. long ret;
  126. /*
  127. * No point in checking CS -- the only way to get here is a user mode
  128. * trap to a high address, which means that we're in 64-bit user code.
  129. */
  130. WARN_ON_ONCE(address != regs->ip);
  131. if (vsyscall_mode == NONE) {
  132. warn_bad_vsyscall(KERN_INFO, regs,
  133. "vsyscall attempted with vsyscall=none");
  134. return false;
  135. }
  136. vsyscall_nr = addr_to_vsyscall_nr(address);
  137. trace_emulate_vsyscall(vsyscall_nr);
  138. if (vsyscall_nr < 0) {
  139. warn_bad_vsyscall(KERN_WARNING, regs,
  140. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  141. goto sigsegv;
  142. }
  143. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  144. warn_bad_vsyscall(KERN_WARNING, regs,
  145. "vsyscall with bad stack (exploit attempt?)");
  146. goto sigsegv;
  147. }
  148. tsk = current;
  149. if (seccomp_mode(&tsk->seccomp))
  150. do_exit(SIGKILL);
  151. switch (vsyscall_nr) {
  152. case 0:
  153. ret = sys_gettimeofday(
  154. (struct timeval __user *)regs->di,
  155. (struct timezone __user *)regs->si);
  156. break;
  157. case 1:
  158. ret = sys_time((time_t __user *)regs->di);
  159. break;
  160. case 2:
  161. ret = sys_getcpu((unsigned __user *)regs->di,
  162. (unsigned __user *)regs->si,
  163. 0);
  164. break;
  165. }
  166. if (ret == -EFAULT) {
  167. /*
  168. * Bad news -- userspace fed a bad pointer to a vsyscall.
  169. *
  170. * With a real vsyscall, that would have caused SIGSEGV.
  171. * To make writing reliable exploits using the emulated
  172. * vsyscalls harder, generate SIGSEGV here as well.
  173. */
  174. warn_bad_vsyscall(KERN_INFO, regs,
  175. "vsyscall fault (exploit attempt?)");
  176. goto sigsegv;
  177. }
  178. regs->ax = ret;
  179. /* Emulate a ret instruction. */
  180. regs->ip = caller;
  181. regs->sp += 8;
  182. return true;
  183. sigsegv:
  184. force_sig(SIGSEGV, current);
  185. return true;
  186. }
  187. /*
  188. * Assume __initcall executes before all user space. Hopefully kmod
  189. * doesn't violate that. We'll find out if it does.
  190. */
  191. static void __cpuinit vsyscall_set_cpu(int cpu)
  192. {
  193. unsigned long d;
  194. unsigned long node = 0;
  195. #ifdef CONFIG_NUMA
  196. node = cpu_to_node(cpu);
  197. #endif
  198. if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
  199. write_rdtscp_aux((node << 12) | cpu);
  200. /*
  201. * Store cpu number in limit so that it can be loaded quickly
  202. * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
  203. */
  204. d = 0x0f40000000000ULL;
  205. d |= cpu;
  206. d |= (node & 0xf) << 12;
  207. d |= (node >> 4) << 48;
  208. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  209. }
  210. static void __cpuinit cpu_vsyscall_init(void *arg)
  211. {
  212. /* preemption should be already off */
  213. vsyscall_set_cpu(raw_smp_processor_id());
  214. }
  215. static int __cpuinit
  216. cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
  217. {
  218. long cpu = (long)arg;
  219. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  220. smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
  221. return NOTIFY_DONE;
  222. }
  223. void __init map_vsyscall(void)
  224. {
  225. extern char __vsyscall_page;
  226. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  227. extern char __vvar_page;
  228. unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
  229. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
  230. vsyscall_mode == NATIVE
  231. ? PAGE_KERNEL_VSYSCALL
  232. : PAGE_KERNEL_VVAR);
  233. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
  234. (unsigned long)VSYSCALL_START);
  235. __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
  236. BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
  237. (unsigned long)VVAR_ADDRESS);
  238. }
  239. static int __init vsyscall_init(void)
  240. {
  241. BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
  242. on_each_cpu(cpu_vsyscall_init, NULL, 1);
  243. /* notifier priority > KVM */
  244. hotcpu_notifier(cpu_vsyscall_notifier, 30);
  245. return 0;
  246. }
  247. __initcall(vsyscall_init);