• Maxim Levitsky's avatar
    KVM: VMX: reset the segment cache after segment init in vmx_vcpu_reset() · 731285fb
    Maxim Levitsky authored
    Reset the segment cache after segment initialization in vmx_vcpu_reset()
    to harden KVM against caching stale/uninitialized data.  Without the
    recent fix to bypass the cache in kvm_arch_vcpu_put(), the following
    scenario is possible:
    
     - vCPU is just created, and the vCPU thread is preempted before
       SS.AR_BYTES is written in vmx_vcpu_reset().
    
     - When scheduling out the vCPU task, kvm_arch_vcpu_in_kernel() =>
       vmx_get_cpl() reads and caches '0' for SS.AR_BYTES.
    
     - vmx_vcpu_reset() => seg_setup() configures SS.AR_BYTES, but doesn't
       invoke vmx_segment_cache_clear() to invalidate the cache.
    
    As a result, KVM retains a stale value in the cache, which can be read,
    e.g. via KVM_GET_SREGS.  Usually this is not a problem because the VMX
    segment cache is reset on each VM-Exit, but if the userspace VMM (e.g KVM
    selftests) reads and writes system registers just after the vCPU was
    created, _without_ modifying SS.AR_BYTES, userspace will write back the
    stale '0' value and ultimately will trigger a VM-Entry failure due to
    incorrect SS segment type.
    
    Invalidating the cache after writing the VMCS doesn't address the general
    issue of cache accesses from IRQ context being unsafe, but it does prevent
    KVM from clobbering the VMCS, i.e. mitigates the harm done _if_ KVM has a
    bug that results in an unsafe cache access.
    Signed-off-by: default avatarMaxim Levitsky <mlevitsk@redhat.com>
    Fixes: 2fb92db1 ("KVM: VMX: Cache vmcs segment fields")
    [sean: rework changelog to account for previous patch]
    Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
    Message-ID: <20241009175002.1118178-3-seanjc@google.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    731285fb
vmx.c 247 KB