From 444c1df9da0c554e53ac2544436783817444715d Mon Sep 17 00:00:00 2001 From: Norrie Taylor Date: Fri, 17 Jul 2026 13:29:22 -0700 Subject: [PATCH 1/4] feat(minimal,diagnostics): add the min bug host diagnostic bundle command Add the bug subcommand: a manifest-bearing minimal-diag-.tar.zst assembled through the diagnostics crate's BundleWriter. Host collectors (system, env, dirs report, config, state listing, logs) run under per-collector timeouts; any failure is recorded in the manifest and the run continues. The command mutates no state and spawns no daemons. Reusable mechanics land in the diagnostics crate so the daemon-side bundle can share them: redact_toml beside redact_json, masked_process_env over a caller-supplied allowlist predicate, newest_rotated log selection, disk_usage (statvfs), and first_stdout_line over command_capture. The CLI keeps only policy: the env allowlist, log prefixes, config paths, and archive layout. Refs: #801 Co-Authored-By: Claude Fable 5 --- Cargo.lock | 4 + crates/diagnostics/Cargo.toml | 1 + crates/diagnostics/src/capture.rs | 31 +++ crates/diagnostics/src/disk.rs | 51 ++++ crates/diagnostics/src/lib.rs | 18 +- crates/diagnostics/src/logs.rs | 72 ++++++ crates/diagnostics/src/redact.rs | 158 ++++++++++++- crates/minimal/Cargo.toml | 3 + crates/minimal/src/diag/collect.rs | 365 +++++++++++++++++++++++++++++ crates/minimal/src/diag/mod.rs | 144 ++++++++++++ crates/minimal/src/diag/redact.rs | 58 +++++ crates/minimal/src/dirs.rs | 30 ++- crates/minimal/src/lib.rs | 10 + crates/minimal/tests/bug.rs | 228 ++++++++++++++++++ 14 files changed, 1159 insertions(+), 14 deletions(-) create mode 100644 crates/diagnostics/src/disk.rs create mode 100644 crates/diagnostics/src/logs.rs create mode 100644 crates/minimal/src/diag/collect.rs create mode 100644 crates/minimal/src/diag/mod.rs create mode 100644 crates/minimal/src/diag/redact.rs create mode 100644 crates/minimal/tests/bug.rs diff --git a/Cargo.lock b/Cargo.lock index 786038aeba..4442eb1764 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1519,6 +1519,7 @@ dependencies = [ "thiserror 2.0.18", "tokio", "tokio-stream", + "toml", "walkdir", ] @@ -3613,7 +3614,9 @@ dependencies = [ "clap", "clap_complete", "constcat", + "diagnostics", "dirs", + "libc", "mctx", "mfile", "minimald", @@ -3628,6 +3631,7 @@ dependencies = [ "sessions", "tempfile", "tokio", + "toml", "tracing", "tracing-subscriber", "version", diff --git a/crates/diagnostics/Cargo.toml b/crates/diagnostics/Cargo.toml index abd171ba69..5c87e45c2e 100644 --- a/crates/diagnostics/Cargo.toml +++ b/crates/diagnostics/Cargo.toml @@ -15,6 +15,7 @@ serde.workspace = true serde_json.workspace = true thiserror.workspace = true tokio.workspace = true +toml.workspace = true walkdir.workspace = true [target.'cfg(unix)'.dependencies] diff --git a/crates/diagnostics/src/capture.rs b/crates/diagnostics/src/capture.rs index 0607f52a1f..b64e7be599 100644 --- a/crates/diagnostics/src/capture.rs +++ b/crates/diagnostics/src/capture.rs @@ -113,6 +113,20 @@ pub async fn command_capture( }) } +/// First line of `cmd args…` stdout, or `None` when the command can't run, +/// fails, or times out. The one-liner for collectors that want a single +/// fact from a tool (`uname -srvm`) and treat any failure as "unknown". +pub async fn first_stdout_line(cmd: &str, args: &[&str], timeout: Duration) -> Option { + let capture = command_capture(cmd, args, timeout).await.ok()?; + capture.status.is_some_and(|s| s.success()).then(|| { + String::from_utf8_lossy(&capture.stdout) + .lines() + .next() + .unwrap_or_default() + .to_string() + }) +} + #[cfg(test)] mod tests { use super::*; @@ -154,4 +168,21 @@ mod tests { .unwrap_err(); assert!(matches!(err, CaptureError::Spawn { .. }), "got: {err}"); } + + #[tokio::test] + async fn first_stdout_line_takes_line_one_and_maps_failure_to_none() { + let line = first_stdout_line( + "sh", + &["-c", "echo first; echo second"], + Duration::from_secs(10), + ) + .await; + assert_eq!(line.as_deref(), Some("first")); + + let failed = first_stdout_line("sh", &["-c", "exit 1"], Duration::from_secs(10)).await; + assert_eq!(failed, None, "non-zero exit is None, not empty"); + let missing = + first_stdout_line("definitely-not-a-real-binary", &[], Duration::from_secs(1)).await; + assert_eq!(missing, None); + } } diff --git a/crates/diagnostics/src/disk.rs b/crates/diagnostics/src/disk.rs new file mode 100644 index 0000000000..7dbe21c663 --- /dev/null +++ b/crates/diagnostics/src/disk.rs @@ -0,0 +1,51 @@ +//! Filesystem capacity probes for diagnostic bundles. +//! +//! Disk exhaustion is a perennial root cause; every bundle producer wants +//! "how full is the filesystem holding X" without shelling out to `df`. + +use std::path::Path; + +/// Capacity of the filesystem holding a path. +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub struct DiskUsage { + pub total_bytes: u64, + /// Bytes available to an unprivileged caller (`f_bavail`, not + /// `f_bfree`) — the number that predicts the next write failing. + pub free_bytes: u64, +} + +/// The capacity of the filesystem holding `path`, or `None` when the path +/// doesn't exist or can't be statted — for a collector, "unknown" is data. +pub fn disk_usage(path: &Path) -> Option { + use std::os::unix::ffi::OsStrExt as _; + let c_path = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?; + let mut stat: libc::statvfs = unsafe { std::mem::zeroed() }; + // SAFETY: valid NUL-terminated path and out-pointer to a zeroed struct. + if unsafe { libc::statvfs(c_path.as_ptr(), &mut stat) } != 0 { + return None; + } + let frsize = stat.f_frsize as u64; + Some(DiskUsage { + total_bytes: stat.f_blocks as u64 * frsize, + free_bytes: stat.f_bavail as u64 * frsize, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn real_path_reports_nonzero_capacity() { + let tmp = tempfile::TempDir::new().unwrap(); + let usage = disk_usage(tmp.path()).expect("tempdir filesystem is stattable"); + assert!(usage.total_bytes > 0); + assert!(usage.free_bytes <= usage.total_bytes); + } + + #[test] + fn missing_path_is_none() { + assert!(disk_usage(Path::new("/nonexistent/diag-disk-probe")).is_none()); + } +} diff --git a/crates/diagnostics/src/lib.rs b/crates/diagnostics/src/lib.rs index d61e1e22db..54ac4a2c2f 100644 --- a/crates/diagnostics/src/lib.rs +++ b/crates/diagnostics/src/lib.rs @@ -9,19 +9,31 @@ //! through, file-backed (CLI) or streaming (daemon), which keeps the //! manifest complete by construction //! - [`manifest`] — the bundle's self-describing manifest types -//! - [`redact`] — key-based masking of secret-shaped values, applied -//! identically by every producer +//! - [`redact`] — key-based masking of secret-shaped values (JSON, TOML, +//! and the process environment), applied identically by every producer //! - [`listing`] — bounded recursive directory listing (names/sizes/metadata) //! - [`capture`] — bounded subprocess output capture for command-shaped //! collectors +//! - [`logs`] — newest-first selection of rotated log files +//! - [`disk`] — filesystem capacity probes +//! +//! The split with the app crates is mechanics vs. policy: the *how* lives +//! here; the *what* — which paths, which log prefixes, which env names are +//! allowlisted, where entries land in the archive — stays with the caller. pub mod bundle; pub mod capture; +#[cfg(unix)] +pub mod disk; pub mod listing; +pub mod logs; pub mod manifest; pub mod redact; pub use bundle::{BundleSink, BundleWriter, LOG_TAIL_CAP}; -pub use capture::{Capture, CaptureError, command_capture}; +pub use capture::{Capture, CaptureError, command_capture, first_stdout_line}; +#[cfg(unix)] +pub use disk::{DiskUsage, disk_usage}; pub use listing::{Listing, listing}; +pub use logs::newest_rotated; pub use manifest::{CollectedEntry, CollectorError, Manifest, Redaction, SkippedEntry}; diff --git a/crates/diagnostics/src/logs.rs b/crates/diagnostics/src/logs.rs new file mode 100644 index 0000000000..a9938f7378 --- /dev/null +++ b/crates/diagnostics/src/logs.rs @@ -0,0 +1,72 @@ +//! Selection of rotated log files for diagnostic bundles. + +use std::path::{Path, PathBuf}; + +/// The newest `limit` files in `dir` whose names start with `prefix`, +/// newest first. +/// +/// "Newest" is reverse-lexicographic filename order: rolling filenames embed +/// their rotation point after the prefix (`minimald.log.2026-07-15`), so the +/// lexicographically greatest name is the most recent. A missing or +/// unreadable `dir` yields an empty list — callers that need to distinguish +/// "no log dir" from "no matches" check the directory first. +pub async fn newest_rotated(dir: &Path, prefix: &str, limit: usize) -> Vec { + let Ok(mut entries) = tokio::fs::read_dir(dir).await else { + return Vec::new(); + }; + let mut matches: Vec = Vec::new(); + while let Ok(Some(entry)) = entries.next_entry().await { + let path = entry.path(); + if path + .file_name() + .and_then(|n| n.to_str()) + .is_some_and(|n| n.starts_with(prefix)) + { + matches.push(path); + } + } + matches.sort(); + matches.reverse(); + matches.truncate(limit); + matches +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn newest_first_capped_and_prefix_filtered() { + let tmp = tempfile::TempDir::new().unwrap(); + for day in 1..=7 { + std::fs::write(tmp.path().join(format!("app.log.2026-07-{day:02}")), "").unwrap(); + } + std::fs::write(tmp.path().join("other.log.2026-07-09"), "").unwrap(); + + let picked = newest_rotated(tmp.path(), "app.log", 5).await; + let names: Vec<&str> = picked + .iter() + .map(|p| p.file_name().unwrap().to_str().unwrap()) + .collect(); + assert_eq!( + names, + [ + "app.log.2026-07-07", + "app.log.2026-07-06", + "app.log.2026-07-05", + "app.log.2026-07-04", + "app.log.2026-07-03", + ], + "newest five, other prefixes excluded" + ); + } + + #[tokio::test] + async fn missing_dir_is_empty() { + assert!( + newest_rotated(Path::new("/nonexistent/diag-log-dir"), "app.log", 5) + .await + .is_empty() + ); + } +} diff --git a/crates/diagnostics/src/redact.rs b/crates/diagnostics/src/redact.rs index 4fc0ea2bdf..c3d41a6cb5 100644 --- a/crates/diagnostics/src/redact.rs +++ b/crates/diagnostics/src/redact.rs @@ -1,10 +1,14 @@ //! Key-based redaction of secret-shaped values in structured data. //! //! Shared by the CLI-side diagnostic bundler and the daemon-side -//! diagnostic RPC so both apply identical rules. Redaction is -//! deliberately conservative: false positives (masking a harmless value) are +//! diagnostic RPC so both apply identical rules, over both formats the +//! producers read (JSON and TOML), plus the process-environment masking +//! both sides use for their `env.json`. Redaction is deliberately +//! conservative: false positives (masking a harmless value) are //! acceptable, false negatives (leaking a secret) are not. +use std::collections::BTreeMap; + use serde_json::Value; /// Key substrings that mark a value as sensitive, matched case-insensitively. @@ -89,6 +93,78 @@ fn redact_json_inner(value: &mut Value, mask_all: bool) { } } +/// Parses `input` as TOML and masks sensitive values: any value whose key is +/// sensitive per [`is_sensitive_key`], and *every* value inside tables named +/// like env-var containers ([`is_env_table_name`]), e.g. a loadout's `[vars]`. +/// +/// Returns the re-serialized document. Comments and key ordering are not +/// preserved — callers record that in the bundle manifest. Unparseable input +/// is an error; callers must withhold the file rather than pass it through. +pub fn redact_toml(input: &str) -> Result { + let table: toml::Table = input.parse()?; + let mut value = toml::Value::Table(table); + redact_toml_value(&mut value, false); + Ok(toml::to_string_pretty(&value).expect("re-serializing a just-parsed TOML value")) +} + +fn redact_toml_value(value: &mut toml::Value, mask_all: bool) { + match value { + toml::Value::Table(table) => { + for (key, child) in table.iter_mut() { + let child_mask = mask_all || is_env_table_name(key) || is_sensitive_key(key); + match child { + toml::Value::Table(_) | toml::Value::Array(_) => { + redact_toml_value(child, child_mask); + } + leaf => { + if child_mask || is_sensitive_key(key) { + *leaf = toml_placeholder(leaf); + } + } + } + } + } + toml::Value::Array(items) => items + .iter_mut() + .for_each(|item| redact_toml_value(item, mask_all)), + leaf => { + if mask_all { + *leaf = toml_placeholder(leaf); + } + } + } +} + +fn toml_placeholder(original: &toml::Value) -> toml::Value { + let len = match original { + toml::Value::String(s) => s.len(), + other => other.to_string().len(), + }; + toml::Value::String(format!("")) +} + +/// The process environment with every value the caller's policy does not +/// explicitly allow masked to `` (N = the OS value's byte +/// length, so size-related issues stay diagnosable). Names are always +/// recorded; only values are masked. +/// +/// Enumerates via `vars_os`, never `vars` — the latter panics on a +/// non-UTF-8 name or value, and a panic inside a collector aborts the whole +/// run with an unfinalized archive. +pub fn masked_process_env(is_value_allowed: impl Fn(&str) -> bool) -> BTreeMap { + std::env::vars_os() + .map(|(name_os, value_os)| { + let name = name_os.to_string_lossy().into_owned(); + let shown = if is_value_allowed(&name) { + value_os.to_string_lossy().into_owned() + } else { + format!("", value_os.as_encoded_bytes().len()) + }; + (name, shown) + }) + .collect() +} + #[cfg(test)] mod tests { use super::*; @@ -194,4 +270,82 @@ mod tests { redact_json(&mut v); assert_eq!(v["auth_retries"], ""); } + + #[test] + fn toml_redacts_vars_tables_and_sensitive_keys() { + let input = r#" + description = "dev loadout" + packages = ["ripgrep", "fd"] + api_token = "abc123" + + [vars] + EDITOR = "vim" + GITHUB_TOKEN = "ghp_zzz" + + [[lifecycle_hooks]] + type = "inline" + value = "echo hi" + "#; + let out = redact_toml(input).expect("valid toml"); + assert!(out.contains(r#"description = "dev loadout""#)); + assert!(out.contains("ripgrep")); + assert!(out.contains(r#"api_token = """#)); + assert!(out.contains(r#"EDITOR = """#)); + assert!(out.contains(r#"GITHUB_TOKEN = """#)); + // Hook bodies are not env values and carry no key-based signal. + assert!(out.contains("echo hi")); + } + + #[test] + fn toml_sensitive_tables_are_masked_wholesale() { + let input = r#" + api_token = ["primary", "secondary"] + + [tokens] + github = "ghp_zzz" + + [credentials.aws] + access_key_id = "AKIA" + "#; + let out = redact_toml(input).expect("valid toml"); + assert!(!out.contains("ghp_zzz")); + assert!(!out.contains("AKIA")); + assert!(!out.contains("primary")); + assert!(!out.contains("secondary")); + } + + #[test] + fn toml_redacts_inherit_vars_wholesale() { + let input = r#" + [vars] + TERM = { inherit = true, default = "xterm-256color" } + "#; + let out = redact_toml(input).expect("valid toml"); + assert!(!out.contains("xterm-256color")); + assert!(out.contains("")); + } + + #[test] + fn unparseable_toml_is_an_error() { + assert!(redact_toml("not = [ valid").is_err()); + } + + #[test] + fn toml_public_key_survives() { + let out = redact_toml(r#"public_key = "wg-pub-abc""#).expect("valid toml"); + assert!(out.contains("wg-pub-abc")); + } + + #[test] + fn masked_process_env_masks_everything_the_policy_rejects() { + // PATH is always present in a test environment. + let env = masked_process_env(|name| name == "PATH"); + assert!(!env["PATH"].starts_with("/minimal`. + pub config: PathBuf, + /// `/minimal` (or the `--minimal-dir` override). + pub state: PathBuf, + /// `/minimal`. + pub cache: PathBuf, + /// Where `minimal mesh join` writes its enrolment file. + pub mesh_enrolment: PathBuf, + /// The invoking directory (disk-full there breaks the bundle write). + pub cwd: PathBuf, +} + +// ── host/system.json ───────────────────────────────────────────────────────── + +#[derive(Serialize)] +struct SystemInfo { + os: &'static str, + arch: &'static str, + kernel: Option, + hostname: Option, + ncpu: Option, + euid: u32, + #[cfg(target_os = "linux")] + kvm: KvmInfo, + disks: Vec, +} + +#[cfg(target_os = "linux")] +#[derive(Serialize)] +struct KvmInfo { + exists: bool, + /// `ok` / `enoent` / `eacces` / other errno string — ENOENT means the + /// module isn't loaded, EACCES means the user isn't in the kvm group. + open_result: String, + mode: Option, + owner_gid: Option, + user_in_kvm_group: Option, +} + +#[derive(Serialize)] +struct DiskInfo { + path: String, + total_bytes: Option, + free_bytes: Option, +} + +/// Timeout for single-fact commands like `uname`; a wedged tool must not +/// eat the collector's whole 30 s budget. +const FACT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); + +pub async fn system(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { + let kernel = diagnostics::first_stdout_line("uname", &["-srvm"], FACT_TIMEOUT).await; + let hostname = diagnostics::first_stdout_line("uname", &["-n"], FACT_TIMEOUT).await; + let info = SystemInfo { + os: std::env::consts::OS, + arch: std::env::consts::ARCH, + kernel, + hostname, + ncpu: std::thread::available_parallelism().map(usize::from).ok(), + // SAFETY: geteuid has no failure modes or preconditions. + euid: unsafe { libc::geteuid() }, + #[cfg(target_os = "linux")] + kvm: kvm_info(), + disks: [&paths.state, &paths.cache, &paths.cwd] + .into_iter() + .map(|p| disk_info(p)) + .collect(), + }; + let json = serde_json::to_vec_pretty(&info).context("serializing system info")?; + w.add_bytes("host/system.json", &json, Redaction::None) + .await +} + +#[cfg(target_os = "linux")] +fn kvm_info() -> KvmInfo { + use std::os::unix::fs::MetadataExt as _; + let meta = std::fs::metadata("/dev/kvm").ok(); + let open_result = match std::fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/kvm") + { + Ok(_) => "ok".to_string(), + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => "enoent".to_string(), + std::io::ErrorKind::PermissionDenied => "eacces".to_string(), + _ => e.to_string(), + }, + }; + KvmInfo { + exists: meta.is_some(), + open_result, + mode: meta.as_ref().map(|m| format!("{:o}", m.mode())), + owner_gid: meta.as_ref().map(|m| m.gid()), + user_in_kvm_group: user_in_group("kvm"), + } +} + +/// Whether the current user's supplementary groups include `name`. +/// `None` when the group doesn't exist or the group list can't be read. +#[cfg(target_os = "linux")] +fn user_in_group(name: &str) -> Option { + let c_name = std::ffi::CString::new(name).ok()?; + // SAFETY: getgrnam reads a NUL-terminated string; the returned struct is + // a static buffer only read before any other getgr* call in this thread. + let grp = unsafe { libc::getgrnam(c_name.as_ptr()) }; + if grp.is_null() { + return None; + } + // SAFETY: non-null getgrnam result points to a valid `group`. + let target_gid = unsafe { (*grp).gr_gid }; + + // SAFETY: first call sizes the buffer, second fills it. + let n = unsafe { libc::getgroups(0, std::ptr::null_mut()) }; + if n < 0 { + return None; + } + let mut gids = vec![0 as libc::gid_t; n as usize]; + // SAFETY: buffer is exactly the size getgroups asked for. + let n = unsafe { libc::getgroups(n, gids.as_mut_ptr()) }; + if n < 0 { + return None; + } + gids.truncate(n as usize); + // SAFETY: getegid has no failure modes. + let egid = unsafe { libc::getegid() }; + Some(gids.contains(&target_gid) || egid == target_gid) +} + +fn disk_info(path: &Path) -> DiskInfo { + let usage = diagnostics::disk_usage(path); + DiskInfo { + path: path.display().to_string(), + total_bytes: usage.map(|u| u.total_bytes), + free_bytes: usage.map(|u| u.free_bytes), + } +} + +// ── host/env.json ──────────────────────────────────────────────────────────── + +pub async fn env(w: &mut BundleWriter) -> Result<(), anyhow::Error> { + let env = masked_process_env(is_env_value_allowlisted); + let json = serde_json::to_vec_pretty(&env).context("serializing env")?; + w.add_bytes("host/env.json", &json, Redaction::Keys).await +} + +// ── config/ ────────────────────────────────────────────────────────────────── + +pub async fn config(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { + add_redacted_toml( + w, + &paths.config.join("config.toml"), + "config/config.toml.redacted", + ) + .await; + add_redacted_toml( + w, + &paths.config.join("user_policy.toml"), + "config/user_policy.toml.redacted", + ) + .await; + + if let Ok(mut entries) = tokio::fs::read_dir(paths.config.join("loadouts")).await { + while let Ok(Some(entry)) = entries.next_entry().await { + let path = entry.path(); + if path.extension().is_some_and(|e| e == "toml") + && let Some(name) = path.file_name().and_then(|n| n.to_str()) + { + add_redacted_toml(w, &path, &format!("config/loadouts/{name}.redacted")).await; + } + } + } + + // mTLS material: cert/CA metadata only, private key never touched. + w.skip( + "config/client.key", + "private key material — never collected", + ); + let certs: std::collections::BTreeMap<&str, Option> = [ + ( + "client.pem", + file_meta(&paths.config.join("client.pem")).await, + ), + ("ca.pem", file_meta(&paths.config.join("ca.pem")).await), + ] + .into_iter() + .collect(); + let json = serde_json::to_vec_pretty(&certs).context("serializing cert metadata")?; + w.add_bytes("config/client-cert.json", &json, Redaction::ListingOnly) + .await?; + + // Mesh enrolment holds only a host:port endpoint. + match tokio::fs::read(&paths.mesh_enrolment).await { + Ok(bytes) => { + w.add_bytes("config/mesh-enrolment", &bytes, Redaction::None) + .await? + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => w.skip("config/mesh-enrolment", format!("unreadable: {e}")), + } + Ok(()) +} + +/// Adds a TOML file with values redacted; a missing file is silently fine, +/// an unparseable or unreadable one is withheld with a manifest note. +async fn add_redacted_toml(w: &mut BundleWriter, src: &Path, dest: &str) { + let content = match tokio::fs::read_to_string(src).await { + Ok(c) => c, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return, + Err(e) => { + w.skip(dest, format!("unreadable: {e}")); + return; + } + }; + match redact_toml(&content) { + Ok(redacted) => { + if let Err(e) = w + .add_bytes(dest, redacted.as_bytes(), Redaction::Keys) + .await + { + w.skip(dest, format!("bundling failed: {e}")); + } + } + Err(e) => w.skip( + dest, + format!("withheld: does not parse as TOML, cannot redact safely: {e}"), + ), + } +} + +#[derive(Serialize)] +struct FileMeta { + bytes: u64, + mtime_unix: Option, +} + +async fn file_meta(path: &Path) -> Option { + let meta = tokio::fs::metadata(path).await.ok()?; + Some(FileMeta { + bytes: meta.len(), + mtime_unix: meta + .modified() + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|d| d.as_secs()), + }) +} + +// ── state/ ─────────────────────────────────────────────────────────────────── + +pub async fn state(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { + // Recursive listing: names, sizes, and metadata only — session workspace + // *contents* stay on the user's machine. + let listing = + diagnostics::listing(&paths.state, LISTING_MAX_ENTRIES).context("listing state dir")?; + w.add_bytes( + "state/listing.txt", + listing.text.as_bytes(), + Redaction::ListingOnly, + ) + .await +} + +// ── logs/ ──────────────────────────────────────────────────────────────────── + +/// How many rotated files per log prefix make it into the bundle. +const LOG_FILES_PER_PREFIX: usize = 5; + +pub async fn logs(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { + let log_dir = paths.state.join("logs"); + if !tokio::fs::try_exists(&log_dir).await.unwrap_or(false) { + // No log dir at all — nothing ever detached. Absence is data. + w.skip("logs/", "no log directory — no daemon has ever logged here"); + } else { + for prefix in ["minimald.log", "minvmd.log"] { + let files = diagnostics::newest_rotated(&log_dir, prefix, LOG_FILES_PER_PREFIX).await; + if files.is_empty() { + w.skip(format!("logs/{prefix}*"), "no files with this prefix"); + continue; + } + for path in files { + let name = path.file_name().unwrap_or_default().to_string_lossy(); + let dest = format!("logs/{name}"); + if let Err(e) = w.add_file_tail(&dest, &path, LOG_TAIL_CAP).await { + w.skip(&dest, format!("unreadable: {e}")); + } + } + } + } + + // Provider-scoped logs. run.log: the detached supervisor's stderr + // redirect (panics, the final error print of a failed boot). boot.log: + // the VMM's hvc0 console capture — kernel prints and the guest pid-1's + // stdout, the only evidence when the guest wedges before (or its + // transport dies after) the daemon is reachable. + for (name, dir) in provider_dirs(&paths.state).await { + for log_name in ["run.log", "boot.log"] { + let dest = format!("providers/{name}/{log_name}"); + match w + .add_file_tail(&dest, &dir.join(log_name), LOG_TAIL_CAP) + .await + { + Ok(()) => {} + Err(e) if is_not_found(&e) => w.skip(&dest, "absent"), + Err(e) => w.skip(&dest, format!("unreadable: {e}")), + } + } + } + Ok(()) +} + +/// True when an `add_file_tail` failure was a plain missing file. +fn is_not_found(err: &anyhow::Error) -> bool { + err.chain() + .filter_map(|c| c.downcast_ref::()) + .any(|io| io.kind() == std::io::ErrorKind::NotFound) +} + +// ── providers/local-/ discovery ─────────────────────────────────────────── + +/// Provider instance dirs under `/providers`, sorted. Empty when the +/// providers dir doesn't exist (nothing was ever spawned). +pub async fn provider_dirs(state: &Path) -> Vec<(String, PathBuf)> { + let Ok(mut entries) = tokio::fs::read_dir(state.join("providers")).await else { + return Vec::new(); + }; + let mut dirs: Vec<(String, PathBuf)> = Vec::new(); + while let Ok(Some(entry)) = entries.next_entry().await { + if !entry.file_type().await.is_ok_and(|t| t.is_dir()) { + continue; + } + let Some(name) = entry.file_name().to_str().map(str::to_string) else { + continue; + }; + if name.starts_with("local-") { + dirs.push((name, entry.path())); + } + } + dirs.sort(); + dirs +} diff --git a/crates/minimal/src/diag/mod.rs b/crates/minimal/src/diag/mod.rs new file mode 100644 index 0000000000..8201bdb3d6 --- /dev/null +++ b/crates/minimal/src/diag/mod.rs @@ -0,0 +1,144 @@ +//! `min bug` — collect a diagnostic bundle for the minimal dev team. +//! +//! One command, one artifact: a `minimal-diag-.tar.zst` containing +//! the logs, config (redacted), and state listings needed to root-cause +//! field issues. Every collector is independent and failure-isolated: a +//! fully broken install still yields a valid archive whose `manifest.json` +//! explains what's missing. Nothing here mutates state or autospawns +//! daemons — diagnosing a wedged system must not change it. + +use std::path::PathBuf; +use std::time::Duration; + +use anyhow::Context as _; +use clap::Args; + +pub mod collect; +pub mod redact; + +use collect::DiagPaths; +use diagnostics::{BundleWriter, Redaction}; + +use crate::GlobalArgs; + +#[derive(Debug, Args)] +pub struct BugArgs { + /// Output path (default: ./minimal-diag-.tar.zst) + #[arg(long, short)] + pub output: Option, +} + +/// A collector timeout generous enough for slow disks, small enough that a +/// hung collector can't stall the whole run. +const COLLECTOR_TIMEOUT: Duration = Duration::from_secs(30); + +/// Runs one collector future with [`COLLECTOR_TIMEOUT`]; failure or timeout +/// is recorded in the manifest and the run continues. A macro (not a +/// function) so the future's borrow of the writer ends before the +/// error-recording arms re-borrow it. +macro_rules! collect_step { + ($w:expr, $name:expr, $fut:expr) => {{ + let name: String = $name.into(); + let started = std::time::Instant::now(); + let result = tokio::time::timeout(COLLECTOR_TIMEOUT, $fut).await; + match result { + Ok(Ok(())) => {} + Ok(Err(e)) => $w.error(name, format!("{e:#}"), started.elapsed()), + Err(_) => $w.error( + name, + format!("timed out after {COLLECTOR_TIMEOUT:?}"), + started.elapsed(), + ), + } + }}; +} + +pub async fn cmd_bug(global: &GlobalArgs, args: BugArgs) -> Result<(), anyhow::Error> { + let created_at = chrono::Utc::now(); + let started = std::time::Instant::now(); + + let bundle_name = format!("minimal-diag-{}", created_at.format("%Y%m%dT%H%M%SZ")); + let out_path = args + .output + .clone() + .unwrap_or_else(|| PathBuf::from(format!("{bundle_name}.tar.zst"))); + + let paths = resolve_paths(global); + let mut w = BundleWriter::create(&out_path, &bundle_name, version::LONG_VERSION).await?; + + // ── Host-side collectors: independent, each failure becomes a + // manifest error rather than aborting the run. + collect_step!(w, "host.system", collect::system(&mut w, &paths)); + collect_step!(w, "host.env", collect::env(&mut w)); + collect_step!(w, "host.dirs", dirs_report(&mut w, global)); + collect_step!(w, "config", collect::config(&mut w, &paths)); + collect_step!(w, "state", collect::state(&mut w, &paths)); + collect_step!(w, "logs", collect::logs(&mut w, &paths)); + + // Daemon-side state (socket probes, guest bundles, per-provider status) + // is collected over the daemon connection, not from the host bundle; + // record what exists so the manifest is honest about coverage. + let providers = collect::provider_dirs(&paths.state).await; + if providers.is_empty() { + w.skip( + "providers/", + "no provider instances found — no daemon was ever spawned here", + ); + } else { + w.skip( + "providers/", + format!( + "{} provider instance(s) present — only run.log/boot.log captured; \ + daemon-side state not collected", + providers.len() + ), + ); + } + + let entries = w.entry_count(); + let errors = w.error_count(); + w.finish(created_at, started.elapsed()) + .await + .context("finalizing diagnostic bundle")?; + + println!( + "Wrote {} ({entries} entries, {errors} error{}{})", + out_path.display(), + if errors == 1 { "" } else { "s" }, + if errors > 0 { + " — see manifest.json" + } else { + "" + }, + ); + println!("Review the contents before sharing; send it to the minimal dev team."); + Ok(()) +} + +/// Resolve every base path the collectors need, honoring the same overrides +/// (`--minimal-dir`, `--config-dir`) the rest of the CLI uses. +fn resolve_paths(global: &GlobalArgs) -> DiagPaths { + DiagPaths { + config: crate::config::resolve_minimal_config_dir(global), + state: match &global.minimal_dir { + Some(dir) => dir.clone(), + None => paths::minimal_state_dir() + .as_utf8_path() + .as_std_path() + .to_path_buf(), + }, + cache: paths::minimal_cache_dir() + .as_utf8_path() + .as_std_path() + .to_path_buf(), + mesh_enrolment: crate::mesh_enrolment_path(global), + cwd: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), + } +} + +/// The `min dirs` table, captured as `host/dirs.txt`. +async fn dirs_report(w: &mut BundleWriter, global: &GlobalArgs) -> Result<(), anyhow::Error> { + let report = crate::dirs::report(global); + w.add_bytes("host/dirs.txt", report.as_bytes(), Redaction::None) + .await +} diff --git a/crates/minimal/src/diag/redact.rs b/crates/minimal/src/diag/redact.rs new file mode 100644 index 0000000000..837fada7ac --- /dev/null +++ b/crates/minimal/src/diag/redact.rs @@ -0,0 +1,58 @@ +//! CLI-side redaction policy: which env-var values may leave the machine. +//! +//! The mechanics — the key-based rules, the TOML/JSON walkers, and the +//! process-env masking — live in [`diagnostics::redact`] so the daemon +//! applies the same behavior; what belongs to the CLI is only this policy: +//! the allowlist of env names whose values are safe and useful verbatim. + +use diagnostics::redact::is_sensitive_key; + +/// Env vars whose *values* are safe and useful to include verbatim. Everything +/// else is reported by name only. +const ENV_VALUE_ALLOWLIST_EXACT: &[&str] = &["RUST_LOG", "HOME", "SHELL", "TERM", "PATH"]; +const ENV_VALUE_ALLOWLIST_PREFIXES: &[&str] = &["XDG_", "MINIMAL_", "MINVMD_", "MINIMALD_"]; + +/// Returns true when the named env var's value may be captured verbatim. +/// A sensitive-shaped name always loses to the allowlist — `MINIMAL_AUTH_TOKEN` +/// matches the project prefix but must never leave the machine. +pub fn is_env_value_allowlisted(name: &str) -> bool { + if is_sensitive_key(name) { + return false; + } + ENV_VALUE_ALLOWLIST_EXACT.contains(&name) + || ENV_VALUE_ALLOWLIST_PREFIXES + .iter() + .any(|p| name.starts_with(p)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn env_allowlist_covers_expected_names() { + for name in [ + "RUST_LOG", + "XDG_STATE_HOME", + "MINIMAL_BIN", + "MINVMD_KERNEL_PATH", + "MINIMALD_DETACHED", + "HOME", + "PATH", + ] { + assert!(is_env_value_allowlisted(name), "{name} should be allowed"); + } + for name in [ + "AWS_SECRET_ACCESS_KEY", + "GITHUB_TOKEN", + "LANG", + "USER", + "MINIMAL_AUTH_TOKEN", + "MINIMALD_API_KEY", + "MINVMD_PASSWORD", + "MINIMALIST_THING", + ] { + assert!(!is_env_value_allowlisted(name), "{name} must not be"); + } + } +} diff --git a/crates/minimal/src/dirs.rs b/crates/minimal/src/dirs.rs index 86535fc16b..b6773338fb 100644 --- a/crates/minimal/src/dirs.rs +++ b/crates/minimal/src/dirs.rs @@ -12,6 +12,13 @@ use crate::mesh_enrolment_path; /// with an "exists?" marker so unused paths don't look identical to /// misconfigured ones. pub fn cmd_dirs(global: &GlobalArgs) -> Result<(), anyhow::Error> { + print!("{}", report(global)); + Ok(()) +} + +/// The `cmd_dirs` table as a string — also captured verbatim into `min bug` +/// diagnostic bundles as `host/dirs.txt`. +pub(crate) fn report(global: &GlobalArgs) -> String { // Shown in the "Daemon logs" note to point users at today's // rolling log file directly, e.g. `minimald.log.2026-07-08`. let today = chrono::Local::now().format("%Y-%m-%d").to_string(); @@ -40,8 +47,7 @@ pub fn cmd_dirs(global: &GlobalArgs) -> Result<(), anyhow::Error> { .to_path_buf(), today, }; - print_dir_rows(&build_dir_rows(&dirs)); - Ok(()) + format_dir_rows(&build_dir_rows(&dirs)) } /// Resolved locations `cmd_dirs` prints, materialized once so @@ -182,9 +188,11 @@ fn build_dir_rows(dirs: &DirsLookup) -> Vec { rows } -/// Print the row list produced by [`build_dir_rows`] with per-column +/// Format the row list produced by [`build_dir_rows`] with per-column /// width-alignment and a blank line between groups. -fn print_dir_rows(rows: &[DirRow]) { +fn format_dir_rows(rows: &[DirRow]) -> String { + use std::fmt::Write as _; + let group_w = rows.iter().map(|r| r.group.len()).max().unwrap_or(6); let name_w = rows.iter().map(|r| r.name.len()).max().unwrap_or(4).max(4); let path_w = rows @@ -193,7 +201,9 @@ fn print_dir_rows(rows: &[DirRow]) { .max() .unwrap_or(4); - println!( + let mut out = String::new(); + let _ = writeln!( + out, "{group:.tar.zst` to the current directory. + /// Secret-shaped values (env vars, tokens) are redacted and session/ + /// project file contents are never included — only name/size listings. + /// Works even when no daemon is running; never starts one. + Bug(diag::BugArgs), /// WireGuard mesh: join, leave, and inspect remote-access state #[cfg(feature = "remote-access")] Mesh(MeshArgs), @@ -453,6 +462,7 @@ pub async fn run(cli: Cli) -> Result<(), anyhow::Error> { command: LoadoutCommand::List(args), }) => loadouts::cmd_loadout_list(args, &cli.global_args), Command::Dirs => dirs::cmd_dirs(&cli.global_args), + Command::Bug(args) => diag::cmd_bug(&cli.global_args, args).await, #[cfg(feature = "remote-access")] Command::Mesh(MeshArgs { command }) => match command { MeshCommand::Status => cmd_mesh_status(&cli.global_args).await, diff --git a/crates/minimal/tests/bug.rs b/crates/minimal/tests/bug.rs new file mode 100644 index 0000000000..47faa76e17 --- /dev/null +++ b/crates/minimal/tests/bug.rs @@ -0,0 +1,228 @@ +//! Integration tests for `min bug` (the diagnostic bundle command). +//! +//! Each test runs `cmd_bug` against temp state/config dirs and unpacks the +//! resulting tarball to assert the bundle contract: what's collected, what's +//! redacted, and what the manifest records for everything that's missing. +//! No daemon is involved — the command must be useful on a machine where +//! nothing was ever spawned. + +use std::collections::BTreeMap; +use std::path::Path; + +use minimal::GlobalArgs; +use minimal::diag::{BugArgs, cmd_bug}; + +/// Unpacks a `.tar.zst` bundle and returns `path-inside-archive -> contents` +/// (paths include the bundle's root directory). +async fn unpack(path: &Path) -> BTreeMap> { + let bytes = tokio::fs::read(path).await.unwrap(); + let decoder = async_compression::tokio::bufread::ZstdDecoder::new(&bytes[..]); + let out = tempfile::TempDir::new().unwrap(); + async_tar::Archive::new(decoder) + .unpack(out.path().to_path_buf()) + .await + .unwrap(); + + let mut files = BTreeMap::new(); + fn walk(base: &Path, dir: &Path, files: &mut BTreeMap>) { + for entry in std::fs::read_dir(dir).unwrap().flatten() { + let path = entry.path(); + if path.is_dir() { + walk(base, &path, files); + } else { + let rel = path.strip_prefix(base).unwrap().to_string_lossy().into(); + files.insert(rel, std::fs::read(&path).unwrap()); + } + } + } + walk(out.path(), out.path(), &mut files); + files +} + +/// Finds the single entry whose bundle-relative path ends with `suffix`. +fn find<'a>(files: &'a BTreeMap>, suffix: &str) -> Option<&'a Vec> { + files + .iter() + .find(|(path, _)| path.ends_with(suffix)) + .map(|(_, contents)| contents) +} + +/// A config dir with a loadout holding a canary secret, to prove redaction. +fn fake_config_dir() -> tempfile::TempDir { + let dir = tempfile::TempDir::new().unwrap(); + let root = dir.path().join("minimal"); + std::fs::create_dir_all(root.join("loadouts")).unwrap(); + std::fs::write( + root.join("config.toml"), + "[loadouts]\ndefault_loadouts = [\"dev\"]\n", + ) + .unwrap(); + std::fs::write( + root.join("loadouts/dev.toml"), + "name = \"dev\"\npackages = [\"ripgrep\"]\n\n[vars]\nGITHUB_TOKEN = \"canary-secret-value\"\n", + ) + .unwrap(); + dir +} + +fn global_args(state: &Path, config: &Path) -> GlobalArgs { + GlobalArgs { + repo_dir: None, + minimal_dir: Some(state.to_path_buf()), + config_dir: Some(config.to_path_buf()), + minvmd: false, + } +} + +fn bug_args(out: &Path) -> BugArgs { + BugArgs { + output: Some(out.to_path_buf()), + } +} + +#[tokio::test] +async fn bug_without_daemon_still_produces_a_bundle() { + let state = tempfile::TempDir::new().unwrap(); + let config = tempfile::TempDir::new().unwrap(); + let args = global_args(state.path(), config.path()); + + let out_dir = tempfile::TempDir::new().unwrap(); + let out = out_dir.path().join("diag.tar.zst"); + cmd_bug(&args, bug_args(&out)).await.unwrap(); + + let files = unpack(&out).await; + let manifest: serde_json::Value = + serde_json::from_slice(find(&files, "manifest.json").expect("manifest")).unwrap(); + assert_eq!(manifest["schema_version"], 1); + assert!(!manifest["collected"].as_array().unwrap().is_empty()); + + // Host collection ran even though nothing else exists. + assert!(find(&files, "host/system.json").is_some()); + assert!(find(&files, "host/env.json").is_some()); + assert!(find(&files, "host/dirs.txt").is_some()); + assert!(find(&files, "state/listing.txt").is_some()); + + // Nothing was ever spawned: the provider and log absences are recorded + // as skips, not silently missing and not errors. + let skipped_whats: Vec<&str> = manifest["skipped"] + .as_array() + .unwrap() + .iter() + .map(|s| s["what"].as_str().unwrap()) + .collect(); + assert!( + skipped_whats.contains(&"providers/"), + "expected a providers/ skip note: {skipped_whats:?}" + ); + assert!( + skipped_whats.contains(&"logs/"), + "expected a logs/ skip note: {skipped_whats:?}" + ); +} + +#[tokio::test] +async fn planted_secrets_never_reach_the_bundle() { + let state = tempfile::TempDir::new().unwrap(); + let config = fake_config_dir(); + let args = global_args(state.path(), config.path()); + + let out_dir = tempfile::TempDir::new().unwrap(); + let out = out_dir.path().join("diag.tar.zst"); + cmd_bug(&args, bug_args(&out)).await.unwrap(); + + let files = unpack(&out).await; + + // The loadout is included with its env-var value masked; keys and + // non-secret values survive so the config is still diagnosable. + let loadout = find(&files, "loadouts/dev.toml.redacted").expect("redacted loadout"); + let loadout = String::from_utf8_lossy(loadout); + assert!(loadout.contains("GITHUB_TOKEN"), "keys survive: {loadout}"); + assert!(loadout.contains("ripgrep"), "packages survive: {loadout}"); + assert!( + loadout.contains(""), + "masked value records its length: {loadout}" + ); + + // The canary value never appears anywhere in the archive. + for (path, contents) in &files { + assert!( + !contents + .windows(b"canary-secret-value".len()) + .any(|window| window == b"canary-secret-value"), + "secret value leaked into the bundle at {path}" + ); + } + + // Key material is never collected, and that is recorded. + assert!(find(&files, "client.key").is_none()); + let manifest: serde_json::Value = + serde_json::from_slice(find(&files, "manifest.json").expect("manifest")).unwrap(); + assert!( + manifest["skipped"] + .as_array() + .unwrap() + .iter() + .any(|s| s["what"].as_str().unwrap().contains("client.key")), + "client.key skip must be recorded: {}", + manifest["skipped"] + ); +} + +#[tokio::test] +async fn logs_collects_newest_five_per_prefix_and_provider_logs() { + let state = tempfile::TempDir::new().unwrap(); + let config = tempfile::TempDir::new().unwrap(); + + let log_dir = state.path().join("logs"); + std::fs::create_dir_all(&log_dir).unwrap(); + for day in 1..=7 { + std::fs::write( + log_dir.join(format!("minimald.log.2026-07-{day:02}")), + format!("day {day}\n"), + ) + .unwrap(); + } + + let provider = state.path().join("providers/local-0"); + std::fs::create_dir_all(&provider).unwrap(); + std::fs::write(provider.join("run.log"), "supervisor stderr\n").unwrap(); + // boot.log deliberately absent — recorded as a skip, not an error. + // Non-`local-` entries are not provider instances and must be ignored. + let stray = state.path().join("providers/remote-x"); + std::fs::create_dir_all(&stray).unwrap(); + std::fs::write(stray.join("run.log"), "not a local provider\n").unwrap(); + + let args = global_args(state.path(), config.path()); + let out_dir = tempfile::TempDir::new().unwrap(); + let out = out_dir.path().join("diag.tar.zst"); + cmd_bug(&args, bug_args(&out)).await.unwrap(); + + let files = unpack(&out).await; + let collected_logs: Vec<&String> = files + .keys() + .filter(|p| p.contains("logs/minimald.log.")) + .collect(); + assert_eq!( + collected_logs.len(), + 5, + "newest five per prefix: {collected_logs:?}" + ); + assert!( + find(&files, "logs/minimald.log.2026-07-07").is_some() + && find(&files, "logs/minimald.log.2026-07-02").is_none(), + "newest-first rotation order: {collected_logs:?}" + ); + + assert!(find(&files, "providers/local-0/run.log").is_some()); + assert!(find(&files, "providers/remote-x/run.log").is_none()); + let manifest: serde_json::Value = + serde_json::from_slice(find(&files, "manifest.json").expect("manifest")).unwrap(); + let skipped = manifest["skipped"].as_array().unwrap(); + assert!( + skipped + .iter() + .any(|s| s["what"] == "providers/local-0/boot.log" && s["reason"] == "absent"), + "absent boot.log is a skip: {skipped:?}" + ); + assert_eq!(manifest["errors"].as_array().unwrap().len(), 0, "no errors"); +} From 9abea0321eefb5507f44b35824d582a0bb8a39c4 Mon Sep 17 00:00:00 2001 From: Norrie Taylor Date: Fri, 17 Jul 2026 14:20:00 -0700 Subject: [PATCH 2/4] fix(minimal,diagnostics): harden the bug collectors per review Three review findings on the host collectors: - Filesystem errors are no longer reported as absence: provider_dirs returns Result (NotFound alone means never-spawned), and the loadouts and log-dir reads record non-NotFound errors instead of claiming the data does not exist. - The no-follow discipline now covers every content read, not just log tails: the O_NOFOLLOW + fstat open moves from add_file_tail into a shared open_regular_nofollow, and config TOMLs and the mesh-enrolment file read through it, so a symlinked config file cannot steer unrelated data into the bundle. - The synchronous listing walk runs under spawn_blocking so a wedged filesystem strands a blocking thread instead of the worker whose collect_step timeout is the failsafe; the crate fn documents the contract. Refs: #801 Co-Authored-By: Claude Fable 5 --- crates/diagnostics/src/bundle.rs | 57 +++++++------ crates/diagnostics/src/lib.rs | 2 +- crates/diagnostics/src/listing.rs | 4 + crates/minimal/src/diag/collect.rs | 130 +++++++++++++++++++---------- crates/minimal/src/diag/mod.rs | 21 +++-- crates/minimal/tests/bug.rs | 28 +++++++ 6 files changed, 166 insertions(+), 76 deletions(-) diff --git a/crates/diagnostics/src/bundle.rs b/crates/diagnostics/src/bundle.rs index 77e496b2e3..a648f540eb 100644 --- a/crates/diagnostics/src/bundle.rs +++ b/crates/diagnostics/src/bundle.rs @@ -141,30 +141,7 @@ impl BundleWriter { src: &Path, cap: u64, ) -> Result<(), anyhow::Error> { - // Open without following symlinks: a symlink planted in a tool-owned - // dir must not steer an unrelated target (a host key, another user's - // log) into a bundle the user then shares. O_NOFOLLOW refuses the - // link at open time — no check-then-open window to race. (It guards - // the final component only; intermediate directories are the tool's - // own, per the threat model.) - let mut opts = tokio::fs::OpenOptions::new(); - opts.read(true); - #[cfg(unix)] - opts.custom_flags(libc::O_NOFOLLOW); - let mut file = opts - .open(src) - .await - .with_context(|| format!("opening {}", src.display()))?; - // fstat the descriptor we actually read from: rejects directories and - // devices, and doubles as the symlink guard where O_NOFOLLOW is - // unavailable. - let meta = file - .metadata() - .await - .with_context(|| format!("statting {}", src.display()))?; - if !meta.is_file() { - anyhow::bail!("{} is not a regular file", src.display()); - } + let (mut file, meta) = open_regular_nofollow(src).await?; let len = meta.len(); let capped = len > cap; if capped { @@ -269,6 +246,38 @@ impl BundleWriter { } } +/// Opens `src` for reading without following symlinks and verifies on the +/// descriptor that it is a regular file — the shared no-follow discipline +/// for every content read that ends up in a bundle. +/// +/// A symlink planted in a tool-owned dir must not steer an unrelated target +/// (a host key, another user's log) into a bundle the user then shares. +/// O_NOFOLLOW refuses the link at open time — no check-then-open window to +/// race — and the fstat rejects directories and devices, doubling as the +/// symlink guard where O_NOFOLLOW is unavailable. (It guards the final +/// component only; intermediate directories are the tool's own, per the +/// threat model.) +pub async fn open_regular_nofollow( + src: &Path, +) -> Result<(tokio::fs::File, std::fs::Metadata), anyhow::Error> { + let mut opts = tokio::fs::OpenOptions::new(); + opts.read(true); + #[cfg(unix)] + opts.custom_flags(libc::O_NOFOLLOW); + let file = opts + .open(src) + .await + .with_context(|| format!("opening {}", src.display()))?; + let meta = file + .metadata() + .await + .with_context(|| format!("statting {}", src.display()))?; + if !meta.is_file() { + anyhow::bail!("{} is not a regular file", src.display()); + } + Ok((file, meta)) +} + #[cfg(test)] pub(crate) mod tests { use super::*; diff --git a/crates/diagnostics/src/lib.rs b/crates/diagnostics/src/lib.rs index 54ac4a2c2f..bf321288cc 100644 --- a/crates/diagnostics/src/lib.rs +++ b/crates/diagnostics/src/lib.rs @@ -30,7 +30,7 @@ pub mod logs; pub mod manifest; pub mod redact; -pub use bundle::{BundleSink, BundleWriter, LOG_TAIL_CAP}; +pub use bundle::{BundleSink, BundleWriter, LOG_TAIL_CAP, open_regular_nofollow}; pub use capture::{Capture, CaptureError, command_capture, first_stdout_line}; #[cfg(unix)] pub use disk::{DiskUsage, disk_usage}; diff --git a/crates/diagnostics/src/listing.rs b/crates/diagnostics/src/listing.rs index 0c3a81fb93..4a8c83b5fa 100644 --- a/crates/diagnostics/src/listing.rs +++ b/crates/diagnostics/src/listing.rs @@ -28,6 +28,10 @@ pub struct Listing { /// explicit trailing truncation marker. The walk is iterative (walkdir), so a /// pathologically deep tree cannot blow the stack, and symlinks are never /// followed. +/// +/// The walk is synchronous. Async callers must wrap it in +/// `tokio::task::spawn_blocking`, or a wedged filesystem blocks the worker +/// thread and their timeouts never fire. pub fn listing(root: &Path, max_entries: usize) -> Result { use std::fmt::Write as _; use std::os::unix::fs::MetadataExt as _; diff --git a/crates/minimal/src/diag/collect.rs b/crates/minimal/src/diag/collect.rs index e3c6e0cf48..ea635861eb 100644 --- a/crates/minimal/src/diag/collect.rs +++ b/crates/minimal/src/diag/collect.rs @@ -13,7 +13,7 @@ use serde::Serialize; use super::redact::is_env_value_allowlisted; use diagnostics::redact::{masked_process_env, redact_toml}; -use diagnostics::{BundleWriter, LOG_TAIL_CAP, Redaction}; +use diagnostics::{BundleWriter, LOG_TAIL_CAP, Redaction, open_regular_nofollow}; /// Cap on entries in a recursive state-dir listing; a runaway tree (huge /// session workspaces) is truncated with a marker line, not walked forever. @@ -184,15 +184,27 @@ pub async fn config(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyho ) .await; - if let Ok(mut entries) = tokio::fs::read_dir(paths.config.join("loadouts")).await { - while let Ok(Some(entry)) = entries.next_entry().await { - let path = entry.path(); - if path.extension().is_some_and(|e| e == "toml") - && let Some(name) = path.file_name().and_then(|n| n.to_str()) - { - add_redacted_toml(w, &path, &format!("config/loadouts/{name}.redacted")).await; + match tokio::fs::read_dir(paths.config.join("loadouts")).await { + Ok(mut entries) => loop { + match entries.next_entry().await { + Ok(Some(entry)) => { + let path = entry.path(); + if path.extension().is_some_and(|e| e == "toml") + && let Some(name) = path.file_name().and_then(|n| n.to_str()) + { + add_redacted_toml(w, &path, &format!("config/loadouts/{name}.redacted")) + .await; + } + } + Ok(None) => break, + Err(e) => { + w.skip("config/loadouts/", format!("listing interrupted: {e}")); + break; + } } - } + }, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => w.skip("config/loadouts/", format!("unreadable: {e}")), } // mTLS material: cert/CA metadata only, private key never touched. @@ -213,26 +225,30 @@ pub async fn config(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyho w.add_bytes("config/client-cert.json", &json, Redaction::ListingOnly) .await?; - // Mesh enrolment holds only a host:port endpoint. - match tokio::fs::read(&paths.mesh_enrolment).await { - Ok(bytes) => { - w.add_bytes("config/mesh-enrolment", &bytes, Redaction::None) + // Mesh enrolment holds only a host:port endpoint. No-follow like every + // other content read: it ships verbatim, so a symlink here would + // exfiltrate an arbitrary file byte-for-byte. + match read_string_nofollow(&paths.mesh_enrolment).await { + Ok(content) => { + w.add_bytes("config/mesh-enrolment", content.as_bytes(), Redaction::None) .await? } - Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} - Err(e) => w.skip("config/mesh-enrolment", format!("unreadable: {e}")), + Err(e) if is_not_found(&e) => {} + Err(e) => w.skip("config/mesh-enrolment", format!("unreadable: {e:#}")), } Ok(()) } /// Adds a TOML file with values redacted; a missing file is silently fine, -/// an unparseable or unreadable one is withheld with a manifest note. +/// an unparseable, unreadable, or symlinked one is withheld with a manifest +/// note (the no-follow discipline covers content reads, not just log tails — +/// a symlinked "config" file must not steer unrelated data into the bundle). async fn add_redacted_toml(w: &mut BundleWriter, src: &Path, dest: &str) { - let content = match tokio::fs::read_to_string(src).await { + let content = match read_string_nofollow(src).await { Ok(c) => c, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => return, + Err(e) if is_not_found(&e) => return, Err(e) => { - w.skip(dest, format!("unreadable: {e}")); + w.skip(dest, format!("unreadable: {e:#}")); return; } }; @@ -252,6 +268,17 @@ async fn add_redacted_toml(w: &mut BundleWriter, src: &Path, dest: &str) { } } +/// Reads a regular file to a string without following symlinks. +async fn read_string_nofollow(src: &Path) -> Result { + use tokio::io::AsyncReadExt as _; + let (mut file, _) = open_regular_nofollow(src).await?; + let mut content = String::new(); + file.read_to_string(&mut content) + .await + .with_context(|| format!("reading {}", src.display()))?; + Ok(content) +} + #[derive(Serialize)] struct FileMeta { bytes: u64, @@ -274,9 +301,15 @@ async fn file_meta(path: &Path) -> Option { pub async fn state(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { // Recursive listing: names, sizes, and metadata only — session workspace - // *contents* stay on the user's machine. + // *contents* stay on the user's machine. The walk is synchronous, so it + // runs on a blocking thread: a wedged filesystem must strand that + // thread, not the worker whose collect_step! timeout is the failsafe. + let state_dir = paths.state.clone(); let listing = - diagnostics::listing(&paths.state, LISTING_MAX_ENTRIES).context("listing state dir")?; + tokio::task::spawn_blocking(move || diagnostics::listing(&state_dir, LISTING_MAX_ENTRIES)) + .await + .context("listing worker")? + .context("listing state dir")?; w.add_bytes( "state/listing.txt", listing.text.as_bytes(), @@ -292,21 +325,28 @@ const LOG_FILES_PER_PREFIX: usize = 5; pub async fn logs(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { let log_dir = paths.state.join("logs"); - if !tokio::fs::try_exists(&log_dir).await.unwrap_or(false) { - // No log dir at all — nothing ever detached. Absence is data. - w.skip("logs/", "no log directory — no daemon has ever logged here"); - } else { - for prefix in ["minimald.log", "minvmd.log"] { - let files = diagnostics::newest_rotated(&log_dir, prefix, LOG_FILES_PER_PREFIX).await; - if files.is_empty() { - w.skip(format!("logs/{prefix}*"), "no files with this prefix"); - continue; - } - for path in files { - let name = path.file_name().unwrap_or_default().to_string_lossy(); - let dest = format!("logs/{name}"); - if let Err(e) = w.add_file_tail(&dest, &path, LOG_TAIL_CAP).await { - w.skip(&dest, format!("unreadable: {e}")); + // Absence and inaccessibility are different facts: "no log directory" + // may only be claimed on a real NotFound, never on EACCES or I/O errors. + match tokio::fs::metadata(&log_dir).await { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + // No log dir at all — nothing ever detached. Absence is data. + w.skip("logs/", "no log directory — no daemon has ever logged here"); + } + Err(e) => w.skip("logs/", format!("unreadable: {e}")), + Ok(_) => { + for prefix in ["minimald.log", "minvmd.log"] { + let files = + diagnostics::newest_rotated(&log_dir, prefix, LOG_FILES_PER_PREFIX).await; + if files.is_empty() { + w.skip(format!("logs/{prefix}*"), "no files with this prefix"); + continue; + } + for path in files { + let name = path.file_name().unwrap_or_default().to_string_lossy(); + let dest = format!("logs/{name}"); + if let Err(e) = w.add_file_tail(&dest, &path, LOG_TAIL_CAP).await { + w.skip(&dest, format!("unreadable: {e}")); + } } } } @@ -317,7 +357,7 @@ pub async fn logs(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow: // the VMM's hvc0 console capture — kernel prints and the guest pid-1's // stdout, the only evidence when the guest wedges before (or its // transport dies after) the daemon is reachable. - for (name, dir) in provider_dirs(&paths.state).await { + for (name, dir) in provider_dirs(&paths.state).await? { for log_name in ["run.log", "boot.log"] { let dest = format!("providers/{name}/{log_name}"); match w @@ -343,13 +383,17 @@ fn is_not_found(err: &anyhow::Error) -> bool { // ── providers/local-/ discovery ─────────────────────────────────────────── /// Provider instance dirs under `/providers`, sorted. Empty when the -/// providers dir doesn't exist (nothing was ever spawned). -pub async fn provider_dirs(state: &Path) -> Vec<(String, PathBuf)> { - let Ok(mut entries) = tokio::fs::read_dir(state.join("providers")).await else { - return Vec::new(); +/// providers dir doesn't exist (nothing was ever spawned); any other +/// filesystem error is an `Err` — "no daemon was ever spawned here" must +/// never be claimed on the strength of an EACCES. +pub async fn provider_dirs(state: &Path) -> Result, std::io::Error> { + let mut entries = match tokio::fs::read_dir(state.join("providers")).await { + Ok(entries) => entries, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()), + Err(e) => return Err(e), }; let mut dirs: Vec<(String, PathBuf)> = Vec::new(); - while let Ok(Some(entry)) = entries.next_entry().await { + while let Some(entry) = entries.next_entry().await? { if !entry.file_type().await.is_ok_and(|t| t.is_dir()) { continue; } @@ -361,5 +405,5 @@ pub async fn provider_dirs(state: &Path) -> Vec<(String, PathBuf)> { } } dirs.sort(); - dirs + Ok(dirs) } diff --git a/crates/minimal/src/diag/mod.rs b/crates/minimal/src/diag/mod.rs index 8201bdb3d6..1d2fbce510 100644 --- a/crates/minimal/src/diag/mod.rs +++ b/crates/minimal/src/diag/mod.rs @@ -77,22 +77,27 @@ pub async fn cmd_bug(global: &GlobalArgs, args: BugArgs) -> Result<(), anyhow::E // Daemon-side state (socket probes, guest bundles, per-provider status) // is collected over the daemon connection, not from the host bundle; - // record what exists so the manifest is honest about coverage. - let providers = collect::provider_dirs(&paths.state).await; - if providers.is_empty() { - w.skip( + // record what exists so the manifest is honest about coverage. An + // unreadable providers dir is an error, not absence. + let providers_started = std::time::Instant::now(); + match collect::provider_dirs(&paths.state).await { + Ok(providers) if providers.is_empty() => w.skip( "providers/", "no provider instances found — no daemon was ever spawned here", - ); - } else { - w.skip( + ), + Ok(providers) => w.skip( "providers/", format!( "{} provider instance(s) present — only run.log/boot.log captured; \ daemon-side state not collected", providers.len() ), - ); + ), + Err(e) => w.error( + "providers".to_string(), + format!("listing provider dirs: {e}"), + providers_started.elapsed(), + ), } let entries = w.entry_count(); diff --git a/crates/minimal/tests/bug.rs b/crates/minimal/tests/bug.rs index 47faa76e17..b7139d84fe 100644 --- a/crates/minimal/tests/bug.rs +++ b/crates/minimal/tests/bug.rs @@ -126,12 +126,31 @@ async fn planted_secrets_never_reach_the_bundle() { let config = fake_config_dir(); let args = global_args(state.path(), config.path()); + // A symlinked "loadout" pointing outside the config dir: TOML-shaped so + // it would survive redaction if the collector followed the link. + let outside = tempfile::TempDir::new().unwrap(); + let target = outside.path().join("outside.toml"); + std::fs::write(&target, "stolen = \"symlink-target-contents\"\n").unwrap(); + std::os::unix::fs::symlink(&target, config.path().join("minimal/loadouts/evil.toml")).unwrap(); + let out_dir = tempfile::TempDir::new().unwrap(); let out = out_dir.path().join("diag.tar.zst"); cmd_bug(&args, bug_args(&out)).await.unwrap(); let files = unpack(&out).await; + // The symlink was refused at open, its target never read, and the + // refusal recorded. + assert!(find(&files, "evil.toml.redacted").is_none()); + for contents in files.values() { + assert!( + !contents + .windows(b"symlink-target-contents".len()) + .any(|w| w == b"symlink-target-contents"), + "symlink target leaked into the bundle" + ); + } + // The loadout is included with its env-var value masked; keys and // non-secret values survive so the config is still diagnosable. let loadout = find(&files, "loadouts/dev.toml.redacted").expect("redacted loadout"); @@ -157,6 +176,15 @@ async fn planted_secrets_never_reach_the_bundle() { assert!(find(&files, "client.key").is_none()); let manifest: serde_json::Value = serde_json::from_slice(find(&files, "manifest.json").expect("manifest")).unwrap(); + assert!( + manifest["skipped"] + .as_array() + .unwrap() + .iter() + .any(|s| s["what"].as_str().unwrap().contains("evil.toml")), + "refused symlink must be recorded as a skip: {}", + manifest["skipped"] + ); assert!( manifest["skipped"] .as_array() From a634fd93bd52b3c4afa08629499a5aa1505b28e3 Mon Sep 17 00:00:00 2001 From: Norrie Taylor Date: Fri, 17 Jul 2026 15:21:37 -0700 Subject: [PATCH 3/4] fix(diagnostics): order rotated-log selection by mtime, not filename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reverse-lexicographic name order is only correct for date-suffixed rolling files. logrotate-style numeric shifting (live file bare, .1 the most recently rotated) sorts in exactly the opposite direction — name order would select the oldest files and drop the live log. Modified-time order is rotation-scheme agnostic; name order (descending) breaks ties. Refs: #801 Co-Authored-By: Claude Fable 5 --- crates/diagnostics/src/logs.rs | 62 +++++++++++++++++++++++++++------- crates/minimal/tests/bug.rs | 15 +++++--- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/crates/diagnostics/src/logs.rs b/crates/diagnostics/src/logs.rs index a9938f7378..78f8ccb1e1 100644 --- a/crates/diagnostics/src/logs.rs +++ b/crates/diagnostics/src/logs.rs @@ -1,20 +1,24 @@ //! Selection of rotated log files for diagnostic bundles. use std::path::{Path, PathBuf}; +use std::time::SystemTime; /// The newest `limit` files in `dir` whose names start with `prefix`, /// newest first. /// -/// "Newest" is reverse-lexicographic filename order: rolling filenames embed -/// their rotation point after the prefix (`minimald.log.2026-07-15`), so the -/// lexicographically greatest name is the most recent. A missing or -/// unreadable `dir` yields an empty list — callers that need to distinguish -/// "no log dir" from "no matches" check the directory first. +/// "Newest" is modified-time order, which is rotation-scheme agnostic: it is +/// correct for date-suffixed rolling names (`app.log.2026-07-15`) and for +/// logrotate-style numeric shifting (`app.log` live, `app.log.1` the most +/// recently rotated) alike — filename order is not, since the two schemes +/// sort in opposite directions. Name order (descending) breaks ties for +/// determinism. A missing or unreadable `dir` yields an empty list — callers +/// that need to distinguish "no log dir" from "no matches" check the +/// directory first. pub async fn newest_rotated(dir: &Path, prefix: &str, limit: usize) -> Vec { let Ok(mut entries) = tokio::fs::read_dir(dir).await else { return Vec::new(); }; - let mut matches: Vec = Vec::new(); + let mut matches: Vec<(SystemTime, PathBuf)> = Vec::new(); while let Ok(Some(entry)) = entries.next_entry().await { let path = entry.path(); if path @@ -22,26 +26,41 @@ pub async fn newest_rotated(dir: &Path, prefix: &str, limit: usize) -> Vec = picked @@ -61,6 +80,25 @@ mod tests { ); } + #[tokio::test] + async fn logrotate_style_numeric_suffixes_order_by_recency() { + let tmp = tempfile::TempDir::new().unwrap(); + // logrotate-style shift: the live file is newest, `.1` the most + // recently rotated, higher suffixes older. Reverse filename order + // would return exactly the wrong end of this list. + write_with_mtime(&tmp.path().join("app.log"), 0); + write_with_mtime(&tmp.path().join("app.log.1"), 100); + write_with_mtime(&tmp.path().join("app.log.2"), 200); + write_with_mtime(&tmp.path().join("app.log.10"), 1000); + + let picked = newest_rotated(tmp.path(), "app.log", 3).await; + let names: Vec<&str> = picked + .iter() + .map(|p| p.file_name().unwrap().to_str().unwrap()) + .collect(); + assert_eq!(names, ["app.log", "app.log.1", "app.log.2"]); + } + #[tokio::test] async fn missing_dir_is_empty() { assert!( diff --git a/crates/minimal/tests/bug.rs b/crates/minimal/tests/bug.rs index b7139d84fe..0621dda50d 100644 --- a/crates/minimal/tests/bug.rs +++ b/crates/minimal/tests/bug.rs @@ -204,11 +204,16 @@ async fn logs_collects_newest_five_per_prefix_and_provider_logs() { let log_dir = state.path().join("logs"); std::fs::create_dir_all(&log_dir).unwrap(); for day in 1..=7 { - std::fs::write( - log_dir.join(format!("minimald.log.2026-07-{day:02}")), - format!("day {day}\n"), - ) - .unwrap(); + // Older dates get explicitly older mtimes: selection is + // modified-time order, not filename order. + let path = log_dir.join(format!("minimald.log.2026-07-{day:02}")); + std::fs::write(&path, format!("day {day}\n")).unwrap(); + std::fs::File::open(&path) + .unwrap() + .set_modified( + std::time::SystemTime::now() - std::time::Duration::from_secs((8 - day) * 100), + ) + .unwrap(); } let provider = state.path().join("providers/local-0"); From 80206fc0f68f578e7e9b9fca8882e65ba10ce4a3 Mon Sep 17 00:00:00 2001 From: Norrie Taylor Date: Fri, 17 Jul 2026 15:57:15 -0700 Subject: [PATCH 4/4] refactor(diagnostics,minimal): move the system probe into the diagnostics crate Every bundle-producing binary wants the same opening facts (OS, arch, kernel, hostname, cpu count, euid, KVM availability, disk capacity), so SystemInfo and its probes move from the CLI collector into diagnostics::system, with the disk paths staying caller policy. The CLI keeps only its serialization into host/system.json, and its direct libc dependency goes with the probe. Refs: #801 Co-Authored-By: Claude Fable 5 --- Cargo.lock | 1 - crates/diagnostics/src/lib.rs | 5 + crates/diagnostics/src/system.rs | 159 +++++++++++++++++++++++++++++ crates/minimal/Cargo.toml | 1 - crates/minimal/src/diag/collect.rs | 121 +--------------------- 5 files changed, 167 insertions(+), 120 deletions(-) create mode 100644 crates/diagnostics/src/system.rs diff --git a/Cargo.lock b/Cargo.lock index 4442eb1764..d1ba658490 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3616,7 +3616,6 @@ dependencies = [ "constcat", "diagnostics", "dirs", - "libc", "mctx", "mfile", "minimald", diff --git a/crates/diagnostics/src/lib.rs b/crates/diagnostics/src/lib.rs index bf321288cc..fa927a2910 100644 --- a/crates/diagnostics/src/lib.rs +++ b/crates/diagnostics/src/lib.rs @@ -16,6 +16,7 @@ //! collectors //! - [`logs`] — newest-first selection of rotated log files //! - [`disk`] — filesystem capacity probes +//! - [`system`] — host identity/capability probe (OS, kernel, KVM, disks) //! //! The split with the app crates is mechanics vs. policy: the *how* lives //! here; the *what* — which paths, which log prefixes, which env names are @@ -29,6 +30,8 @@ pub mod listing; pub mod logs; pub mod manifest; pub mod redact; +#[cfg(unix)] +pub mod system; pub use bundle::{BundleSink, BundleWriter, LOG_TAIL_CAP, open_regular_nofollow}; pub use capture::{Capture, CaptureError, command_capture, first_stdout_line}; @@ -37,3 +40,5 @@ pub use disk::{DiskUsage, disk_usage}; pub use listing::{Listing, listing}; pub use logs::newest_rotated; pub use manifest::{CollectedEntry, CollectorError, Manifest, Redaction, SkippedEntry}; +#[cfg(unix)] +pub use system::{DiskInfo, SystemInfo, system_info}; diff --git a/crates/diagnostics/src/system.rs b/crates/diagnostics/src/system.rs new file mode 100644 index 0000000000..0a4d85c6e2 --- /dev/null +++ b/crates/diagnostics/src/system.rs @@ -0,0 +1,159 @@ +//! Host system identity and capability probe for diagnostic bundles. +//! +//! Every binary that produces a bundle wants the same opening facts — what +//! OS/arch/kernel is this, who is running it, can it virtualize, how full +//! are the disks that matter. The probe is the mechanic; *which* disks +//! matter is the caller's policy, passed in as paths. + +use std::path::Path; +use std::time::Duration; + +use serde::Serialize; + +/// Timeout for single-fact commands like `uname`; a wedged tool must not +/// eat a collector's whole budget. +const FACT_TIMEOUT: Duration = Duration::from_secs(5); + +/// The opening facts of a diagnostic bundle. Every probe is best-effort: +/// a field that cannot be read is `None`, never an error. +#[derive(Debug, Serialize)] +#[non_exhaustive] +pub struct SystemInfo { + pub os: &'static str, + pub arch: &'static str, + pub kernel: Option, + pub hostname: Option, + pub ncpu: Option, + pub euid: u32, + #[cfg(target_os = "linux")] + pub kvm: KvmInfo, + pub disks: Vec, +} + +#[cfg(target_os = "linux")] +#[derive(Debug, Serialize)] +#[non_exhaustive] +pub struct KvmInfo { + pub exists: bool, + /// `ok` / `enoent` / `eacces` / other errno string — ENOENT means the + /// module isn't loaded, EACCES means the user isn't in the kvm group. + pub open_result: String, + pub mode: Option, + pub owner_gid: Option, + pub user_in_kvm_group: Option, +} + +/// Capacity of one caller-chosen path's filesystem, `None`s when the path +/// doesn't exist (which is itself data). +#[derive(Debug, Serialize)] +#[non_exhaustive] +pub struct DiskInfo { + pub path: String, + pub total_bytes: Option, + pub free_bytes: Option, +} + +/// Gathers [`SystemInfo`], probing the filesystems holding `disk_paths` +/// (the caller's policy: state dir, cache dir, the bundle's destination — +/// whatever disk-full would break for that binary). +pub async fn system_info(disk_paths: &[&Path]) -> SystemInfo { + let kernel = crate::first_stdout_line("uname", &["-srvm"], FACT_TIMEOUT).await; + let hostname = crate::first_stdout_line("uname", &["-n"], FACT_TIMEOUT).await; + SystemInfo { + os: std::env::consts::OS, + arch: std::env::consts::ARCH, + kernel, + hostname, + ncpu: std::thread::available_parallelism().map(usize::from).ok(), + // SAFETY: geteuid has no failure modes or preconditions. + euid: unsafe { libc::geteuid() }, + #[cfg(target_os = "linux")] + kvm: kvm_info(), + disks: disk_paths.iter().map(|p| disk_info(p)).collect(), + } +} + +fn disk_info(path: &Path) -> DiskInfo { + let usage = crate::disk_usage(path); + DiskInfo { + path: path.display().to_string(), + total_bytes: usage.map(|u| u.total_bytes), + free_bytes: usage.map(|u| u.free_bytes), + } +} + +#[cfg(target_os = "linux")] +fn kvm_info() -> KvmInfo { + use std::os::unix::fs::MetadataExt as _; + let meta = std::fs::metadata("/dev/kvm").ok(); + let open_result = match std::fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/kvm") + { + Ok(_) => "ok".to_string(), + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => "enoent".to_string(), + std::io::ErrorKind::PermissionDenied => "eacces".to_string(), + _ => e.to_string(), + }, + }; + KvmInfo { + exists: meta.is_some(), + open_result, + mode: meta.as_ref().map(|m| format!("{:o}", m.mode())), + owner_gid: meta.as_ref().map(|m| m.gid()), + user_in_kvm_group: user_in_group("kvm"), + } +} + +/// Whether the current user's supplementary groups include `name`. +/// `None` when the group doesn't exist or the group list can't be read. +#[cfg(target_os = "linux")] +fn user_in_group(name: &str) -> Option { + let c_name = std::ffi::CString::new(name).ok()?; + // SAFETY: getgrnam reads a NUL-terminated string; the returned struct is + // a static buffer only read before any other getgr* call in this thread. + let grp = unsafe { libc::getgrnam(c_name.as_ptr()) }; + if grp.is_null() { + return None; + } + // SAFETY: non-null getgrnam result points to a valid `group`. + let target_gid = unsafe { (*grp).gr_gid }; + + // SAFETY: first call sizes the buffer, second fills it. + let n = unsafe { libc::getgroups(0, std::ptr::null_mut()) }; + if n < 0 { + return None; + } + let mut gids = vec![0 as libc::gid_t; n as usize]; + // SAFETY: buffer is exactly the size getgroups asked for. + let n = unsafe { libc::getgroups(n, gids.as_mut_ptr()) }; + if n < 0 { + return None; + } + gids.truncate(n as usize); + // SAFETY: getegid has no failure modes. + let egid = unsafe { libc::getegid() }; + Some(gids.contains(&target_gid) || egid == target_gid) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn probes_are_best_effort_and_disks_follow_caller_paths() { + let tmp = tempfile::TempDir::new().unwrap(); + let info = system_info(&[tmp.path(), Path::new("/nonexistent/diag-x")]).await; + assert_eq!(info.os, std::env::consts::OS); + assert_eq!(info.disks.len(), 2); + assert!(info.disks[0].total_bytes.is_some()); + assert!( + info.disks[1].total_bytes.is_none(), + "a missing path is Nones, not an error" + ); + let json = serde_json::to_value(&info).unwrap(); + assert!(json["euid"].is_u64()); + } +} diff --git a/crates/minimal/Cargo.toml b/crates/minimal/Cargo.toml index 5d02a8c851..17a8c8a299 100644 --- a/crates/minimal/Cargo.toml +++ b/crates/minimal/Cargo.toml @@ -31,7 +31,6 @@ chrono.workspace = true constcat.workspace = true diagnostics.workspace = true dirs.workspace = true -libc.workspace = true mctx.workspace = true mfile.workspace = true minimald-rpc.workspace = true diff --git a/crates/minimal/src/diag/collect.rs b/crates/minimal/src/diag/collect.rs index ea635861eb..25d118432d 100644 --- a/crates/minimal/src/diag/collect.rs +++ b/crates/minimal/src/diag/collect.rs @@ -36,130 +36,15 @@ pub struct DiagPaths { // ── host/system.json ───────────────────────────────────────────────────────── -#[derive(Serialize)] -struct SystemInfo { - os: &'static str, - arch: &'static str, - kernel: Option, - hostname: Option, - ncpu: Option, - euid: u32, - #[cfg(target_os = "linux")] - kvm: KvmInfo, - disks: Vec, -} - -#[cfg(target_os = "linux")] -#[derive(Serialize)] -struct KvmInfo { - exists: bool, - /// `ok` / `enoent` / `eacces` / other errno string — ENOENT means the - /// module isn't loaded, EACCES means the user isn't in the kvm group. - open_result: String, - mode: Option, - owner_gid: Option, - user_in_kvm_group: Option, -} - -#[derive(Serialize)] -struct DiskInfo { - path: String, - total_bytes: Option, - free_bytes: Option, -} - -/// Timeout for single-fact commands like `uname`; a wedged tool must not -/// eat the collector's whole 30 s budget. -const FACT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); - pub async fn system(w: &mut BundleWriter, paths: &DiagPaths) -> Result<(), anyhow::Error> { - let kernel = diagnostics::first_stdout_line("uname", &["-srvm"], FACT_TIMEOUT).await; - let hostname = diagnostics::first_stdout_line("uname", &["-n"], FACT_TIMEOUT).await; - let info = SystemInfo { - os: std::env::consts::OS, - arch: std::env::consts::ARCH, - kernel, - hostname, - ncpu: std::thread::available_parallelism().map(usize::from).ok(), - // SAFETY: geteuid has no failure modes or preconditions. - euid: unsafe { libc::geteuid() }, - #[cfg(target_os = "linux")] - kvm: kvm_info(), - disks: [&paths.state, &paths.cache, &paths.cwd] - .into_iter() - .map(|p| disk_info(p)) - .collect(), - }; + // Which filesystems matter is this binary's policy: the state dir, the + // cache dir, and the invoking directory the archive is written into. + let info = diagnostics::system_info(&[&paths.state, &paths.cache, &paths.cwd]).await; let json = serde_json::to_vec_pretty(&info).context("serializing system info")?; w.add_bytes("host/system.json", &json, Redaction::None) .await } -#[cfg(target_os = "linux")] -fn kvm_info() -> KvmInfo { - use std::os::unix::fs::MetadataExt as _; - let meta = std::fs::metadata("/dev/kvm").ok(); - let open_result = match std::fs::OpenOptions::new() - .read(true) - .write(true) - .open("/dev/kvm") - { - Ok(_) => "ok".to_string(), - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => "enoent".to_string(), - std::io::ErrorKind::PermissionDenied => "eacces".to_string(), - _ => e.to_string(), - }, - }; - KvmInfo { - exists: meta.is_some(), - open_result, - mode: meta.as_ref().map(|m| format!("{:o}", m.mode())), - owner_gid: meta.as_ref().map(|m| m.gid()), - user_in_kvm_group: user_in_group("kvm"), - } -} - -/// Whether the current user's supplementary groups include `name`. -/// `None` when the group doesn't exist or the group list can't be read. -#[cfg(target_os = "linux")] -fn user_in_group(name: &str) -> Option { - let c_name = std::ffi::CString::new(name).ok()?; - // SAFETY: getgrnam reads a NUL-terminated string; the returned struct is - // a static buffer only read before any other getgr* call in this thread. - let grp = unsafe { libc::getgrnam(c_name.as_ptr()) }; - if grp.is_null() { - return None; - } - // SAFETY: non-null getgrnam result points to a valid `group`. - let target_gid = unsafe { (*grp).gr_gid }; - - // SAFETY: first call sizes the buffer, second fills it. - let n = unsafe { libc::getgroups(0, std::ptr::null_mut()) }; - if n < 0 { - return None; - } - let mut gids = vec![0 as libc::gid_t; n as usize]; - // SAFETY: buffer is exactly the size getgroups asked for. - let n = unsafe { libc::getgroups(n, gids.as_mut_ptr()) }; - if n < 0 { - return None; - } - gids.truncate(n as usize); - // SAFETY: getegid has no failure modes. - let egid = unsafe { libc::getegid() }; - Some(gids.contains(&target_gid) || egid == target_gid) -} - -fn disk_info(path: &Path) -> DiskInfo { - let usage = diagnostics::disk_usage(path); - DiskInfo { - path: path.display().to_string(), - total_bytes: usage.map(|u| u.total_bytes), - free_bytes: usage.map(|u| u.free_bytes), - } -} - // ── host/env.json ──────────────────────────────────────────────────────────── pub async fn env(w: &mut BundleWriter) -> Result<(), anyhow::Error> {