Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1357420ybf; Thu, 27 Feb 2020 09:25:19 -0800 (PST) X-Google-Smtp-Source: APXvYqysu+wvXopBpZhf41Q/O9xznM4uICFjooiFTYH/4wcQaiPrzXzzDEncuq2rKOPteGlTBvfJ X-Received: by 2002:a9d:12af:: with SMTP id g44mr665408otg.332.1582824318887; Thu, 27 Feb 2020 09:25:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582824318; cv=none; d=google.com; s=arc-20160816; b=B6i0GVv/Z4fqGgV+4QCiCiLTazy+hzsBDoUvxX9X8m8VIzjG7+NG5Zak2PEh7U52J3 aa/5lQ/ufgXAIxXS/EYhXkzlnN5pa3q3t7ufM+pJnU4TqyXCCIUbuJNkYzDc8VYUmMm7 O1EfHQ3hMDqsh1QyuqVCJctU8FesG6/C/+BLnN5Ro4HBM1+eUjUa9wz7+NLpneufrGo6 aPH2rV62fx/jTeHcOIkU6N+6Q0H3ZqoCN0upmroW8OIm3ObAydpT9cSUUzAG34uY8JbN DHtvvMHDm5PHOBvH0jQUy1MiomcyFom2O+Rs9RYgIsXc0I168NmavNRPHwAqlCGD0qT+ G26g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6XC36jUNMrRkhe8v5qZn1zVMtVX2JPkjS3b1Prtcpv4=; b=eWJPD2fSFJ9qoQnaYm/4rjHTmANJTEssGSKLgwH07MZ5F/za48dvAE1nEAo+NQlLUh 0RMz3TW7VH68aFLPzszqnFgBoIK7n2vQPDGT+GeiXJ52kvGHN0ylrhyUV8GERa+sIclF CGmM7mnaUrIFcwlxYphyUE2pzF/mNGcX7kGV0MDkAQbsY1ZYz59/nxzPvY9Kbv81YEUD EcSU/XoL/vZKIH+Hyet3TRV2Yv7sjRMAnfuuwQlwgCF+Hghm2X110LK+GNwRYoykI1rx ++//G/1tc3prufX3XDa/7t1V4f7bBdluoiIMue1JpeFlbpFl5pf6bZbG54iTVnm1hz0h fQmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VAtT8x1x; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u132si214067oie.250.2020.02.27.09.25.06; Thu, 27 Feb 2020 09:25:18 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VAtT8x1x; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730430AbgB0RX4 (ORCPT + 99 others); Thu, 27 Feb 2020 12:23:56 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:57782 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730384AbgB0RXz (ORCPT ); Thu, 27 Feb 2020 12:23:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582824234; 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: in-reply-to:in-reply-to:references:references; bh=6XC36jUNMrRkhe8v5qZn1zVMtVX2JPkjS3b1Prtcpv4=; b=VAtT8x1xtlsjQTp1WTDDwnFC/pqkHt4cUFRigyaJkyjO5whVYYP9VJkIkwriXHf9k32F+c NqnetouCVnf2dc6zN+rdSfK4FXFsm2sAcayVHrZmXat9Ulw+/Y9MOpWcbAi4Z11tJ5g7EF udbFGjsfWIR05/etBrivqr6ot/Vseu0= 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-233-ezsQoYhqMwCkTfO-IVeMCg-1; Thu, 27 Feb 2020 12:23:53 -0500 X-MC-Unique: ezsQoYhqMwCkTfO-IVeMCg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6812F107ACCA; Thu, 27 Feb 2020 17:23:50 +0000 (UTC) Received: from millenium-falcon.redhat.com (unknown [10.36.118.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id AEC9C1001B2C; Thu, 27 Feb 2020 17:23:44 +0000 (UTC) From: Mohammed Gamal To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: sean.j.christopherson@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, linux-kernel@vger.kernel.org, Mohammed Gamal Subject: [PATCH 5/5] KVM: x86: mmu: Add guest physical address check in translate_gpa() Date: Thu, 27 Feb 2020 19:23:06 +0200 Message-Id: <20200227172306.21426-6-mgamal@redhat.com> In-Reply-To: <20200227172306.21426-1-mgamal@redhat.com> References: <20200227172306.21426-1-mgamal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In case of running a guest with 4-level page tables on a 5-level page table host, it might happen that a guest might have a physical address with reserved bits set, but the host won't see that and trap it. Hence, we need to check page faults' physical addresses against the guest= 's maximum physical memory and if it's exceeded, we need to add the PFERR_RSVD_MASK bits to the PF's error code. Also make sure the error code isn't overwritten by the page table walker. Signed-off-by: Mohammed Gamal --- arch/x86/kvm/mmu/mmu.c | 4 ++++ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 099643edfdeb..994e8377b65f 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -523,6 +523,10 @@ static bool check_mmio_spte(struct kvm_vcpu *vcpu, u= 64 spte) static gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, struct x86_exception *exception) { + /* Check if guest physical address doesn't exceed guest maximum */ + if (gpa >=3D (1ull << cpuid_maxphyaddr(vcpu))) + exception->error_code |=3D PFERR_RSVD_MASK; + return gpa; } =20 diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmp= l.h index e4c8a4cbf407..aa3db722604b 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -476,7 +476,7 @@ static int FNAME(walk_addr_generic)(struct guest_walk= er *walker, =20 walker->fault.vector =3D PF_VECTOR; walker->fault.error_code_valid =3D true; - walker->fault.error_code =3D errcode; + walker->fault.error_code |=3D errcode; =20 #if PTTYPE =3D=3D PTTYPE_EPT /* --=20 2.21.1