Skip to contentSkip to conversation

KVM: x86/mmu: Check GPA bounds for 4-level TDP roots

PATCH New Wang Jiayue 2 participants 2 messages Dec 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: x86/mmu: Check GPA bounds for 4-level TDP roots

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")
---
 arch/x86/kvm/mmu/mmu.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +25 -1
index 2571dae03bdd..9fe2dde17951 100644
@@ -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

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KVM: x86/mmu: skip of private/protected checks in kvm_mmu_do_page_fault() - [Low] kvm: x86/mmu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: x86/mmu: skip of private/protected checks in kvm_mmu_do_page_fault()
- [Low] kvm: x86/mmu: commit message omits trigger context and link to Fixes commit
--

commit ee3d26fb92d3c5226d597ac7d5e6d4211015ddc4
Author: Wang Jiayue <[email protected]>

Quoted 7 lines

KVM: x86/mmu: Check GPA bounds for 4-level TDP roots

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.

Quoted 6 lines

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")

[Severity: Low]
Does the commit message explain how this relates to 10c30de01921 ("kvm: mmu:
Use fast PF path for access tracking of huge pages when possible"), or how an out-of-range GPA reaches a 4-level TDP root?

10c30de01921 only added kvm_slot_dirty_track_enabled(fault->slot) in fast_page_fault(), turning an aliased noslot write fault on a huge SPTE into a NULL pointer dereference, which isn't mentioned here.

The underlying 4-level TDP aliasing in kvm_mmu_do_page_fault() and handle_mmio_page_fault() also predates 10c30de01921, going back to d468d94b7baf ("KVM: x86: Dynamically calculate TDP level from max level and MAXPHYADDR") or fb72d1674d86 ("KVM: MMU: add TDP support to the KVM MMU").

Explaining how out-of-range faults reach a 4-level TDP root (e.g. a host with

Quoted 1 line

48-bit MAXPHYADDR using 4-level TDP, or KVM_PRE_FAULT_MEMORY) and clarifying

the Fixes tag would help review and backport triage.

Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +0 -0
index 2571dae03bdd3..9fe2dde179513 100644

[ ... ]

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +6 -0
@@ -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.
+		 */

arch/x86/kvm/mmu/mmu.c static int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 5108

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +9 -0
+		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;
+		}
 	}

[Severity: Medium]
Can returning RET_PF_EMULATE here livelock confidential guests and bypass kvm_is_private_gfn() checks?

For SEV-ES/SEV-SNP and TDX guests, x86_emulate_instruction() calls svm_check_emulate_instruction() or vt_check_emulate_instruction(), which return X86EMUL_RETRY_INSTR. With no MMIO SPTE installed, the vCPU re-enters at the same RIP in an infinite VM-exit loop instead of exiting to userspace.

This also skips the fault->is_private != kvm_is_private_gfn() check in kvm_mmu_faultin_pfn(). A shared fault on an unmapped out-of-range GFN marked private via KVM_SET_MEMORY_ATTRIBUTES (or a PFERR_RSVD_MASK fault in KVM_X86_SW_PROTECTED_VM) emulates MMIO instead of KVM_EXIT_MEMORY_FAULT.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1