Skip to contentSkip to conversation

KVM: nVMX: Propagate GUEST_LINEAR_ADDRESS to vmcs12 only if it's defined

PATCH New Mahmoud Abumandour 1 participant 1 message Oct 30

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

PATCH 1/5 KVM: nVMX: Propagate GUEST_LINEAR_ADDRESS to vmcs12 only if it's defined

Copy GUEST_LINEAR_ADDRESS from vmcs02 to vmcs12 only when reflecting a hardware VM-Exit for which the SDM defines the field, i.e. an EPT Violation that sets bit 7 of the exit qualification, INS/OUTS, or LMSW with a memory operand, instead of on every nested VM-Exit.

For other VM-Exits the field is undefined, and in practice it holds a value that need not belong to L2. On Cascade Lake and on both core types of Alder Lake, CPUID, HLT, VMCALL, RDMSR, INVD, #UD, MTF and VMX-preemption timer VM-Exits store the linear address of the most recent EPT Violation on the logical CPU into the current VMCS, even if that EPT Violation was taken with a different VMCS. Because sync_vmcs02_to_vmcs12() copies the field unconditionally, an L1 that runs its L2 to a CPUID VM-Exit reads the guest-linear address of another VM's most recent EPT Violation on the same logical CPU, e.g. of that VM's first access to a page the host has not yet mapped. Sampled continuously, this reveals the KASLR bases of a Linux VM that shares the logical CPU.

EPT Violations that KVM synthesizes when an access it emulates for L2 faults in L1's EPT leak the same way: KVM reports GVA_IS_VALID in the exit qualification, but propagates whatever vmcs02 holds.

For VM-Exits that don't define the field, leave vmcs12's value as is, so that L1 reads the address of an earlier VM-Exit of the same vmcs12, or whatever L1 itself wrote. The SDM also leaves the field undefined for INS/OUTS whose segment is unusable, which this keeps copying; there Cascade Lake stores the operand's own linear address.

Fixes 119a9c01a592 ("KVM: nVMX: pass valid guest linear-address to the L1")
Assisted-by LLM
---
 arch/x86/kvm/vmx/nested.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +32 -2
index 9b0bfa2f85..30a71f9398 100644
@@ -418,6 +418,28 @@ static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp,
 		kvm_mmu_invalidate_addr(vcpu, &vcpu->arch.ngpa_walk, addr, roots);
 }
 
+/*
+ * GUEST_LINEAR_ADDRESS is defined only for the VM-Exits below.  On others, the
+ * CPU may store the linear address of an earlier VM-Exit on the same logical
+ * CPU, including one taken with a different VMCS, e.g. by a different VM.
+ */
+static bool nested_vmx_exit_has_gla(union vmx_exit_reason exit_reason,
+				    unsigned long exit_qual)
+{
+	switch (exit_reason.basic) {
+	case EXIT_REASON_EPT_VIOLATION:
+		return exit_qual & EPT_VIOLATION_GVA_IS_VALID;
+	case EXIT_REASON_IO_INSTRUCTION:
+		/* INS and OUTS. */
+		return exit_qual & 16;
+	case EXIT_REASON_CR_ACCESS:
+		/* LMSW with a memory operand. */
+		return ((exit_qual >> 4) & 3) == 3 && (exit_qual & BIT(6));
+	default:
+		return false;
+	}
+}
+
 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
 					 struct x86_exception *fault,
 					 bool from_hardware)
@@ -477,6 +499,12 @@ static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
 				exit_qualification |= fault->exit_qualification & mask;
 
 			vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
+
+			if (from_hardware &&
+			    nested_vmx_exit_has_gla(vmx->vt.exit_reason,
+						    vt_get_exit_qual(vcpu)))
+				vmcs12->guest_linear_address =
+					vmcs_readl(GUEST_LINEAR_ADDRESS);
 		}
 
 		/*
@@ -4705,8 +4733,6 @@ static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
 		}
 	}
 
-	vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
-
 	if (nested_cpu_has_vid(vmcs12))
 		vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
 
@@ -6757,6 +6783,10 @@ bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
 	}
 	exit_qual = vt_get_exit_qual(vcpu);
 
+	if (nested_vmx_exit_has_gla(exit_reason, exit_qual))
+		get_vmcs12(vcpu)->guest_linear_address =
+			vmcs_readl(GUEST_LINEAR_ADDRESS);
+
 reflect_vmexit:
 	nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
 	return true;
Signature · 1 line
2.25.1