mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 1/5] KVM: nSVM: Pre-check current (L2's) CR3 during KVM_SET_NESTED_STATE
Date: Tue, 17 Nov 2026 13:09:30 -0800	[thread overview]
Message-ID: <20261117210934.7765-2-seanjc@google.com> (raw)
In-Reply-To: <20261117210934.7765-1-seanjc@google.com>

When loading nested state, explicitly check that the current CR3 is valid
before loading any state, as a misbehaving userspace can muck with guest
CPUID to make KVM think the current CR3 is invalid, and thus cause the late
"load" of CR3 to fail when initializing the guest's MMU.  If that happens,
KVM will leave half-baked state in the vCPU, e.g. in L2 with L1's page
walker, and if nested NPT is disabled, L1's PGD as well.

Alternatively, KVM could try and unwind vCPU state if the late CR3 "load"
fails, but that is subtly quite difficult as it would require snapshotting
and restoring the previous state.  Simply leaving nested on failure isn't
an option either, because that too would leave the vCPU with half-baked
state (in L1 with L2 state and a very stale MMU).

Pre-checking CR3 will also allow hoisting the CR3 check out of
nested_svm_load_cr3() entirely.

WARN and bug the VM if the late CR3 load fails, as loading nested state
never loads PDPTRs, i.e. failure is impossible, and KVM doesn't unwind on
failure (see above).  I.e. if the impossible happens, KVM needs to block
all operations on the half-baked vCPU.

Fixes: e1779c2714c3 ("KVM: x86: nSVM: fix potential NULL derefernce on nested migration")
Cc: stable@vger.kernel.org
Reported-by: Stefan Teodorescu <fane@google.com>
Reported-by: Sashiko Bot <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-210535@kernel.org
Closes: https://lore.kernel.org/all/sashiko-outbox-205939@kernel.org
Closes: https://lore.kernel.org/all/sashiko-outbox-193903@kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/nested.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 07554089ec05..1e3842851dae 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -2047,6 +2047,9 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
 		goto out_free;
 
+	if (!kvm_vcpu_is_legal_cr3(vcpu, vcpu->arch.cr3))
+		goto out_free;
+
 	/*
 	 * Validate host state saved from before VMRUN (see
 	 * nested_svm_check_permissions). Note that the g_pat field is not
@@ -2114,7 +2117,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 	 */
 	ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
 				  nested_npt_enabled(svm), false);
-	if (ret)
+	if (KVM_BUG_ON(ret, vcpu->kvm))
 		goto out_free;
 
 	svm->nested.force_msr_bitmap_recalc = true;
-- 
2.56.1.703.gf848ed807c-goog


  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 ` Sean Christopherson [this message]
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 ` [PATCH v2 5/5] KVM: x86: Initialize MMU+PGD before loading PDPTRs on nested transitions Sean Christopherson

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-2-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®