Skip to content

openvmm: opening a snapshot with an unknown paired-scratch restore mode panics #1361

Closed

Description

@tangruize

Opening a microVM snapshot whose paired scratch block has a restore_mode this OpenVMM does not know hits an unreachable! instead of returning an "unsupported" error. OpenVMM builds with panic = 'abort', so the process aborts, before any VM is built. Only a manifest this OpenVMM did not write reaches it (corrupted, crafted, or from a newer version with a new mode), through --restore-snapshot or a management-RPC CreateVm with a restore_path.

Reproduction

Take a valid paired-scratch snapshot, set the restore_mode of its scratch block (the sandbox block whose artifact is scratch.img) to "a-future-restore-mode", and call OpenedSnapshot::open on the directory. It should fail with "snapshot scratch restore mode 'a-future-restore-mode' is unsupported"; it panics with "internal error: entered unreachable code: machine-contract shape validation rejects unknown restore modes". The test unknown_paired_scratch_restore_mode_is_rejected_on_open that fix.patch adds does exactly this, and fails before the fix.

Cause

validate_snapshot_tier assumes that the machine-contract shape check has already rejected unknown modes:

// openvmm/openvmm_helpers/src/snapshot/microvm.rs
if let Some(scratch) = paired_scratch_block(manifest) {
    match scratch.restore_mode.as_str() {
        "" | SNAPSHOT_SCRATCH_RESTORE_PRIVATE_COPY => {}
        // ...
        _ => unreachable!("machine-contract shape validation rejects unknown restore modes"),
    }
}

But decode_snapshot_manifest runs the shape check after validate_manifest_contents, which calls validate_snapshot_tier:

// openvmm/openvmm_helpers/src/snapshot/restore.rs
validate_manifest_header(&manifest)?;
validate_manifest_contents(&manifest)?;
if let Some(contract) = &manifest.machine_contract {
    microvm::validate_machine_contract_shape(

Fix

fix.patch replaces the arm with mode => anyhow::bail!("snapshot scratch restore mode '{mode}' is unsupported"), the shape check's own message. It leaves the validation order unchanged, so every other malformed manifest still gets the same first error. The test (in openvmm_helpers' restore.rs) asserts that open returns this error, and that validate_machine_contract_shape gives the same text for the same manifest.

Activity

  1. tangruize commented on Nov 8, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 3cf58a5234ed824435825c9f87187f5129219724. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/openvmm/openvmm_helpers/src/snapshot/microvm.rs b/openvmm/openvmm_helpers/src/snapshot/microvm.rs
    index 0664d1a..534cbcc 100644
    --- a/openvmm/openvmm_helpers/src/snapshot/microvm.rs
    +++ b/openvmm/openvmm_helpers/src/snapshot/microvm.rs
    @@ -2271,7 +2271,7 @@ pub(super) fn validate_snapshot_tier(manifest: &SnapshotManifest) -> anyhow::Res
                     manifest.restore_policy == SNAPSHOT_RESTORE_POLICY_RESUME,
                     "direct-claimed scratch materialization requires resume restore policy"
                 ),
    -            _ => unreachable!("machine-contract shape validation rejects unknown restore modes"),
    +            mode => anyhow::bail!("snapshot scratch restore mode '{mode}' is unsupported"),
             }
         }
         if manifest.snapshot_tier == SNAPSHOT_TIER_PLATFORM {
    diff --git a/openvmm/openvmm_helpers/src/snapshot/restore.rs b/openvmm/openvmm_helpers/src/snapshot/restore.rs
    index 44d2750..8d91966 100644
    --- a/openvmm/openvmm_helpers/src/snapshot/restore.rs
    +++ b/openvmm/openvmm_helpers/src/snapshot/restore.rs
    @@ -959,4 +959,51 @@ fn opened_snapshot_rejects_reparse_artifact() {
                     || error.to_string().contains("not a regular file")
             );
         }
    +
    +    /// Opening a snapshot whose paired scratch records
    +    /// a restore mode this OpenVMM does not know fails with the error of the
    +    /// machine-contract shape check. `decode_snapshot_manifest` runs the
    +    /// contents check, whose capture-tier check matches the restore mode,
    +    /// before the shape check; before the fix that match ended in an
    +    /// `unreachable!` and the open panicked.
    +    #[test]
    +    fn unknown_paired_scratch_restore_mode_is_rejected_on_open() {
    +        let scratch = vec![0x5a; 512];
    +        let mut manifest = paired_scratch_manifest(&scratch);
    +        let contract = manifest.machine_contract.as_mut().unwrap();
    +        let block = contract.microvm_sandbox_blocks.last_mut().unwrap();
    +        block.restore_mode = "a-future-restore-mode".to_owned();
    +        let state = b"state";
    +        manifest.state_size_bytes = state.len() as u64;
    +        let dir = tempfile::tempdir().unwrap();
    +        std::fs::write(
    +            dir.path().join(MANIFEST_FILE_NAME),
    +            mesh::payload::encode(manifest.clone()),
    +        )
    +        .unwrap();
    +        std::fs::write(dir.path().join(STATE_FILE_NAME), state).unwrap();
    +        std::fs::write(
    +            dir.path().join(MEMORY_FILE_NAME),
    +            vec![0_u8; manifest.memory_size_bytes as usize],
    +        )
    +        .unwrap();
    +        std::fs::write(dir.path().join(SCRATCH_FILE_NAME), &scratch).unwrap();
    +        let error = match OpenedSnapshot::open(dir.path()) {
    +            Ok(_) => panic!("a snapshot with an unknown scratch restore mode opened"),
    +            Err(error) => error.to_string(),
    +        };
    +        assert_eq!(
    +            error,
    +            "snapshot scratch restore mode 'a-future-restore-mode' is unsupported"
    +        );
    +        // The shape check, which runs later on this path, rejects the
    +        // manifest with the same error.
    +        let shape_error = microvm::validate_machine_contract_shape(
    +            manifest.machine_contract.as_ref().unwrap(),
    +            manifest.memory_size_bytes,
    +            manifest.vp_count,
    +        )
    +        .unwrap_err();
    +        assert_eq!(error, shape_error.to_string());
    +    }
     }
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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions