vsyscall_64.c 8.4 KB

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