From 40a6682e11ec05cccd06b3f3ac82db59adb5b682 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 03:15:48 +0200 Subject: [PATCH] Projection: create the staged file with its final mode, drop the post-rename chmod CultCache stages a temp file and renames it over the store; the temp file is created under Idunn's UMask=027, so the visible projection was 0640 until publish_projection_mode chmodded it after the rename and the directory fsync. A reader running as another user (Odin) failed with EACCES in that window. The store write now runs on a helper thread that unshares CLONE_FS and sets umask 022, so the staged file and the .lock are created 0644 and never visible in another mode; other threads keep 027. publish_projection_mode is deleted. CultCache has no mode parameter, so this lives in Idunn. The ACL note in migration.md now says the ACL is no longer required. Test: a reader polls the directory across 300 publishes and records the mode of every store and lock inode it sees; all must be 0644. Co-Authored-By: Claude Sonnet 5.5 --- docs/migration.md | 2 +- src/drivers.rs | 137 +++++++++++++++++++++++++++++++++------------- 2 files changed, 100 insertions(+), 39 deletions(-) diff --git a/docs/migration.md b/docs/migration.md index 0c810fc..a37faa3 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -520,7 +520,7 @@ started, ran, and then died on a file it could not open: | `runtime_root` and every directory above it | traversable by the workload: `o+x`, or group-owned by the state group with `g+x` | the workload is a `DynamicUser` and owns nothing, and `ReadOnlyPaths=` binds the bundle's leaf into the namespace without granting traversal above it. Idunn now checks the whole chain and names the closed directory | | `runtime_root` | setgid, group-owned by the state group (`2750 root:`) | `harden_root_authority_file` requires the write-lease record's group to equal its parent's, and only setgid makes a newly created record inherit it. Without it the record lands `root:root` and the workload cannot read its own lease | | the write-lease record | `root:`, mode `0640` | follows from the setgid parent; the workload reads it through the state group. Idunn writes it, so fixing an existing wrong-group record means deleting it, and that is safe only when no transaction is live | -| `/var/lib/gamecult/idunn-projection` | default ACL `u::rw-,g::r--,o::r--` | Idunn runs with `UMask=027`, so each publish creates the store and its `.lock` at `0640`. The code re-opens them after every write, but that leaves a window; a default ACL sets the mode at creation and closes it | +| `/var/lib/gamecult/idunn-projection` | default ACL `u::rw-,g::r--,o::r--` (no longer required) | Idunn runs with `UMask=027`, but writes the projection on a thread with umask `022`, so the staged file and its `.lock` are created `0644` and are never visible in another mode. The ACL is redundant defense in depth; a rebuilt host works without it | | `/etc/nginx/idunn-stream-routes` | `root:root`, not group-writable | "route authority parent is not canonical root-owned and nonwritable". It was `root:idunn 0775` from the generation where Idunn ran unprivileged and needed to write it as a group member | Two shapes are worth stating plainly because they are easy to get backwards. diff --git a/src/drivers.rs b/src/drivers.rs index 8743b94..e44356e 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -4395,8 +4395,7 @@ impl CultCacheTopologyDriver { let Some(replacement) = mutate(&entries)? else { return Ok(()); }; - if store.compare_exchange_snapshot(&entries, &replacement)? { - publish_projection_mode(&self.projection_store)?; + if with_published_umask(|| store.compare_exchange_snapshot(&entries, &replacement))? { return Ok(()); } } @@ -7582,25 +7581,37 @@ fn ensure_bundle_is_reachable_by_workload( /// reads it to verify its own Expected incarnation against the Idunn anchor. /// /// Idunn runs with `UMask=027`, which is right for its private state and wrong -/// for this one file -- it lands `0640 root:root`, and a `DynamicUser` workload -/// gets EACCES. A chmod by hand does not hold, because each publish writes a -/// new file. Integrity here comes from the signatures over the records, not -/// from the mode, so the published copy is readable. -fn publish_projection_mode(path: &Path) -> Result<()> { - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - // The lock sibling too: CultCache opens it alongside the store, so a - // 0640 lock denies the read just as surely as a 0640 store, and it is - // created fresh under Idunn's umask on every publish. - for path in [path.to_path_buf(), authority_lock_path(path)] { - if path.exists() { - fs::set_permissions(&path, fs::Permissions::from_mode(0o644)) - .with_context(|| format!("publishing {}", path.display()))?; - } - } - } - Ok(()) +/// for this one file and its `.lock` sibling: they would land `0640 root:root` +/// and a `DynamicUser` workload gets EACCES. CultCache stages a temporary file +/// and renames it over the store, so the mode must be right when the staged +/// file is *created*; a chmod after the rename leaves a window in which the +/// visible file is unreadable. Integrity here comes from the signatures over +/// the records, not from the mode, so the published copy is readable. +/// +/// `action` runs on a helper thread with its own umask (`unshare(CLONE_FS)`), +/// so files it creates are `0644` while the umask of every other thread in +/// Idunn stays `0027`. CultCache has no mode parameter to do this itself. +#[cfg(target_os = "linux")] +fn with_published_umask(action: impl FnOnce() -> Result + Send) -> Result { + std::thread::scope(|scope| { + scope + .spawn(|| { + // SAFETY: both calls only touch this thread's filesystem attributes. + if unsafe { libc::unshare(libc::CLONE_FS) } != 0 { + return Err(anyhow::Error::from(std::io::Error::last_os_error()) + .context("giving the projection writer its own umask")); + } + unsafe { libc::umask(0o022) }; + action() + }) + .join() + .unwrap_or_else(|_| bail!("projection writer thread panicked")) + }) +} + +#[cfg(not(target_os = "linux"))] +fn with_published_umask(action: impl FnOnce() -> Result) -> Result { + action() } fn build_machine_id(workspace: &Path) -> Result { @@ -9532,27 +9543,77 @@ Content-Le".to_vec()), |_| {}), )); } - #[cfg(unix)] + /// Runs `body` on a thread whose umask is Idunn's `027`, without touching + /// the umask of the test process. + #[cfg(target_os = "linux")] + fn on_thread_with_umask_027(body: impl FnOnce() -> T + Send) -> T { + std::thread::scope(|scope| { + scope + .spawn(|| { + assert_eq!(unsafe { libc::unshare(libc::CLONE_FS) }, 0); + unsafe { libc::umask(0o027) }; + body() + }) + .join() + .unwrap() + }) + } + + #[cfg(target_os = "linux")] #[test] - fn the_published_projection_is_readable_by_a_workload() { - use std::os::unix::fs::PermissionsExt; + fn the_published_projection_is_never_visible_in_another_mode() { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + use std::sync::atomic::{AtomicBool, Ordering}; let temp = tempfile::tempdir().unwrap(); - let path = temp.path().join("topology.cc"); - std::fs::write(&path, b"x").unwrap(); - // What Idunn's UMask=027 leaves behind. - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o640)).unwrap(); + let store = temp.path().join("topology.cc"); let lock = temp.path().join("topology.cc.lock"); - std::fs::write(&lock, b"").unwrap(); - std::fs::set_permissions(&lock, std::fs::Permissions::from_mode(0o640)).unwrap(); - publish_projection_mode(&path).unwrap(); - let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777; - assert_eq!( - mode, 0o644, - "every target must be able to read the projection" - ); - let lock_mode = std::fs::metadata(&lock).unwrap().permissions().mode() & 0o777; - assert_eq!(lock_mode, 0o644, "the lock is opened alongside the store"); + let done = AtomicBool::new(false); + let (observed, ()) = std::thread::scope(|scope| { + // A reader in another process sees only what the directory shows: + // record the mode of every store and lock inode it can open. + let reader = scope.spawn(|| { + let mut seen = std::collections::BTreeSet::new(); + while !done.load(Ordering::Acquire) { + for path in [&store, &lock] { + if let Ok(meta) = fs::metadata(path) { + seen.insert((path.clone(), meta.ino(), meta.mode() & 0o777)); + } + } + } + seen + }); + let writes = on_thread_with_umask_027(|| { + let store_backing = SingleFileMessagePackBackingStore::new(&store); + let mut current: Vec = Vec::new(); + for round in 0..300u32 { + let mut next = current.clone(); + next.push(CultCacheEnvelope { + key: format!("k{round}"), + r#type: "probe".into(), + payload: vec![round as u8], + stored_at: "2026-09-30T00:00:00Z".into(), + schema_id: None, + }); + with_published_umask(|| store_backing.compare_exchange_snapshot(¤t, &next)) + .unwrap(); + current = next; + } + }); + done.store(true, Ordering::Release); + (reader.join().unwrap(), writes) + }); + assert!(!observed.is_empty(), "the reader never saw the store"); + for (path, _, mode) in &observed { + assert_eq!( + *mode, + 0o644, + "{} was visible with mode {mode:o}", + path.display() + ); + } + let final_mode = fs::metadata(&store).unwrap().permissions().mode() & 0o777; + assert_eq!(final_mode, 0o644); } #[cfg(unix)]