Skip to content

openvmm: a restored non-Fixed APIC timer makes KVM inject INIT, SIPI or NMI #1365

Closed

Description

@tangruize

On KVM, restoring a snapshot whose APIC page has the LVT timer unmasked in a delivery mode other than Fixed, and the timer already expired (one-shot with a current count of 0, or TSC-deadline past its deadline), makes KVM fire the re-armed timer in that mode. Depending on the mode, the restored VP then resets (INIT), latches a SIPI, wakes from halt (RemoteRead), or takes an NMI or SMI, none of which the snapshot holds. On an APICv/AVIC host this happens inside KVM_SET_LAPIC, and on any other host at the first KVM_RUN. A guest cannot set the timer's delivery mode, because KVM masks LVTT writes, so only a crafted snapshot file reaches this.

Reproduction

restore_all a KVM VP saved state whose lvt_timer is unmasked, one-shot, with delivery mode INIT (5), a nonzero initial count and a current count of 0. Expected: the restore is refused. Actual: it succeeds, with nothing visible at the restore, because KVM's APIC is not read back (can_compare is false). The INIT that KVM then latches (before restore_all returns on an APICv host) was not reproduced.

Cause

KvmVpStateAccess::set_apic (vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs) passes the saved page to KVM_SET_LAPIC unchanged:

// vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
fn set_apic(&mut self, value: &vp::Apic) -> Result<(), Self::Error> {
    // Set this first to set the APIC mode before updating the APIC register
    // state.
    self.kvm()
        .set_msrs(&[(x86defs::X86X_MSR_APIC_BASE, value.apic_base)])?;

    self.kvm().set_lapic(&value.registers().as_page())?;
    Ok(())
}

KVM re-arms the timer from the current count and delivers its expiry through kvm_apic_local_deliver with the LVT's delivery mode (arch/x86/kvm/lapic.c).

Fix

fix.patch adds check_restorable_apic_timer. set_apic calls it first, before it writes APIC_BASE or the page, and it returns KvmError::InvalidState when the LVT timer is unmasked and its mode is not Fixed. LowestPriority and ExtINT are refused too, since no save holds them. The reset APIC is unaffected, because its timer is masked. Such a restore then fails with restore_all's StateError for the APIC; every page OpenVMM saves is unchanged. The unit test restore_of_an_apic_timer_in_a_mode_other_than_fixed_is_refused covers modes 1 to 7 masked and unmasked, and the Fixed and reset values. The patch has no KVM-backed test.

A related limit, which this patch does not change: KVM also fires an expired one-shot Fixed timer once more after a restore.

Activity

  1. tangruize commented on Nov 8, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 3cf58a5234ed824435825c9f87187f5129219724. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
    index 59dd45b..841bb68 100644
    --- a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
    +++ b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
    @@ -457,6 +457,8 @@ fn apic(&mut self) -> Result<vp::Apic, Self::Error> {
         }
     
         fn set_apic(&mut self, value: &vp::Apic) -> Result<(), Self::Error> {
    +        check_restorable_apic_timer(value.registers().lvt_timer)?;
    +
             // Set this first to set the APIC mode before updating the APIC register
             // state.
             self.kvm()
    @@ -708,3 +710,43 @@ fn set_nested_state(&mut self, _value: &vp::NestedState) -> Result<(), Self::Err
             Err(KvmError::NotSupported)
         }
     }
    +
    +/// Refuses an APIC page whose LVT timer KVM would fire with a mode other than
    +/// Fixed.
    +///
    +/// `KVM_SET_LAPIC` re-arms the timer from the page's current count and
    +/// delivers an expired timer with the LVT's delivery mode (inside the ioctl on
    +/// an APICv host, otherwise at the next `KVM_RUN`). Any mode but Fixed would
    +/// latch an INIT or SIPI, wake a halted VP or queue an NMI or SMI that the saved
    +/// state does not hold. A guest cannot write the timer's delivery mode, so a
    +/// save never holds such a page.
    +fn check_restorable_apic_timer(lvt_timer: u32) -> Result<(), KvmError> {
    +    let lvt_timer = x86defs::apic::Lvt::from(lvt_timer);
    +    if !lvt_timer.masked() && lvt_timer.delivery_mode() != x86defs::apic::DeliveryMode::FIXED.0 {
    +        return Err(KvmError::InvalidState(
    +            "an unmasked APIC timer whose delivery mode is not Fixed",
    +        ));
    +    }
    +    Ok(())
    +}
    +
    +#[cfg(test)]
    +mod tests {
    +    use super::*;
    +    use x86defs::apic::Lvt;
    +
    +    #[test]
    +    fn restore_of_an_apic_timer_in_a_mode_other_than_fixed_is_refused() {
    +        for mode in 1..=7 {
    +            let lvt = Lvt::new().with_vector(0x30).with_delivery_mode(mode);
    +            assert!(
    +                check_restorable_apic_timer(lvt.into()).is_err(),
    +                "mode {mode}: refused"
    +            );
    +            check_restorable_apic_timer(lvt.with_masked(true).into()).unwrap();
    +        }
    +        check_restorable_apic_timer(Lvt::new().with_vector(0x30).into()).unwrap();
    +        // The architectural reset value: masked.
    +        check_restorable_apic_timer(0x10000).unwrap();
    +    }
    +}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions