KVM: x86: Reject SMM on VMs with private memory
PATCH 1/1 KVM: x86: Reject SMM on VMs with private memory
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.
--- 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(+)
arch/x86/kvm/svm/svm.c +8 -0
@@ -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
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -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.
arch/x86/kvm/x86.c +9 -0
@@ -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 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Critical] KVM: x86: pending SMI in kvm_vcpu_ioctl_x86_set_vcpu_events() enters SMM --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Critical] KVM: x86: pending SMI in kvm_vcpu_ioctl_x86_set_vcpu_events() enters SMM
--
commit c37a22adf8ef4d4936cc15c833970255bb255e89
Author: Younho Choi <[email protected]>
KVM: x86: Reject SMM on VMs with private memory
This commit prevents VMs with private memory from advertising KVM_CAP_X86_SMM, emulating MSR_IA32_SMBASE, or enabling SMM via KVM_SET_VCPU_EVENTS.
Quoted 10 lines
arch/x86/kvm/x86.c +2
-0@@ -3039,6 +3039,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
arch/x86/kvm/x86.c static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, · line 3053
Quoted 10 lines
arch/x86/kvm/x86.c +7
-0+* 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 ;
[Severity: Critical]
Should this check also reject events->smi.pending (and events->smi.smm_inside_nmi), matching the !CONFIG_KVM_SMM check further down in kvm_vcpu_ioctl_x86_set_vcpu_events()?
Passing events.smi.smm = 0 and events.smi.pending = 1 on a VM with private memory still sets vcpu->arch.smi_pending. On the next KVM_RUN, kvm_check_and_inject_events() calls enter_smm() (or hits KVM_BUG_ON() in vt_smi_allowed() on TDX VMs), entering address space 1 and triggering the vcpu->last_used_slot generation-aliasing use-after-free in kvm_vcpu_gfn_to_memslot().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 KVM: x86: Reject SMM on VMs with private memory
In reply to Younho ChoiA VM created with private memory (KVM_X86_SW_PROTECTED_VM, SEV-SNP, TDX) reports one memslot address space, as kvm_arch_nr_memslot_as_ids() returns 1 when kvm_arch_has_private_mem() is true. SMM still selects address space 1 via kvm_arch_vcpu_memslots_id(), despite the existing comment that SMM is unsupported for guests with private memory.
Address space 1 starts at generation 1 and is never swapped for a private-memory VM. Address space 0 advances by kvm_arch_nr_memslot_as_ids() (i.e. 1) per swap, so after a single memslot operation both address spaces have generation 1. The per-vCPU last_used_slot cache in kvm_vcpu_gfn_to_memslot() checks only the generation number, so an address-space-0 slot can be reused for an in-SMM address-space-1 lookup. After the slot is deleted and freed, the stale cache can cause a host-kernel use-after-free of struct kvm_memory_slot. This was reproduced on KVM_X86_SW_PROTECTED_VM by a process with access to /dev/kvm. The same aliasing can leave SMM-root SPTEs that mmu_notifier invalidation does not zap, because it walks only address space 0.
Enforce the documented assumption that SMM is unsupported for VMs with private memory. Make the VMX and SVM has_emulated_msr() hooks report MSR_IA32_SMBASE unsupported for those VMs; TDX already reports it unsupported. This disables VM-scoped KVM_CAP_X86_SMM reporting and makes kvm_inject_smi() reject KVM_SMI and APIC-delivered SMIs. Reject in-SMM state, pending SMIs, and smm_inside_nmi via KVM_SET_VCPU_EVENTS, matching the !CONFIG_KVM_SMM validation. Host-initiated SMBASE MSR access is unchanged.
ChatGPT Codex assisted with analysis, patch drafting, and test design.
can utilize private memory")
---
Changes in v2:
- Reject pending SMIs and smm_inside_nmi in KVM_SET_VCPU_EVENTS, as pointed out by Sashiko.
- Correct the description of VM-scoped SMM capability and SMBASE support.
Testing:
- On a KVM_X86_SW_PROTECTED_VM, v1 accepted pending=1 and entered SMM on KVM_RUN. Deleting its memslot then triggered a KASAN slab UAF. v2 rejects the ioctl with -EINVAL and stays outside SMM after KVM_RUN.
- Nine ioctl regression cases pass on v2, including KVM_SMI rejection, VM-scoped capability reporting, and normal-VM ioctl behavior.
- A private-VM KVM ioctl lifecycle test passes clean event-state save/restore, latched INIT, and host SMBASE MSR roundtrips. An invalid pending-SMI restore returns -EINVAL without changing NMI state, and the restored vCPU runs guest code. Full VMM migration was not tested.
- Separate CONFIG_KVM_AMD_SEV=y and CONFIG_KVM_INTEL_TDX=y builds compile the affected SVM, SEV, VMX, TDX, and common x86 objects.
- Source audit confirms tdx_vm_init() sets has_private_mem before vCPU creation, so the common ioctl guard also applies to TDX. SVM/SNP and TDX hardware were not available for runtime tests.
- A standalone reproducer is available to maintainers on request.
arch/x86/kvm/svm/svm.c | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 8 ++++++++ arch/x86/kvm/x86.c | 11 +++++++++++ 3 files changed, 27 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
@@ -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
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -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.
arch/x86/kvm/x86.c +0 -0
@@ -3050,6 +3050,17 @@ 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) &&
+ kvm_arch_has_private_mem(vcpu->kvm) &&
+ (events->smi.smm || events->smi.pending ||
+ events->smi.smm_inside_nmi))
+ return -EINVAL;
+
if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
if (!vcpu->kvm->arch.exception_payload_enabled)
return -EINVAL;
Signature · 1 line
2.43.0
PATCH v3 1/1 KVM: x86: Reject SMM on VMs with private memory
In reply to Younho ChoiFrom: Younho Choi <[email protected]>
A VM created with private memory (KVM_X86_SW_PROTECTED_VM, SEV-SNP, TDX) reports one memslot address space, as kvm_arch_nr_memslot_as_ids() returns 1 when kvm_arch_has_private_mem() is true. SMM still selects address space 1 via kvm_arch_vcpu_memslots_id(), despite the existing comment that SMM is unsupported for guests with private memory.
Address space 1 starts at generation 1 and is never swapped for a private-memory VM. Address space 0 advances by kvm_arch_nr_memslot_as_ids() (i.e. 1) per swap, so after a single memslot operation both address spaces have generation 1. The per-vCPU last_used_slot cache in kvm_vcpu_gfn_to_memslot() checks only the generation number, so an address-space-0 slot can be reused for an in-SMM address-space-1 lookup. After the slot is deleted and freed, the stale cache can cause a host-kernel use-after-free of struct kvm_memory_slot. This was reproduced on KVM_X86_SW_PROTECTED_VM by a process with access to /dev/kvm. The same aliasing can leave SMM-root SPTEs that mmu_notifier invalidation does not zap, because it walks only address space 0.
Enforce the documented assumption that SMM is unsupported for VMs with private memory. Make the VMX and SVM has_emulated_msr() hooks report MSR_IA32_SMBASE unsupported for those VMs; TDX already reports it unsupported. This disables VM-scoped KVM_CAP_X86_SMM reporting and makes kvm_inject_smi() reject KVM_SMI and APIC-delivered SMIs. Reject in-SMM state, pending SMIs, and smm_inside_nmi via KVM_SET_VCPU_EVENTS, matching the !CONFIG_KVM_SMM validation. Host-initiated SMBASE MSR access is unchanged.
ChatGPT Codex assisted with analysis, patch drafting, and test design.
can utilize private memory")
---
Changes in v3:
- Resend with an in-body From header to preserve author attribution; no code changes from v2.
Changes in v2:
- Reject pending SMIs and smm_inside_nmi in KVM_SET_VCPU_EVENTS, as pointed out by Sashiko.
- Correct the description of VM-scoped SMM capability and SMBASE support.
Testing:
- On a KVM_X86_SW_PROTECTED_VM, v1 accepted pending=1 and entered SMM on KVM_RUN. Deleting its memslot then triggered a KASAN slab UAF. v2 rejects the ioctl with -EINVAL and stays outside SMM after KVM_RUN.
- Nine ioctl regression cases pass on v2, including KVM_SMI rejection, VM-scoped capability reporting, and normal-VM ioctl behavior.
- A private-VM KVM ioctl lifecycle test passes clean event-state save/restore, latched INIT, and host SMBASE MSR roundtrips. An invalid pending-SMI restore returns -EINVAL without changing NMI state, and the restored vCPU runs guest code. Full VMM migration was not tested.
- Separate CONFIG_KVM_AMD_SEV=y and CONFIG_KVM_INTEL_TDX=y builds compile the affected SVM, SEV, VMX, TDX, and common x86 objects.
- Source audit confirms tdx_vm_init() sets has_private_mem before vCPU creation, so the common ioctl guard also applies to TDX. SVM/SNP and TDX hardware were not available for runtime tests.
- A standalone reproducer is available to maintainers on request.
arch/x86/kvm/svm/svm.c | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 8 ++++++++ arch/x86/kvm/x86.c | 11 +++++++++++ 3 files changed, 27 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
@@ -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
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -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.
arch/x86/kvm/x86.c +0 -0
@@ -3050,6 +3050,17 @@ 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) &&
+ kvm_arch_has_private_mem(vcpu->kvm) &&
+ (events->smi.smm || events->smi.pending ||
+ events->smi.smm_inside_nmi))
+ return -EINVAL;
+
if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
if (!vcpu->kvm->arch.exception_payload_enabled)
return -EINVAL;
Signature · 1 line
2.43.0