Path: blob/master/arch/x86/entry/vsyscall/vsyscall_64.c
26489 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (c) 2012-2014 Andy Lutomirski <[email protected]>3*4* Based on the original implementation which is:5* Copyright (C) 2001 Andrea Arcangeli <[email protected]> SuSE6* Copyright 2003 Andi Kleen, SuSE Labs.7*8* Parts of the original code have been moved to arch/x86/vdso/vma.c9*10* This file implements vsyscall emulation. vsyscalls are a legacy ABI:11* Userspace can request certain kernel services by calling fixed12* addresses. This concept is problematic:13*14* - It interferes with ASLR.15* - It's awkward to write code that lives in kernel addresses but is16* 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 that21* will use a vsyscall if the vDSO is present. I hope that there will22* 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 page25* fault to a vsyscall address.26*/2728#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>3435#include <asm/vsyscall.h>36#include <asm/unistd.h>37#include <asm/fixmap.h>38#include <asm/traps.h>39#include <asm/paravirt.h>4041#define CREATE_TRACE_POINTS42#include "vsyscall_trace.h"4344static enum { EMULATE, XONLY, NONE } vsyscall_mode __ro_after_init =45#ifdef CONFIG_LEGACY_VSYSCALL_NONE46NONE;47#elif defined(CONFIG_LEGACY_VSYSCALL_XONLY)48XONLY;49#else50#error VSYSCALL config is broken51#endif5253static int __init vsyscall_setup(char *str)54{55if (str) {56if (!strcmp("emulate", str))57vsyscall_mode = EMULATE;58else if (!strcmp("xonly", str))59vsyscall_mode = XONLY;60else if (!strcmp("none", str))61vsyscall_mode = NONE;62else63return -EINVAL;6465return 0;66}6768return -EINVAL;69}70early_param("vsyscall", vsyscall_setup);7172static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,73const char *message)74{75if (!show_unhandled_signals)76return;7778printk_ratelimited("%s%s[%d] %s ip:%lx cs:%x sp:%lx ax:%lx si:%lx di:%lx\n",79level, current->comm, task_pid_nr(current),80message, regs->ip, regs->cs,81regs->sp, regs->ax, regs->si, regs->di);82}8384static int addr_to_vsyscall_nr(unsigned long addr)85{86int nr;8788if ((addr & ~0xC00UL) != VSYSCALL_ADDR)89return -EINVAL;9091nr = (addr & 0xC00UL) >> 10;92if (nr >= 3)93return -EINVAL;9495return nr;96}9798static bool write_ok_or_segv(unsigned long ptr, size_t size)99{100if (!access_ok((void __user *)ptr, size)) {101struct thread_struct *thread = ¤t->thread;102103thread->error_code = X86_PF_USER | X86_PF_WRITE;104thread->cr2 = ptr;105thread->trap_nr = X86_TRAP_PF;106107force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)ptr);108return false;109} else {110return true;111}112}113114bool emulate_vsyscall(unsigned long error_code,115struct pt_regs *regs, unsigned long address)116{117unsigned long caller;118int vsyscall_nr, syscall_nr, tmp;119long ret;120unsigned long orig_dx;121122/* Write faults or kernel-privilege faults never get fixed up. */123if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER)124return false;125126if (!(error_code & X86_PF_INSTR)) {127/* Failed vsyscall read */128if (vsyscall_mode == EMULATE)129return false;130131/*132* User code tried and failed to read the vsyscall page.133*/134warn_bad_vsyscall(KERN_INFO, regs, "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround");135return false;136}137138/*139* No point in checking CS -- the only way to get here is a user mode140* trap to a high address, which means that we're in 64-bit user code.141*/142143WARN_ON_ONCE(address != regs->ip);144145if (vsyscall_mode == NONE) {146warn_bad_vsyscall(KERN_INFO, regs,147"vsyscall attempted with vsyscall=none");148return false;149}150151vsyscall_nr = addr_to_vsyscall_nr(address);152153trace_emulate_vsyscall(vsyscall_nr);154155if (vsyscall_nr < 0) {156warn_bad_vsyscall(KERN_WARNING, regs,157"misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");158goto sigsegv;159}160161if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {162warn_bad_vsyscall(KERN_WARNING, regs,163"vsyscall with bad stack (exploit attempt?)");164goto sigsegv;165}166167/*168* Check for access_ok violations and find the syscall nr.169*170* NULL is a valid user pointer (in the access_ok sense) on 32-bit and171* 64-bit, so we don't need to special-case it here. For all the172* vsyscalls, NULL means "don't write anything" not "write it at173* address 0".174*/175switch (vsyscall_nr) {176case 0:177if (!write_ok_or_segv(regs->di, sizeof(struct __kernel_old_timeval)) ||178!write_ok_or_segv(regs->si, sizeof(struct timezone))) {179ret = -EFAULT;180goto check_fault;181}182183syscall_nr = __NR_gettimeofday;184break;185186case 1:187if (!write_ok_or_segv(regs->di, sizeof(__kernel_old_time_t))) {188ret = -EFAULT;189goto check_fault;190}191192syscall_nr = __NR_time;193break;194195case 2:196if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||197!write_ok_or_segv(regs->si, sizeof(unsigned))) {198ret = -EFAULT;199goto check_fault;200}201202syscall_nr = __NR_getcpu;203break;204}205206/*207* Handle seccomp. regs->ip must be the original value.208* See seccomp_send_sigsys and Documentation/userspace-api/seccomp_filter.rst.209*210* We could optimize the seccomp disabled case, but performance211* here doesn't matter.212*/213regs->orig_ax = syscall_nr;214regs->ax = -ENOSYS;215tmp = secure_computing();216if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {217warn_bad_vsyscall(KERN_DEBUG, regs,218"seccomp tried to change syscall nr or ip");219force_exit_sig(SIGSYS);220return true;221}222regs->orig_ax = -1;223if (tmp)224goto do_ret; /* skip requested */225226/*227* With a real vsyscall, page faults cause SIGSEGV.228*/229ret = -EFAULT;230switch (vsyscall_nr) {231case 0:232/* this decodes regs->di and regs->si on its own */233ret = __x64_sys_gettimeofday(regs);234break;235236case 1:237/* this decodes regs->di on its own */238ret = __x64_sys_time(regs);239break;240241case 2:242/* while we could clobber regs->dx, we didn't in the past... */243orig_dx = regs->dx;244regs->dx = 0;245/* this decodes regs->di, regs->si and regs->dx on its own */246ret = __x64_sys_getcpu(regs);247regs->dx = orig_dx;248break;249}250251check_fault:252if (ret == -EFAULT) {253/* Bad news -- userspace fed a bad pointer to a vsyscall. */254warn_bad_vsyscall(KERN_INFO, regs,255"vsyscall fault (exploit attempt?)");256goto sigsegv;257}258259regs->ax = ret;260261do_ret:262/* Emulate a ret instruction. */263regs->ip = caller;264regs->sp += 8;265return true;266267sigsegv:268force_sig(SIGSEGV);269return true;270}271272/*273* A pseudo VMA to allow ptrace access for the vsyscall page. This only274* covers the 64bit vsyscall page now. 32bit has a real VMA now and does275* not need special handling anymore:276*/277static const char *gate_vma_name(struct vm_area_struct *vma)278{279return "[vsyscall]";280}281static const struct vm_operations_struct gate_vma_ops = {282.name = gate_vma_name,283};284static struct vm_area_struct gate_vma __ro_after_init = {285.vm_start = VSYSCALL_ADDR,286.vm_end = VSYSCALL_ADDR + PAGE_SIZE,287.vm_page_prot = PAGE_READONLY_EXEC,288.vm_flags = VM_READ | VM_EXEC,289.vm_ops = &gate_vma_ops,290};291292struct vm_area_struct *get_gate_vma(struct mm_struct *mm)293{294#ifdef CONFIG_COMPAT295if (!mm || !test_bit(MM_CONTEXT_HAS_VSYSCALL, &mm->context.flags))296return NULL;297#endif298if (vsyscall_mode == NONE)299return NULL;300return &gate_vma;301}302303int in_gate_area(struct mm_struct *mm, unsigned long addr)304{305struct vm_area_struct *vma = get_gate_vma(mm);306307if (!vma)308return 0;309310return (addr >= vma->vm_start) && (addr < vma->vm_end);311}312313/*314* Use this when you have no reliable mm, typically from interrupt315* context. It is less reliable than using a task's mm and may give316* false positives.317*/318int in_gate_area_no_mm(unsigned long addr)319{320return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;321}322323/*324* The VSYSCALL page is the only user-accessible page in the kernel address325* range. Normally, the kernel page tables can have _PAGE_USER clear, but326* the tables covering VSYSCALL_ADDR need _PAGE_USER set if vsyscalls327* are enabled.328*329* Some day we may create a "minimal" vsyscall mode in which we emulate330* vsyscalls but leave the page not present. If so, we skip calling331* this.332*/333void __init set_vsyscall_pgtable_user_bits(pgd_t *root)334{335pgd_t *pgd;336p4d_t *p4d;337pud_t *pud;338pmd_t *pmd;339340pgd = pgd_offset_pgd(root, VSYSCALL_ADDR);341set_pgd(pgd, __pgd(pgd_val(*pgd) | _PAGE_USER));342p4d = p4d_offset(pgd, VSYSCALL_ADDR);343set_p4d(p4d, __p4d(p4d_val(*p4d) | _PAGE_USER));344pud = pud_offset(p4d, VSYSCALL_ADDR);345set_pud(pud, __pud(pud_val(*pud) | _PAGE_USER));346pmd = pmd_offset(pud, VSYSCALL_ADDR);347set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_USER));348}349350void __init map_vsyscall(void)351{352extern char __vsyscall_page;353unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);354355/*356* For full emulation, the page needs to exist for real. In357* execute-only mode, there is no PTE at all backing the vsyscall358* page.359*/360if (vsyscall_mode == EMULATE) {361__set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,362PAGE_KERNEL_VVAR);363set_vsyscall_pgtable_user_bits(swapper_pg_dir);364}365366if (vsyscall_mode == XONLY)367vm_flags_init(&gate_vma, VM_EXEC);368369BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=370(unsigned long)VSYSCALL_ADDR);371}372373374