vsyscall_64.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2014 Andy Lutomirski <luto@amacapital.net>
  4. *
  5. * Based on the original implementation which is:
  6. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  7. * Copyright 2003 Andi Kleen, SuSE Labs.
  8. *
  9. * Parts of the original code have been moved to arch/x86/vdso/vma.c
  10. *
  11. * This file implements vsyscall emulation. vsyscalls are a legacy ABI:
  12. * Userspace can request certain kernel services by calling fixed
  13. * addresses. This concept is problematic:
  14. *
  15. * - It interferes with ASLR.
  16. * - It's awkward to write code that lives in kernel addresses but is
  17. * callable by userspace at fixed addresses.
  18. * - The whole concept is impossible for 32-bit compat userspace.
  19. * - UML cannot easily virtualize a vsyscall.
  20. *
  21. * As of mid-2014, I believe that there is no new userspace code that
  22. * will use a vsyscall if the vDSO is present. I hope that there will
  23. * soon be no new userspace code that will ever use a vsyscall.
  24. *
  25. * The code in this file emulates vsyscalls when notified of a page
  26. * fault to a vsyscall address.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/timer.h>
  30. #include <linux/sched/signal.h>
  31. #include <linux/mm_types.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/ratelimit.h>
  34. #include <asm/vsyscall.h>
  35. #include <asm/unistd.h>
  36. #include <asm/fixmap.h>
  37. #include <asm/traps.h>
  38. #include <asm/paravirt.h>
  39. #define CREATE_TRACE_POINTS
  40. #include "vsyscall_trace.h"
  41. static enum { EMULATE, NONE } vsyscall_mode =
  42. #ifdef 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("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. clear_siginfo(&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. unsigned long orig_dx;
  112. /*
  113. * No point in checking CS -- the only way to get here is a user mode
  114. * trap to a high address, which means that we're in 64-bit user code.
  115. */
  116. WARN_ON_ONCE(address != regs->ip);
  117. if (vsyscall_mode == NONE) {
  118. warn_bad_vsyscall(KERN_INFO, regs,
  119. "vsyscall attempted with vsyscall=none");
  120. return false;
  121. }
  122. vsyscall_nr = addr_to_vsyscall_nr(address);
  123. trace_emulate_vsyscall(vsyscall_nr);
  124. if (vsyscall_nr < 0) {
  125. warn_bad_vsyscall(KERN_WARNING, regs,
  126. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  127. goto sigsegv;
  128. }
  129. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  130. warn_bad_vsyscall(KERN_WARNING, regs,
  131. "vsyscall with bad stack (exploit attempt?)");
  132. goto sigsegv;
  133. }
  134. tsk = current;
  135. /*
  136. * Check for access_ok violations and find the syscall nr.
  137. *
  138. * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
  139. * 64-bit, so we don't need to special-case it here. For all the
  140. * vsyscalls, NULL means "don't write anything" not "write it at
  141. * address 0".
  142. */
  143. switch (vsyscall_nr) {
  144. case 0:
  145. if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
  146. !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
  147. ret = -EFAULT;
  148. goto check_fault;
  149. }
  150. syscall_nr = __NR_gettimeofday;
  151. break;
  152. case 1:
  153. if (!write_ok_or_segv(regs->di, sizeof(time_t))) {
  154. ret = -EFAULT;
  155. goto check_fault;
  156. }
  157. syscall_nr = __NR_time;
  158. break;
  159. case 2:
  160. if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
  161. !write_ok_or_segv(regs->si, sizeof(unsigned))) {
  162. ret = -EFAULT;
  163. goto check_fault;
  164. }
  165. syscall_nr = __NR_getcpu;
  166. break;
  167. }
  168. /*
  169. * Handle seccomp. regs->ip must be the original value.
  170. * See seccomp_send_sigsys and Documentation/userspace-api/seccomp_filter.rst.
  171. *
  172. * We could optimize the seccomp disabled case, but performance
  173. * here doesn't matter.
  174. */
  175. regs->orig_ax = syscall_nr;
  176. regs->ax = -ENOSYS;
  177. tmp = secure_computing(NULL);
  178. if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
  179. warn_bad_vsyscall(KERN_DEBUG, regs,
  180. "seccomp tried to change syscall nr or ip");
  181. do_exit(SIGSYS);
  182. }
  183. regs->orig_ax = -1;
  184. if (tmp)
  185. goto do_ret; /* skip requested */
  186. /*
  187. * With a real vsyscall, page faults cause SIGSEGV. We want to
  188. * preserve that behavior to make writing exploits harder.
  189. */
  190. prev_sig_on_uaccess_err = current->thread.sig_on_uaccess_err;
  191. current->thread.sig_on_uaccess_err = 1;
  192. ret = -EFAULT;
  193. switch (vsyscall_nr) {
  194. case 0:
  195. /* this decodes regs->di and regs->si on its own */
  196. ret = __x64_sys_gettimeofday(regs);
  197. break;
  198. case 1:
  199. /* this decodes regs->di on its own */
  200. ret = __x64_sys_time(regs);
  201. break;
  202. case 2:
  203. /* while we could clobber regs->dx, we didn't in the past... */
  204. orig_dx = regs->dx;
  205. regs->dx = 0;
  206. /* this decodes regs->di, regs->si and regs->dx on its own */
  207. ret = __x64_sys_getcpu(regs);
  208. regs->dx = orig_dx;
  209. break;
  210. }
  211. current->thread.sig_on_uaccess_err = prev_sig_on_uaccess_err;
  212. check_fault:
  213. if (ret == -EFAULT) {
  214. /* Bad news -- userspace fed a bad pointer to a vsyscall. */
  215. warn_bad_vsyscall(KERN_INFO, regs,
  216. "vsyscall fault (exploit attempt?)");
  217. /*
  218. * If we failed to generate a signal for any reason,
  219. * generate one here. (This should be impossible.)
  220. */
  221. if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
  222. !sigismember(&tsk->pending.signal, SIGSEGV)))
  223. goto sigsegv;
  224. return true; /* Don't emulate the ret. */
  225. }
  226. regs->ax = ret;
  227. do_ret:
  228. /* Emulate a ret instruction. */
  229. regs->ip = caller;
  230. regs->sp += 8;
  231. return true;
  232. sigsegv:
  233. force_sig(SIGSEGV, current);
  234. return true;
  235. }
  236. /*
  237. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  238. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  239. * not need special handling anymore:
  240. */
  241. static const char *gate_vma_name(struct vm_area_struct *vma)
  242. {
  243. return "[vsyscall]";
  244. }
  245. static const struct vm_operations_struct gate_vma_ops = {
  246. .name = gate_vma_name,
  247. };
  248. static struct vm_area_struct gate_vma = {
  249. .vm_start = VSYSCALL_ADDR,
  250. .vm_end = VSYSCALL_ADDR + PAGE_SIZE,
  251. .vm_page_prot = PAGE_READONLY_EXEC,
  252. .vm_flags = VM_READ | VM_EXEC,
  253. .vm_ops = &gate_vma_ops,
  254. };
  255. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  256. {
  257. #ifdef CONFIG_COMPAT
  258. if (!mm || mm->context.ia32_compat)
  259. return NULL;
  260. #endif
  261. if (vsyscall_mode == NONE)
  262. return NULL;
  263. return &gate_vma;
  264. }
  265. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  266. {
  267. struct vm_area_struct *vma = get_gate_vma(mm);
  268. if (!vma)
  269. return 0;
  270. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  271. }
  272. /*
  273. * Use this when you have no reliable mm, typically from interrupt
  274. * context. It is less reliable than using a task's mm and may give
  275. * false positives.
  276. */
  277. int in_gate_area_no_mm(unsigned long addr)
  278. {
  279. return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;
  280. }
  281. /*
  282. * The VSYSCALL page is the only user-accessible page in the kernel address
  283. * range. Normally, the kernel page tables can have _PAGE_USER clear, but
  284. * the tables covering VSYSCALL_ADDR need _PAGE_USER set if vsyscalls
  285. * are enabled.
  286. *
  287. * Some day we may create a "minimal" vsyscall mode in which we emulate
  288. * vsyscalls but leave the page not present. If so, we skip calling
  289. * this.
  290. */
  291. void __init set_vsyscall_pgtable_user_bits(pgd_t *root)
  292. {
  293. pgd_t *pgd;
  294. p4d_t *p4d;
  295. pud_t *pud;
  296. pmd_t *pmd;
  297. pgd = pgd_offset_pgd(root, VSYSCALL_ADDR);
  298. set_pgd(pgd, __pgd(pgd_val(*pgd) | _PAGE_USER));
  299. p4d = p4d_offset(pgd, VSYSCALL_ADDR);
  300. #if CONFIG_PGTABLE_LEVELS >= 5
  301. set_p4d(p4d, __p4d(p4d_val(*p4d) | _PAGE_USER));
  302. #endif
  303. pud = pud_offset(p4d, VSYSCALL_ADDR);
  304. set_pud(pud, __pud(pud_val(*pud) | _PAGE_USER));
  305. pmd = pmd_offset(pud, VSYSCALL_ADDR);
  306. set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_USER));
  307. }
  308. void __init map_vsyscall(void)
  309. {
  310. extern char __vsyscall_page;
  311. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  312. if (vsyscall_mode != NONE) {
  313. __set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
  314. PAGE_KERNEL_VVAR);
  315. set_vsyscall_pgtable_user_bits(swapper_pg_dir);
  316. }
  317. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
  318. (unsigned long)VSYSCALL_ADDR);
  319. }