Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1350460ybg; Thu, 4 Jun 2020 07:36:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyU1JyilzIWnXL5JIMU6xXEhmiT6E14mQ0uGTreqhVItkwHR69Aps4bovpSAVfLhdT4rtsz X-Received: by 2002:a17:906:1857:: with SMTP id w23mr4248439eje.273.1591281364849; Thu, 04 Jun 2020 07:36:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591281364; cv=none; d=google.com; s=arc-20160816; b=pKWZSVV1CHIu6xxfONs+H/0kX6hcNT9VOLjYU+GdpwAHCErWLwJbu2g2rKmT/6Nvh/ MgWU82O9AgAyAjpgzk4C3Tu/sCLzY6ZQB+7aL+WPBpF2MB4BtWbKfg/fShWK9xvdtu1v zcob0+J/PKTKlqAHh2JqlNq1V6UTO9JOs9eCpmhITB5mvrKGKad6p5oSYjfN2DNX4CG4 9hvvEa3+TWstL35ZOb7yyftnjX3o8lPgfWCsQvoXhnDHx6d5avckzN/Ou/s8GMvDa0TD IOvVIBV6BkjboOIk7mfGaTemWYW+c3z0raJVx/mK6mng9Ue3dPX8Ho4x64PKW6jVSJfl QVfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=h9M1fLh8Ru9H4w+WFRAfiLMCmRmx0L8dSLxiYIXuu4E=; b=rzTw9ETetrU97MEx3ci/f/5a2ibcRKF0uCAHeCE7Icg4y4VabuH97hpCjgt2dzac+e 26GUleMZe5YD9n9TT7RMWwDj30S+Rilcg1fZq4nkcVqOWztTFiyUpVJpvCWZcwUspgba EkACcQ57CIf4hv5K5X6m2MF2w4/EBCQN+A+1pfMLhQJQa/Dd6tIczwtH192dPJPhCt6r SGNYwjfXdarIJqkazKhXaRX56g+fxi0VDQsiWV/V5RkJHZ4mndpVXbAjpBnlXgk+P3Qs HVuV0ZbqdK5W5ojBb2jyRxTHJ7boYpRgNPrLxg9dFfhkpJeZFoy6c76E/wY+31E5zTid BpMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EA8m89u9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w19si1620633edt.402.2020.06.04.07.35.41; Thu, 04 Jun 2020 07:36:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EA8m89u9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729019AbgFDOc0 (ORCPT + 99 others); Thu, 4 Jun 2020 10:32:26 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:59194 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728919AbgFDOc0 (ORCPT ); Thu, 4 Jun 2020 10:32:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591281144; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=h9M1fLh8Ru9H4w+WFRAfiLMCmRmx0L8dSLxiYIXuu4E=; b=EA8m89u9f8/g+TqsmQ3ZIOv2b/lDQrZViHjh3wwcFYyFitIOTM6RJ2vnHNK0syReTTUzTN meke3oqv4RBmX0XZtTZRO1q/SUKqMPelHWdY+fAHRnPCO36cop1s2L2vQJp9sr9f2/hQhZ 2zAG7HYpcLkVPUsn0wHtyNSoxeqsV9Q= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-238-QJFS2nS7OyOa-MMajM20aw-1; Thu, 04 Jun 2020 10:32:20 -0400 X-MC-Unique: QJFS2nS7OyOa-MMajM20aw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7B75B1800D42; Thu, 4 Jun 2020 14:32:02 +0000 (UTC) Received: from vitty.brq.redhat.com (unknown [10.40.195.49]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3C6FB7CCC1; Thu, 4 Jun 2020 14:32:00 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Wanpeng Li , Jim Mattson , linux-kernel@vger.kernel.org Subject: [PATCH] KVM: nVMX: Inject #GP when nested_vmx_get_vmptr() fails to read guest memory Date: Thu, 4 Jun 2020 16:31:58 +0200 Message-Id: <20200604143158.484651-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Syzbot reports the following issue: WARNING: CPU: 0 PID: 6819 at arch/x86/kvm/x86.c:618 kvm_inject_emulated_page_fault+0x210/0x290 arch/x86/kvm/x86.c:618 ... Call Trace: ... RIP: 0010:kvm_inject_emulated_page_fault+0x210/0x290 arch/x86/kvm/x86.c:618 ... nested_vmx_get_vmptr+0x1f9/0x2a0 arch/x86/kvm/vmx/nested.c:4638 handle_vmon arch/x86/kvm/vmx/nested.c:4767 [inline] handle_vmon+0x168/0x3a0 arch/x86/kvm/vmx/nested.c:4728 vmx_handle_exit+0x29c/0x1260 arch/x86/kvm/vmx/vmx.c:6067 'exception' we're trying to inject with kvm_inject_emulated_page_fault() comes from nested_vmx_get_vmptr() kvm_read_guest_virt() kvm_read_guest_virt_helper() vcpu->arch.walk_mmu->gva_to_gpa() but it is only set when GVA to GPA conversion fails. In case it doesn't but we still fail kvm_vcpu_read_guest_page(), X86EMUL_IO_NEEDED is returned and nested_vmx_get_vmptr() calls kvm_inject_emulated_page_fault() with zeroed 'exception'. This happen when e.g. VMXON/VMPTRLD/VMCLEAR argument is MMIO. KVM could've handled the request correctly by going to userspace and performing I/O but there doesn't seem to be a good need for such requests in the first place. Sane guests should not call VMXON/VMPTRLD/VMCLEAR with anything but normal memory. Just inject #GP to find insane ones. Reported-by: syzbot+2a7156e11dc199bdbd8a@syzkaller.appspotmail.com Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/vmx/nested.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 9c74a732b08d..05d57c3cb1ce 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4628,14 +4628,29 @@ static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) { gva_t gva; struct x86_exception e; + int r; if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), vmcs_read32(VMX_INSTRUCTION_INFO), false, sizeof(*vmpointer), &gva)) return 1; - if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { - kvm_inject_emulated_page_fault(vcpu, &e); + r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); + if (r != X86EMUL_CONTINUE) { + if (r == X86EMUL_PROPAGATE_FAULT) { + kvm_inject_emulated_page_fault(vcpu, &e); + } else { + /* + * X86EMUL_IO_NEEDED is returned when kvm_vcpu_read_guest_page() + * fails to read guest's memory (e.g. when 'gva' points to MMIO + * space). While KVM could've handled the request correctly by + * exiting to userspace and performing I/O, there doesn't seem + * to be a real use-case behind such requests, just inject #GP + * for now. + */ + kvm_inject_gp(vcpu, 0); + } + return 1; } -- 2.25.4