Skip to content

Commit 9bc610b

Browse files
committed
KVM: x86: Harden KVM against imbalanced load/put of guest FPU state
Assert, via KVM_BUG_ON(), that guest FPU state isn't/is in use when loading/putting the FPU to help detect KVM bugs without needing an assist from KASAN. If an imbalanced load/put is detected, skip the redundant load/put to avoid clobbering guest state and/or crashing the host. Note, kvm_access_xstate_msr() already provides a similar assertion. Reviewed-by: Yao Yuan <yaoyuan@linux.alibaba.com> Reviewed-by: Chao Gao <chao.gao@intel.com> Link: https://patch.msgid.link/20251030185802.3375059-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 8819a49 commit 9bc610b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

arch/x86/kvm/x86.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11807,6 +11807,9 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
1180711807
/* Swap (qemu) user FPU context for the guest FPU context. */
1180811808
static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
1180911809
{
11810+
if (KVM_BUG_ON(vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm))
11811+
return;
11812+
1181011813
/* Exclude PKRU, it's restored separately immediately after VM-Exit. */
1181111814
fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true);
1181211815
trace_kvm_fpu(1);
@@ -11815,6 +11818,9 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
1181511818
/* When vcpu_run ends, restore user space FPU context. */
1181611819
static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
1181711820
{
11821+
if (KVM_BUG_ON(!vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm))
11822+
return;
11823+
1181811824
fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false);
1181911825
++vcpu->stat.fpu_reload;
1182011826
trace_kvm_fpu(0);

0 commit comments

Comments
 (0)