Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1119113ybf; Fri, 28 Feb 2020 14:36:46 -0800 (PST) X-Google-Smtp-Source: APXvYqyuaHvS+AFNV2fFSp2VEKEawCNCOLdr8SqPk2K+TiJOEe+yWWuVlvnUs/kIT1/gF+270bJG X-Received: by 2002:a9d:2028:: with SMTP id n37mr5220960ota.127.1582929406182; Fri, 28 Feb 2020 14:36:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582929406; cv=none; d=google.com; s=arc-20160816; b=cyorpFX5eUj3KmDw5cKAMDxOkU5A9NSrzoGFYPtMVPVY3pkln1JkWfjOlwNpS1BnIV RAahouBQipDEj19B2SYg3AkgiCvMdLpaHcX04C4W1VZPca8XOBwVuU6gTsNxOhS1tE/n 0Yeun64Rw9Lnsyj/988wfzFEd+g2d+ebjsZT2w0A9PQasWGnIQ5340HMtUBsgCrfKNWQ bLAPvWI0biwGuA/Qb2fysGys3Jc0AZ4Qg79DTx96XnOLlv4OGqEQj9xo+pwXA7CUXWX1 fdaoqk9pk7sSFtvn8edaHuWzfnPvgFuur6CtTfC9OY+/K4xJ7LuR0qIG8d3ACCXY31tb mMxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=itGLyJFhteHoHRXbJwQ7xHud93OcnuprMWdfOSiPrOY=; b=z3E0NyURr5/H4Nexxvg10uHOTEC4//z8b8+4olXh6joNh2adUW1llebUPMLwlLl0EP uzaI+CH5H4PB7YxjDNH2UHwgMI4qhFNQdj44mFvnLr6/IdKHV1cF9chQN9OYB0AVs/4j S07Poe5QTWaSB8s+Dr5Ap2CCkGuoxvzYvIwsJohCDcGbxoeRyFLHf1J+kq3SNOQ7+dI+ uqP1lbds96GG30x6Ta6ZQ+lcMsDOAqM6vu05taeBBUayUBApXh0lu0f46kXvdEPQP21k rp1X8uVySg+646/z6w/gOscZYq+kGHoEZgjpebEhafbAKm0KFBkzSMvxgavoVtxvXsD+ /1ZQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v10si2249325ote.275.2020.02.28.14.36.33; Fri, 28 Feb 2020 14:36:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726642AbgB1WgY (ORCPT + 99 others); Fri, 28 Feb 2020 17:36:24 -0500 Received: from mga14.intel.com ([192.55.52.115]:63859 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725957AbgB1WgX (ORCPT ); Fri, 28 Feb 2020 17:36:23 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Feb 2020 14:36:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,497,1574150400"; d="scan'208";a="411545131" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by orsmga005.jf.intel.com with ESMTP; 28 Feb 2020 14:36:22 -0800 Date: Fri, 28 Feb 2020 14:36:22 -0800 From: Sean Christopherson To: Jim Mattson Cc: Mohammed Gamal , kvm list , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Joerg Roedel , LKML Subject: Re: [PATCH 2/5] KVM: VMX: Add guest physical address check in EPT violation and misconfig Message-ID: <20200228223622.GK2329@linux.intel.com> References: <20200227172306.21426-1-mgamal@redhat.com> <20200227172306.21426-3-mgamal@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 27, 2020 at 09:55:32AM -0800, Jim Mattson wrote: > On Thu, Feb 27, 2020 at 9:23 AM Mohammed Gamal wrote: > > > > Check guest physical address against it's maximum physical memory. If > Nit: "its," without an apostrophe. > > > the guest's physical address exceeds the maximum (i.e. has reserved bits > > set), inject a guest page fault with PFERR_RSVD_MASK. Wish I had actually read this series when it first flew by, just spent several hours debugging this exact thing when running the "access" test. > > Signed-off-by: Mohammed Gamal > > --- > > arch/x86/kvm/vmx/vmx.c | 13 +++++++++++++ > > 1 file changed, 13 insertions(+) > > > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > > index 63aaf44edd1f..477d196aa235 100644 > > --- a/arch/x86/kvm/vmx/vmx.c > > +++ b/arch/x86/kvm/vmx/vmx.c > > @@ -5162,6 +5162,12 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) > > gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); > > trace_kvm_page_fault(gpa, exit_qualification); > > > > + /* Check if guest gpa doesn't exceed physical memory limits */ > > + if (gpa >= (1ull << cpuid_maxphyaddr(vcpu))) { Add a helper for this, it's easier than copy-pasting the comment and code everywhere. BIT_ULL() is also handy. static inline bool kvm_mmu_is_illegal_gpa(gpa_t gpa) { return (gpa < BIT_ULL(cpuid_maxphyaddr(vcpu))); } > > + kvm_inject_rsvd_bits_pf(vcpu, gpa); > > Even if PFERR_RSVD_MASK is set in the page fault error code, shouldn't > we set still conditionally set: > PFERR_WRITE_MASK - for an attempted write > PFERR_USER_MASK - for a usermode access > PFERR_FETCH_MASK - for an instruction fetch Yep. Move this down below where error_code is calculated. Then the code should be something like this. Not fun to handle this with EPT :-( Note, VMCS.GUEST_LINEAR_ADDRESS isn't guaranteed to be accurate, e.g. if the guest is putting bad gpas into Intel PT, but I don't think we have any choice but to blindly cram it in and hope for the best. if (unlikely(kvm_mmu_is_illegal_gpa(vcpu, gpa))) { /* Morph the EPT error code into a #PF error code. */ error_code &= ~(PFERR_USER_MASK | PFERR_GUEST_FINAL_MASK | PFERR_GUEST_PAGE_MASK); if (vmx_get_cpl(vcpu) == 3) error_code |= PFERR_USER_MASK; error_code |= PFERR_PRESENT_MASK; kvm_inject_rsvd_bits_pf(vcpu, vmcs_readl(GUEST_LINEAR_ADDRESS), error_code); return 1; } > > + return 1; > > + } > > + > > /* Is it a read fault? */ > > error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) > > ? PFERR_USER_MASK : 0; > > @@ -5193,6 +5199,13 @@ static int handle_ept_misconfig(struct kvm_vcpu *vcpu) > > * nGPA here instead of the required GPA. > > */ > > gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); > > + > > + /* Check if guest gpa doesn't exceed physical memory limits */ > > + if (gpa >= (1ull << cpuid_maxphyaddr(vcpu))) { > > + kvm_inject_rsvd_bits_pf(vcpu, gpa); > > And here as well? This shouldn't happen. If KVM creates a bad EPTE for an illegal GPA, we done goofed up. I.e. if (WARN_ON_ONCE(kvm_mmu_is_illegal_gpa(vcpu, gpa))) { vcpu->run->blah = blah; return 0; } > > > + return 1; > > + } > > + > > if (!is_guest_mode(vcpu) && > > !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { > > trace_kvm_fast_mmio(gpa); > > -- > > 2.21.1 > >