KVM: x86: Fix WFS vs. pending SMI WARN
Sean Christopherson
maintainercover KVM: x86: Fix WFS vs. pending SMI WARN
Fix a user-triggerable WARN that syzkaller found by stuffing INIT_RECEIVED, a.k.a. WFS, and then putting the vCPU into VMX Root Mode (post-VMXON). Use the same approach KVM uses for dealing with "impossible" emulation when running a !URG guest, and simply wait until KVM_RUN to detect that the vCPU has architecturally impossible state.
Sean Christopherson (4):
KVM: x86: Drop pending_smi vs. INIT_RECEIVED check when setting
MP_STATE
KVM: x86: WARN and reject KVM_RUN if vCPU's MP_STATE is SIPI_RECEIVED
KVM: x86: Move INIT_RECEIVED vs. INIT/SIPI blocked check to KVM_RUN
KVM: x86: Refactor handling of SIPI_RECEIVED when setting MP_STATE
arch/x86/kvm/x86.c | 49 ++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 19 deletions(-)
base-commit: 61374cc145f4a56377eaf87c7409a97ec7a34041
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Sean Christopherson
maintainerPATCH 1/4 KVM: x86: Drop pending_smi vs. INIT_RECEIVED check when setting MP_STATE
Allow userspace to set a vCPU's mp_state to INIT_RECEIVED in conjunction with a pending SMI, as rejecting that combination could result in KVM disallowing reflecting the output from KVM_GET_VCPU_EVENTS back into KVM via KVM_SET_VCPU_EVENTS.
At the time the check was added, smi_pending could only be set in the context of KVM_RUN, with the vCPU in the RUNNABLE state. I.e. it was impossible for KVM to save vCPU state such that userspace could see a pending SMI for a vCPU in WFS.
That no longer holds true now that KVM processes requested SMIs during KVM_GET_VCPU_EVENTS, e.g. if a vCPU receives an SMI while in WFS, and then userspace saves vCPU state.
Note, this may partially re-open the user-triggerable WARN that was mostly closed by commit 28bf28887976 ("KVM: x86: fix user triggerable warning in kvm_apic_accept_events()"), but that WARN can already be triggered in several other ways, e.g. if userspace stuffs VMXON=1 after putting the vCPU into WFS. That issue will be addressed in an upcoming commit, in a more robust fashion (hopefully).
--- arch/x86/kvm/x86.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +2 -3
@@ -11895,10 +11895,9 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,/* * Pending INITs are reported using KVM_SET_VCPU_EVENTS, disallow * forcing the guest into INIT/SIPI if those events are supposed to be -* blocked. KVM prioritizes SMI over INIT, so reject INIT/SIPI state -* if an SMI is pending as well. +* blocked. */ -if ((!kvm_apic_init_sipi_allowed (vcpu) || vcpu->arch.smi_pending) && +if (!kvm_apic_init_sipi_allowed (vcpu) && (mp_state->mp_state ==KVM_MP_STATE_SIPI_RECEIVED || mp_state->mp_state ==KVM_MP_STATE_INIT_RECEIVED )) goto out;
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Sean Christopherson
maintainerPATCH 2/4 KVM: x86: WARN and reject KVM_RUN if vCPU's MP_STATE is SIPI_RECEIVED
In reply to Sean ChristophersonWARN if KVM_RUN is reached with a vCPU's mp_state set to SIPI_RECEIVED, as KVM no longer uses SIPI_RECEIVED internally, and should morph SIPI_RECEIVED into INIT_RECEIVED with a pending SIPI if userspace forces SIPI_RECEIVED.
See commit 66450a21f996 ("KVM: x86: Rework INIT and SIPI handling") for more history and details.
--- arch/x86/kvm/x86.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +15 -1
@@ -11547,6 +11547,20 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)trace_kvm_fpu (0 ); } +static int kvm_x86_vcpu_pre_run (struct kvm_vcpu *vcpu) +{ +/* +* SIPI_RECEIVED is obsolete; KVM leaves the vCPU in Wait-For-SIPI and +* tracks the pending SIPI separately. SIPI_RECEIVED is still accepted +* by KVM_SET_VCPU_EVENTS for backwards compatibility, but should be +* converted to INIT_RECEIVED. +*/ +if (WARN_ON_ONCE (vcpu->arch.mp_state ==KVM_MP_STATE_SIPI_RECEIVED )) +return -EINVAL ; + +return kvm_x86_call (vcpu_pre_run)(vcpu); +} +int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu) {struct kvm_queued_exception *ex = &vcpu->arch.exception;
@@ -11649,7 +11663,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) goto out; } - r =kvm_x86_call (vcpu_pre_run)(vcpu); + r =kvm_x86_vcpu_pre_run (vcpu);if (r <=0 ) goto out;
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Sean Christopherson
maintainerPATCH 3/4 KVM: x86: Move INIT_RECEIVED vs. INIT/SIPI blocked check to KVM_RUN
In reply to Sean ChristophersonCheck for the should-be-impossible scenario of a vCPU being in Wait-For-SIPI with INIT/SIPI blocked during KVM_RUN instead of trying to detect and prevent illegal combinations in every ioctl that sets relevant state. Attempting to handle every possible "set" path is a losing game of whack-a-mole, and risks breaking userspace. E.g. INIT/SIPI are blocked on Intel if the vCPU is in VMX Root mode (post-VMXON), and on AMD if GIF=0. Handling those scenarios would require potentially breaking changes to {vmx,svm}_set_nested_state().
Moving the check to KVM_RUN fixes a syzkaller-induced splat due to the aforementioned VMXON case, and in theory should close the hole once and for all.
Note, kvm_x86_vcpu_pre_run() already handles SIPI_RECEIVED, only the WFS case needs additional attention.
--- arch/x86/kvm/x86.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +8 -16
@@ -5487,12 +5487,6 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))return -EINVAL ; -/* INITs are latched while in SMM */ -if (events->flags &KVM_VCPUEVENT_VALID_SMM && - (events->smi.smm || events->smi.pending) && - vcpu->arch.mp_state ==KVM_MP_STATE_INIT_RECEIVED ) -return -EINVAL ; -process_nmi (vcpu);/*
@@ -11558,6 +11552,14 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcpu)if (WARN_ON_ONCE (vcpu->arch.mp_state ==KVM_MP_STATE_SIPI_RECEIVED ))return -EINVAL ; +/* +* Disallow running the vCPU if userspace forced it into an impossible +* MP_STATE, e.g. if the vCPU is in WFS but SIPI is blocked. +*/ +if (vcpu->arch.mp_state ==KVM_MP_STATE_INIT_RECEIVED && + !kvm_apic_init_sipi_allowed (vcpu)) +return -EINVAL ; +return kvm_x86_call (vcpu_pre_run)(vcpu); }
@@ -11906,16 +11908,6 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, goto out; } -/* -* Pending INITs are reported using KVM_SET_VCPU_EVENTS, disallow -* forcing the guest into INIT/SIPI if those events are supposed to be -* blocked. -*/ -if (!kvm_apic_init_sipi_allowed (vcpu) && - (mp_state->mp_state ==KVM_MP_STATE_SIPI_RECEIVED || - mp_state->mp_state ==KVM_MP_STATE_INIT_RECEIVED )) - goto out; -if (mp_state->mp_state ==KVM_MP_STATE_SIPI_RECEIVED ) {kvm_set_mp_state (vcpu,KVM_MP_STATE_INIT_RECEIVED );set_bit (KVM_APIC_SIPI , &vcpu->arch.apic->pending_events);
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Sean Christopherson
maintainerPATCH 4/4 KVM: x86: Refactor handling of SIPI_RECEIVED when setting MP_STATE
In reply to Sean ChristophersonConvert the incoming mp_state to INIT_RECIEVED instead of manually calling kvm_set_mp_state() to make it more obvious that the SIPI_RECEIVED logic is translating the incoming state to KVM's internal tracking, as opposed to being some entirely unique flow.
Opportunistically add a comment to explain what the code is doing.
No functional change intended.
--- arch/x86/kvm/x86.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +9 -3
@@ -11908,11 +11908,17 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, goto out; } +/* +* SIPI_RECEIVED is obsolete and no longer used internally; KVM instead +* leaves the vCPU in INIT_RECIEVED (Wait-For-SIPI) and pends the SIPI. +* Translate SIPI_RECEIVED as appropriate for backwards compatibility. +*/ if (mp_state->mp_state ==KVM_MP_STATE_SIPI_RECEIVED ) { -kvm_set_mp_state (vcpu,KVM_MP_STATE_INIT_RECEIVED ); + mp_state->mp_state =KVM_MP_STATE_INIT_RECEIVED ;set_bit (KVM_APIC_SIPI , &vcpu->arch.apic->pending_events); - }else -kvm_set_mp_state (vcpu, mp_state->mp_state); + } + +kvm_set_mp_state (vcpu, mp_state->mp_state);kvm_make_request (KVM_REQ_EVENT , vcpu); ret =0 ;
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Quoted 6 lines · Sean Christopherson
Fix a user-triggerable WARN that syzkaller found by stuffing
INIT_RECEIVED, a.k.a. WFS, and then putting the vCPU into VMX Root Mode (post-VMXON). Use the same approach KVM uses for dealing with "impossible" emulation when running a !URG guest, and simply wait untilKVM_RUNto detect that the vCPU has architecturally impossible state.
Quoted 8 lines
Sean Christopherson (4):
KVM: x86: Droppending_smivs.INIT_RECEIVEDcheck when setting
MP_STATE
KVM: x86: WARN and rejectKVM_RUNif vCPU'sMP_STATEisSIPI_RECEIVED
KVM: x86: MoveINIT_RECEIVEDvs. INIT/SIPI blocked check toKVM_RUN
KVM: x86: Refactor handling ofSIPI_RECEIVEDwhen settingMP_STATE[...]
Applied to kvm-x86 misc, thanks!
[1/4] KVM: x86: Drop pending_smi vs. INIT_RECEIVED check when setting MP_STATE
https://github.com/kvm-x86/linux/commit/c4a37acc5193 [2/4] KVM: x86: WARN and reject KVM_RUN if vCPU's MP_STATE is SIPI_RECEIVED https://github.com/kvm-x86/linux/commit/16777ebded41 [3/4] KVM: x86: Move INIT_RECEIVED vs. INIT/SIPI blocked check to KVM_RUN https://github.com/kvm-x86/linux/commit/0fe3e8d804fd [4/4] KVM: x86: Refactor handling of SIPI_RECEIVED when setting MP_STATE https://github.com/kvm-x86/linux/commit/58c81bc1e71d