mind-body: the binary restricts its umask; the writer refuses unsafe installs
Soul s2: under a permissive umask the store was group/world-writable between CultCache's rename and the chmod, and an fd opened there survived it. The mind-control binary now sets umask 077 first (the library never touches the umask). The writer refuses to write through an existing directory, lock or store that is not root's or is group/world-writable, with the typed UnsafeStore error naming path, mode and owner. Tests: a non-root open-for-write probe against the real binary under umask 000; refusals for 0777/0775 directories, 0666/0664 locks and stores, and a non-root-owned directory; accepted root installs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2377fa6d40
commit
5cccab6899
6 changed files with 231 additions and 12 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//! The `mind-control` command line: the operator's hand on one instance's
|
||||
//! control store. It performs no permission check of its own: the file is
|
||||
//! control store. It checks no caller identity: the file is
|
||||
//! root-owned and the filesystem is the authority.
|
||||
//!
|
||||
//! ```text
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@
|
|||
//! The reader opens the file read-only through CultCache's read-only snapshot
|
||||
//! door: it takes no lock and creates no sibling file, so the units she runs as
|
||||
//! need no write authority anywhere under `/etc`. Writing is the operator's;
|
||||
//! the filesystem is the authority and nothing here checks permission.
|
||||
//! the filesystem is the authority. The writer only refuses to write through an
|
||||
//! existing directory, lock or store that is not root's or that group or world
|
||||
//! can write (`UnsafeStore`); it checks no caller identity.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
|
@ -215,6 +217,45 @@ fn set_mode(path: &Path, mode: u32) -> Result<()> {
|
|||
.with_context(|| format!("failed to set the mode of {}", path.display()))
|
||||
}
|
||||
|
||||
/// A control path the writer will not write through: it is not root's, or
|
||||
/// group or world can write it. Names the path, its mode and its owner.
|
||||
#[derive(Debug)]
|
||||
pub struct UnsafeStore {
|
||||
pub path: PathBuf,
|
||||
pub mode: u32,
|
||||
pub uid: u32,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for UnsafeStore {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"refusing to write: {} is mode {:04o} owned by uid {}; it must be owned by root (uid 0) and not group- or world-writable",
|
||||
self.path.display(),
|
||||
self.mode,
|
||||
self.uid
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for UnsafeStore {}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn require_root_only_writable(path: &Path) -> Result<()> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let meta = std::fs::metadata(path).with_context(|| format!("failed to inspect {}", path.display()))?;
|
||||
let (mode, uid) = (meta.mode() & 0o7777, meta.uid());
|
||||
if uid != 0 || mode & 0o022 != 0 {
|
||||
return Err(UnsafeStore { path: path.to_path_buf(), mode, uid }.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn require_root_only_writable(_path: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn set_mode(_path: &Path, _mode: u32) -> Result<()> {
|
||||
Ok(())
|
||||
|
|
@ -231,16 +272,24 @@ impl ControlWriter {
|
|||
|
||||
/// 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.
|
||||
/// A directory, lock or store that already exists must be root's and not
|
||||
/// group- or world-writable, else the write is refused (`UnsafeStore`): the
|
||||
/// operator fixes the install. 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")?;
|
||||
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);
|
||||
for existing in [directory, lock.as_path(), self.path.as_path()] {
|
||||
if existing.exists() {
|
||||
require_root_only_writable(existing)?;
|
||||
}
|
||||
}
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,26 @@
|
|||
//! `mind-control`: the operator's hand on a Mind's brake and burn-rate dial.
|
||||
//! All behaviour is `mind_body::cli`; this reads the process's environment and
|
||||
//! clock, the two things the library never reads.
|
||||
//! clock, the two things the library never reads, and owns the process umask.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/// CultCache stages the store file with the process umask and offers no mode,
|
||||
/// so the umask is what keeps a staged file from being group- or world-writable
|
||||
/// for the instant between its rename and the writer's chmod. The binary owns
|
||||
/// its process state; the library never touches the umask.
|
||||
#[cfg(unix)]
|
||||
fn restrict_umask() {
|
||||
// SAFETY: umask only sets this process's file-creation mask.
|
||||
unsafe {
|
||||
libc::umask(0o077);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn restrict_umask() {}
|
||||
|
||||
fn main() {
|
||||
restrict_umask();
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let set_by = mind_body::cli::operator_name(std::env::var("SUDO_USER").ok(), std::env::var("USER").ok());
|
||||
let result = mind_body::cli::run(
|
||||
|
|
|
|||
Loading…
Reference in a new issue