From ade0e47f4808303d70bb5ca4b08cb928bf7ab191 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 02:35:18 -0800 Subject: [PATCH] Scripts: refused writes create no file; Linux reads the casefold flag A refused rename that falls back to copying into the target no longer creates a missing target before the final authorization check. The target is created only after that check, on every platform, so a refusal at the final check leaves the path absent. An existing target is still opened with O_CREAT, so Linux's fs.protected_regular applies. imageCapture no longer treats an empty Linux folder as ignoring the Unicode normalization form. It reads the folder's casefold inode flag (FS_IOC_GETFLAGS) instead, so a missing linked cafe\u{301}.png no longer matches cafe\u{e9}.png in an ordinary folder, and a casefolded folder still matches both spellings. --- .../engine/src/script/dom/raster_capture.rs | 55 ++++++++++++++++++- crates/format/src/atomic.rs | 46 +++++++++++----- 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/script/dom/raster_capture.rs b/crates/engine/src/script/dom/raster_capture.rs index 38c8eb5437..b7385a6b94 100644 --- a/crates/engine/src/script/dom/raster_capture.rs +++ b/crates/engine/src/script/dom/raster_capture.rs @@ -347,7 +347,8 @@ fn same_file(a: &Placed, b: &Placed) -> bool { /// decomposed (NFD) forms, and that is not a symbolic link, is looked up in the other form, and /// names ignore the form when that finds the same entry. With no such entry, names ignore the form /// on macOS, whose volumes (APFS and HFS+) look names up that way, and on Linux in a folder that -/// ignores letter case (ext4 and f2fs casefolding also ignore the form), and keep it elsewhere. +/// carries the casefold inode flag ([`casefolded`]): ext4 and f2fs casefolding also ignores the +/// form. Elsewhere, and on Linux when the flag can't be read, names keep the form. fn ignores_normalization(folder: &std::path::Path) -> bool { use unicode_normalization::UnicodeNormalization; for entry in std::fs::read_dir(folder).into_iter().flatten().flatten().take(256) { @@ -358,7 +359,31 @@ fn ignores_normalization(folder: &std::path::Path) -> bool { return finds_same(&entry.path(), &folder.join(other)); } } - cfg!(target_os = "macos") || (cfg!(target_os = "linux") && ignores_case(folder)) + #[cfg(target_os = "linux")] + { + casefolded(folder) + } + #[cfg(not(target_os = "linux"))] + { + cfg!(target_os = "macos") + } +} + +/// `FS_CASEFOLD_FL` from Linux's `include/uapi/linux/fs.h`: the folder looks names up casefolded. +#[cfg(target_os = "linux")] +const FS_CASEFOLD_FL: u32 = 0x4000_0000; + +/// Whether the Linux folder `folder` carries the casefold inode flag, read with +/// `FS_IOC_GETFLAGS`. `false` when the folder can't be opened or its flags can't be read. +#[cfg(target_os = "linux")] +fn casefolded(folder: &std::path::Path) -> bool { + std::fs::File::open(folder).ok().and_then(|d| rustix::fs::ioctl_getflags(&d).ok()).is_some_and(|f| has_casefold(f.bits())) +} + +/// Whether the inode flags `bits` include `FS_CASEFOLD_FL`. +#[cfg(target_os = "linux")] +fn has_casefold(bits: u32) -> bool { + bits & FS_CASEFOLD_FL != 0 } /// The check `imageCapture` makes on the file it writes, `p`, given as written or as the real path @@ -640,4 +665,30 @@ mod source_tests { assert!(same_file(&target, &source)); }); } + + /// In an empty Linux folder, names that differ only in their normalization form are one file + /// only when the folder is casefolded. On a folder without the flag they are distinct, and + /// nothing is written into the folder to find out. + #[cfg(target_os = "linux")] + #[test] + fn names_in_another_normalization_form_match_only_in_a_casefolded_empty_folder() { + let dir = vectorcraft_testkit::TempDir::new("capture-nfd-distinct"); + let folder = std::fs::canonicalize(&*dir).unwrap(); + let (linked, capture): (Placed, Placed) = ((folder.clone(), "cafe\u{301}.png".into()), (folder.clone(), "caf\u{e9}.png".into())); + assert_eq!(same_file(&capture, &linked), casefolded(&folder)); + assert_eq!(std::fs::read_dir(&folder).unwrap().count(), 0); + } + + /// A casefolded folder, which looks a name up in either normalization form, is recognized by + /// its inode flag, and a folder that can't be read is not taken for one. + #[cfg(target_os = "linux")] + #[test] + fn a_casefolded_folder_is_recognized_by_its_inode_flag() { + assert!(has_casefold(FS_CASEFOLD_FL)); + assert!(has_casefold(FS_CASEFOLD_FL | 0x10)); + assert!(!has_casefold(0x10)); + assert!(!has_casefold(0)); + let dir = vectorcraft_testkit::TempDir::new("capture-nfd-missing"); + assert!(!casefolded(&dir.join("missing"))); + } } diff --git a/crates/format/src/atomic.rs b/crates/format/src/atomic.rs index 30c55f3cc0..83901874fd 100644 --- a/crates/format/src/atomic.rs +++ b/crates/format/src/atomic.rs @@ -473,8 +473,17 @@ fn write_over(tmp: &Path, target: &Path, refused: io::Error, check: &dyn Fn() -> remove_tmp: &|| { let _ = std::fs::remove_file(tmp); }, - open_target: &|write| if write { OpenOptions::new().write(true).create(true).truncate(false).open(target) } else { File::open(target) }, - #[cfg(unix)] + // A missing target is created by `create_target`, after the last check. An existing one is + // opened with `O_CREAT` as before, so Linux's `fs.protected_regular` still applies to it. + open_target: &|write| { + if !write { + return File::open(target); + } + if matches!(std::fs::symlink_metadata(target), Err(e) if e.kind() == io::ErrorKind::NotFound) { + return Err(io::ErrorKind::NotFound.into()); + } + OpenOptions::new().write(true).create(true).truncate(false).open(target) + }, create_target: &|| create_new(target), synced: &|| sync_written_parent(target), still_allowed: check, @@ -497,10 +506,9 @@ struct Over<'a> { /// Best effort. remove_tmp: &'a dyn Fn(), /// Opens the target without truncating it for writing when `true`, or for reading when `false`. - /// Checked Unix writes defer creation of a missing target to `create_target`. + /// It never creates a missing target: `create_target` does that, after the final check. open_target: &'a dyn Fn(bool) -> io::Result, /// Creates a missing target after the final check and fails if its name is taken. - #[cfg(unix)] create_target: &'a dyn Fn() -> io::Result, /// The target's folder flushed, after the target is written. synced: &'a dyn Fn() -> io::Result<()>, @@ -510,7 +518,7 @@ struct Over<'a> { /// [`write_over`] through `over`. fn write_over_in(over: &Over<'_>, refused: io::Error) -> io::Result<()> { - // Checked Unix writes create a missing target only after the final check. + // A missing target is created only after the final check. if let Err(e) = (over.still_allowed)() { (over.remove_tmp)(); return Err(e); @@ -518,7 +526,6 @@ fn write_over_in(over: &Over<'_>, refused: io::Error) -> io::Result<()> { let opened = (over.open_tmp)().and_then(|from| { let to = match (over.open_target)(true) { Ok(to) => Some(to), - #[cfg(unix)] Err(e) if e.kind() == io::ErrorKind::NotFound => None, Err(e) => return Err(e), }; @@ -563,7 +570,6 @@ fn write_over_in(over: &Over<'_>, refused: io::Error) -> io::Result<()> { } let mut to = match to { Some(to) => to, - #[cfg(unix)] None => match (over.create_target)() { Ok(to) => to, // A file put at the name meanwhile is left alone, and reported as there. @@ -576,11 +582,6 @@ fn write_over_in(over: &Over<'_>, refused: io::Error) -> io::Result<()> { return Err(refused); } }, - #[cfg(not(unix))] - None => { - (over.remove_tmp)(); - return Err(refused); - } }; let copied = to.set_len(0).and_then(|()| io::copy(&mut from, &mut to)).and_then(|_| sync_file(&to)); drop((from, to)); @@ -1116,6 +1117,26 @@ mod tests { let _ = std::fs::remove_dir_all(d); } + /// A refused rename onto a missing file that is no longer allowed at the final check does not + /// create the file. + #[test] + fn a_refused_rename_does_not_create_a_missing_file_once_no_longer_allowed() { + let d = dir("no-rename-missing-disallowed"); + let (path, tmp) = (d.join("doc.txt"), d.join("doc.tmp")); + std::fs::write(&tmp, b"new").unwrap(); + let checks = std::cell::Cell::new(0); + let check = || { + checks.set(checks.get() + 1); + if checks.get() == 1 { Ok(()) } else { Err(io::Error::other("moved outside")) } + }; + let e = write_over(&tmp, &path, denied::<()>().unwrap_err(), &check).unwrap_err(); + assert_eq!(checks.get(), 2); + assert!(e.to_string().contains("moved outside"), "{e}"); + assert!(!path.exists(), "the refused write created {}", path.display()); + assert!(!tmp.exists()); + let _ = std::fs::remove_dir_all(d); + } + /// A refused rename whose folder is no longer allowed by the time the file would be overwritten /// leaves the file as it was, and neither copy stays behind. #[test] @@ -1137,7 +1158,6 @@ mod tests { let _ = std::fs::remove_file(&tmp); }, open_target: &|write| if write { OpenOptions::new().write(true).open(&path) } else { File::open(&path) }, - #[cfg(unix)] create_target: &|| create_new(&path), synced: &|| Ok(()), still_allowed: &|| {