[Devel] [PATCH RK hci-8.0 3/3] ms/KVM: x86: Unconditionally reset MMU context when emulating INIT

Konstantin Khorenko khorenko at virtuozzo.com
Wed Nov 25 19:48:02 MSK 2026


From: Sean Christopherson <seanjc at google.com>

[ Upstream commit c7c288d178cb0b78d3464a616b16b1aa0b316049 ]

Force an MMU reset when emulating an INIT even if paging was already
disabled, as the MMU page role includes guest_mode (L2), i.e. an INIT while
L2 is running with CR0.PG=0 will result in KVM running L1 with a stale MMU
context.

Alternatively, the reset could be scoped strictly to paging being enabled
or L2 being active, but SMM is also encoded in the page role.  Even though
it _should_ be impossible for the vCPU to be INIT'd while SMM is active,
there's zero value in not handling SMM.  At that point, the only scenario
where KVM _doesn't_ need to reset the MMU context is on an INIT with the
vCPU running in Real Mode or Unpaged Protect Mode, and while INIT in those
modes is somewhat common, it's far from a hot path and re-initializing the
MMU is not expensive, especially when the role isn't changing (KVM elides
most of the re-configuration in that case).

Note, like the Fixes commit, this bug only affects nested SVM as INIT is an
unconditional intercept on VMX, i.e. it's impossible to emulate INIT while
L2 is active.

Temporarily keep the guest TLB flush when CR0.PG is being cleared even
though it is fully redundant with the flush that is emitted on all INIT
events (KVM itself only does RESET when a vCPU is being created, i.e. when
CR0.PG must be '0').

Fixes: ed129ec9057f ("KVM: x86: forcibly leave nested mode on vCPU reset")
Reported-by: Stefan Teodorescu <fane at google.com>
Reported-by: Oren Yomtov <thenameisoren at gmail.com>
Reported-by: Chris Mason <mason at kernel.org>
Cc: stable at vger.kernel.org
Cc: Yosry Ahmed <yosry at kernel.org>
Signed-off-by: Sean Christopherson <seanjc at google.com>
Message-ID: <20261022015407.279870-2-seanjc at google.com>
Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
Signed-off-by: Sasha Levin <sashal at kernel.org>
(based on commit f55a3e4e08dc004b4bab7c1e9323d7e7e96e266a)

https://virtuozzo.atlassian.net/browse/VSTOR-156442
Feature: fix ms/kvm

ReadyKernel-specific changes:
The fix is moved out of kvm_vcpu_reset() into svm_leave_nested():
- kvm_vcpu_reset() can't be livepatched: klp-build rejects its
  kvm_x86_call() static calls, their keys live in kvm.ko.
- kvm-amd.ko can't call kvm_mmu_reset_context() from a livepatch: it
  does not reference that symbol today ("unexpected klp reloc for new
  symbol").
- svm_leave_nested() is where INIT drops guest_mode, through
  kvm_vcpu_reset() -> kvm_leave_nested(). Re-initialize the MMU there
  as a nested #VMEXIT does in nested_svm_load_cr3(): kvm_init_mmu()
  computes the role without guest_mode, kvm_mmu_new_pgd() moves L1 off
  the root built for L2. The root vmcb01 points to is then rewritten
  (KVM_REQ_LOAD_MMU_PGD or a reload) before the next VM-Enter, instead
  of the root L1 had before VMRUN, which may be freed by then.
- Request MMU sync and TLB flush as nested_svm_transition_tlb_flush()
  does: kvm_mmu_new_pgd() may reuse a cached root and leaves both to
  its caller.
- INIT with CR0.PG=1 is reset by kvm_vcpu_reset() anyway. With CR0.PG=0
  (the bug) the role depends on guest_mode and smm only, so the one set
  here is final. INIT in SMM, also covered upstream, stays as is: it
  should be impossible.
- The other callers of svm_leave_nested() (set nested state, RSM
  failure, KVM_SET_VCPU_EVENTS SMM change, shutdown intercept in L2,
  vCPU free) re-initialize the MMU too when the vCPU leaves L2. An
  unintercepted EFER.SVME clear by L2 gets the fix this way as well;
  upstream c7c288d does not cover that path.
- Not covered: the shutdown intercept outside L2 with CR0.PG=0 clears
  vmcb01 and kvm_vcpu_reset() skips the MMU reset; upstream fixes that
  as a side effect. It does not involve nested state.
- Blank lines of svm_leave_nested() are dropped to keep the line
  numbers of nested.c intact.

The svm_leave_nested() variant is Fima Shevrin's.

RK_BUG: VSTOR-156442
RK_CVE: CVE-2026-99312
RK_CHANGELOG: Fix a guest-to-host use-after-free in nested KVM on AMD.

Co-developed-by: Fima Shevrin <efim.shevrin at virtuozzo.com>
Signed-off-by: Fima Shevrin <efim.shevrin at virtuozzo.com>
Reviewed-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 arch/x86/kvm/svm/nested.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index b7fd2e8699987..7095ed7ce2e01 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -1339,14 +1339,14 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 	if (is_guest_mode(vcpu)) {
 		svm->nested.nested_run_pending = 0;
 		svm->nested.vmcb12_gpa = INVALID_GPA;
-
 		leave_guest_mode(vcpu);
-
 		svm_switch_vmcb(svm, &svm->vmcb01);
-
 		nested_svm_uninit_mmu_context(vcpu);
 		vmcb_mark_all_dirty(svm->vmcb);
-
+		kvm_init_mmu(vcpu);
+		kvm_mmu_new_pgd(vcpu, vcpu->arch.cr3);
+		kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
+		kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 		if (kvm_apicv_activated(vcpu->kvm))
 			kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
 	}
-- 
2.43.0



More information about the Devel mailing list