Skip to content

openvmm: an out-of-range restored IOAPIC id panics the next ID-register read #1172

Closed

Description

@tangruize

IOAPIC restore accepts saved ids above 15, even though the emulated ID register has only a four-bit id field. A crafted or foreign saved state can restore successfully and crash the VM worker on the guest's next ordinary ID-register read.

In vm/devices/chipset/src/ioapic/mod.rs, IoApicDevice::restore extracts the saved u8 id but proceeds to validate the redirection table without checking the id's field width:

        fn restore(&mut self, state: state::SavedState) -> Result<(), RestoreError> {
            let state::SavedState {
                redirection_entries,
                line_levels,
                id,
                index,
            } = state;
            if redirection_entries.len() != self.irqs.len() {
                return Err(RestoreError::Other(WrongNumberOfRedirectionEntries.into()));
            }

The method later assigns self.id = id. When read_register constructs the ID register with with_id(self.id), an id greater than 0xf triggers the bitfield setter's bounds panic. That check is active in release builds as well as debug builds.

Reproduction

Save a 24-line IoApicDevice, change the saved id to 16, and restore it. Select the ID register through the IOAPIC index register at 0xfec00000, then read four bytes from the data register at 0xfec00010.

Restore accepts the state; the read panics because 16 is outside the ID field's [0, 15] range.

Fix

fix.patch checks id <= 0xf at the start of restore and returns RestoreError::InvalidSavedState otherwise, before modifying interrupt routing or device state. Representable ids retain their existing behavior, and rejecting an invalid state leaves the previous device state intact.

Activity

  1. tangruize commented on Nov 1, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 998b9f2402050cd16d1faa897bc831ef1434be48. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/chipset/src/ioapic/mod.rs b/vm/devices/chipset/src/ioapic/mod.rs
    index dc1eca62b..17453613e 100644
    --- a/vm/devices/chipset/src/ioapic/mod.rs
    +++ b/vm/devices/chipset/src/ioapic/mod.rs
    @@ -434,6 +434,10 @@ pub struct SavedState {
         #[error("wrong number of redirection entries")]
         struct WrongNumberOfRedirectionEntries;
     
    +    #[derive(Error, Debug)]
    +    #[error("IOAPIC id {0} does not fit the ID register's 4-bit field")]
    +    struct InvalidId(u8);
    +
         impl SaveRestore for IoApicDevice {
             type SavedState = state::SavedState;
     
    @@ -462,6 +466,9 @@ fn restore(&mut self, state: state::SavedState) -> Result<(), RestoreError> {
                     id,
                     index,
                 } = state;
    +            if id > 0xf {
    +                return Err(RestoreError::InvalidSavedState(InvalidId(id).into()));
    +            }
                 if redirection_entries.len() != self.irqs.len() {
                     return Err(RestoreError::Other(WrongNumberOfRedirectionEntries.into()));
                 }
    @@ -490,6 +497,57 @@ fn restore(&mut self, state: state::SavedState) -> Result<(), RestoreError> {
                 Ok(())
             }
         }
    +
    +    #[cfg(test)]
    +    mod tests {
    +        use super::*;
    +        use crate::ioapic::IOAPIC_DEVICE_MMIO_REGION_BASE_ADDRESS;
    +        use crate::ioapic::spec::Register;
    +        use chipset_device::io::IoResult;
    +        use chipset_device::mmio::MmioIntercept;
    +        use chipset_resources::ioapic::IoApicRouting;
    +        use test_with_tracing::test;
    +
    +        struct NoRouting;
    +
    +        impl IoApicRouting for NoRouting {
    +            fn assert(&self, _irq: u8) {}
    +            fn set_route(&self, _irq: u8, _request: Option<(u64, u32)>) {}
    +        }
    +
    +        #[test]
    +        fn restored_id_above_15_does_not_panic_the_id_read() {
    +            let mut ioapic = IoApicDevice::new(24, Box::new(NoRouting));
    +            let mut valid = ioapic.save().unwrap();
    +            valid.id = 15;
    +            ioapic.restore(valid).unwrap();
    +            let before = mesh::payload::encode(ioapic.save().unwrap());
    +            for id in [16, u8::MAX] {
    +                let mut invalid = ioapic.save().unwrap();
    +                invalid.id = id;
    +                invalid.index = 1;
    +                invalid.redirection_entries[0] = 0;
    +                assert!(matches!(
    +                    ioapic.restore(invalid),
    +                    Err(RestoreError::InvalidSavedState(_))
    +                ));
    +                assert_eq!(mesh::payload::encode(ioapic.save().unwrap()), before);
    +            }
    +
    +            let index = IOAPIC_DEVICE_MMIO_REGION_BASE_ADDRESS + Register::INDEX.0;
    +            let data_register = IOAPIC_DEVICE_MMIO_REGION_BASE_ADDRESS + Register::DATA.0;
    +            assert!(matches!(
    +                ioapic.mmio_write(index, &[IndexRegister::ID.0]),
    +                IoResult::Ok
    +            ));
    +            let mut dword = [0u8; 4];
    +            assert!(matches!(
    +                ioapic.mmio_read(data_register, &mut dword),
    +                IoResult::Ok
    +            ));
    +            assert_eq!(u32::from_ne_bytes(dword), 15 << 24);
    +        }
    +    }
     }
     
     impl MmioIntercept for IoApicDevice {
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