From 7d24fbda627a2e107d8dfb347f9929d7dca63cb0 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 14 Oct 2026 15:07:57 +0200 Subject: [PATCH] fix(M2.2): honor read-only persistence directories Reject atomic replacement when the owned parent directory lacks its owner-write bit, even when a privileged worker could bypass normal DAC checks. Assert the explicit permission error and preservation of the previous document. Tests: cargo fmt --all -- --check; cargo check --workspace --all-targets --all-features --locked; cargo test --workspace --all-targets --all-features --locked; cargo test --workspace --doc --all-features --locked; cargo clippy --workspace --all-targets --all-features --locked -- -D warnings; cargo build --workspace --release --all-features --locked; cargo check --workspace --all-targets --no-default-features --locked; cargo test --workspace --all-targets --no-default-features --locked (all pass) --- config/src/files.rs | 17 +++++++++++++++++ config/tests/persistence.rs | 12 ++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/config/src/files.rs b/config/src/files.rs index 5948bec..95e8879 100644 --- a/config/src/files.rs +++ b/config/src/files.rs @@ -96,6 +96,7 @@ impl AtomicFile { ) })?; check_owned_not_symlink(parent)?; + check_owner_writable_directory(parent)?; if path.exists() { check_owned_not_symlink(path)?; } @@ -144,6 +145,22 @@ pub(crate) fn check_owned_not_symlink(path: &Path) -> Result<(), PersistenceErro Ok(()) } +fn check_owner_writable_directory(path: &Path) -> Result<(), PersistenceError> { + let metadata = fs::metadata(path) + .map_err(|source| PersistenceError::io("inspect directory permissions", path, source))?; + if metadata.permissions().mode() & 0o200 == 0 { + return Err(PersistenceError::io( + "create temporary file", + path, + io::Error::new( + io::ErrorKind::PermissionDenied, + "owner-write permission is not set on the directory", + ), + )); + } + Ok(()) +} + pub(crate) fn load_state(path: &Path) -> Result { let contents = match fs::read_to_string(path) { Ok(contents) => contents, diff --git a/config/tests/persistence.rs b/config/tests/persistence.rs index 49b9de1..6a336a4 100644 --- a/config/tests/persistence.rs +++ b/config/tests/persistence.rs @@ -6,8 +6,8 @@ use std::{ }; use fader_config::{ - AtomicFile, ConfigError, MigrationError, ProfileDocument, StateLoad, XdgEnvironment, XdgPaths, - migrate_profile, + AtomicFile, ConfigError, MigrationError, PersistenceError, ProfileDocument, StateLoad, + XdgEnvironment, XdgPaths, migrate_profile, }; use tempfile::tempdir; @@ -101,10 +101,14 @@ fn permission_failure_leaves_the_old_document_readable() { AtomicFile::write(&target, b"old").unwrap(); let directory = target.parent().unwrap(); fs::set_permissions(directory, fs::Permissions::from_mode(0o500)).unwrap(); - let result = AtomicFile::write(&target, b"replacement"); + let error = AtomicFile::write(&target, b"replacement").expect_err("directory is read-only"); fs::set_permissions(directory, fs::Permissions::from_mode(0o700)).unwrap(); - assert!(result.is_err()); + assert!(matches!( + error, + PersistenceError::Io { source, .. } + if source.kind() == io::ErrorKind::PermissionDenied + )); assert_eq!(fs::read(&target).unwrap(), b"old"); }