vsyscall_64.c 6.9 KB

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