Skip to content

chipset: PIT restore accepts a control word with rw = 0, then hits unreachable!() on the next access #1066

Closed

Description

@q5438722

Summary

PitDevice::restore installs each saved control word through ControlWord::from, which accepts any byte. rw = 0 is the encoding of the counter-latch command. set_control handles it as a latch and never stores it, but restore does store it. The next read or write of that counter then hits unreachable!() in Timer::write (pit/mod.rs:334) or Timer::read (pit/mod.rs:375).

Repro

pit_restore_rw0.txt

Add the attached pit_restore_rw0.rs (.txt) to vm/devices/chipset/tests/ and run:

cargo test -p chipset --test pit_restore_rw0

The test saves a fresh PIT and changes counter 0's control field in the encoded saved state from 0x10 to 0x00. It then restores into a new device and accesses port 0x40.

Expected

restore fails with RestoreError::InvalidSavedState, and the device stays usable.

Actual

restore returns Ok:

thread 'restored_rw0_control_is_rejected' panicked at vm/devices/chipset/tests/pit_restore_rw0.rs:41:9:
saved state with a rw = 0 control word was accepted

A later access to the counter then panics:

panicked at vm/devices/chipset/src/pit/mod.rs:334:18:
internal error: entered unreachable code

Suggested fix

Validate the control words before installing any state:

--- a/vm/devices/chipset/src/pit/mod.rs
+++ b/vm/devices/chipset/src/pit/mod.rs
@@ -755,6 +755,8 @@ pub struct SavedState {
     enum PitDeviceRestoreError {
         #[error("last tick time is after current time")]
         InvalidLastTick,
+        #[error("timer {0} has no read/write mode in its control word")]
+        MissingAccessMode(usize),
     }
 
     impl SaveRestore for PitDevice {
@@ -812,6 +814,17 @@ fn save(&mut self) -> Result<Self::SavedState, SaveError> {
         fn restore(&mut self, state: Self::SavedState) -> Result<(), RestoreError> {
             let state::SavedState { timers, last } = state;
 
+            // rw = 0 encodes the counter latch command, which is never stored
+            // as a timer's access mode.
+            if let Some(i) = timers
+                .iter()
+                .position(|timer| ControlWord::from(timer.control).rw() == 0)
+            {
+                return Err(RestoreError::InvalidSavedState(
+                    PitDeviceRestoreError::MissingAccessMode(i).into(),
+                ));
+            }
+
             for (timer, state) in self.timers.iter_mut().zip(timers) {
                 let state::SavedTimerState {
                     ce,

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