From 0e46a01edadcf01af3ccc633f817026ecfe60e9a Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Sat, 7 Nov 2026 13:29:38 -0800 Subject: [PATCH] openvmm_helpers: leave the change time out of snapshot file generations A capture with automatic RAM publishes memory.bin as a hard link of the RAM file that the source created beside the destination, and the source removes that file's temporary name as it tears down, after the commit. A restore that opened the snapshot before then failed with "snapshot memory generation changed after it was opened" (microsoft/nvx#1144). On Linux, the generation that OpenedSnapshot records for an opened file, and checks again before and after it maps the memory, included the file's change time, which the removal moves with the link count, although the file's identity, length, modification time, and content stay as they were. A change of the file's mode or owner moved it too. Leave the change time out of the Linux generation, which keeps the device, inode, length, and modification time. A write still moves the modification time, so a restore still finds a same-length change of the memory between its open and its mapping, while a change of the file's links, mode, or owner alone no longer fails a restore, nor the bounded reads of the manifest and saved state. The snapshot format guide says what Linux restores check. Tests: a restore opens a snapshot whose memory.bin links the automatic RAM file of its source; the source then drops that file, which leaves memory.bin with one link and a new change time but its modification time, and the restore still validates and duplicates the memory that it opened and reads back its content. Without the change, the validation fails with "snapshot memory generation changed after it was opened", on tmpfs and ext4, with Linux 6.6 and 6.18. The existing test of a same-length write after the open still finds the change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Guide/src/dev_guide/snapshot_format.md | 14 +++-- openvmm/openvmm_helpers/src/snapshot/fs.rs | 11 ++-- .../openvmm_helpers/src/snapshot/restore.rs | 61 +++++++++++++++++++ 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/Guide/src/dev_guide/snapshot_format.md b/Guide/src/dev_guide/snapshot_format.md index 9ee923237c6..d533163a010 100644 --- a/Guide/src/dev_guide/snapshot_format.md +++ b/Guide/src/dev_guide/snapshot_format.md @@ -89,11 +89,15 @@ relative to that handle. Windows uses read-only handles with `FILE_SHARE_READ` only, rejects reparse points, compares `FILE_ID_INFO` and EOF before and after creating the private COW section, and keeps the directory and artifact guards in the VM worker until teardown. Linux keeps the exact `O_NOFOLLOW` directory -and regular-file descriptors and rejects observable metadata changes before -handoff, so renaming or replacing the original path cannot substitute another -generation. Linux file descriptors do not provide mandatory write exclusion; -deployments that need authenticated or write-proof local artifacts must add a -stronger mode such as a lease, fs-verity, or a verified artifact broker. +and regular-file descriptors, and before handoff rejects a change of the device, +inode, length, or modification time of the opened manifest, state, or memory +file, so renaming or replacing the original path cannot substitute another +generation. A change of their links, mode, or owner alone passes, which lets a +restore open a snapshot before the capture's source, as it tears down, removes +its RAM file's temporary name, the other link of an automatic `memory.bin`. +Linux file descriptors do not provide mandatory write exclusion; deployments +that need authenticated or write-proof local artifacts must add a stronger mode +such as a lease, fs-verity, or a verified artifact broker. ## Scratch (`scratch.img`) diff --git a/openvmm/openvmm_helpers/src/snapshot/fs.rs b/openvmm/openvmm_helpers/src/snapshot/fs.rs index 225366bc81d..820ec9fe4de 100644 --- a/openvmm/openvmm_helpers/src/snapshot/fs.rs +++ b/openvmm/openvmm_helpers/src/snapshot/fs.rs @@ -1010,6 +1010,13 @@ fn reject_windows_reparse_point( Ok(()) } +/// The generation of an opened file on Linux: its device, inode, length, and +/// modification time. It leaves out the change time, which a change of the +/// file's links, mode, or owner moves without changing its content. The source +/// of a capture with automatic RAM removes its RAM file's temporary name as it +/// tears down, after the commit, which moves the change time of the +/// `memory.bin` that links the same file, and a restore that opened the +/// snapshot before then must still find the generation that it opened. #[cfg(target_os = "linux")] #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(super) struct OpenedFileGeneration { @@ -1018,8 +1025,6 @@ pub(super) struct OpenedFileGeneration { length: u64, modified_seconds: i64, modified_nanoseconds: i64, - changed_seconds: i64, - changed_nanoseconds: i64, } #[cfg(windows)] @@ -1058,8 +1063,6 @@ pub(super) fn opened_file_generation( 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 index 4624ac983b1..56d7a720e39 100644 --- a/openvmm/openvmm_helpers/src/snapshot/restore.rs +++ b/openvmm/openvmm_helpers/src/snapshot/restore.rs @@ -402,11 +402,15 @@ fn validate_snapshot_directory( #[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; @@ -883,6 +887,63 @@ mod tests { ); } + /// A capture with automatic RAM publishes `memory.bin` as a hard link of + /// the RAM file that the source created beside the destination, and the + /// source removes that file's temporary name when it tears down, after the + /// commit. The removal changes `memory.bin`'s change time, but not its + /// identity, length, modification time, or content, so a restore that + /// opened the snapshot before the removal still maps the memory that it + /// opened. + #[cfg(target_os = "linux")] + #[test] + fn source_ram_removal_after_open_keeps_the_memory_generation() { + use std::os::unix::fs::MetadataExt; + + let dir = tempfile::tempdir().unwrap(); + let snap_dir = dir.path().join("snap"); + 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 mut ram_handle = ram.reopen().unwrap(); + ram_handle.write_all(&[0x5a_u8; 1024]).unwrap(); + write_snapshot_from_owned_memory_and_scratch_files( + &snap_dir, + &test_manifest(), + b"state", + &ram_handle, + None, + ) + .unwrap(); + let memory_path = snap_dir.join(MEMORY_FILE_NAME); + let published = std::fs::metadata(&memory_path).unwrap(); + assert_eq!(published.ino(), ram.as_file().metadata().unwrap().ino()); + assert_eq!(published.nlink(), 2); + + let snapshot = OpenedSnapshot::open(&snap_dir).unwrap(); + // Let the clock pass the recorded times, even on a file system that + // keeps them in whole seconds. + std::thread::sleep(std::time::Duration::from_secs(1)); + drop(ram); + + let unlinked = std::fs::metadata(&memory_path).unwrap(); + assert_eq!(unlinked.nlink(), 1); + assert_ne!( + (unlinked.ctime(), unlinked.ctime_nsec()), + (published.ctime(), published.ctime_nsec()) + ); + assert_eq!( + (unlinked.mtime(), unlinked.mtime_nsec()), + (published.mtime(), published.mtime_nsec()) + ); + snapshot.validate_memory_generation(1024).unwrap(); + let mut memory = snapshot.duplicate_memory_file_for_mapping(1024).unwrap(); + let mut bytes = Vec::new(); + memory.read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, vec![0x5a_u8; 1024]); + } + #[cfg(windows)] #[test] fn opened_snapshot_denies_mutation_until_guards_drop() {