vsyscall_64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2014 Andy Lutomirski <[email protected]>
  4. *
  5. * Based on the original implementation which is:
  6. * Copyright (C) 2001 Andrea Arcangeli <[email protected]> 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, XONLY, NONE } vsyscall_mode __ro_after_init =
  42. #ifdef CONFIG_LEGACY_VSYSCALL_NONE
  43. NONE;
  44. #elif defined(CONFIG_LEGACY_VSYSCALL_XONLY)
  45. XONLY;
  46. #else
  47. #error VSYSCALL config is broken
  48. #endif
  49. static int __init vsyscall_setup(char *str)
  50. {
  51. if (str) {
  52. if (!strcmp("emulate", str))
  53. vsyscall_mode = EMULATE;
  54. else if (!strcmp("xonly", str))
  55. vsyscall_mode = XONLY;
  56. else if (!strcmp("none", str))
  57. vsyscall_mode = NONE;
  58. else
  59. return -EINVAL;
  60. return 0;
  61. }
  62. return -EINVAL;
  63. }
  64. early_param("vsyscall", vsyscall_setup);
  65. static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
  66. const char *message)
  67. {
  68. if (!show_unhandled_signals)
  69. return;
  70. printk_ratelimited("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
  71. level, current->comm, task_pid_nr(current),
  72. message, regs->ip, regs->cs,
  73. regs->sp, regs->ax, regs->si, regs->di);
  74. }
  75. static int addr_to_vsyscall_nr(unsigned long addr)
  76. {
  77. int nr;
  78. if ((addr & ~0xC00UL) != VSYSCALL_ADDR)
  79. return -EINVAL;
  80. nr = (addr & 0xC00UL) >> 10;
  81. if (nr >= 3)
  82. return -EINVAL;
  83. return nr;
  84. }
  85. static bool write_ok_or_segv(unsigned long ptr, size_t size)
  86. {
  87. /*
  88. * XXX: if access_ok, get_user, and put_user handled
  89. * sig_on_uaccess_err, this could go away.
  90. */
  91. if (!access_ok((void __user *)ptr, size)) {
  92. struct thread_struct *thread = &current->thread;
  93. thread->error_code = X86_PF_USER | X86_PF_WRITE;
  94. thread->cr2 = ptr;
  95. thread->trap_nr = X86_TRAP_PF;
  96. force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)ptr);
  97. return false;
  98. } else {
  99. return true;
  100. }
  101. }
  102. bool emulate_vsyscall(unsigned long error_code,
  103. struct pt_regs *regs, unsigned long address)
  104. {
  105. struct task_struct *tsk;
  106. unsigned long caller;
  107. int vsyscall_nr, syscall_nr, tmp;
  108. int prev_sig_on_uaccess_err;
  109. long ret;
  110. unsigned long orig_dx;
  111. /* Write faults or kernel-privilege faults never get fixed up. */
  112. if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER)
  113. return false;
  114. if (!(error_code & X86_PF_INSTR)) {
  115. /* Failed vsyscall read */
  116. if (vsyscall_mode == EMULATE)
  117. return false;
  118. /*
  119. * User code tried and failed to read the vsyscall page.
  120. */
  121. warn_bad_vsyscall(KERN_INFO, regs, "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround");
  122. return false;
  123. }
  124. /*
  125. * No point in checking CS -- the only way to get here is a user mode
  126. * trap to a high address, which means that we're in 64-bit user code.
  127. */
  128. WARN_ON_ONCE(address != regs->ip);
  129. if (vsyscall_mode == NONE) {
  130. warn_bad_vsyscall(KERN_INFO, regs,
  131. "vsyscall attempted with vsyscall=none");
  132. return false;
  133. }
  134. vsyscall_nr = addr_to_vsyscall_nr(address);
  135. trace_emulate_vsyscall(vsyscall_nr);
  136. if (vsyscall_nr < 0) {
  137. warn_bad_vsyscall(KERN_WARNING, regs,
  138. "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
  139. goto sigsegv;
  140. }
  141. if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
  142. warn_bad_vsyscall(KERN_WARNING, regs,
  143. "vsyscall with bad stack (exploit attempt?)");
  144. goto sigsegv;
  145. }
  146. tsk = current;
  147. /*
  148. * Check for access_ok violations and find the syscall nr.
  149. *
  150. * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
  151. * 64-bit, so we don't need to special-case it here. For all the
  152. * vsyscalls, NULL means "don't write anything" not "write it at
  153. * address 0".
  154. */
  155. switch (vsyscall_nr) {
  156. case 0:
  157. if (!write_ok_or_segv(regs->di, sizeof(struct __kernel_old_timeval)) ||
  158. !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
  159. ret = -EFAULT;
  160. goto check_fault;
  161. }
  162. syscall_nr = __NR_gettimeofday;
  163. break;
  164. case 1:
  165. if (!write_ok_or_segv(regs->di, sizeof(__kernel_old_time_t))) {
  166. ret = -EFAULT;
  167. goto check_fault;
  168. }
  169. syscall_nr = __NR_time;
  170. break;
  171. case 2:
  172. if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
  173. !write_ok_or_segv(regs->si, sizeof(unsigned))) {
  174. ret = -EFAULT;
  175. goto check_fault;
  176. }
  177. syscall_nr = __NR_getcpu;
  178. break;
  179. }
  180. /*
  181. * Handle seccomp. regs->ip must be the original value.
  182. * See seccomp_send_sigsys and Documentation/userspace-api/seccomp_filter.rst.
  183. *
  184. * We could optimize the seccomp disabled case, but performance
  185. * here doesn't matter.
  186. */
  187. regs->orig_ax = syscall_nr;
  188. regs->ax = -ENOSYS;
  189. tmp = secure_computing();
  190. if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
  191. warn_bad_vsyscall(KERN_DEBUG, regs,
  192. "seccomp tried to change syscall nr or ip");
  193. force_exit_sig(SIGSYS);
  194. return true;
  195. }
  196. regs->orig_ax = -1;
  197. if (tmp)
  198. goto do_ret; /* skip requested */
  199. /*
  200. * With a real vsyscall, page faults cause SIGSEGV. We want to
  201. * preserve that behavior to make writing exploits harder.
  202. */
  203. prev_sig_on_uaccess_err = current->thread.sig_on_uaccess_err;
  204. current->thread.sig_on_uaccess_err = 1;
  205. ret = -EFAULT;
  206. switch (vsyscall_nr) {
  207. case 0:
  208. /* this decodes regs->di and regs->si on its own */
  209. ret = __x64_sys_gettimeofday(regs);
  210. break;
  211. case 1:
  212. /* this decodes regs->di on its own */
  213. ret = __x64_sys_time(regs);
  214. break;
  215. case 2:
  216. /* while we could clobber regs->dx, we didn't in the past... */
  217. orig_dx = regs->dx;
  218. regs->dx = 0;
  219. /* this decodes regs->di, regs->si and regs->dx on its own */
  220. ret = __x64_sys_getcpu(regs);
  221. regs->dx = orig_dx;
  222. break;
  223. }
  224. current->thread.sig_on_uaccess_err = prev_sig_on_uaccess_err;
  225. check_fault:
  226. if (ret == -EFAULT) {
  227. /* Bad news -- userspace fed a bad pointer to a vsyscall. */
  228. warn_bad_vsyscall(KERN_INFO, regs,
  229. "vsyscall fault (exploit attempt?)");
  230. /*
  231. * If we failed to generate a signal for any reason,
  232. * generate one here. (This should be impossible.)
  233. */
  234. if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
  235. !sigismember(&tsk->pending.signal, SIGSEGV)))
  236. goto sigsegv;
  237. return true; /* Don't emulate the ret. */
  238. }
  239. regs->ax = ret;
  240. do_ret:
  241. /* Emulate a ret instruction. */
  242. regs->ip = caller;
  243. regs->sp += 8;
  244. return true;
  245. sigsegv:
  246. force_sig(SIGSEGV);
  247. return true;
  248. }
  249. /*
  250. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  251. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  252. * not need special handling anymore:
  253. */
  254. static const char *gate_vma_name(struct vm_area_struct *vma)
  255. {
  256. return "[vsyscall]";
  257. }
  258. static const struct vm_operations_struct gate_vma_ops = {
  259. .name = gate_vma_name,
  260. };
  261. static struct vm_area_struct gate_vma __ro_after_init = {
  262. .vm_start = VSYSCALL_ADDR,
  263. .vm_end = VSYSCALL_ADDR + PAGE_SIZE,
  264. .vm_page_prot = PAGE_READONLY_EXEC,
  265. .vm_flags = VM_READ | VM_EXEC,
  266. .vm_ops = &gate_vma_ops,
  267. };
  268. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  269. {
  270. #ifdef CONFIG_COMPAT
  271. if (!mm || !(mm->context.flags & MM_CONTEXT_HAS_VSYSCALL))
  272. return NULL;
  273. #endif
  274. if (vsyscall_mode == NONE)
  275. return NULL;
  276. return &gate_vma;
  277. }
  278. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  279. {
  280. struct vm_area_struct *vma = get_gate_vma(mm);
  281. if (!vma)
  282. return 0;
  283. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  284. }
  285. /*
  286. * Use this when you have no reliable mm, typically from interrupt
  287. * context. It is less reliable than using a task's mm and may give
  288. * false positives.
  289. */
  290. int in_gate_area_no_mm(unsigned long addr)
  291. {
  292. return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;
  293. }
  294. /*
  295. * The VSYSCALL page is the only user-accessible page in the kernel address
  296. * range. Normally, the kernel page tables can have _PAGE_USER clear, but
  297. * the tables covering VSYSCALL_ADDR need _PAGE_USER set if vsyscalls
  298. * are enabled.
  299. *
  300. * Some day we may create a "minimal" vsyscall mode in which we emulate
  301. * vsyscalls but leave the page not present. If so, we skip calling
  302. * this.
  303. */
  304. void __init set_vsyscall_pgtable_user_bits(pgd_t *root)
  305. {
  306. pgd_t *pgd;
  307. p4d_t *p4d;
  308. pud_t *pud;
  309. pmd_t *pmd;
  310. pgd = pgd_offset_pgd(root, VSYSCALL_ADDR);
  311. set_pgd(pgd, __pgd(pgd_val(*pgd) | _PAGE_USER));
  312. p4d = p4d_offset(pgd, VSYSCALL_ADDR);
  313. #if CONFIG_PGTABLE_LEVELS >= 5
  314. set_p4d(p4d, __p4d(p4d_val(*p4d) | _PAGE_USER));
  315. #endif
  316. pud = pud_offset(p4d, VSYSCALL_ADDR);
  317. set_pud(pud, __pud(pud_val(*pud) | _PAGE_USER));
  318. pmd = pmd_offset(pud, VSYSCALL_ADDR);
  319. set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_USER));
  320. }
  321. void __init map_vsyscall(void)
  322. {
  323. extern char __vsyscall_page;
  324. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  325. /*
  326. * For full emulation, the page needs to exist for real. In
  327. * execute-only mode, there is no PTE at all backing the vsyscall
  328. * page.
  329. */
  330. if (vsyscall_mode == EMULATE) {
  331. __set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
  332. PAGE_KERNEL_VVAR);
  333. set_vsyscall_pgtable_user_bits(swapper_pg_dir);
  334. }
  335. if (vsyscall_mode == XONLY)
  336. vm_flags_init(&gate_vma, VM_EXEC);
  337. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
  338. (unsigned long)VSYSCALL_ADDR);
  339. }