From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Stefan Teodorescu <fane@google.com>,
Sashiko Bot <sashiko-bot@kernel.org>,
syzbot@syzkaller.appspotmail.com, Chris Mason <mason@kernel.org>
Subject: [PATCH v2 5/5] KVM: x86: Initialize MMU+PGD before loading PDPTRs on nested transitions
Date: Tue, 17 Nov 2026 13:09:34 -0800 [thread overview]
Message-ID: <20261117210934.7765-6-seanjc@google.com> (raw)
In-Reply-To: <20261117210934.7765-1-seanjc@google.com>
Initialize the MMU and PGD (root) before loading PDPTRs when emulating a
nested VM-Enter or VM-Exit to ensure the MMU is up-to-date even if loading
PDPTRs fails. If a nested VM-Exit fails to load L1's PDPTEs, and L1 ran L2
without EPT/NP, KVM can run L1 on a freed MMU root, i.e. the CPU's page
walk is a use-after-free, if userspace blows past the KVM_EXIT_SHUTDOWN.
root_mmu vmcx01 vmcx02
======== ====== ======
L1 runs R1 R1
VM-Enter to L2 R2 R1 R2
memslot deleted, L2 runs R3 R1 R3 R1, R2 freed
VM-Exit, bad PDPTEs R3 R1
KVM_EXIT_SHUTDOWN R3 R1
KVM_RUN enters L1 R3 R1
Unconditionally initializing the MMU if KVM sets CR3 is a-ok, as the nested
VM-Exit paths need exactly that behavior, emulation of VM-Enter generates a
VM-Exit on both SVM and VMX (i.e. KVM is guaranteed to load L1 state and
re-initialize the MMU), and RSM+KVM_SET_NESTED_STATE defer loading PDPTRs
until the next entry, i.e. don't load PDPTRs and thus can't fail.
Note, pre-checking the PDPTRs isn't an option because they come from guest
memory when TDP is disabled, i.e. the PDPTEs in memory can legally change
between the VM-Enter and VM-Exit.
Fixes: 1dc35dacc16b ("KVM: nVMX: check host CR3 on vmentry and vmexit")
Fixes: d82aaef9c88a ("KVM: nSVM: use nested_svm_load_cr3() on guest->host switch")
Reported-by: Chris Mason <mason@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/regs.c | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/arch/x86/kvm/regs.c b/arch/x86/kvm/regs.c
index b43ad11f061c..1cbc7b9f6ef4 100644
--- a/arch/x86/kvm/regs.c
+++ b/arch/x86/kvm/regs.c
@@ -204,23 +204,29 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(load_pdptrs);
int kvm_nested_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
bool nested_tdp, bool reload_pdptrs)
{
+ vcpu->arch.cr3 = cr3;
+ kvm_register_mark_dirty(vcpu, VCPU_REG_CR3);
+
+ /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
+ kvm_init_mmu(vcpu);
+
+ if (!nested_tdp)
+ kvm_mmu_new_pgd(vcpu, cr3);
+
/*
* If TDP is enabled, then the PDPTRs either come from the VMCS (EPT)
* or are read from memory on-demand (NPT), i.e. aren't loaded here.
+ * Load PDPTRs *after* initializing the MMU and PGD, to ensure the MMU
+ * is initialized even if the nested transition ultimately fails. On
+ * an emulated VM-Exit, failure will put the vCPU in a shutdown state,
+ * but KVM still needs to keep the MMU up-to-date. On an emulated
+ * VM-Entry, failure will generate a nested VM-Exit, i.e. L1's state
+ * and MMU will be explicitly loaded.
*/
if (reload_pdptrs && !nested_tdp && is_pae_paging(vcpu) &&
!load_pdptrs(vcpu, cr3))
return -EINVAL;
- vcpu->arch.cr3 = cr3;
- kvm_register_mark_dirty(vcpu, VCPU_REG_CR3);
-
- /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
- kvm_init_mmu(vcpu);
-
- if (!nested_tdp)
- kvm_mmu_new_pgd(vcpu, cr3);
-
return 0;
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_nested_load_cr3);
--
2.56.1.703.gf848ed807c-goog
prev parent reply other threads:[~2026-11-17 21:09 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 21:09 [PATCH v2 0/5] KVM: x86: Fix nested CR3 load bugs Sean Christopherson
2026-11-17 21:09 ` [PATCH v2 1/5] KVM: nSVM: Pre-check current (L2's) CR3 during KVM_SET_NESTED_STATE Sean Christopherson
2026-11-17 21:09 ` [PATCH v2 2/5] KVM: nVMX/nSVM: check unconditionally whether CR3 is valid Sean Christopherson
2026-11-17 21:09 ` [PATCH v2 3/5] KVM: nSVM: Mark CR3 as dirty when loading CR3 on nested transitions Sean Christopherson
2026-11-17 21:09 ` [PATCH v2 4/5] KVM: x86: Consolidate nVMX vs. nSVM logic for loading CR3 on nested xfer Sean Christopherson
2026-11-17 21:09 ` Sean Christopherson [this message]
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=20261117210934.7765-6-seanjc@google.com \
--to=seanjc@google.com \
--cc=fane@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
--cc=pbonzini@redhat.com \
--cc=sashiko-bot@kernel.org \
--cc=syzbot@syzkaller.appspotmail.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®