From: Konstantin Khorenko <khorenko@virtuozzo.com>
To: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Cc: OpenVZ devel list <devel@openvz.org>,
"Denis V. Lunev" <den@virtuozzo.com>
Subject: [Devel] [PATCH RK hci-8.0 3/3] ms/KVM: x86: Unconditionally reset MMU context when emulating INIT
Date: Wed, 25 Nov 2026 17:48:02 +0100 [thread overview]
Message-ID: <20261125164802.402895-3-khorenko@virtuozzo.com> (raw)
In-Reply-To: <20261125164802.402895-1-khorenko@virtuozzo.com>
From: Sean Christopherson <seanjc@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@google.com>
Reported-by: Oren Yomtov <thenameisoren@gmail.com>
Reported-by: Chris Mason <mason@kernel.org>
Cc: stable@vger.kernel.org
Cc: Yosry Ahmed <yosry@kernel.org>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-ID: <20261022015407.279870-2-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Sasha Levin <sashal@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@virtuozzo.com>
Signed-off-by: Fima Shevrin <efim.shevrin@virtuozzo.com>
Reviewed-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Signed-off-by: Konstantin Khorenko <khorenko@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
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
next prev parent reply other threads:[~2026-11-25 16:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 16:48 [Devel] [PATCH RK hci-8.0 1/3] ve: downgrade the trusted exec/mmap denial from WARN to pr_warn Konstantin Khorenko
2026-11-25 16:48 ` [Devel] [PATCH RK hci-8.0 2/3] ve: start the kthreads of a container in its ve namespace Konstantin Khorenko
2026-12-01 0:20 ` no-reply
2026-11-25 16:48 ` Konstantin Khorenko [this message]
2026-12-01 0:25 ` [Devel] [PATCH RK hci-8.0 3/3] ms/KVM: x86: Unconditionally reset MMU context when emulating INIT no-reply
2026-12-01 0:16 ` [Devel] [PATCH RK hci-8.0 1/3] ve: downgrade the trusted exec/mmap denial from WARN to pr_warn no-reply
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=20261125164802.402895-3-khorenko@virtuozzo.com \
--to=khorenko@virtuozzo.com \
--cc=den@virtuozzo.com \
--cc=devel@openvz.org \
--cc=ptikhomirov@virtuozzo.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