From: gdool88@gmail.com
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Younho Choi <gdool88@gmail.com>
Subject: [PATCH v2] KVM: x86: Reject SMM on VMs with private memory
Date: Tue, 24 Nov 2026 06:42:01 -0600 [thread overview]
Message-ID: <nh-cfa27f45-ff01-4946-b3cd-9fb1bb6dee4e@google.com> (raw)
In-Reply-To: <20261124032748.1221979-1-gdool88@gmail.com>
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.
Fixes: 89ea60c2c7b5 ("KVM: x86: Add support for "protected VMs" that
can utilize private memory")
Cc: stable@vger.kernel.org
Signed-off-by: Younho Choi <gdool88@gmail.com>
---
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(+)
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..4faafc407 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -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;
--
2.43.0
next prev parent reply other threads:[~2026-11-24 12:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 3:27 [PATCH] " Younho Choi
2026-11-24 12:42 ` gdool88 [this message]
2026-11-24 12:44 ` [PATCH v3] " gdool88
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=nh-cfa27f45-ff01-4946-b3cd-9fb1bb6dee4e@google.com \
--to=gdool88@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®