formats: create atomic-write temps exclusively so a planted symlink is never followed (#502)

This commit is contained in:
Dylan M. Taylor
2026-11-14 17:23:06 -05:00
committed by GitHub
parent 73083d418a
commit 0fa3a392fd
2 changed files with 45 additions and 9 deletions
+43 -5
View File
@@ -112,15 +112,53 @@ pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
Ok(std::fs::read(path)?)
}
/// Write `bytes` to `path` atomically: `<path>.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(())
}
}
+2 -4
View File
@@ -101,11 +101,9 @@ pub fn save_project_bytes(pf: &ProjectFile) -> Result<Vec<u8>> {
/// Save atomically: `<path>.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)?;