A VM created with private memory (KVM_X86_SW_PROTECTED_VM, SEV-SNP, TDX) reports a single memslot address space, because kvm_arch_nr_memslot_as_ids() returns 1 when kvm_arch_has_private_mem() is true. Nothing, however, prevents such a VM's vCPU from entering SMM, in which case kvm_arch_vcpu_memslots_id() still selects address space 1. The existing comment "SMM is currently unsupported for guests with private memory" is not enforced anywhere. Because address space 1 is initialised with generation 1 and is never swapped for a private-memory VM, while address space 0's generation advances by kvm_arch_nr_memslot_as_ids() (i.e. 1) per swap, after a single memslot operation both address spaces have generation 1. The per-vCPU last_used_slot cache in kvm_vcpu_gfn_to_memslot() validates only on the generation number, so a slot cached in address space 0 is reused for an in-SMM (address space 1) lookup. After that slot is deleted and freed, the stale per-vCPU cache is dereferenced: a host-kernel use-after-free of struct kvm_memory_slot reachable by an unprivileged process that can open /dev/kvm. The same aliasing also leaves SMM-root SPTEs that mmu_notifier invalidation never zaps (it walks only address space 0), and can trigger a BUG() in the TDP MMU. Enforce the documented assumption: do not support SMM on VMs with private memory. Stop advertising KVM_CAP_X86_SMM and emulating MSR_IA32_SMBASE for such VMs (which also makes kvm_inject_smi() and guest-driven SMIs fail), and reject enabling SMM via KVM_SET_VCPU_EVENTS. Fixes: 89ea60c2c7b5 ("KVM: x86: Add support for "protected VMs" that can utilize private memory") Signed-off-by: Younho Choi --- arch/x86/kvm/svm/svm.c | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 8 ++++++++ arch/x86/kvm/x86.c | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e30ee8062..0b8cd1502 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4841,6 +4841,14 @@ static bool svm_has_emulated_msr(struct kvm *kvm, u32 index) if (!IS_ENABLED(CONFIG_KVM_SMM)) return false; + /* + * SMM uses a second (SMRAM) address space, which a VM with + * private memory does not have; SMM is unsupported for such + * VMs (see kvm_arch_nr_memslot_as_ids()). + */ + if (kvm && kvm_arch_has_private_mem(kvm)) + return false; + #ifdef CONFIG_KVM_AMD_SEV /* * KVM can't access register state to emulate SMM for SEV-ES diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 82eb891f6..b6d5b33c1 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7098,6 +7098,14 @@ bool vmx_has_emulated_msr(struct kvm *kvm, u32 index) case MSR_IA32_SMBASE: if (!IS_ENABLED(CONFIG_KVM_SMM)) return false; + /* + * SMM uses a second (SMRAM) address space. A VM with private + * memory exposes only one address space (see + * kvm_arch_nr_memslot_as_ids()), so SMM is not supported for + * such VMs; advertising it leads to memslot aliasing. + */ + if (kvm && kvm_arch_has_private_mem(kvm)) + return false; /* * We cannot do SMM unless we can run the guest in big * real mode. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 26e7beaff..59565747d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3050,6 +3050,15 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, | KVM_VCPUEVENT_VALID_TRIPLE_FAULT)) return -EINVAL; + /* + * SMM is unsupported for VMs with private memory, which expose a single + * memslot address space; entering SMM would select address space 1 and + * alias it with address space 0 (see kvm_arch_nr_memslot_as_ids()). + */ + if ((events->flags & KVM_VCPUEVENT_VALID_SMM) && events->smi.smm && + kvm_arch_has_private_mem(vcpu->kvm)) + return -EINVAL; + if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { if (!vcpu->kvm->arch.exception_payload_enabled) return -EINVAL; -- 2.43.0