Skip to content

openvmm: snapshot publication can replace an empty directory on musl and Windows #1362

Closed

Description

@tangruize

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). nix provides renameat2 only for glibc; libc is already a Unix dependency of openvmm_helpers.
  • Windows (WHP build): MoveFileExW without MOVEFILE_REPLACE_EXISTING, which fails if the destination exists. windows-sys with Win32_Storage_FileSystem is already a Windows dependency of openvmm_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.

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