Skip to content

Commit 3dde46a

Browse files
committed
KVM: nVMX: Assert that vcpu->mutex is held when accessing secondary VMCSes
Add lockdep assertions in get_vmcs12() and get_shadow_vmcs12() to verify the vCPU's mutex is held, as the returned VMCS objects are dynamically allocated/freed when nested VMX is turned on/off, i.e. accessing vmcs12 structures without holding vcpu->mutex is susceptible to use-after-free. Waive the assertion if the VM is being destroyed, as KVM currently forces a nested VM-Exit when freeing the vCPU. If/when that wart is fixed, the assertion can/should be converted to an unqualified lockdep assertion. See also https://lore.kernel.org/all/Zsd0TqCeY3B5Sb5b@google.com. Link: https://lore.kernel.org/r/20240906043413.1049633-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 1ed0f11 commit 3dde46a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

arch/x86/kvm/vmx/nested.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
3939

4040
static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
4141
{
42+
lockdep_assert_once(lockdep_is_held(&vcpu->mutex) ||
43+
!refcount_read(&vcpu->kvm->users_count));
44+
4245
return to_vmx(vcpu)->nested.cached_vmcs12;
4346
}
4447

4548
static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
4649
{
50+
lockdep_assert_once(lockdep_is_held(&vcpu->mutex) ||
51+
!refcount_read(&vcpu->kvm->users_count));
52+
4753
return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
4854
}
4955

0 commit comments

Comments
 (0)