fix(M2.2): honor read-only persistence directories
Rust CI fader_master passed CI.
MadGobblins/fader_master/pipeline/head This commit looks good Details

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)
This commit is contained in:
Dandaedre 2026-10-14 15:07:57 +02:00
parent 81241452fb
commit 7d24fbda62
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
2 changed files with 25 additions and 4 deletions

View File

@ -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<StateLoad, PersistenceError> {
let contents = match fs::read_to_string(path) {
Ok(contents) => contents,

View File

@ -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");
}