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() {