From 0a3b9c3ba5e3a7f875c7732ba325c92771dfdc07 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 9 Oct 2026 16:24:58 +0200 Subject: [PATCH] mind-body: the writer creates the control store 0755 and 0644 whatever the umask Soul s1: created under umask 000 the directory was 777 and the files 666, so a non-root user could write the store. The writer now sets the directory (when it creates it), the lock file and the store file explicitly, after the setter's own refusals so a refused write still creates nothing. CultCache stages with the process umask and offers no mode, so the store file is set after the rename. Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/control.rs | 61 ++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/crates/mind-body/src/control.rs b/crates/mind-body/src/control.rs index 3806d98..d0b0282 100644 --- a/crates/mind-body/src/control.rs +++ b/crates/mind-body/src/control.rs @@ -199,20 +199,72 @@ pub fn read_effective(source: &impl ControlSource) -> Effective { /// first write. pub struct ControlWriter { cache: CultCache, + path: PathBuf, +} + +/// The modes the store is created with, whatever the operator's umask: the +/// directory and the files under it are world-readable and writable by their +/// owner alone, which is what makes the filesystem the write authority. +const DIR_MODE: u32 = 0o755; +const FILE_MODE: u32 = 0o644; + +#[cfg(unix)] +fn set_mode(path: &Path, mode: u32) -> Result<()> { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)) + .with_context(|| format!("failed to set the mode of {}", path.display())) +} + +#[cfg(not(unix))] +fn set_mode(_path: &Path, _mode: u32) -> Result<()> { + Ok(()) } impl ControlWriter { pub fn open(path: impl Into) -> Result { + let path = path.into(); let mut cache = registered_cache()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(path))?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path))?; cache.pull_all_backing_stores()?; - Ok(Self { cache }) + Ok(Self { cache, path }) + } + + /// Before the first write: the control directory exists at 0755 and the + /// lock file at 0644, set explicitly because creation takes the umask. + /// Called after a setter's own refusals, so a refused write creates nothing. + fn prepare(&self) -> Result<()> { + let directory = self.path.parent().context("control path has no directory")?; + if !directory.exists() { + std::fs::create_dir_all(directory).with_context(|| format!("failed to create {}", directory.display()))?; + set_mode(directory, DIR_MODE)?; + } + let mut lock_name = self.path.file_name().context("control path has no file name")?.to_os_string(); + lock_name.push(".lock"); + let lock = self.path.with_file_name(lock_name); + if !lock.exists() { + std::fs::OpenOptions::new() + .create(true) + .write(true) + .truncate(false) + .open(&lock) + .with_context(|| format!("failed to open {}", lock.display()))?; + set_mode(&lock, FILE_MODE)?; + } + Ok(()) + } + + /// After a write: the store file the atomic rename put in place takes the + /// umask-free mode. CultCache stages with the process umask and offers no + /// way to pass a mode, so this is where the mode can be set. + fn settle(&self) -> Result<()> { + set_mode(&self.path, FILE_MODE) } pub fn set_brake(&mut self, released: bool, set_at: DateTime, set_by: &str) -> Result<()> { let brake = Brake { released, set_at, set_by: set_by.to_string() }; + self.prepare()?; self.cache.put(BRAKE_KEY, &brake)?; - Ok(()) + self.settle() } /// Refuses an out-of-bounds dial before anything is written, so the store @@ -225,7 +277,8 @@ impl ControlWriter { heat_max() ); } + self.prepare()?; self.cache.put(DIAL_KEY, &dial)?; - Ok(()) + self.settle() } }