Skip to contentSkip to conversation

KVM: nVMX: SIPI delivered into a wait-for-SIPI L2 when INIT and SIPI are both pending (no SIPI VM-Exit to L1)

BUG Amirmohammad Eftekhar 1 participant 1 message Nov 25 KVM

lore
Share & tools
lore
#0 lore raw

When an L2 vCPU is in the wait-for-SIPI (WFS) activity state and both an INIT and a SIPI are pending, KVM can deliver the SIPI into L2 instead of generating the SIPI VM-Exit to L1. Per the SDM, a SIPI that arrives while a logical processor is in wait-for-SIPI causes a VM exit directly; for a nested guest that is exit reason 4 (SIPI_SIGNAL) to L1. INIT, in contrast, is blocked in WFS and must not cause an exit.

The three pieces are each correct in isolation and wrong together:

1. vmx_check_nested_events() handles INIT first and returns 0 without looking at SIPI. For an L2 in WFS it clears KVM_APIC_INIT (correctly with no VM-exit, since mp_state == KVM_MP_STATE_INIT_RECEIVED) and returns; the KVM_APIC_SIPI branch only runs on a later call.

2. kvm_apic_accept_events() calls kvm_check_nested_events() once and then processes INIT and SIPI itself. In guest mode it still delivers the SIPI via kvm_x86_call(vcpu_deliver_sipi_vector) while the vCPU is in guest mode: that writes CS.selector = vector << 8, CS.base = vector << 12 and RIP = 0 into vmcs02 and sets the vCPU RUNNABLE.

3. The common path hides this. vcpu_block() calls kvm_check_nested_events() right before kvm_apic_accept_events(), so the first call consumes the INIT and the call inside kvm_apic_accept_events() turns the remaining SIPI into the correct VM-exit. Callers that reach kvm_apic_accept_events() without that preceding re-check are exposed.

Confirmed trigger: KVM_GET_MP_STATE on the WFS vCPU while INIT and SIPI are latched. It calls kvm_apic_accept_events() directly, the INIT is cleared with no exit, and the SIPI is delivered into L2. There is also an in-kernel two-sender race in vcpu_block() when INIT and SIPI land between its own kvm_check_nested_events() and the one inside kvm_apic_accept_events().

Effect: L1 never receives the architecturally guaranteed SIPI VM-Exit. L2 is made runnable at vector<<8:0 in whatever mode L1 left it in (only CS selector/base and RIP are rewritten), so an L1 hypervisor that relies on SIPI exits to start its APs is bypassed and L2 starts the AP itself. With unrestricted guest off in vmcs12 the rewritten CS can also make L2 state invalid and lead to a nested triple-fault exit to L1.

Reproduced on an Intel host and, in a nested KASAN L1, on mainline (7.3.0-rc3) and on 6.1.74. Controls: a SIPI-only arm and a vCPU left blocked in KVM_RUN both take the correct SIPI_SIGNAL VM-exit. The INIT-then-SIPI arm shows deliver_sipi with hflags guest_mode=1 and no nested exit, and KVM_GET_NESTED_STATE afterwards shows GUEST_MODE=1 with vmcs12 rip=0 cs_sel=0 (it was rip=0x200040 cs_sel=0x8 act=3). No warning, BUG or KASAN in either kernel.

I have not attached a fix because the right place to close this is not obvious to me: either kvm_apic_accept_events() should not deliver an INIT or SIPI while the vCPU is in guest mode and should instead force the nested-events path, or vmx_check_nested_events() should prefer a pending SIPI over the INIT for a WFS L2. I am happy to send a tested patch and a selftest once the preferred direction is clear.

The guest-mode check in kvm_apic_accept_events() was added by 1c96dcceaeb3 to force a VM-exit before incorrectly injecting an INIT or SIPI into a guest; the early return after the WFS INIT branch in bf0cd88ce363 breaks that intent when INIT and SIPI are both pending.

Fixes bf0cd88ce363 ("KVM: x86: emulate wait-for-SIPI and SIPI-VMExit")