From 0fa3a392fd61b3e02a86e8136bfbaa178be76100 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 17:23:06 -0500 Subject: [PATCH] formats: create atomic-write temps exclusively so a planted symlink is never followed (#502) --- crates/formats/src/lib.rs | 48 +++++++++++++++++++++++--- crates/formats/src/project_file/mod.rs | 6 ++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/crates/formats/src/lib.rs b/crates/formats/src/lib.rs index d8550531..3708d737 100644 --- a/crates/formats/src/lib.rs +++ b/crates/formats/src/lib.rs @@ -112,15 +112,53 @@ pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result> { Ok(std::fs::read(path)?) } -/// Write `bytes` to `path` atomically: `.tmp` first, then rename over the target. +/// Create a new temporary file beside `path`, refusing to reuse an existing (possibly symlinked) name. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn create_temp(path: &std::path::Path) -> Result<(std::path::PathBuf, std::fs::File)> { + let parent = path.parent().unwrap_or_else(|| std::path::Path::new(".")); + let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("out"); + for attempt in 0..100u32 { + let tmp = parent.join(format!(".{name}.{}.{attempt}.tmp", std::process::id())); + match std::fs::OpenOptions::new().write(true).create_new(true).open(&tmp) { + Ok(file) => return Ok((tmp, file)), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e.into()), + } + } + Err(FormatError::Io(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "could not create a temporary file"))) +} + +/// Write `bytes` to `path` atomically: a new temporary file first, then rename over the target. #[cfg(not(target_arch = "wasm32"))] pub fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> { - let mut tmp = path.as_os_str().to_owned(); - tmp.push(".tmp"); - let tmp = std::path::PathBuf::from(tmp); - let res = std::fs::write(&tmp, bytes).and_then(|_| std::fs::rename(&tmp, path)); + use std::io::Write; + let (tmp, mut file) = create_temp(path)?; + let res = file.write_all(bytes).and_then(|_| file.sync_all()); + drop(file); + let res = res.and_then(|_| std::fs::rename(&tmp, path)); if res.is_err() { let _ = std::fs::remove_file(&tmp); } Ok(res?) } + +#[cfg(all(test, unix))] +mod atomic_tests { + use super::write_atomic; + + #[test] + fn write_atomic_does_not_follow_a_planted_temp_symlink() -> std::io::Result<()> { + let dir = std::env::temp_dir().join(format!("tc-atomic-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir)?; + let target = dir.join("out.png"); + let victim = dir.join("victim.txt"); + std::fs::write(&victim, b"safe")?; + std::os::unix::fs::symlink(&victim, dir.join("out.png.tmp"))?; + write_atomic(&target, b"new").map_err(std::io::Error::other)?; + assert_eq!(std::fs::read(&victim)?, b"safe"); + assert_eq!(std::fs::read(&target)?, b"new"); + let _ = std::fs::remove_dir_all(&dir); + Ok(()) + } +} diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs index 8390220a..e982d761 100644 --- a/crates/formats/src/project_file/mod.rs +++ b/crates/formats/src/project_file/mod.rs @@ -101,11 +101,9 @@ pub fn save_project_bytes(pf: &ProjectFile) -> Result> { /// Save atomically: `.tmp` first, then rename over `path`. #[cfg(not(target_arch = "wasm32"))] pub fn save_project(path: &std::path::Path, pf: &ProjectFile) -> Result<()> { - let mut tmp = path.as_os_str().to_owned(); - tmp.push(".tmp"); - let tmp = std::path::PathBuf::from(tmp); + let (tmp, file) = crate::create_temp(path)?; let res = (|| -> Result<()> { - let mut file = std::io::BufWriter::new(std::fs::File::create(&tmp)?); + let mut file = std::io::BufWriter::new(file); write_project(&mut file, pf)?; file.into_inner().map_err(|e| e.into_error())?.sync_all()?; std::fs::rename(&tmp, path)?;