vsyscall_64.c 8.3 KB

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