diff --git a/.github/scripts/check-no-test-trusted-root.sh b/.github/scripts/check-no-test-trusted-root.sh index 12589ef..c24a06d 100755 --- a/.github/scripts/check-no-test-trusted-root.sh +++ b/.github/scripts/check-no-test-trusted-root.sh @@ -54,15 +54,89 @@ if ! grep -q 'refusing an optimized build' "$err"; then exit 1 fi -# Every release, publish, or tag workflow must run this script. Prove the -# guard rejects such a workflow without it, then check the real ones. + +# Every release, publish, or tag workflow must run this script on a run: +# line. Prove the guard rejects each release form without it (including a +# comment-only mention), accepts it with it, then check the real workflows. probe=$(mktemp -d) trap 'rm -f "$err"; rm -rf "$probe"' EXIT -printf 'on:\n push:\n tags: ["v*"]\njobs:\n p:\n runs-on: ubuntu-latest\n steps:\n - run: cargo publish --dry-run\n' >"$probe/release.yml" -if "$root/.github/scripts/check-release-workflows.sh" "$probe" 2>/dev/null; then - echo "release-workflow guard accepted a workflow without this check" >&2 - exit 1 -fi -printf ' - run: .github/scripts/check-no-test-trusted-root.sh\n' >>"$probe/release.yml" -"$root/.github/scripts/check-release-workflows.sh" "$probe" -"$root/.github/scripts/check-release-workflows.sh" +guard="$root/.github/scripts/check-release-workflows.sh" +case_no=0 +# expect_guard want(pass|fail) name < workflow +expect_guard() { + case_no=$((case_no + 1)) + d="$probe/$case_no" + mkdir -p "$d" + cat >"$d/wf.yml" + if "$guard" "$d" 2>/dev/null; then got=pass; else got=fail; fi + if [ "$got" != "$1" ]; then + echo "release-workflow guard: $2: expected $1, got $got" >&2 + exit 1 + fi +} +steps='jobs: + p: + runs-on: ubuntu-latest + steps: + - run: echo build' +check=' - run: .github/scripts/check-no-test-trusted-root.sh' + +expect_guard fail "push tags" <` block. set -eu root=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) dir=${1:-"$root/.github/workflows"} +# Print the file with YAML/shell comments removed. +uncommented() { + sed -e 's/^[[:space:]]*#.*$//' -e 's/[[:space:]]#.*$//' "$1" +} + +releases() { + uncommented "$1" | grep -E -q \ + -e '^[[:space:]]*(release|registry_package|tags):' \ + -e '^[[:space:]]*["'\'']?on["'\'']?:.*(^|[^A-Za-z0-9_-])(release|registry_package)([^A-Za-z0-9_-]|$)' \ + -e 'cargo[[:space:]]+publish' \ + -e 'gh[[:space:]]+release' \ + -e 'softprops/action-gh-release' \ + -e 'git[[:space:]]+push.*--tags' \ + -e 'git[[:space:]]+tag[[:space:]]' +} + +runs_guard() { + uncommented "$1" | awk ' + { + line = $0 + ind = match(line, /[^ ]/) - 1 + if (inblk && line ~ /[^ \t]/ && ind <= blkind) inblk = 0 + if (inblk) { + if (line ~ /check-no-test-trusted-root\.sh/) found = 1 + next + } + if (line ~ /^[ \t]*(-[ \t]+)?run:/) { + if (line ~ /run:[ \t]*[|>][-+0-9]*[ \t]*$/) { + inblk = 1 + blkind = ind + } else if (line ~ /check-no-test-trusted-root\.sh/) { + found = 1 + } + } + } + END { exit found ? 0 : 1 }' +} + fail=0 for wf in "$dir"/*.yml "$dir"/*.yaml; do [ -f "$wf" ] || continue - if grep -E -q '^[[:space:]]*(release|tags|registry_package):|cargo[[:space:]]+publish|gh[[:space:]]+release|softprops/action-gh-release|git[[:space:]]+push[^#]*--tags|git[[:space:]]+tag[[:space:]]' "$wf"; then - if ! grep -q 'check-no-test-trusted-root\.sh' "$wf"; then - echo "$wf: release/publish/tag workflow does not run check-no-test-trusted-root.sh" >&2 - fail=1 - fi + if releases "$wf" && ! runs_guard "$wf"; then + echo "$wf: release/publish/tag workflow does not run check-no-test-trusted-root.sh on a run: line" >&2 + fail=1 fi done exit "$fail" diff --git a/CHANGELOG.md b/CHANGELOG.md index 677227e..6741a25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Versioning: se - `geode run --home` refuses a path that is itself a symlink. The flag docs now say plainly that these checks are a usability guard, not a security boundary. - CI fails if any release, publish, or tag workflow does not run `check-no-test-trusted-root.sh`. - The interim custody argv lint compares path components and requires the pinned command to sit directly in `/usr/local/libexec/hedronite`. +- `geode-grotto` also refuses its `test-trusted-root` feature in any optimized build, matching the CLI guard. +- The release-workflow guard ignores comments, requires `check-no-test-trusted-root.sh` on a `run:` line or inside a run block, and detects release triggers in map, flow-list (`on: [release]`), and scalar (`on: release`) form. +- `cargo test -p geode-cli` with default features passes. The run tests that spawn a child with a secret need the test-only host override and are compiled only with `test-trusted-root`, which CI uses. - Local `geode run` enforces `allow_hosts` for every secret binding on every platform, including macOS. The check is the custody allowlist (`AllowList`): a host that is not listed is `host_denied` and is not dialed. A secret binding with an empty or unusable `allow_hosts` is refused before spawn (non-zero exit). There is no environment variable, flag, or config key that disables the check. Pass hosts with repeatable `--allow-host`. macOS confines the child with a deny-by-default seatbelt: file and process access is limited to what the child needs to run, mach-lookup is a named allowlist with no network service, Apple events are denied, and network helper programs cannot be exec'd. LaunchServices, the preferences daemon, and the keychain service are not on that list. The seatbelt used when there is no egress proxy is deny-by-default as well and has no outbound network. Outbound network for a confined run is allowed only to the local egress proxy. The proxy listens on both loopback stacks, because the seatbelt loopback name covers both, and refuses to start if either bind fails. Each run puts a token in the proxy URL; a request without it is not dialed. A process that can read the child environment can present that token. A DNS answer in a loopback, private, link-local, multicast, benchmarking, reserved, NAT64, or other non-global range is not dialed unless the request host is an IP literal already on `allow_hosts`. Proxy denials are audit rows with outcome `host_denied`. Linux keeps the network namespace and the same proxy checks. - The egress dial filter treats the local-use NAT64 prefix `64:ff9b:1::/48` as restricted, next to the well-known prefix `64:ff9b::/96`. A name that resolves into that range is not dialed unless the request host is an IP literal already on `allow_hosts`. diff --git a/crates/geode-cli/tests/p0_custody_home.rs b/crates/geode-cli/tests/p0_custody_home.rs index 3398a65..23bfabc 100644 --- a/crates/geode-cli/tests/p0_custody_home.rs +++ b/crates/geode-cli/tests/p0_custody_home.rs @@ -67,7 +67,6 @@ fn has_secret_bytes(text: &str) -> bool { MARKERS.iter().any(|m| text.contains(m)) } -/// Every SPEC rule `text` breaks. Empty means the argv is valid. /// The pinned command must be a file directly in the libexec dir. Compares /// path components, not text. `Path::components` drops `//`, a trailing /// `/`, and inner `.`, so the raw string is checked for those too. @@ -87,6 +86,7 @@ fn pinned_cmd_path(path: &str) -> bool { p.parent() == Some(Path::new(DIR)) && p.file_name().is_some_and(|n| !n.is_empty()) } +/// Every SPEC rule `text` breaks. Empty means the argv is valid. fn broken_rules(text: &str) -> BTreeSet<&'static str> { let mut bad = BTreeSet::new(); if text.contains('\0') { diff --git a/crates/geode-cli/tests/run_cli.rs b/crates/geode-cli/tests/run_cli.rs index 939822e..0d3d3b2 100644 --- a/crates/geode-cli/tests/run_cli.rs +++ b/crates/geode-cli/tests/run_cli.rs @@ -19,6 +19,12 @@ //! `test-trusted-root`). That feature is also what builds the helper, so //! the helper tests are compiled only when it is on. A root-owned install //! is `#[ignore]` and is not part of the default run. +//! +//! Tests that spawn a child with a secret need `--test-unconfined-hosts`, +//! which exists only with `test-trusted-root`, because a secret run with no +//! allowed hosts is refused before spawn. They are compiled only with that +//! feature, so a default-feature run stays green and still runs the no-host +//! refusal tests. CI runs this file with the feature. use std::path::{Path, PathBuf}; use std::process::{Command, Output}; @@ -161,6 +167,7 @@ fn canonical_on_path(name: &str) -> String { .to_owned() } +#[cfg(feature = "test-trusted-root")] fn on_path(name: &str) -> bool { std::env::var_os("PATH") .is_some_and(|p| std::env::split_paths(&p).any(|d| d.join(name).is_file())) @@ -191,6 +198,7 @@ fn assert_no_dummy(o: &Output, what: &str) { ); } +#[cfg(feature = "test-trusted-root")] #[test] fn echo_base64_xxd_redact_and_row_is_local() { let fx = Fx::new(); @@ -247,6 +255,7 @@ fn echo_base64_xxd_redact_and_row_is_local() { ); } +#[cfg(feature = "test-trusted-root")] #[test] fn child_env_is_built_from_scratch() { let fx = Fx::new(); @@ -271,6 +280,7 @@ fn child_env_is_built_from_scratch() { assert!(!env.contains("XDG_STATE_HOME")); } +#[cfg(feature = "test-trusted-root")] #[test] fn child_exit_3_is_spawned_and_passed_through() { let fx = Fx::new(); @@ -300,6 +310,7 @@ fn child_exit_3_is_spawned_and_passed_through() { assert_no_dummy(&j, "json"); } +#[cfg(feature = "test-trusted-root")] #[test] fn timeout_terms_the_child() { let fx = Fx::new(); @@ -429,6 +440,7 @@ fn local_allow_hosts_outcome_is_per_os() { } } +#[cfg(feature = "test-trusted-root")] #[test] fn refusals_do_not_spawn() { let fx = Fx::new(); @@ -497,6 +509,7 @@ fn refusals_do_not_spawn() { assert!(!fx.marker().exists(), "a refused run spawned a child"); } +#[cfg(feature = "test-trusted-root")] #[test] fn policy_deny_is_exit_3_and_does_not_spawn() { let fx = Fx::new(); @@ -535,6 +548,7 @@ fn policy_deny_is_exit_3_and_does_not_spawn() { assert_eq!((*custody, argv0.as_str(), *exit_code), (0, "", None)); } +#[cfg(feature = "test-trusted-root")] #[test] fn needle_in_argv_is_argv_rejected_without_hashing_argv() { let fx = Fx::new(); @@ -651,6 +665,7 @@ fn allow_cmd_must_be_canonical() { } } +#[cfg(feature = "test-trusted-root")] #[test] fn audit_verify_good_then_tampered() { let fx = Fx::new(); @@ -733,6 +748,7 @@ fn fix3_audit_failure_before_spawn_releases_nothing() { assert_eq!(fx.rows().len(), before, "no row was written"); } +#[cfg(feature = "test-trusted-root")] #[test] fn fix3_success_writes_one_intent_row_without_the_value() { let fx = Fx::new(); @@ -757,6 +773,7 @@ fn fix3_success_writes_one_intent_row_without_the_value() { assert_eq!(code(&v), Some(0), "{}", err(&v)); } +#[cfg(feature = "test-trusted-root")] #[test] fn fix3_close_row_failure_is_non_zero_but_output_still_redacted() { let fx = Fx::new(); @@ -773,6 +790,7 @@ fn fix3_close_row_failure_is_non_zero_but_output_still_redacted() { assert_eq!(fx.rows().last().unwrap().0, "started", "intent row stays"); } +#[cfg(feature = "test-trusted-root")] #[test] fn fix3_deny_path_writes_row_or_fails_closed() { let fx = Fx::new(); @@ -795,6 +813,7 @@ fn fix3_deny_path_writes_row_or_fails_closed() { assert!(!fx.marker().exists()); } +#[cfg(feature = "test-trusted-root")] fn sha256_hex(path: &Path) -> String { use sha2::{Digest, Sha256}; Sha256::digest(std::fs::read(path).expect("read pin target")) @@ -805,6 +824,7 @@ fn sha256_hex(path: &Path) -> String { /// Copy `src` to `dir/bin/name` as a 0755 file. The pin path is this absolute /// path. A `#!` file cannot be pinned on Linux. +#[cfg(feature = "test-trusted-root")] fn copy_exec(dir: &Path, name: &str, src: &Path) -> PathBuf { use std::os::unix::fs::PermissionsExt; let bin = dir.join("bin"); @@ -821,6 +841,7 @@ fn copy_exec(dir: &Path, name: &str, src: &Path) -> PathBuf { dest } +#[cfg(feature = "test-trusted-root")] fn write_pins(dir: &Path, rules: &serde_json::Value) { std::fs::write(dir.join("pins.json"), serde_json::to_vec(rules).unwrap()).unwrap(); } @@ -1266,6 +1287,7 @@ fn pinned_user_tmp_writable_and_symlink_are_refused() { let _ = std::fs::remove_dir_all(&base); } +#[cfg(feature = "test-trusted-root")] #[test] fn pinned_wrong_digest_refused() { let fx = Fx::new(); @@ -1298,6 +1320,7 @@ fn pinned_wrong_digest_refused() { assert_eq!(fx.rows().last().unwrap().0, "policy_deny"); } +#[cfg(feature = "test-trusted-root")] #[test] fn unpinned_path_refused() { let fx = Fx::new(); @@ -1328,6 +1351,7 @@ fn unpinned_path_refused() { assert_eq!(fx.rows().last().unwrap().0, "policy_deny"); } +#[cfg(feature = "test-trusted-root")] #[test] fn args_prefix_mismatch_refused() { let fx = Fx::new(); @@ -1359,6 +1383,7 @@ fn args_prefix_mismatch_refused() { assert!(!fx.marker().exists(), "args_prefix mismatch spawned"); } +#[cfg(feature = "test-trusted-root")] #[test] fn relative_pin_refused() { let fx = Fx::new(); @@ -1422,6 +1447,7 @@ fn relative_pin_refused() { assert!(!fx.marker().exists()); } +#[cfg(feature = "test-trusted-root")] #[test] fn symlink_swap_refused() { use std::os::unix::fs::PermissionsExt; @@ -1478,6 +1504,7 @@ fn symlink_swap_refused() { assert!(!marker.exists(), "symlink swap spawned"); } +#[cfg(feature = "test-trusted-root")] #[test] fn require_pin_refuses_unpinned_rules_and_off_allows_them() { let fx = Fx::new(); diff --git a/crates/geode-core/Cargo.toml b/crates/geode-core/Cargo.toml index 52ba122..118fa53 100644 --- a/crates/geode-core/Cargo.toml +++ b/crates/geode-core/Cargo.toml @@ -16,10 +16,10 @@ path = "src/lib.rs" default = [] # Test-only and insecure. Off unless a build asks for it. Exposes # `plan_with_test_root`, which moves the pin trust root. The crate -# refuses to compile this feature only when debug assertions are off -# (`cargo build --release`, `cargo install`). A dev or test build can -# still enable it. That is not a release guarantee. Release builds of -# `geode` do not enable it. +# refuses to compile this feature when debug assertions are off +# (`cargo build --release`, `cargo install`) and, from build.rs, at any +# opt-level other than 0. A plain dev or test build can still enable it. +# Release builds of `geode` do not enable it. test-trusted-root = [] [dependencies] diff --git a/crates/geode-core/build.rs b/crates/geode-core/build.rs new file mode 100644 index 0000000..2f5f963 --- /dev/null +++ b/crates/geode-core/build.rs @@ -0,0 +1,20 @@ +//! Build-time guard for the test-only `test-trusted-root` feature. +//! +//! `src/inject.rs` refuses the feature when debug assertions are off. A +//! custom profile can turn debug assertions on with optimizations still +//! enabled, so this script also refuses the feature at any `opt-level` +//! other than 0. Debug assertions are not checked here: older Cargo does +//! not pass that cfg to build scripts. Test and dev builds use opt-level 0 +//! and are unaffected. + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_TEST_TRUSTED_ROOT"); + let feature = std::env::var_os("CARGO_FEATURE_TEST_TRUSTED_ROOT").is_some(); + let opt = std::env::var("OPT_LEVEL").unwrap_or_default(); + if feature && opt != "0" { + panic!( + "feature `test-trusted-root` is test-only; refusing an optimized build (opt-level {opt})" + ); + } +} diff --git a/crates/geode-core/src/inject.rs b/crates/geode-core/src/inject.rs index 4335144..f88ec47 100644 --- a/crates/geode-core/src/inject.rs +++ b/crates/geode-core/src/inject.rs @@ -227,9 +227,11 @@ pub fn plan( plan_anchored(caller, vault, req, Path::new("/"), 0) } -// The compile refusal is `not(debug_assertions)` only (`cargo build --release`, -// `cargo install`). A dev or test build can still enable `test-trusted-root`. -// That is not a release guarantee. +// Two compile refusals keep `test-trusted-root` out of an optimized build: +// this one (`not(debug_assertions)`: `cargo build --release`, `cargo +// install`), and `build.rs`, which also refuses any `opt-level` other than +// 0, so a custom profile with debug assertions on cannot ship it. A plain +// dev or test build can still enable the feature. #[cfg(all(feature = "test-trusted-root", not(debug_assertions)))] compile_error!( "feature `test-trusted-root` is test-only and insecure (it moves the pin trust root); \ @@ -240,9 +242,9 @@ compile_error!( /// /// Test-only and insecure. Compiled for unit tests and for the /// `test-trusted-root` feature, which lets a caller move the pin trust -/// root. The feature fails to compile only when debug assertions are off -/// (`cargo build --release`, `cargo install`). A dev build can still enable -/// it. A release `geode` does not call this and does not contain it. +/// root. The feature fails to compile when debug assertions are off +/// (`cargo build --release`, `cargo install`) or at any opt-level other +/// than 0 (`build.rs`). A plain dev build can still enable it. A release `geode` does not call this and does not contain it. #[cfg(any(test, feature = "test-trusted-root"))] pub fn plan_with_test_root( caller: &Caller<'_>,