Skip to content

openvmm: PIT restore accepts RW=0 and subsequent counter I/O can panic #1147

Closed

Description

@tangruize

A modified snapshot can restore a PIT timer with RW=0 in its control word. Restore succeeds, but the guest's next counter read or write can reach an unreachable! access-mode branch and panic the VM worker.

In vm/devices/chipset/src/pit/mod.rs, PitDevice::restore decodes the saved control byte directly into TimerState without validating its access mode:

                    out,
                    control: ControlWord::from(control), // no unused bits
                    gate,
                    null,
                    read_high,

RW occupies bits 4–5. Zero encodes a counter-latch command, not a programmed counter's access mode. Ordinary guest control writes handle that command by latching the count without storing it, so counter I/O assumes the stored mode is LOW, HIGH or LOW_HIGH. Restoring an unchecked byte bypasses that invariant.

Reproduction

Capture PIT state, clear bits 4–5 of any channel's saved control byte, then restore it. Access the corresponding counter port: 0x40, 0x41 or 0x42.

The restore accepts RW=0 instead of returning InvalidSavedState, leaving the invalid mode for the subsequent guest access. This state requires a modified saved-state payload; normal PIT programming cannot persist the latch-command encoding.

Fix

fix.patch decodes and checks the control word before assigning it to the timer. It rejects RW=0 with InvalidSavedState and retains the three valid access modes, preventing saved-state input from installing a counter mode that the I/O paths cannot handle.

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/pit/mod.rs b/vm/devices/chipset/src/pit/mod.rs
    index 4f1e8bf90..c6336cb8c 100644
    --- a/vm/devices/chipset/src/pit/mod.rs
    +++ b/vm/devices/chipset/src/pit/mod.rs
    @@ -833,6 +833,8 @@ pub struct SavedState {
         enum PitDeviceRestoreError {
             #[error("last tick time is after current time")]
             InvalidLastTick,
    +        #[error("timer control word {0:#04x} has access mode 0, which no programmed counter holds")]
    +        InvalidControlWord(u8),
         }
     
         impl SaveRestore for PitDevice {
    @@ -908,6 +910,15 @@ fn restore(&mut self, state: Self::SavedState) -> Result<(), RestoreError> {
                         cr_low,
                     } = state;
     
    +                // Access mode 0 is the counter latch command's encoding: a control word
    +                // write latches the count for it and never stores it.
    +                let control = ControlWord::from(control); // no unused bits
    +                if control.rw() == 0 {
    +                    return Err(RestoreError::InvalidSavedState(
    +                        PitDeviceRestoreError::InvalidControlWord(control.into()).into(),
    +                    ));
    +                }
    +
                     timer.state = TimerState {
                         ce,
                         cr,
    @@ -921,7 +932,7 @@ fn restore(&mut self, state: Self::SavedState) -> Result<(), RestoreError> {
                             state::SavedCountState::Counting => CountState::Counting,
                         },
                         out,
    -                    control: ControlWord::from(control), // no unused bits
    +                    control,
                         gate,
                         null,
                         read_high,
    @@ -1356,4 +1367,46 @@ fn time_abi_rejects_periodic_channel0_at_save_and_restore() {
             device(true).restore(saved).unwrap();
             drop(keeper);
         }
    +
    +    /// PIT-3 (BUG-15), the witness flipped: a saved control word with access mode 0 (the counter
    +    /// latch command's encoding, which no programmed counter holds) fails the restore with an
    +    /// InvalidSavedState error, for each channel, and the guest's next counter read does not
    +    /// panic. Every control word with an access mode still restores.
    +    #[test]
    +    fn pit3_restore_rejects_a_control_word_without_access_mode() {
    +        use super::PitDevice;
    +        use chipset_device::pio::PortIoIntercept;
    +        use vmcore::save_restore::RestoreError;
    +        use vmcore::save_restore::SaveRestore;
    +        use vmcore::vmtime::VmTime;
    +
    +        let mut pool = pal_async::DefaultPool::new();
    +        let driver = pool.driver();
    +        let keeper = vmcore::vmtime::VmTimeKeeper::new(&driver, VmTime::from_100ns(0));
    +        let source = pool.run_until(keeper.builder().build(&driver)).unwrap();
    +        let mut captured = PitDevice::new(LineInterrupt::detached(), source.access("pit"));
    +        let mut restores = 0;
    +        for channel in 0..3 {
    +            for control in 0..=0x3fu8 {
    +                let mut saved = captured.save().unwrap();
    +                saved.timers[channel].control = control;
    +                let mut restored = PitDevice::new(LineInterrupt::detached(), source.access("pit"));
    +                let result = restored.restore(saved);
    +                if (control >> 4) & 3 == 0 {
    +                    let Err(RestoreError::InvalidSavedState(err)) = result else {
    +                        panic!("channel {channel} control {control:#x}: {result:?}");
    +                    };
    +                    assert!(format!("{err:#}").contains("access mode 0"), "{err:#}");
    +                } else {
    +                    result.unwrap();
    +                }
    +                // The next counter read does not panic.
    +                let mut data = [0];
    +                restored.io_read(0x40 + channel as u16, &mut data).unwrap();
    +                restores += 1;
    +            }
    +        }
    +        assert_eq!(restores, 3 * 64);
    +        drop(keeper);
    +    }
     }
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