Skip to content

openvmm: restoring an injected #BP or #OF on KVM loses it or panics debug #1385

Closed

Description

@tangruize

On KVM, restoring a VP saved state whose activity has an injected #BP (vector 3) or #OF (vector 4) as its pending_interruption succeeds, but KVM never reports the exception afterwards. A debug build then panics in restore_all's read-back check ("restore state mismatch"), and a release build restores a VP whose activity reads back without the exception. Only a crafted or corrupted snapshot reaches this: KVM never reports such an exception as injected, so no save holds one.

Reproduction

Take a KVM VP's save_all(), set its activity's pending_interruption to Exception { vector: 3, error_code: None }, and restore_all it. Expected: the restore is refused. Actual: a debug build panics (actual pending_interruption: None, expected Some(Exception { vector: 3, error_code: None })); a release build returns Ok, and activity() then reads back no pending interruption.

Cause

KvmVpStateAccess::set_activity (vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs) passes any injected exception to KVM_SET_VCPU_EVENTS:

// vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
match pending_interruption {
    Some(vp::PendingInterruption::Exception { vector, error_code }) => {
        events.exception.injected = true.into();
        events.exception.nr = vector;
        events.exception.has_error_code = error_code.is_some().into();
        events.exception.error_code = error_code.unwrap_or(0);
    }
    // ...
}

self.kvm().set_vcpu_events(&events)?;

KVM accepts vectors 3 and 4, since it refuses only a vector above 31 or NMI's. But it treats them as soft exceptions (kvm_exception_is_soft), and injecting one needs the length of the instruction that raised it, which the ioctl does not carry. So KVM_GET_VCPU_EVENTS never reports them as injected (arch/x86/kvm/x86.c).

Fix

fix.patch adds check_restorable_interruption. set_activity calls it before it changes any state, and it returns KvmError::InvalidState for an injected #BP or #OF, as set_activity already does for a pending ExtINT. Such a restore then fails with restore_all's StateError for the activity; every other activity is unchanged. The unit test restore_of_an_injected_soft_exception_is_refused checks that vectors 3 and 4 are refused and that #GP, a hard interrupt and an NMI are still accepted. The patch has no KVM-backed test.

Related: #1169.

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..0315b07 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
    @@ -343,6 +343,8 @@ fn set_activity(&mut self, value: &vp::Activity) -> Result<(), Self::Error> {
                 ));
             }
     
    +        check_restorable_interruption(pending_interruption)?;
    +
             let state = match mp_state {
                 vp::MpState::Running => kvm::KVM_MP_STATE_RUNNABLE,
                 vp::MpState::WaitForSipi => kvm::KVM_MP_STATE_INIT_RECEIVED,
    @@ -708,3 +710,52 @@ fn set_nested_state(&mut self, _value: &vp::NestedState) -> Result<(), Self::Err
             Err(KvmError::NotSupported)
         }
     }
    +
    +/// Refuses an injected interruption that KVM cannot restore.
    +///
    +/// KVM never reports an injected soft exception, #BP or #OF, as injected
    +/// (`KVM_GET_VCPU_EVENTS`): injecting one needs the instruction's length, which
    +/// the ioctl does not carry. So `KVM_SET_VCPU_EVENTS` accepts one that the VP
    +/// then never delivers, and a save of a real VP never holds one.
    +fn check_restorable_interruption(
    +    pending_interruption: Option<vp::PendingInterruption>,
    +) -> Result<(), KvmError> {
    +    if let Some(vp::PendingInterruption::Exception { vector, .. }) = pending_interruption {
    +        if vector == x86defs::Exception::BREAKPOINT.0 || vector == x86defs::Exception::OVERFLOW.0 {
    +            return Err(KvmError::InvalidState(
    +                "an injected #BP or #OF, which KVM cannot restore",
    +            ));
    +        }
    +    }
    +    Ok(())
    +}
    +
    +#[cfg(test)]
    +mod tests {
    +    use super::*;
    +
    +    #[test]
    +    fn restore_of_an_injected_soft_exception_is_refused() {
    +        for vector in [3, 4] {
    +            assert!(
    +                check_restorable_interruption(Some(vp::PendingInterruption::Exception {
    +                    vector,
    +                    error_code: None,
    +                }))
    +                .is_err(),
    +                "vector {vector}"
    +            );
    +        }
    +        for interruption in [
    +            None,
    +            Some(vp::PendingInterruption::Exception {
    +                vector: 13,
    +                error_code: Some(0),
    +            }),
    +            Some(vp::PendingInterruption::Interrupt { vector: 0x30 }),
    +            Some(vp::PendingInterruption::Nmi),
    +        ] {
    +            check_restorable_interruption(interruption).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