Skip to content

openvmm: unsupported snapshot state panics restore after potentially changing the partition #1168

Closed

Description

@tangruize

A decoded snapshot containing CPU or VM elements unsupported by the destination partition panics the VM worker instead of returning a restore error. Supported elements earlier in the restore order may already have changed the partition.

In vmm_core/virt/src/state.rs, state_trait! generates this per-element restore block:

if let Some(value) = state.$get.as_ref() {
    // TODO: assert good or not?
    assert!(<$ty as $crate::state::StateElement<$caps, $vp>>::is_present(self.caps()));
    self.$set(value).map_err(|err| $crate::state::StateError{phase: concat!("restore ", stringify!($id)), err})?;
    if cfg!(debug_assertions) {
        if <$ty as $crate::state::StateElement<$caps, $vp>>::can_compare(self.caps()) {
            assert_eq!(&self.$get().expect($id), value, "restore state mismatch (actual/expected)");
        }
    }
}

A field's presence in decoded protobuf input does not establish backend support. The unconditional assertion checks a property of external snapshot input, and checking it inside the setter loop is too late to prevent earlier mutation.

Trigger

Restore a decoded snapshot containing SynIC CPU state into a KVM partition without Hyper-V enlightenments. VM-level hypercall or reference-TSC state presents the same problem when those elements are unsupported. Ordinary saves omit unavailable elements, but crafted or incompatible snapshot input can still contain them.

Fix

fix.patch preflights every present snapshot element against destination capabilities before invoking any setter or commit. An unsupported element returns StateError; the backend-error source becomes optional because this rejection occurs before backend access. Backend failures retain their actual error sources.

This prevents partial mutation caused by unsupported elements. It does not make restore transactional against arbitrary failures from later backend setters.

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/vmm_core/virt/src/state.rs b/vmm_core/virt/src/state.rs
    index f30ba98e1..13d668a57 100644
    --- a/vmm_core/virt/src/state.rs
    +++ b/vmm_core/virt/src/state.rs
    @@ -39,7 +39,7 @@ pub trait HvRegisterState<T, const COUNT: usize>: Default {
     pub struct StateError<T: 'static + Debug + std::error::Error> {
         pub(crate) phase: &'static str,
         #[source]
    -    pub(crate) err: T,
    +    pub(crate) err: Option<T>,
     }
     
     mod macros {
    @@ -86,7 +86,7 @@ fn save_all(&mut self) -> Result<$save_state, $crate::state::StateError<Self::Er
                         let mut save_state = $save_state::default();
                         $(
                             if <$ty as $crate::state::StateElement<$caps, $vp>>::is_present(self.caps()) {
    -                            save_state.$get = Some(self.$get().map_err(|err| $crate::state::StateError{phase: concat!("save ", stringify!($id)), err})?);
    +                            save_state.$get = Some(self.$get().map_err(|err| $crate::state::StateError{phase: concat!("save ", stringify!($id)), err: Some(err)})?);
                             }
                         )*
     
    @@ -96,11 +96,14 @@ fn save_all(&mut self) -> Result<$save_state, $crate::state::StateError<Self::Er
                     /// Restore state elements saved in save state.
                     #[allow(unused_variables)]
                     fn restore_all(&mut self, state: &$save_state) -> Result<(), $crate::state::StateError<Self::Error>> {
    +                    $(
    +                        if state.$get.is_some() && !<$ty as $crate::state::StateElement<$caps, $vp>>::is_present(self.caps()) {
    +                            return Err($crate::state::StateError{phase: concat!("restore ", stringify!($id), ": unsupported state"), err: None});
    +                        }
    +                    )*
                         $(
                             if let Some(value) = state.$get.as_ref() {
    -                            // TODO: assert good or not?
    -                            assert!(<$ty as $crate::state::StateElement<$caps, $vp>>::is_present(self.caps()));
    -                            self.$set(value).map_err(|err| $crate::state::StateError{phase: concat!("restore ", stringify!($id)), err})?;
    +                            self.$set(value).map_err(|err| $crate::state::StateError{phase: concat!("restore ", stringify!($id)), err: Some(err)})?;
                                 if cfg!(debug_assertions) {
                                     if <$ty as $crate::state::StateElement<$caps, $vp>>::can_compare(self.caps()) {
                                         assert_eq!(&self.$get().expect($id), value, "restore state mismatch (actual/expected)");
    @@ -109,14 +112,14 @@ fn restore_all(&mut self, state: &$save_state) -> Result<(), $crate::state::Stat
                             }
                         )*
     
    -                    self.commit().map_err(|err| $crate::state::StateError{phase: "commit restore", err})
    +                    self.commit().map_err(|err| $crate::state::StateError{phase: "commit restore", err: Some(err)})
                     }
     
                     /// Resets all the state elements to their initial state (after machine reset).
                     fn reset_all(&mut self, vp_info: &$vp) -> Result<(), $crate::state::StateError<Self::Error>> {
                         $(
                             if <$ty as $crate::state::StateElement<$caps, $vp>>::is_present(self.caps()) {
    -                            self.$set(&<$ty as $crate::state::StateElement<$caps, $vp>>::at_reset(self.caps(), vp_info)).map_err(|err| $crate::state::StateError{phase: concat!("reset ", stringify!($id)), err})?;
    +                            self.$set(&<$ty as $crate::state::StateElement<$caps, $vp>>::at_reset(self.caps(), vp_info)).map_err(|err| $crate::state::StateError{phase: concat!("reset ", stringify!($id)), err: Some(err)})?;
                             }
                         )*
     
    @@ -163,3 +166,84 @@ pub struct $save_state {
     
         pub(crate) use state_trait;
     }
    +
    +#[cfg(test)]
    +#[expect(
    +    dead_code,
    +    reason = "the test backend exercises only restore operations"
    +)]
    +mod tests {
    +    use super::*;
    +    use test_with_tracing::test;
    +
    +    #[derive(Debug, Default, PartialEq, Eq, Inspect, mesh_protobuf::Protobuf)]
    +    #[mesh(package = "virt.test")]
    +    pub struct Value {
    +        #[mesh(1)]
    +        value: u32,
    +    }
    +
    +    impl StateElement<bool, ()> for Value {
    +        fn is_present(caps: &bool) -> bool {
    +            *caps
    +        }
    +
    +        fn at_reset(_: &bool, _: &()) -> Self {
    +            Self::default()
    +        }
    +    }
    +
    +    state_trait!(
    +        "Test state",
    +        Access,
    +        bool,
    +        (),
    +        Saved,
    +        "virt.test",
    +        (1, "value", value, set_value, Value),
    +    );
    +
    +    struct Backend {
    +        present: bool,
    +        writes: usize,
    +    }
    +
    +    impl Access for Backend {
    +        type Error = std::io::Error;
    +
    +        fn caps(&self) -> &bool {
    +            &self.present
    +        }
    +
    +        fn commit(&mut self) -> Result<(), Self::Error> {
    +            self.writes += 1;
    +            Ok(())
    +        }
    +
    +        fn value(&mut self) -> Result<Value, Self::Error> {
    +            Ok(Value::default())
    +        }
    +
    +        fn set_value(&mut self, _: &Value) -> Result<(), Self::Error> {
    +            self.writes += 1;
    +            Ok(())
    +        }
    +    }
    +
    +    #[test]
    +    fn unsupported_restore_is_rejected_without_backend_mutation() {
    +        let mut backend = Backend {
    +            present: false,
    +            writes: 0,
    +        };
    +        let error = backend
    +            .restore_all(&Saved {
    +                value: Some(Value::default()),
    +            })
    +            .unwrap_err();
    +        assert!(error.err.is_none());
    +        assert_eq!(backend.writes, 0);
    +        backend.restore_all(&Saved::default()).unwrap();
    +        assert_eq!(backend.writes, 1);
    +    }
    +}
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