Repository navigation
Description
Outside Linux glibc builds, snapshot publication's final rename replaces a destination directory that another process created, empty, after the publisher's last check that the destination is absent. The publisher reports success, although write_snapshot documents that the final directory must not already exist. Whatever the other process then writes into its directory lands in the published snapshot. It affects NVX's MSHV build (static musl) and WHP build (Windows), as well as macOS and the BSDs. NVX's KVM build (Linux glibc) uses renameat2(RENAME_NOREPLACE) and is not affected. Only a host process that creates the empty directory in that window triggers it; a directory with content is never replaced.
Reproduction
On a musl or Windows build, create the destination as an empty directory after the ensure_path_absent in write_snapshot_with_memory_publication and before its staging.publish (for example while the publisher is stopped at a breakpoint there). Publication should fail and leave the directory alone. Instead the rename succeeds and the snapshot takes the directory's place. No musl or Windows build was run; the fallback's body, run on a Linux glibc host, replaced an empty destination this way, and the process that had created it then wrote into the published snapshot.
Cause
StagingDirectory::publish (openvmm/openvmm_helpers/src/snapshot/publish.rs) commits with rename_no_replace, whose fallback for every target other than Linux glibc is a plain rename:
// openvmm/openvmm_helpers/src/snapshot/fs.rs
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
pub(super) fn rename_no_replace(source: &Path, destination: &Path) -> anyhow::Result<()> {
fs_err::rename(source, destination)?;
Ok(())
}POSIX rename replaces an empty destination directory. std::fs::rename documents the same behaviour on Windows 10 1607 and later, on filesystems that support FileRenameInfoEx such as NTFS, because it retries a denied MoveFileExW with POSIX rename semantics. Checking for the destination again before the rename cannot close the window.
Fix
No patch is attached. The recommended fix gives each target an atomic no-replace rename, which the filesystem refuses if the destination exists:
- Linux musl (MSHV build): call the syscall directly,
libc::syscall(libc::SYS_renameat2, .., libc::RENAME_NOREPLACE).nixprovidesrenameat2only for glibc;libcis already a Unix dependency ofopenvmm_helpers. - Windows (WHP build):
MoveFileExWwithoutMOVEFILE_REPLACE_EXISTING, which fails if the destination exists.windows-syswithWin32_Storage_FileSystemis already a Windows dependency ofopenvmm_helpers. - Other targets: macOS has
renamex_np(.., RENAME_EXCL); anything else should fail publication with an "unsupported" error rather than fall back to a plain rename.
Activity
openvmm_helpers: don't publish a snapshot over an empty directory
openvmm: pin no-replace snapshot publication on musl and Windows
openvmm_helpers: don't publish a snapshot over an empty directory