Re: [PATCH v2 1/5] KVM: nSVM: Pre-check current (L2's) CR3 during KVM_SET_NESTED_STATE
From: Yosry Ahmed
Date: Wed Nov 25 2026 - 16:26:54 EST
On Tue, Nov 17, 2026 at 1:09 PM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxxxxxxx
> Reported-by: Stefan Teodorescu <fane@xxxxxxxxxx>
> Reported-by: Sashiko Bot <sashiko-bot@xxxxxxxxxx>
> Closes: https://lore.kernel.org/all/sashiko-outbox-210535@xxxxxxxxxx
> Closes: https://lore.kernel.org/all/sashiko-outbox-205939@xxxxxxxxxx
> Closes: https://lore.kernel.org/all/sashiko-outbox-193903@xxxxxxxxxx
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Reviewed-by: Yosry Ahmed <yosry@xxxxxxxxxx>