Skip to content

openvmm: source RAM unlink invalidates an already-opened snapshot #1144

Closed

Description

@tangruize

A valid microVM snapshot can fail restore when source teardown occurs after the restore has opened its RAM file. Automatic publication hard-links the source's RAM backing as memory.bin; dropping the source's temporary file removes the sibling link and changes the inode's ctime without changing the snapshot bytes.

In openvmm/openvmm_helpers/src/snapshot/fs.rs, the Linux opened_file_generation records that metadata change time:

        length: metadata.len(),
        modified_seconds: metadata.mtime(),
        modified_nanoseconds: metadata.mtime_nsec(),
        changed_seconds: metadata.ctime(),
        changed_nanoseconds: metadata.ctime_nsec(),
    })
}

OpenedSnapshot saves this generation when it opens the memory handle. Later validate_memory_generation and duplicate_memory_file_for_mapping compare against it, so a source unlink makes mapping preparation reject a snapshot whose identity, length, modification time and content are unchanged.

Trigger

  1. Capture using automatic RAM backing and publish memory.bin as a hard link.
  2. Open the snapshot for restore while the source still owns its temporary backing file.
  3. Tear down the source, removing that other link.
  4. Validate or duplicate the opened RAM handle for mapping.

The final step incorrectly reports that the memory generation changed.

Fix

fix.patch removes ctime from the Linux generation comparison and retains file identity, length and modification time. Link-count changes, chmod and chown no longer invalidate the opened generation.

The alternative is a publication redesign that prevents source teardown from changing the published inode's metadata. Removing ctime fixes the normal unlink race without changing the publication mechanism.

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/openvmm/openvmm_helpers/src/snapshot/fs.rs b/openvmm/openvmm_helpers/src/snapshot/fs.rs
    --- a/openvmm/openvmm_helpers/src/snapshot/fs.rs
    +++ b/openvmm/openvmm_helpers/src/snapshot/fs.rs
    @@ -1012,6 +1012,10 @@
         Ok(())
     }
     
    +/// The identity, length and modification time of an opened file. The change time (ctime) is
    +/// not recorded: metadata-only changes (link count, mode, owner) change it without changing the
    +/// content, for example the capture source unlinking the RAM temp file that a published
    +/// `memory.bin` hard-links.
     #[cfg(target_os = "linux")]
     #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     pub(super) struct OpenedFileGeneration {
    @@ -1020,8 +1024,6 @@
         length: u64,
         modified_seconds: i64,
         modified_nanoseconds: i64,
    -    changed_seconds: i64,
    -    changed_nanoseconds: i64,
     }
     
     #[cfg(windows)]
    @@ -1060,8 +1062,6 @@
             length: metadata.len(),
             modified_seconds: metadata.mtime(),
             modified_nanoseconds: metadata.mtime_nsec(),
    -        changed_seconds: metadata.ctime(),
    -        changed_nanoseconds: metadata.ctime_nsec(),
         })
     }
     
    diff --git a/openvmm/openvmm_helpers/src/snapshot/restore.rs b/openvmm/openvmm_helpers/src/snapshot/restore.rs
    --- a/openvmm/openvmm_helpers/src/snapshot/restore.rs
    +++ b/openvmm/openvmm_helpers/src/snapshot/restore.rs
    @@ -498,11 +498,15 @@
     #[cfg(test)]
     mod tests {
         use super::super::format::SNAPSHOT_TIER_INSTANCE_CHECKPOINT;
    +    #[cfg(target_os = "linux")]
    +    use super::super::fs::initialize_snapshot_memory_backing_file;
         use super::super::microvm::paired_scratch_manifest;
         use super::super::microvm::test_machine_contract;
         use super::super::publish::write_snapshot;
         #[cfg(target_os = "linux")]
         use super::super::publish::write_snapshot_from_memory_and_scratch_files;
    +    #[cfg(target_os = "linux")]
    +    use super::super::publish::write_snapshot_from_owned_memory_and_scratch_files;
         use super::super::tests::test_manifest;
         use super::*;
         use openvmm_defs::microvm::MICROVM_COMMAND_LINE_MAX_SIZE;
    @@ -1027,4 +1031,61 @@
                     || error.to_string().contains("not a regular file")
             );
         }
    -}
    +
    +    /// A capture with automatic RAM publishes `memory.bin` as a hard link of the source's RAM temp
    +    /// file (`.openvmm-microvm-memory-*`, the `NamedTempFile` that `MicrovmLaunch::new` creates in
    +    /// the destination's parent), and the source unlinks that temp file when it drops it at
    +    /// teardown, after the commit; the unlink changes `memory.bin`'s change time (ctime), not its
    +    /// file object or content. A restore that opened the snapshot before that unlink keeps its
    +    /// memory generation: `validate_memory_generation` and `duplicate_memory_file_for_mapping` pass
    +    /// (a generation that recorded ctime failed both: "memory generation changed after it was
    +    /// opened").
    +    #[cfg(target_os = "linux")]
    +    #[test]
    +    fn source_ram_unlink_keeps_the_opened_memory_generation() {
    +        use std::os::unix::fs::MetadataExt;
    +        let dir = tempfile::tempdir().unwrap();
    +        let snap_dir = dir.path().join("snap");
    +        // The automatic RAM backing, as `MicrovmLaunch::new` creates and opens it.
    +        let ram = tempfile::Builder::new()
    +            .prefix(".openvmm-microvm-memory-")
    +            .tempfile_in(dir.path())
    +            .unwrap();
    +        initialize_snapshot_memory_backing_file(ram.as_file(), 1024).unwrap();
    +        let handle = ram.reopen().unwrap();
    +        std::os::unix::fs::FileExt::write_all_at(&handle, b"guest RAM", 0).unwrap();
    +        write_snapshot_from_owned_memory_and_scratch_files(
    +            &snap_dir,
    +            &test_manifest(),
    +            b"state",
    +            &handle,
    +            None,
    +        )
    +        .unwrap();
    +        let memory_path = snap_dir.join(MEMORY_FILE_NAME);
    +        assert_eq!(
    +            std::fs::metadata(&memory_path).unwrap().ino(),
    +            ram.as_file().metadata().unwrap().ino(),
    +            "memory.bin is a hard link of the RAM temp file"
    +        );
    +        let content = std::fs::read(&memory_path).unwrap();
    +
    +        let snapshot = OpenedSnapshot::open(&snap_dir).unwrap();
    +        snapshot.validate_memory_generation(1024).unwrap();
    +        let ctime_before = std::fs::metadata(&memory_path).unwrap().ctime_nsec();
    +        std::thread::sleep(std::time::Duration::from_millis(20));
    +        // The source's teardown drops its NamedTempFile, which unlinks the RAM temp file.
    +        drop(ram);
    +
    +        let metadata = std::fs::metadata(&memory_path).unwrap();
    +        assert_eq!(metadata.nlink(), 1);
    +        assert_ne!(
    +            metadata.ctime_nsec(),
    +            ctime_before,
    +            "the unlink changes ctime"
    +        );
    +        assert_eq!(std::fs::read(&memory_path).unwrap(), content);
    +        snapshot.validate_memory_generation(1024).unwrap();
    +        snapshot.duplicate_memory_file_for_mapping(1024).unwrap();
    +    }
    +}
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