vsyscall_64.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. struct thread_struct *thread = &current->thread;
  89. thread->error_code = 6; /* user fault, no page, write */
  90. thread->cr2 = ptr;
  91. thread->trap_nr = X86_TRAP_PF;
  92. force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)ptr, current);
  93. return false;
  94. } else {
  95. return true;
  96. }
  97. }
  98. bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
  99. {
  100. struct task_struct *tsk;
  101. unsigned long caller;
  102. int vsyscall_nr, syscall_nr, tmp;
  103. int prev_sig_on_uaccess_err;
  104. long ret;
  105. unsigned long orig_dx;
  106. /*
  107. * No point in checking CS -- the only way to get here is a user mode
  108. * trap to a high address, which means that we're in 64-bit user code.
  109. */
  110. WARN_ON_ONCE(address != regs->ip);
  111. if (vsyscall_mode == NONE) {
  112. warn_bad_vsyscall(KERN_INFO, regs,
  113. "vsyscall attempted with vsyscall=none");
  114. return false;
  115. }
  116. vsyscall_nr = addr_to_vsyscall_nr(address);
  117. trace_emulate_vsyscall(vsyscall_nr);
  118. if (vsyscall_nr < 0) {
  119. warn_bad_vsyscall(KERN_WARNING, regs,
  120. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  121. goto sigsegv;
  122. }
  123. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  124. warn_bad_vsyscall(KERN_WARNING, regs,
  125. "vsyscall with bad stack (exploit attempt?)");
  126. goto sigsegv;
  127. }
  128. tsk = current;
  129. /*
  130. * Check for access_ok violations and find the syscall nr.
  131. *
  132. * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
  133. * 64-bit, so we don't need to special-case it here. For all the
  134. * vsyscalls, NULL means "don't write anything" not "write it at
  135. * address 0".
  136. */
  137. switch (vsyscall_nr) {
  138. case 0:
  139. if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
  140. !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
  141. ret = -EFAULT;
  142. goto check_fault;
  143. }
  144. syscall_nr = __NR_gettimeofday;
  145. break;
  146. case 1:
  147. if (!write_ok_or_segv(regs->di, sizeof(time_t))) {
  148. ret = -EFAULT;
  149. goto check_fault;
  150. }
  151. syscall_nr = __NR_time;
  152. break;
  153. case 2:
  154. if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
  155. !write_ok_or_segv(regs->si, sizeof(unsigned))) {
  156. ret = -EFAULT;
  157. goto check_fault;
  158. }
  159. syscall_nr = __NR_getcpu;
  160. break;
  161. }
  162. /*
  163. * Handle seccomp. regs->ip must be the original value.
  164. * See seccomp_send_sigsys and Documentation/userspace-api/seccomp_filter.rst.
  165. *
  166. * We could optimize the seccomp disabled case, but performance
  167. * here doesn't matter.
  168. */
  169. regs->orig_ax = syscall_nr;
  170. regs->ax = -ENOSYS;
  171. tmp = secure_computing(NULL);
  172. if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
  173. warn_bad_vsyscall(KERN_DEBUG, regs,
  174. "seccomp tried to change syscall nr or ip");
  175. do_exit(SIGSYS);
  176. }
  177. regs->orig_ax = -1;
  178. if (tmp)
  179. goto do_ret; /* skip requested */
  180. /*
  181. * With a real vsyscall, page faults cause SIGSEGV. We want to
  182. * preserve that behavior to make writing exploits harder.
  183. */
  184. prev_sig_on_uaccess_err = current->thread.sig_on_uaccess_err;
  185. current->thread.sig_on_uaccess_err = 1;
  186. ret = -EFAULT;
  187. switch (vsyscall_nr) {
  188. case 0:
  189. /* this decodes regs->di and regs->si on its own */
  190. ret = __x64_sys_gettimeofday(regs);
  191. break;
  192. case 1:
  193. /* this decodes regs->di on its own */
  194. ret = __x64_sys_time(regs);
  195. break;
  196. case 2:
  197. /* while we could clobber regs->dx, we didn't in the past... */
  198. orig_dx = regs->dx;
  199. regs->dx = 0;
  200. /* this decodes regs->di, regs->si and regs->dx on its own */
  201. ret = __x64_sys_getcpu(regs);
  202. regs->dx = orig_dx;
  203. break;
  204. }
  205. current->thread.sig_on_uaccess_err = prev_sig_on_uaccess_err;
  206. check_fault:
  207. if (ret == -EFAULT) {
  208. /* Bad news -- userspace fed a bad pointer to a vsyscall. */
  209. warn_bad_vsyscall(KERN_INFO, regs,
  210. "vsyscall fault (exploit attempt?)");
  211. /*
  212. * If we failed to generate a signal for any reason,
  213. * generate one here. (This should be impossible.)
  214. */
  215. if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
  216. !sigismember(&tsk->pending.signal, SIGSEGV)))
  217. goto sigsegv;
  218. return true; /* Don't emulate the ret. */
  219. }
  220. regs->ax = ret;
  221. do_ret:
  222. /* Emulate a ret instruction. */
  223. regs->ip = caller;
  224. regs->sp += 8;
  225. return true;
  226. sigsegv:
  227. force_sig(SIGSEGV, current);
  228. return true;
  229. }
  230. /*
  231. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  232. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  233. * not need special handling anymore:
  234. */
  235. static const char *gate_vma_name(struct vm_area_struct *vma)
  236. {
  237. return "[vsyscall]";
  238. }
  239. static const struct vm_operations_struct gate_vma_ops = {
  240. .name = gate_vma_name,
  241. };
  242. static struct vm_area_struct gate_vma = {
  243. .vm_start = VSYSCALL_ADDR,
  244. .vm_end = VSYSCALL_ADDR + PAGE_SIZE,
  245. .vm_page_prot = PAGE_READONLY_EXEC,
  246. .vm_flags = VM_READ | VM_EXEC,
  247. .vm_ops = &gate_vma_ops,
  248. };
  249. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  250. {
  251. #ifdef CONFIG_COMPAT
  252. if (!mm || mm->context.ia32_compat)
  253. return NULL;
  254. #endif
  255. if (vsyscall_mode == NONE)
  256. return NULL;
  257. return &gate_vma;
  258. }
  259. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  260. {
  261. struct vm_area_struct *vma = get_gate_vma(mm);
  262. if (!vma)
  263. return 0;
  264. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  265. }
  266. /*
  267. * Use this when you have no reliable mm, typically from interrupt
  268. * context. It is less reliable than using a task's mm and may give
  269. * false positives.
  270. */
  271. int in_gate_area_no_mm(unsigned long addr)
  272. {
  273. return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;
  274. }
  275. /*
  276. * The VSYSCALL page is the only user-accessible page in the kernel address
  277. * range. Normally, the kernel page tables can have _PAGE_USER clear, but
  278. * the tables covering VSYSCALL_ADDR need _PAGE_USER set if vsyscalls
  279. * are enabled.
  280. *
  281. * Some day we may create a "minimal" vsyscall mode in which we emulate
  282. * vsyscalls but leave the page not present. If so, we skip calling
  283. * this.
  284. */
  285. void __init set_vsyscall_pgtable_user_bits(pgd_t *root)
  286. {
  287. pgd_t *pgd;
  288. p4d_t *p4d;
  289. pud_t *pud;
  290. pmd_t *pmd;
  291. pgd = pgd_offset_pgd(root, VSYSCALL_ADDR);
  292. set_pgd(pgd, __pgd(pgd_val(*pgd) | _PAGE_USER));
  293. p4d = p4d_offset(pgd, VSYSCALL_ADDR);
  294. #if CONFIG_PGTABLE_LEVELS >= 5
  295. set_p4d(p4d, __p4d(p4d_val(*p4d) | _PAGE_USER));
  296. #endif
  297. pud = pud_offset(p4d, VSYSCALL_ADDR);
  298. set_pud(pud, __pud(pud_val(*pud) | _PAGE_USER));
  299. pmd = pmd_offset(pud, VSYSCALL_ADDR);
  300. set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_USER));
  301. }
  302. void __init map_vsyscall(void)
  303. {
  304. extern char __vsyscall_page;
  305. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  306. if (vsyscall_mode != NONE) {
  307. __set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
  308. PAGE_KERNEL_VVAR);
  309. set_vsyscall_pgtable_user_bits(swapper_pg_dir);
  310. }
  311. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
  312. (unsigned long)VSYSCALL_ADDR);
  313. }