vsyscall_64.c 8.3 KB

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