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(()) }