All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wang Jiayue <akaieurus@gmail.com>
To: seanjc@google.com, pbonzini@redhat.com
Cc: kvm@vger.kernel.org
Subject: [PATCH] KVM: x86/mmu: Check GPA bounds for 4-level TDP roots
Date: Sat,  5 Dec 2026 11:41:25 +0800	[thread overview]
Message-ID: <20261205034125.682290-1-akaieurus@gmail.com> (raw)

Check the GPA before walking a 4-level TDP root, including when
looking up an MMIO SPTE. Memslot lookup uses the full GFN, while the
SPTE walk uses only the address bits covered by the root. An
out-of-range fault can therefore resolve to an unrelated SPTE.

Reject out-of-range prefaults with -EOPNOTSUPP. For guest faults,
emulate only shared accesses without a memslot. Return -EFAULT with
KVM_EXIT_MEMORY_FAULT for other out-of-range guest faults.

Fixes: 10c30de01921 ("kvm: mmu: Use fast PF path for access tracking of huge pages when possible")
Signed-off-by: Wang Jiayue <akaieurus@gmail.com>
---
 arch/x86/kvm/mmu/mmu.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 2571dae03bdd..9fe2dde17951 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5049,6 +5049,13 @@ static int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faul
 	return direct_page_fault(vcpu, fault);
 }
 
+static bool kvm_tdp_gpa_exceeds_root(struct kvm_mmu *mmu, gpa_t gpa)
+{
+	return (gpa >> SPTE_LEVEL_SHIFT(PT64_ROOT_4LEVEL + 1)) &&
+	       mmu->root_role.level == PT64_ROOT_4LEVEL &&
+	       mmu->page_fault == kvm_tdp_page_fault;
+}
+
 static int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 				 u64 err, bool prefetch, int *emulation_type,
 				 u8 *level)
@@ -5083,6 +5090,21 @@ static int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 		 */
 		fault.gfn = gpa_to_gfn(fault.addr) & ~kvm_gfn_direct_bits(vcpu->kvm);
 		fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
+
+		/*
+		 * Don't walk the root for an out-of-range GPA.  SPTE indexing
+		 * drops the upper bits, but memslot lookup doesn't, so the
+		 * walk would operate on SPTEs for a different GFN.
+		 */
+		if (unlikely(kvm_tdp_gpa_exceeds_root(vcpu->arch.mmu, fault.addr))) {
+			if (fault.prefetch)
+				return -EOPNOTSUPP;
+			if (!fault.slot && !fault.is_private)
+				return RET_PF_EMULATE;
+
+			kvm_mmu_prepare_memory_fault_exit(vcpu, &fault);
+			return -EFAULT;
+		}
 	}
 
 	/*
@@ -6689,7 +6711,9 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
 		if (KVM_BUG_ON(error_code & PFERR_PRIVATE_ACCESS, vcpu->kvm))
 			return -EIO;
 
-		r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct);
+		/* Don't let the MMIO lookup walk an aliased SPTE. */
+		if (!kvm_tdp_gpa_exceeds_root(vcpu->arch.mmu, cr2_or_gpa))
+			r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct);
 		if (r == RET_PF_EMULATE)
 			goto emulate;
 	}

base-commit: 4c9f462c7094c480144c98a942af7e935fa87a6a
-- 
2.53.0


             reply	other threads:[~2026-12-05  3:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  3:41 Wang Jiayue [this message]
2026-12-05  4:16 ` [PATCH] KVM: x86/mmu: Check GPA bounds for 4-level TDP roots sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205034125.682290-1-akaieurus@gmail.com \
    --to=akaieurus@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.