From ae13fdc0608bf57b91e0f6bdff4daaf1df06a68b Mon Sep 17 00:00:00 2001 From: Sergio Demian Lerner Date: Sat, 21 Nov 2026 12:53:21 +0000 Subject: [PATCH] Do not let the node's umask decide who can read a segment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both of these were found by enabling segbuild on the mainnet node and reading what it actually did, which is the only way either would have surfaced. The node runs under `UMask=0077`. Sensible hardening, and the builder inherited it, so the first segment it produced was `drwx--S---`: correct, verified, and openable by nothing but the node. drwx--S--- 909-9420001-9429888 <- built by the node -rw------- BOUNDARIES -rw------- BUILT drwxrwsr-x 055-5849091-5895185 <- built offline, under a normal umask That is the worst shape a failure can take here, because it is silent and it looks like something else. The build log says 9,888 blocks and 9,888 state roots verified, which is true. A consumer then gets a permission error, which looks nothing like "this segment is fine, you just cannot read it" -- and `du` reports 1 MB, because it cannot descend. The set exists to be read by other processes, so the mode is now set explicitly after the rename rather than inherited: 0755 for directories, 0644 for files, and the same for the logs, which are operational state a human and the offline tools read. Loosening the unit's umask would have been the wrong fix; the node's hardening is not the problem. Best-effort and not fatal: a segment that is correct but awkwardly permissioned is still correct, so a failure logs the `chmod` that fixes it and carries on. Unix only, gated, because CI builds this on Windows too. The second half is the same defect one layer up. The node started with the set owned by the user who had built it offline, could not append `BOUNDARIES`, said so once: WARN segbuild: could not record the catch-up boundary at #9429867: opening /var/lib/rustock/segbuild/BOUNDARIES: Permission denied and then had nothing further to say, because the next attempt would have come a gigabyte of writes later -- about a month. The feature was inert and the log looked healthy. So startup now probes the directory by writing a file and removing it, and refuses with the `chown`/`chmod`/`setgid` commands that fix it. That is §7.2's rule: "the set is current" is not the same as "this node can extend it", and a config error that makes the feature impossible belongs at startup, before anything is serving, not in a single warning. With both in place the node closed its 10,888-block coverage gap in 108 seconds at 92 blk/s, verifying every state root, into a 232 MB segment recorded against the chain's own hash at #9,429,888. Co-Authored-By: Claude Opus 5 --- crates/cli/src/main.rs | 29 ++++++++ crates/execution/src/segment_build.rs | 88 +++++++++++++++++++++++ crates/storage/src/segbuild_boundaries.rs | 18 +++++ 3 files changed, 135 insertions(+) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 3bd9c778..675f2b05 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3177,6 +3177,35 @@ async fn run(local_offset: Option) -> Result<()> { dir, args.segbuild_target_mb * (1 << 20), )?); + // Probe the directory rather than trust it. The node runs with + // `UMask=0077` and the set may be owned by whoever built it + // offline, so "the set is current" is not the same as "this node + // can extend it". §7.2's rule applies: a config error that makes + // the feature impossible is loud at startup, before anything is + // serving. + // + // Not hypothetical. On 2026-11-21 this node started with the set + // owned by another user, could not append `BOUNDARIES`, said so + // once, and then had nothing further to say -- the next attempt + // would have been a gigabyte of writes later, about a month. The + // feature was inert and the log looked fine. + let probe = dir.join(".writable-probe"); + std::fs::write(&probe, b"") + .and_then(|()| std::fs::remove_file(&probe)) + .map_err(|e| { + anyhow::anyhow!( + "--segbuild-out {} is not writable by this node ({e}). The segment \ + set is often owned by whoever built it offline, while the node \ + runs as its own user. Give the node ownership, or group write with \ + setgid so both keep access:\n chown -R {}: {}\n chmod -R \ + g+w {}\n find {} -type d -exec chmod g+s {{}} +", + dir.display(), + std::env::var("USER").unwrap_or_else(|_| "".into()), + dir.display(), + dir.display(), + dir.display() + ) + })?; if let Some(reason) = boundaries.halted() { warn!("segbuild is halted and will build nothing: {reason}"); } diff --git a/crates/execution/src/segment_build.rs b/crates/execution/src/segment_build.rs index 2db3bb50..a143faaf 100644 --- a/crates/execution/src/segment_build.rs +++ b/crates/execution/src/segment_build.rs @@ -111,6 +111,60 @@ pub struct BuildOutcome { pub end_hash: [u8; 32], } +/// Make a finished segment readable by whatever will consume it. +/// +/// The node runs with `UMask=0077` -- sensible hardening for a node -- so +/// everything it creates is owner-only. A segment nothing but the node can open +/// defeats the entire purpose of the set, and the failure is silent: the build +/// verifies, the log says so, and a consumer gets a permission error that looks +/// nothing like "this segment is fine, you just cannot read it". +/// +/// So the mode is set explicitly rather than inherited. Offline builds produce +/// `drwxr-xr-x`/`-rw-r--r--` because the tool runs under a normal umask, and +/// this makes the node's output match, so a consumer cannot tell which built +/// which. +/// +/// Best-effort: a segment that is correct but awkwardly permissioned is still +/// a correct segment, so a failure here is logged and not fatal. +#[cfg(unix)] +fn relax_permissions(dir: &Path) { + use std::os::unix::fs::PermissionsExt; + + fn walk(path: &Path, depth: u32) -> std::io::Result<()> { + // Segment stores are two levels deep (`/sealed/*.sst`); the + // bound is only here so a symlink loop cannot spin. + if depth > 8 { + return Ok(()); + } + let meta = std::fs::symlink_metadata(path)?; + if meta.file_type().is_symlink() { + return Ok(()); + } + let mode = if meta.is_dir() { 0o755 } else { 0o644 }; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))?; + if meta.is_dir() { + for entry in std::fs::read_dir(path)? { + walk(&entry?.path(), depth + 1)?; + } + } + Ok(()) + } + + if let Err(e) = walk(dir, 0) { + warn!( + target: "rustock::segbuild", + "Built {} but could not make it readable ({e}); a consumer running as another \ + user will get a permission error rather than data. Fix with \ + `chmod -R a+rX {}`.", + dir.display(), + dir.display() + ); + } +} + +#[cfg(not(unix))] +fn relax_permissions(_dir: &Path) {} + /// The directory a build in progress uses. Deliberately not parseable as /// `NNN-start-end`, so no reader can mistake it for a segment. pub fn pending_dir(out_root: &Path, start: u64, end: u64) -> PathBuf { @@ -395,6 +449,9 @@ pub fn build_range( std::fs::rename(&pending, &final_dir).with_context(|| { format!("renaming {} to {}", pending.display(), final_dir.display()) })?; + // After the rename, so a half-built directory is never briefly readable as + // a segment, and before the log line, so "Built" means "built and usable". + relax_permissions(&final_dir); let secs = t0.elapsed().as_secs_f64(); info!( @@ -669,6 +726,37 @@ mod tests { assert_eq!(next_index(d.path()).unwrap(), 909); } + /// A segment nothing but the node can open is useless, and the failure is + /// silent: the build verifies, the log says so, and a consumer gets a + /// permission error that looks nothing like "this segment is fine". + #[cfg(unix)] + #[test] + fn a_finished_segment_is_readable_by_a_consumer() { + use std::os::unix::fs::PermissionsExt; + let d = tempfile::tempdir().unwrap(); + let seg = d.path().join("909-1-2"); + std::fs::create_dir_all(seg.join("sealed")).unwrap(); + std::fs::write(seg.join("sealed").join("000001.sst"), b"nodes").unwrap(); + + // What `UMask=0077` leaves behind, which is what the mainnet node runs. + for p in [seg.join("sealed").join("000001.sst"), seg.join("sealed"), seg.clone()] { + std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o700)).unwrap(); + } + + relax_permissions(&seg); + + let mode = |p: &std::path::Path| { + std::fs::metadata(p).unwrap().permissions().mode() & 0o777 + }; + assert_eq!(mode(&seg), 0o755, "the segment directory must be enterable"); + assert_eq!(mode(&seg.join("sealed")), 0o755, "and so must its store"); + assert_eq!( + mode(&seg.join("sealed").join("000001.sst")), + 0o644, + "and its files readable" + ); + } + #[test] fn the_set_end_is_where_a_continuous_builder_picks_up() { let d = tempfile::tempdir().unwrap(); diff --git a/crates/storage/src/segbuild_boundaries.rs b/crates/storage/src/segbuild_boundaries.rs index c181799f..9f53dda3 100644 --- a/crates/storage/src/segbuild_boundaries.rs +++ b/crates/storage/src/segbuild_boundaries.rs @@ -145,6 +145,7 @@ impl SegmentBoundaries { let dir = dir.into(); std::fs::create_dir_all(&dir) .with_context(|| format!("creating {}", dir.display()))?; + set_mode(&dir, 0o755); let boundaries = read_boundaries(&dir.join(BOUNDARIES))?; let built = read_built(&dir.join(BUILT))?; let uncovered = read_uncovered(&dir.join(UNCOVERED))?; @@ -395,6 +396,7 @@ impl SegmentBoundaries { ), ) .with_context(|| format!("writing {}", path.display()))?; + set_mode(&path, 0o644); Ok(()) } @@ -470,6 +472,21 @@ fn unix_now() -> u64 { .unwrap_or(0) } +/// Give a path a mode, ignoring failure. +/// +/// These logs are operational state that a human and the offline tools read, +/// and the node creates them under `UMask=0077`, which would leave them +/// owner-only. Set the mode rather than inherit it. Not fatal: a log that is +/// written but awkwardly permissioned has still recorded the fact. +#[cfg(unix)] +fn set_mode(path: &Path, mode: u32) { + use std::os::unix::fs::PermissionsExt; + let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)); +} + +#[cfg(not(unix))] +fn set_mode(_path: &Path, _mode: u32) {} + /// Append one line and fsync it. /// /// `sync_all` rather than a buffered write: a boundary that is in the page @@ -484,6 +501,7 @@ fn append_line(path: &Path, line: &str) -> Result<()> { .with_context(|| format!("opening {}", path.display()))?; writeln!(f, "{line}").with_context(|| format!("writing {}", path.display()))?; f.sync_all().with_context(|| format!("syncing {}", path.display()))?; + set_mode(path, 0o644); Ok(()) }