vsyscall_64.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2012-2014 Andy Lutomirski <luto@amacapital.net>
  3. *
  4. * Based on the original implementation which is:
  5. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  6. * Copyright 2003 Andi Kleen, SuSE Labs.
  7. *
  8. * Parts of the original code have been moved to arch/x86/vdso/vma.c
  9. *
  10. * This file implements vsyscall emulation. vsyscalls are a legacy ABI:
  11. * Userspace can request certain kernel services by calling fixed
  12. * addresses. This concept is problematic:
  13. *
  14. * - It interferes with ASLR.
  15. * - It's awkward to write code that lives in kernel addresses but is
  16. * callable by userspace at fixed addresses.
  17. * - The whole concept is impossible for 32-bit compat userspace.
  18. * - UML cannot easily virtualize a vsyscall.
  19. *
  20. * As of mid-2014, I believe that there is no new userspace code that
  21. * will use a vsyscall if the vDSO is present. I hope that there will
  22. * soon be no new userspace code that will ever use a vsyscall.
  23. *
  24. * The code in this file emulates vsyscalls when notified of a page
  25. * fault to a vsyscall address.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/timer.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/ratelimit.h>
  31. #include <asm/vsyscall.h>
  32. #include <asm/unistd.h>
  33. #include <asm/fixmap.h>
  34. #include <asm/traps.h>
  35. #define CREATE_TRACE_POINTS
  36. #include "vsyscall_trace.h"
  37. static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
  38. static int __init vsyscall_setup(char *str)
  39. {
  40. if (str) {
  41. if (!strcmp("emulate", str))
  42. vsyscall_mode = EMULATE;
  43. else if (!strcmp("native", str))
  44. vsyscall_mode = NATIVE;
  45. else if (!strcmp("none", str))
  46. vsyscall_mode = NONE;
  47. else
  48. return -EINVAL;
  49. return 0;
  50. }
  51. return -EINVAL;
  52. }
  53. early_param("vsyscall", vsyscall_setup);
  54. static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
  55. const char *message)
  56. {
  57. if (!show_unhandled_signals)
  58. return;
  59. printk_ratelimited("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
  60. level, current->comm, task_pid_nr(current),
  61. message, regs->ip, regs->cs,
  62. regs->sp, regs->ax, regs->si, regs->di);
  63. }
  64. static int addr_to_vsyscall_nr(unsigned long addr)
  65. {
  66. int nr;
  67. if ((addr & ~0xC00UL) != VSYSCALL_ADDR)
  68. return -EINVAL;
  69. nr = (addr & 0xC00UL) >> 10;
  70. if (nr >= 3)
  71. return -EINVAL;
  72. return nr;
  73. }
  74. static bool write_ok_or_segv(unsigned long ptr, size_t size)
  75. {
  76. /*
  77. * XXX: if access_ok, get_user, and put_user handled
  78. * sig_on_uaccess_error, this could go away.
  79. */
  80. if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
  81. siginfo_t info;
  82. struct thread_struct *thread = &current->thread;
  83. thread->error_code = 6; /* user fault, no page, write */
  84. thread->cr2 = ptr;
  85. thread->trap_nr = X86_TRAP_PF;
  86. memset(&info, 0, sizeof(info));
  87. info.si_signo = SIGSEGV;
  88. info.si_errno = 0;
  89. info.si_code = SEGV_MAPERR;
  90. info.si_addr = (void __user *)ptr;
  91. force_sig_info(SIGSEGV, &info, current);
  92. return false;
  93. } else {
  94. return true;
  95. }
  96. }
  97. bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
  98. {
  99. struct task_struct *tsk;
  100. unsigned long caller;
  101. int vsyscall_nr, syscall_nr, tmp;
  102. int prev_sig_on_uaccess_error;
  103. long ret;
  104. /*
  105. * No point in checking CS -- the only way to get here is a user mode
  106. * trap to a high address, which means that we're in 64-bit user code.
  107. */
  108. WARN_ON_ONCE(address != regs->ip);
  109. if (vsyscall_mode == NONE) {
  110. warn_bad_vsyscall(KERN_INFO, regs,
  111. "vsyscall attempted with vsyscall=none");
  112. return false;
  113. }
  114. vsyscall_nr = addr_to_vsyscall_nr(address);
  115. trace_emulate_vsyscall(vsyscall_nr);
  116. if (vsyscall_nr < 0) {
  117. warn_bad_vsyscall(KERN_WARNING, regs,
  118. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  119. goto sigsegv;
  120. }
  121. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  122. warn_bad_vsyscall(KERN_WARNING, regs,
  123. "vsyscall with bad stack (exploit attempt?)");
  124. goto sigsegv;
  125. }
  126. tsk = current;
  127. /*
  128. * Check for access_ok violations and find the syscall nr.
  129. *
  130. * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
  131. * 64-bit, so we don't need to special-case it here. For all the
  132. * vsyscalls, NULL means "don't write anything" not "write it at
  133. * address 0".
  134. */
  135. switch (vsyscall_nr) {
  136. case 0:
  137. if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
  138. !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
  139. ret = -EFAULT;
  140. goto check_fault;
  141. }
  142. syscall_nr = __NR_gettimeofday;
  143. break;
  144. case 1:
  145. if (!write_ok_or_segv(regs->di, sizeof(time_t))) {
  146. ret = -EFAULT;
  147. goto check_fault;
  148. }
  149. syscall_nr = __NR_time;
  150. break;
  151. case 2:
  152. if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
  153. !write_ok_or_segv(regs->si, sizeof(unsigned))) {
  154. ret = -EFAULT;
  155. goto check_fault;
  156. }
  157. syscall_nr = __NR_getcpu;
  158. break;
  159. }
  160. /*
  161. * Handle seccomp. regs->ip must be the original value.
  162. * See seccomp_send_sigsys and Documentation/prctl/seccomp_filter.txt.
  163. *
  164. * We could optimize the seccomp disabled case, but performance
  165. * here doesn't matter.
  166. */
  167. regs->orig_ax = syscall_nr;
  168. regs->ax = -ENOSYS;
  169. tmp = secure_computing();
  170. if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
  171. warn_bad_vsyscall(KERN_DEBUG, regs,
  172. "seccomp tried to change syscall nr or ip");
  173. do_exit(SIGSYS);
  174. }
  175. regs->orig_ax = -1;
  176. if (tmp)
  177. goto do_ret; /* skip requested */
  178. /*
  179. * With a real vsyscall, page faults cause SIGSEGV. We want to
  180. * preserve that behavior to make writing exploits harder.
  181. */
  182. prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
  183. current_thread_info()->sig_on_uaccess_error = 1;
  184. ret = -EFAULT;
  185. switch (vsyscall_nr) {
  186. case 0:
  187. ret = sys_gettimeofday(
  188. (struct timeval __user *)regs->di,
  189. (struct timezone __user *)regs->si);
  190. break;
  191. case 1:
  192. ret = sys_time((time_t __user *)regs->di);
  193. break;
  194. case 2:
  195. ret = sys_getcpu((unsigned __user *)regs->di,
  196. (unsigned __user *)regs->si,
  197. NULL);
  198. break;
  199. }
  200. current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
  201. check_fault:
  202. if (ret == -EFAULT) {
  203. /* Bad news -- userspace fed a bad pointer to a vsyscall. */
  204. warn_bad_vsyscall(KERN_INFO, regs,
  205. "vsyscall fault (exploit attempt?)");
  206. /*
  207. * If we failed to generate a signal for any reason,
  208. * generate one here. (This should be impossible.)
  209. */
  210. if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
  211. !sigismember(&tsk->pending.signal, SIGSEGV)))
  212. goto sigsegv;
  213. return true; /* Don't emulate the ret. */
  214. }
  215. regs->ax = ret;
  216. do_ret:
  217. /* Emulate a ret instruction. */
  218. regs->ip = caller;
  219. regs->sp += 8;
  220. return true;
  221. sigsegv:
  222. force_sig(SIGSEGV, current);
  223. return true;
  224. }
  225. /*
  226. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  227. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  228. * not need special handling anymore:
  229. */
  230. static const char *gate_vma_name(struct vm_area_struct *vma)
  231. {
  232. return "[vsyscall]";
  233. }
  234. static const struct vm_operations_struct gate_vma_ops = {
  235. .name = gate_vma_name,
  236. };
  237. static struct vm_area_struct gate_vma = {
  238. .vm_start = VSYSCALL_ADDR,
  239. .vm_end = VSYSCALL_ADDR + PAGE_SIZE,
  240. .vm_page_prot = PAGE_READONLY_EXEC,
  241. .vm_flags = VM_READ | VM_EXEC,
  242. .vm_ops = &gate_vma_ops,
  243. };
  244. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  245. {
  246. #ifdef CONFIG_COMPAT
  247. if (!mm || mm->context.ia32_compat)
  248. return NULL;
  249. #endif
  250. if (vsyscall_mode == NONE)
  251. return NULL;
  252. return &gate_vma;
  253. }
  254. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  255. {
  256. struct vm_area_struct *vma = get_gate_vma(mm);
  257. if (!vma)
  258. return 0;
  259. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  260. }
  261. /*
  262. * Use this when you have no reliable mm, typically from interrupt
  263. * context. It is less reliable than using a task's mm and may give
  264. * false positives.
  265. */
  266. int in_gate_area_no_mm(unsigned long addr)
  267. {
  268. return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;
  269. }
  270. void __init map_vsyscall(void)
  271. {
  272. extern char __vsyscall_page;
  273. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  274. if (vsyscall_mode != NONE)
  275. __set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
  276. vsyscall_mode == NATIVE
  277. ? PAGE_KERNEL_VSYSCALL
  278. : PAGE_KERNEL_VVAR);
  279. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
  280. (unsigned long)VSYSCALL_ADDR);
  281. }