Skip to content

openvmm: a missing device's full-width or empty range overflows its length #1409

Open

Description

@tangruize

MissingDev::from_manifest computes each claimed region's length as range.end() - range.start() + 1. For a range that spans the whole port or MMIO space (0..=0xffff, 0..=u64::MAX) the + 1 overflows, and for an empty range (start after end) the - underflows. A build with overflow checks, such as OpenVMM's dev profile, which aborts on panic, panics while the device is built. A release build registers a full-width range with length 0, so it claims nothing, and an empty range with a length near the type's maximum, which the chipset's DeviceRange::map then refuses with a panic (by reading). OpenVMM's own callers claim fixed, valid ranges, and the microVM's missing devices claim none, so only a caller that passes such a range in a MissingDevManifest or a MissingDevHandle resource reaches it.

Reproduction

The patch's tests (missing_dev) a_full_width_range_is_mapped_in_two_halves and an_empty_range_claims_nothing fail without the fix. In the dev profile both panic in from_manifest on the overflow. In release, 0..=0xffff is registered with length 0, and 5..=3 with length 0xffff. a_claimed_range_is_mapped_with_its_length passes with and without the fix.

Cause

MissingDevManifest::claim_pio and claim_mmio, and the resolver of MissingDevHandle, accept any range, and from_manifest computes the length unchecked:

// vm/devices/missing_dev/src/lib.rs
let mut control = register_pio.new_io_region(name, range.end() - range.start() + 1);
control.map(*range.start());
// ...
let mut control =
register_mmio.new_io_region(name, range.end() - range.start() + 1);

A full-width range's length, 0x10000 or 2^64, does not fit in the u16 or u64 that new_io_region takes. In release, the chipset's region ignores a length of 0, and checks the region's end with expect, which the wrapped length of an empty range always fails:

// vmm_core/vmotherboard/src/chipset/backing/arc_mutex/services.rs
if self.len == 0 {
return;
}
// ...
addr.checked_add(self.len - 1).expect("overflow during addition, not possible in real hardware"),

Fix

fix.patch registers each claimed range through a new helper, regions. An empty range registers no region. A full-width range registers two halves, whose lengths fit, both under the range's name: region names are used only for tracing and conflict messages, so two regions can share one. Every other range is registered as before. It adds the three tests above. The alternative is to reject such ranges with an error; that would make from_manifest and the resolver, whose error type is Infallible, fallible, so the patch does not.

Related: #1384.

Activity

  1. tangruize commented on Nov 9, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 881619911d1a6ec2fec7bebdad7cfe69d09018f1. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/missing_dev/src/lib.rs b/vm/devices/missing_dev/src/lib.rs
    index c00b10d1b..3d58809a1 100644
    --- a/vm/devices/missing_dev/src/lib.rs
    +++ b/vm/devices/missing_dev/src/lib.rs
    @@ -65,32 +65,51 @@ pub fn from_manifest(
         ) -> MissingDev {
             let MissingDevManifest { pio, mmio, pci } = manifest;
     
    -        let pio_control = pio
    -            .iter()
    -            .map(|(name, range)| {
    -                let mut control = register_pio.new_io_region(name, range.end() - range.start() + 1);
    -                control.map(*range.start());
    -                control
    -            })
    -            .collect();
    -
    -        let mmio_control = mmio
    -            .iter()
    -            .map(|(name, range)| {
    -                let mut control =
    -                    register_mmio.new_io_region(name, range.end() - range.start() + 1);
    -                control.map(*range.start());
    -                control
    -            })
    -            .collect();
    +        let mut pio_control = Vec::new();
    +        for (name, range) in &pio {
    +            let (start, end) = (u64::from(*range.start()), u64::from(*range.end()));
    +            for (start, len) in regions(start, end, u16::MAX.into()) {
    +                // Each region lies within the range, so its start and its
    +                // length fit in a port number.
    +                let mut control = register_pio.new_io_region(name, len as u16);
    +                control.map(start as u16);
    +                pio_control.push(control);
    +            }
    +        }
    +
    +        let mut mmio_control = Vec::new();
    +        for (name, range) in &mmio {
    +            for (start, len) in regions(*range.start(), *range.end(), u64::MAX) {
    +                let mut control = register_mmio.new_io_region(name, len);
    +                control.map(start);
    +                mmio_control.push(control);
    +            }
    +        }
     
             MissingDev {
                 pio,
                 mmio,
                 pci,
     
    -            _pio_control: pio_control,
    -            _mmio_control: mmio_control,
    +            _pio_control: pio_control.into(),
    +            _mmio_control: mmio_control.into(),
    +        }
    +    }
    +}
    +
    +/// The regions that claim `start..=end`, as `(start, length)` pairs whose
    +/// lengths are at most `max_len`, the largest address of the address space:
    +/// none for an empty range, and two halves for a range that spans the whole
    +/// address space, whose length is one more than `max_len`.
    +fn regions(start: u64, end: u64, max_len: u64) -> Vec<(u64, u64)> {
    +    if start > end {
    +        return Vec::new();
    +    }
    +    match (end - start).checked_add(1) {
    +        Some(len) if len <= max_len => vec![(start, len)],
    +        _ => {
    +            let mid = start + (end - start) / 2;
    +            vec![(start, mid - start + 1), (mid + 1, end - mid)]
             }
         }
     }
    @@ -294,3 +313,125 @@ fn resolve(
             }
         }
     }
    +
    +#[cfg(test)]
    +mod tests {
    +    use super::*;
    +    use chipset_device::mmio::ExternallyManagedMmioIntercepts;
    +    use chipset_device::pio::ExternallyManagedPortIoIntercepts;
    +
    +    /// A registration whose regions remember where they were mapped.
    +    struct Recorder;
    +
    +    /// A region that remembers its length and the address it was mapped at.
    +    struct Region<T> {
    +        addr: Option<T>,
    +        len: T,
    +    }
    +
    +    impl RegisterPortIoIntercept for Recorder {
    +        fn new_io_region(
    +            &mut self,
    +            _region_name: &str,
    +            len: u16,
    +        ) -> Box<dyn ControlPortIoIntercept> {
    +            Box::new(Region { addr: None, len })
    +        }
    +    }
    +
    +    impl RegisterMmioIntercept for Recorder {
    +        fn new_io_region(&mut self, _region_name: &str, len: u64) -> Box<dyn ControlMmioIntercept> {
    +            Box::new(Region { addr: None, len })
    +        }
    +    }
    +
    +    impl ControlPortIoIntercept for Region<u16> {
    +        fn region_name(&self) -> &str {
    +            "region"
    +        }
    +        fn map(&mut self, addr: u16) {
    +            self.addr = Some(addr);
    +        }
    +        fn unmap(&mut self) {
    +            self.addr = None;
    +        }
    +        fn addr(&self) -> Option<u16> {
    +            self.addr
    +        }
    +        fn len(&self) -> u16 {
    +            self.len
    +        }
    +        fn offset_of(&self, _addr: u16) -> Option<u16> {
    +            None
    +        }
    +    }
    +
    +    impl ControlMmioIntercept for Region<u64> {
    +        fn region_name(&self) -> &str {
    +            "region"
    +        }
    +        fn map(&mut self, addr: u64) {
    +            self.addr = Some(addr);
    +        }
    +        fn unmap(&mut self) {
    +            self.addr = None;
    +        }
    +        fn addr(&self) -> Option<u64> {
    +            self.addr
    +        }
    +        fn len(&self) -> u64 {
    +            self.len
    +        }
    +        fn offset_of(&self, _addr: u64) -> Option<u64> {
    +            None
    +        }
    +    }
    +
    +    /// The regions that claiming `range` maps, as `(address, length)`.
    +    fn pio(range: RangeInclusive<u16>) -> Vec<(Option<u16>, u16)> {
    +        let dev = MissingDev::from_manifest(
    +            MissingDevManifest::new().claim_pio("r", range),
    +            &mut ExternallyManagedMmioIntercepts,
    +            &mut Recorder,
    +        );
    +        dev._pio_control
    +            .iter()
    +            .map(|c| (c.addr(), c.len()))
    +            .collect()
    +    }
    +
    +    /// The regions that claiming `range` maps, as `(address, length)`.
    +    fn mmio(range: RangeInclusive<u64>) -> Vec<(Option<u64>, u64)> {
    +        let dev = MissingDev::from_manifest(
    +            MissingDevManifest::new().claim_mmio("r", range),
    +            &mut Recorder,
    +            &mut ExternallyManagedPortIoIntercepts,
    +        );
    +        dev._mmio_control
    +            .iter()
    +            .map(|c| (c.addr(), c.len()))
    +            .collect()
    +    }
    +
    +    #[test]
    +    fn a_claimed_range_is_mapped_with_its_length() {
    +        assert_eq!(pio(0x60..=0x64), [(Some(0x60), 5)]);
    +        assert_eq!(pio(0..=0xfffe), [(Some(0), 0xffff)]);
    +        assert_eq!(mmio(0x1000..=0x1fff), [(Some(0x1000), 0x1000)]);
    +    }
    +
    +    #[test]
    +    fn a_full_width_range_is_mapped_in_two_halves() {
    +        assert_eq!(pio(0..=0xffff), [(Some(0), 0x8000), (Some(0x8000), 0x8000)]);
    +        assert_eq!(
    +            mmio(0..=u64::MAX),
    +            [(Some(0), 1 << 63), (Some(1 << 63), 1 << 63)]
    +        );
    +    }
    +
    +    #[test]
    +    fn an_empty_range_claims_nothing() {
    +        assert_eq!(pio(RangeInclusive::new(5, 3)), []);
    +        assert_eq!(mmio(RangeInclusive::new(5, 3)), []);
    +    }
    +}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: openvmmInitial title-keyword triage suggestion

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions