KVM: nVMX: Hide shadow VMCS right after VMCLEAR
Subsystems (4)
Paolo Bonzini
maintainerPATCH 1/1 KVM: nVMX: Hide shadow VMCS right after VMCLEAR
From: Hyunwoo Kim <[email protected]>
free_nested() frees the shadow VMCS while vmcs01 still points to it. But because it is asynchronous with respect to loaded_vmcs_clear(), the vCPU might migrate before the pointer is cleared and __loaded_vmcs_clear() may then execute VMCLEAR.
The VMCS needs to stay attached until its explicit VMCLEAR completes, but then it can be hidden and the page safely freed.
--- arch/x86/kvm/vmx/nested.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +9 -2
@@ -336,6 +336,7 @@ static void nested_put_vmcs12_pages(struct kvm_vcpu *vcpu)static void free_nested (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +struct vmcs *shadow_vmcs;if (WARN_ON_ONCE (vmx->loaded_vmcs != &vmx->vmcs01))vmx_switch_vmcs (vcpu, &vmx->vmcs01);
@@ -353,9 +354,15 @@ static void free_nested(struct kvm_vcpu *vcpu) vmx->nested.current_vmptr =INVALID_GPA ;if (enable_shadow_vmcs) {vmx_disable_shadow_vmcs (vmx); -vmcs_clear (vmx->vmcs01.shadow_vmcs); -free_vmcs (vmx->vmcs01.shadow_vmcs); + +/* +* Keep the pointer visible until after VMCLEAR, so migration +* can clear an active shadow VMCS on the old CPU. +*/ + shadow_vmcs = vmx->vmcs01.shadow_vmcs; +vmcs_clear (shadow_vmcs); vmx->vmcs01.shadow_vmcs =NULL ; +free_vmcs (shadow_vmcs); }kfree (vmx->nested.cached_vmcs12); vmx->nested.cached_vmcs12 =NULL ;
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing compiler barrier allows pointer nullification to be reordered before VMCLEAR, recreating…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing compiler barrier allows pointer nullification to be reordered before VMCLEAR, recreating the UAF. --
--- Patch [1]: [PATCH] KVM: nVMX: Hide shadow VMCS right after VMCLEAR --- commit 002acbf68f96812492b255bf63d17f12b9b5b24a
Author: Hyunwoo Kim <[email protected]>
KVM: nVMX: Hide shadow VMCS right after VMCLEAR
This commit updates free_nested() to keep the shadow VMCS pointer visible until after VMCLEAR has completed. This change is intended to ensure that if the vCPU migrates, the migration process can still clear an active shadow VMCS on the old CPU.
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0
[ ... ]
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -350,9 +351,15 @@ static void free_nested(struct kvm_vcpu *vcpu) vmx->nested.current_vmptr =INVALID_GPA ;if (enable_shadow_vmcs) {vmx_disable_shadow_vmcs (vmx);
arch/x86/kvm/vmx/nested.c static void free_nested(struct kvm_vcpu *vcpu) · line 361
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +7
-2-vmcs_clear (vmx->vmcs01.shadow_vmcs); -free_vmcs (vmx->vmcs01.shadow_vmcs); + +/* +* Keep the pointer visible until after VMCLEAR, so migration +* can clear an active shadow VMCS on the old CPU. +*/ + shadow_vmcs = vmx->vmcs01.shadow_vmcs; +vmcs_clear (shadow_vmcs); vmx->vmcs01.shadow_vmcs =NULL ;
[Severity: High]
Does this sequence need a compiler barrier (such as barrier() or WRITE_ONCE()) to prevent the compiler from hoisting the NULL assignment above vmcs_clear()?
Because vmcs_clear() relies on an inline assembly block without a "memory" clobber and has no direct data dependency with vmx->vmcs01.shadow_vmcs, the compiler could legally reorder the store.
If a preemption occurs right between the hoisted assignment and the VMCLEAR, and the vCPU migrates, vcpu_load() on the new CPU would IPI the old CPU to clear the loaded VMCS. The old CPU would see the NULL pointer and skip flushing, potentially leaving an active pointer to freed memory in its hardware cache.
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +1
-0+free_vmcs (shadow_vmcs); }kfree (vmx->nested.cached_vmcs12); vmx->nested.cached_vmcs12 =NULL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
maintainerQuoted 28 lines · On Tue, Jul 21, 2026, [email protected] wrote:
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0[ ... ]
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -350,9 +351,15 @@ static void free_nested(struct kvm_vcpu *vcpu) vmx->nested.current_vmptr =INVALID_GPA ;if (enable_shadow_vmcs) {vmx_disable_shadow_vmcs (vmx);arch/x86/kvm/vmx/nested.c static void free_nested(struct kvm_vcpu *vcpu) · line 361
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +7
-2-vmcs_clear (vmx->vmcs01.shadow_vmcs); -free_vmcs (vmx->vmcs01.shadow_vmcs); + +/* +* Keep the pointer visible until after VMCLEAR, so migration +* can clear an active shadow VMCS on the old CPU. +*/ + shadow_vmcs = vmx->vmcs01.shadow_vmcs; +vmcs_clear (shadow_vmcs); vmx->vmcs01.shadow_vmcs =NULL ;[Severity: High]
Does this sequence need a compiler barrier (such asbarrier()orWRITE_ONCE()) to prevent the compiler from hoisting the NULL assignment abovevmcs_clear()?Because
vmcs_clear()relies on an inline assembly block without a "memory" clobber and has no direct data dependency withvmx->vmcs01.shadow_vmcs, the compiler could legally reorder the store.
Quoted 5 lines
If a preemption occurs right between the hoisted assignment and the VMCLEAR, and the vCPU migrates,
vcpu_load()on the new CPU would IPI the old CPU to clear the loaded VMCS. The old CPU would see the NULL pointer and skip flushing, potentially leaving an active pointer to freed memory in its hardware cache.
FWIW, I had a pretty strong preference for Paolo's off-list suggestion of explicitly disabling preemption, even before seeing Sashiko's response. Every other path the VMCLEARs the shadow VMCS runs with preemption disabled, IMO it's more intuitive to follow suit here. And even with the comment, it took a good 5-10 minutes of staring to understand what exactly is going wrong, though that's probably a me problem.
Quoted 7 lines
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +1
-0+free_vmcs (shadow_vmcs); }kfree (vmx->nested.cached_vmcs12); vmx->nested.cached_vmcs12 =NULL ;Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Paolo Bonzini
maintainerQuoted 6 lines · Sean Christopherson
FWIW, I had a pretty strong preference for Paolo's off-list suggestion of explicitly disabling preemption, even before seeing Sashiko's response. Every other path the VMCLEARs the shadow VMCS runs with preemption disabled, IMO it's more intuitive to follow suit here. And even with the comment, it took a good 5-10 minutes of staring to understand what exactly is going wrong, though that's probably a me problem.
Well, it would be at least an us problem---but I thought I was in the minority... The only (IMO bad) reason to leave preemption enabled is to obscure the change, but that's a lost battle anyway these days so why bother.
That said, I am surprised that VMCLEAR/VMREAD/VMWRITE/VMPTRLD do not have a memory clobber, since they access internal processor registers and the compiler may reorder them without the clobber. Fixing that would also work around Sashiko's complaint, so I guess we should do it anyway? And disabling preemption can be done separately on top of this change, which is already on its way to Linus.
Paolo
Sean Christopherson
maintainerQuoted 13 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
FWIW, I had a pretty strong preference for Paolo's off-list suggestion of explicitly disabling preemption, even before seeing Sashiko's response. Every other path the VMCLEARs the shadow VMCS runs with preemption disabled, IMO it's more intuitive to follow suit here. And even with the comment, it took a good 5-10 minutes of staring to understand what exactly is going wrong, though that's probably a me problem.
Well, it would be at least an us problem---but I thought I was in the minority... The only (IMO bad) reason to leave preemption enabled is to obscure the change, but that's a lost battle anyway these days so why bother.
Quoted 5 lines
That said, I am surprised that VMCLEAR/VMREAD/VMWRITE/VMPTRLD do not have a memory clobber, since they access internal processor registers and the compiler may reorder them without the clobber. Fixing that would also work around Sashiko's complaint, so I guess we should do it anyway?
Ya, good point. We should probably hardcode that into vmx_asm1() and vmx_asm2()? INVVPID and INVEPT also need the "memory" clobber, no?
Quoted 2 lines
And disabling preemption can be done separately on top of this change, which is already on its way to Linus.
+1
Paolo Bonzini
maintainerQuoted 7 lines · Sean Christopherson
Quoted 5 lines
That said, I am surprised that VMCLEAR/VMREAD/VMWRITE/VMPTRLD do not have a memory clobber, since they access internal processor registers and the compiler may reorder them without the clobber. Fixing that would also work around Sashiko's complaint, so I guess we should do it anyway?
Ya, good point. We should probably hardcode that into
vmx_asm1()andvmx_asm2()?
Definitely the easiest, even ignoring INVVPID/INVEPT...
Quoted 1 line
INVVPID and INVEPT also need the "memory" clobber, no?
... which strictly speaking do need the clobber, but in practice their effect is only visible to the next VMLAUNCH/VMRESUME so the risk of bad consequences from reordering is non-existent.
Patch going out soon...
Paolo