diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 83e3ff9..70c9b56 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -23,6 +23,8 @@ jobs: run: bash infra/bootstrap/test-host-guard.sh - name: Check verifier helper argument guard run: bash infra/bootstrap/test-verify-helper.sh + - name: Check unprivileged guard test helper + run: bash infra/test/test-run-as-unprivileged.sh - name: Check host audit root guard run: bash infra/bootstrap/test-audit-host.sh - name: Check Compose helper allowlist @@ -43,6 +45,7 @@ jobs: infra/deploy/install-helper.sh infra/deploy/compose-helper.sh infra/deploy/test-compose-helper.sh \ infra/deploy/prepare-production-config.sh infra/deploy/test-production-config-guard.sh \ infra/bootstrap/test-audit-host.sh infra/bootstrap/test-verify-helper.sh \ + infra/test/run-as-unprivileged.sh infra/test/test-run-as-unprivileged.sh \ infra/runner/install-runner.sh infra/runner/install-runner-lib.sh \ infra/runner/act-runner-healthcheck.sh infra/runner/test-runner-config.sh \ infra/runner/test-ci-container-operations.sh \ diff --git a/docs/operations/CI_RUNNER.md b/docs/operations/CI_RUNNER.md index 9f17105..619c560 100644 --- a/docs/operations/CI_RUNNER.md +++ b/docs/operations/CI_RUNNER.md @@ -254,6 +254,11 @@ the Actions jobs endpoint is empty for run 51, so its failure remains unattributed. The owner later provided the full log for task 305 (run 53): `Check host audit root guard` exited 1 before Docker builds began. The test had assumed its caller was non-root, but Gitea job commands run as root in this -container. The test now uses `setpriv` to invoke the audit as UID 65534 when -the test itself starts as root; the audit's root-only guard is unchanged. It -prints a diagnostic and captured output if that assertion fails. +container. The next run confirmed this guard passed, then failed at +`Check production config root guard` for the same UID assumption; Compose +allowlist validation passed. The production-config and runner-installer tests +had the same flaw. These three tests now use the shared +`infra/test/run-as-unprivileged.sh` helper to test denial as UID 65534 when CI +itself runs as root. The helper has a workflow regression test and prints a +diagnostic if `setpriv` cannot switch user. All production guards remain +unchanged. diff --git a/docs/operations/CURRENT_STATE.md b/docs/operations/CURRENT_STATE.md index 6db2787..6e434de 100644 --- a/docs/operations/CURRENT_STATE.md +++ b/docs/operations/CURRENT_STATE.md @@ -153,9 +153,13 @@ unverified. Telegram (6), backup/restore (16), recovery host identity guard, and verifier helper guard passed; Docker image builds were not reached. Reproduction with `EUID=0` and `bash -x` confirmed the test ran the audit as root and then failed - because the expected non-root denial message was absent. The workflow job - container runs commands as root, so the regression test now drops to UID - 65534 with `setpriv` before invoking the unchanged root guard. + because the expected non-root denial message was absent. Run 55 then confirmed + this test passed after dropping to UID 65534, while the next step, + `Check production config root guard`, failed for the same reason. The + Compose allowlist test passed. Root-context reproductions confirmed both + production-config and runner-installer guard tests had the same latent issue. + All three now use the shared `infra/test/run-as-unprivileged.sh` helper, which + has its own UID regression test. The production security guards are unchanged. - An isolated local Compose smoke test built backend/OpenSearch images, reached healthy status for OpenSearch, backend, and Caddy, and returned HTTP 200 for `/review` through Caddy. Its containers, networks, and volumes were diff --git a/infra/bootstrap/test-audit-host.sh b/infra/bootstrap/test-audit-host.sh index e0e7679..190cb4f 100644 --- a/infra/bootstrap/test-audit-host.sh +++ b/infra/bootstrap/test-audit-host.sh @@ -4,22 +4,11 @@ set -euo pipefail output=$(mktemp) trap 'rm -f "$output"' EXIT audit_script="$(dirname "$0")/audit-host.sh" -if [[ $EUID -eq 0 ]]; then - if ! command -v setpriv >/dev/null 2>&1; then - echo 'Cannot test the host audit root guard as root: setpriv is required.' >&2 - exit 1 - fi - if setpriv --reuid=65534 --regid=65534 --clear-groups -- bash "$audit_script" >"$output" 2>&1; then - audit_status=0 - else - audit_status=$? - fi +run_as_unprivileged="$(dirname "$0")/../test/run-as-unprivileged.sh" +if bash "$run_as_unprivileged" bash "$audit_script" >"$output" 2>&1; then + audit_status=0 else - if bash "$audit_script" >"$output" 2>&1; then - audit_status=0 - else - audit_status=$? - fi + audit_status=$? fi if ! grep -Fqx 'Run this read-only audit as root.' "$output"; then echo "Host audit root guard did not reject unprivileged execution (audit exit code: $audit_status). Output:" >&2 diff --git a/infra/deploy/test-production-config-guard.sh b/infra/deploy/test-production-config-guard.sh index cf7ed3e..90f8d49 100644 --- a/infra/deploy/test-production-config-guard.sh +++ b/infra/deploy/test-production-config-guard.sh @@ -3,8 +3,16 @@ set -euo pipefail output=$(mktemp) trap 'rm -f "$output"' EXIT -if bash "$(dirname "$0")/prepare-production-config.sh" >"$output" 2>&1; then - echo 'Production config setup unexpectedly succeeded without root.' >&2 +config_script="$(dirname "$0")/prepare-production-config.sh" +run_as_unprivileged="$(dirname "$0")/../test/run-as-unprivileged.sh" +if bash "$run_as_unprivileged" bash "$config_script" >"$output" 2>&1; then + config_status=0 +else + config_status=$? +fi +if ! grep -Fqx 'Run this configuration step as root.' "$output"; then + echo "Production config root guard did not reject unprivileged execution (exit code: $config_status). Output:" >&2 + cat "$output" >&2 exit 1 fi -grep -qx 'Run this configuration step as root.' "$output" +echo 'PASS production config root guard rejects unprivileged execution' diff --git a/infra/runner/test-install-runner-guard.sh b/infra/runner/test-install-runner-guard.sh index 4e52819..e9f77b8 100644 --- a/infra/runner/test-install-runner-guard.sh +++ b/infra/runner/test-install-runner-guard.sh @@ -3,8 +3,16 @@ set -Eeuo pipefail output=$(mktemp) trap 'rm -f "$output"' EXIT -if bash "$(dirname "$0")/install-runner.sh" >"$output" 2>&1; then - echo 'Runner installer unexpectedly succeeded without root.' >&2 +installer="$(dirname "$0")/install-runner.sh" +run_as_unprivileged="$(dirname "$0")/../test/run-as-unprivileged.sh" +if bash "$run_as_unprivileged" bash "$installer" >"$output" 2>&1; then + installer_status=0 +else + installer_status=$? +fi +if ! grep -Fqx 'ERROR: run this script as root' "$output"; then + echo "Runner installer root guard did not reject unprivileged execution (exit code: $installer_status). Output:" >&2 + cat "$output" >&2 exit 1 fi -grep -qx 'ERROR: run this script as root' "$output" +echo 'PASS runner installer root guard rejects unprivileged execution' diff --git a/infra/test/run-as-unprivileged.sh b/infra/test/run-as-unprivileged.sh new file mode 100644 index 0000000..7e6e07a --- /dev/null +++ b/infra/test/run-as-unprivileged.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +if (($# == 0)); then + echo 'Usage: run-as-unprivileged.sh command [args...]' >&2 + exit 2 +fi + +if [[ $EUID -eq 0 ]]; then + if ! command -v setpriv >/dev/null 2>&1; then + echo 'Cannot run the guard test as non-root: setpriv is required.' >&2 + exit 1 + fi + exec setpriv --reuid=65534 --regid=65534 --clear-groups -- "$@" +fi + +exec "$@" diff --git a/infra/test/test-run-as-unprivileged.sh b/infra/test/test-run-as-unprivileged.sh new file mode 100644 index 0000000..05e9a81 --- /dev/null +++ b/infra/test/test-run-as-unprivileged.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail + +helper="$(dirname "$0")/run-as-unprivileged.sh" +expected_uid=$EUID +if [[ $EUID -eq 0 ]]; then + expected_uid=65534 +fi + +if ! actual_uid=$(bash "$helper" id -u); then + echo 'Failed to run the test command as an unprivileged user.' >&2 + exit 1 +fi +if [[ $actual_uid != "$expected_uid" ]]; then + echo "Expected test command UID $expected_uid, got $actual_uid." >&2 + exit 1 +fi + +echo "PASS test command runs as UID $actual_uid"