vsyscall_64.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
  53. static int __init vsyscall_setup(char *str)
  54. {
  55. if (str) {
  56. if (!strcmp("emulate", str))
  57. vsyscall_mode = EMULATE;
  58. else if (!strcmp("native", str))
  59. vsyscall_mode = NATIVE;
  60. else if (!strcmp("none", str))
  61. vsyscall_mode = NONE;
  62. else
  63. return -EINVAL;
  64. return 0;
  65. }
  66. return -EINVAL;
  67. }
  68. early_param("vsyscall", vsyscall_setup);
  69. void update_vsyscall_tz(void)
  70. {
  71. vsyscall_gtod_data.sys_tz = sys_tz;
  72. }
  73. void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
  74. struct clocksource *clock, u32 mult)
  75. {
  76. write_seqcount_begin(&vsyscall_gtod_data.seq);
  77. /* copy vsyscall data */
  78. vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode;
  79. vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
  80. vsyscall_gtod_data.clock.mask = clock->mask;
  81. vsyscall_gtod_data.clock.mult = mult;
  82. vsyscall_gtod_data.clock.shift = clock->shift;
  83. vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
  84. vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
  85. vsyscall_gtod_data.wall_to_monotonic = *wtm;
  86. vsyscall_gtod_data.wall_time_coarse = __current_kernel_time();
  87. write_seqcount_end(&vsyscall_gtod_data.seq);
  88. }
  89. static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
  90. const char *message)
  91. {
  92. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
  93. struct task_struct *tsk;
  94. if (!show_unhandled_signals || !__ratelimit(&rs))
  95. return;
  96. tsk = current;
  97. printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
  98. level, tsk->comm, task_pid_nr(tsk),
  99. message, regs->ip, regs->cs,
  100. regs->sp, regs->ax, regs->si, regs->di);
  101. }
  102. static int addr_to_vsyscall_nr(unsigned long addr)
  103. {
  104. int nr;
  105. if ((addr & ~0xC00UL) != VSYSCALL_START)
  106. return -EINVAL;
  107. nr = (addr & 0xC00UL) >> 10;
  108. if (nr >= 3)
  109. return -EINVAL;
  110. return nr;
  111. }
  112. static bool write_ok_or_segv(unsigned long ptr, size_t size)
  113. {
  114. /*
  115. * XXX: if access_ok, get_user, and put_user handled
  116. * sig_on_uaccess_error, this could go away.
  117. */
  118. if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
  119. siginfo_t info;
  120. struct thread_struct *thread = &current->thread;
  121. thread->error_code = 6; /* user fault, no page, write */
  122. thread->cr2 = ptr;
  123. thread->trap_no = 14;
  124. memset(&info, 0, sizeof(info));
  125. info.si_signo = SIGSEGV;
  126. info.si_errno = 0;
  127. info.si_code = SEGV_MAPERR;
  128. info.si_addr = (void __user *)ptr;
  129. force_sig_info(SIGSEGV, &info, current);
  130. return false;
  131. } else {
  132. return true;
  133. }
  134. }
  135. bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
  136. {
  137. struct task_struct *tsk;
  138. unsigned long caller;
  139. int vsyscall_nr;
  140. int prev_sig_on_uaccess_error;
  141. long ret;
  142. /*
  143. * No point in checking CS -- the only way to get here is a user mode
  144. * trap to a high address, which means that we're in 64-bit user code.
  145. */
  146. WARN_ON_ONCE(address != regs->ip);
  147. if (vsyscall_mode == NONE) {
  148. warn_bad_vsyscall(KERN_INFO, regs,
  149. "vsyscall attempted with vsyscall=none");
  150. return false;
  151. }
  152. vsyscall_nr = addr_to_vsyscall_nr(address);
  153. trace_emulate_vsyscall(vsyscall_nr);
  154. if (vsyscall_nr < 0) {
  155. warn_bad_vsyscall(KERN_WARNING, regs,
  156. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  157. goto sigsegv;
  158. }
  159. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  160. warn_bad_vsyscall(KERN_WARNING, regs,
  161. "vsyscall with bad stack (exploit attempt?)");
  162. goto sigsegv;
  163. }
  164. tsk = current;
  165. if (seccomp_mode(&tsk->seccomp))
  166. do_exit(SIGKILL);
  167. /*
  168. * With a real vsyscall, page faults cause SIGSEGV. We want to
  169. * preserve that behavior to make writing exploits harder.
  170. */
  171. prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
  172. current_thread_info()->sig_on_uaccess_error = 1;
  173. /*
  174. * 0 is a valid user pointer (in the access_ok sense) on 32-bit and
  175. * 64-bit, so we don't need to special-case it here. For all the
  176. * vsyscalls, 0 means "don't write anything" not "write it at
  177. * address 0".
  178. */
  179. ret = -EFAULT;
  180. switch (vsyscall_nr) {
  181. case 0:
  182. if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
  183. !write_ok_or_segv(regs->si, sizeof(struct timezone)))
  184. break;
  185. ret = sys_gettimeofday(
  186. (struct timeval __user *)regs->di,
  187. (struct timezone __user *)regs->si);
  188. break;
  189. case 1:
  190. if (!write_ok_or_segv(regs->di, sizeof(time_t)))
  191. break;
  192. ret = sys_time((time_t __user *)regs->di);
  193. break;
  194. case 2:
  195. if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
  196. !write_ok_or_segv(regs->si, sizeof(unsigned)))
  197. break;
  198. ret = sys_getcpu((unsigned __user *)regs->di,
  199. (unsigned __user *)regs->si,
  200. 0);
  201. break;
  202. }
  203. current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
  204. if (ret == -EFAULT) {
  205. /* Bad news -- userspace fed a bad pointer to a vsyscall. */
  206. warn_bad_vsyscall(KERN_INFO, regs,
  207. "vsyscall fault (exploit attempt?)");
  208. /*
  209. * If we failed to generate a signal for any reason,
  210. * generate one here. (This should be impossible.)
  211. */
  212. if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
  213. !sigismember(&tsk->pending.signal, SIGSEGV)))
  214. goto sigsegv;
  215. return true; /* Don't emulate the ret. */
  216. }
  217. regs->ax = ret;
  218. /* Emulate a ret instruction. */
  219. regs->ip = caller;
  220. regs->sp += 8;
  221. return true;
  222. sigsegv:
  223. force_sig(SIGSEGV, current);
  224. return true;
  225. }
  226. /*
  227. * Assume __initcall executes before all user space. Hopefully kmod
  228. * doesn't violate that. We'll find out if it does.
  229. */
  230. static void __cpuinit vsyscall_set_cpu(int cpu)
  231. {
  232. unsigned long d;
  233. unsigned long node = 0;
  234. #ifdef CONFIG_NUMA
  235. node = cpu_to_node(cpu);
  236. #endif
  237. if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
  238. write_rdtscp_aux((node << 12) | cpu);
  239. /*
  240. * Store cpu number in limit so that it can be loaded quickly
  241. * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
  242. */
  243. d = 0x0f40000000000ULL;
  244. d |= cpu;
  245. d |= (node & 0xf) << 12;
  246. d |= (node >> 4) << 48;
  247. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  248. }
  249. static void __cpuinit cpu_vsyscall_init(void *arg)
  250. {
  251. /* preemption should be already off */
  252. vsyscall_set_cpu(raw_smp_processor_id());
  253. }
  254. static int __cpuinit
  255. cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
  256. {
  257. long cpu = (long)arg;
  258. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  259. smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
  260. return NOTIFY_DONE;
  261. }
  262. void __init map_vsyscall(void)
  263. {
  264. extern char __vsyscall_page;
  265. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  266. extern char __vvar_page;
  267. unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
  268. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
  269. vsyscall_mode == NATIVE
  270. ? PAGE_KERNEL_VSYSCALL
  271. : PAGE_KERNEL_VVAR);
  272. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
  273. (unsigned long)VSYSCALL_START);
  274. __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
  275. BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
  276. (unsigned long)VVAR_ADDRESS);
  277. }
  278. static int __init vsyscall_init(void)
  279. {
  280. BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
  281. on_each_cpu(cpu_vsyscall_init, NULL, 1);
  282. /* notifier priority > KVM */
  283. hotcpu_notifier(cpu_vsyscall_notifier, 30);
  284. return 0;
  285. }
  286. __initcall(vsyscall_init);