From 990e984ab6f9c0ebe250ec3244b0362744192ee9 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sat, 21 Nov 2026 17:14:43 +0800 Subject: [PATCH] ci(security): bind the G-C lane to one SHA and emit evidence Add tools/ci/strict-evidence.py, which records only measured host/source facts: tested SHA (git-verified when a checkout exists), source archive digest, kernel, bwrap/rustc/cargo, uid, cgroup2, runner kind and outcome. The lane now writes this document on every exit path, refuses a root-only PASS, and covers the strict @knowledge-inert and forged-marker JSON contracts in addition to the existing 7/7 adversarial matrix and positive CLI checks. Rework security-linux.yml to run on trusted master pushes or an explicit source_sha, bind the checkout to the tested SHA, create a git archive for the digest, avoid PR execution and upload the evidence artifact. This is step 4 of the CI/security RFC (B04/B09/B10); the runner must still be an ephemeral VM. --- .github/workflows/security-linux.yml | 68 +++++++++- tests/security/run-lane.sh | 75 ++++++++++- tools/ci/strict-evidence.py | 190 +++++++++++++++++++++++++++ 3 files changed, 326 insertions(+), 7 deletions(-) create mode 100755 tools/ci/strict-evidence.py diff --git a/.github/workflows/security-linux.yml b/.github/workflows/security-linux.yml index 5c6218a..c4161dc 100644 --- a/.github/workflows/security-linux.yml +++ b/.github/workflows/security-linux.yml @@ -5,27 +5,87 @@ name: Linux strict security lane (G-C) # mount, PID and network namespaces and the Bubblewrap AppArmor profile. # A missing runner or a skipped test fails the gate; it is never counted as a # pass. See docs/security-gate.md and ADR-0021. +# +# Trust model: the runner registration must be issued by a trusted, short-lived +# orchestrator and the VM destroyed after the job. A persistent self-hosted +# runner with GitHub/SSH/cloud credentials is NOT acceptable. The workflow +# records tested_sha separately from workflow_sha and never runs on PRs. on: + push: + branches: [master] workflow_dispatch: - schedule: - - cron: "17 3 * * 1" + inputs: + source_sha: + description: "Exact reviewed commit SHA" + required: true + type: string + +permissions: + contents: read jobs: strict-security: name: strict isolation escape matrix + if: github.event_name != 'pull_request' runs-on: [self-hosted, linux, marqdo-security] timeout-minutes: 45 steps: - uses: actions/checkout@v4 - - name: Host capability report + with: + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.source_sha || github.sha }} + persist-credentials: false + + - name: Bind tested SHA and archive + shell: bash run: | + set -euo pipefail + tested_sha="$(git rev-parse HEAD)" + expected="${{ github.event_name == 'workflow_dispatch' && inputs.source_sha || github.sha }}" + if [ "$tested_sha" != "$expected" ]; then + echo "tested SHA $tested_sha does not match expected $expected" >&2 + exit 1 + fi + printf 'TESTED_SHA=%s\n' "$tested_sha" >> "$GITHUB_ENV" + printf 'WORKFLOW_SHA=%s\n' "$GITHUB_SHA" >> "$GITHUB_ENV" + archive="$RUNNER_TEMP/marqdo-source-$tested_sha.tar" + git archive --format=tar "$tested_sha" > "$archive" + printf 'SOURCE_ARCHIVE_SHA256=%s\n' \ + "sha256:$(sha256sum "$archive" | cut -d' ' -f1)" >> "$GITHUB_ENV" + + - name: Host capability report + shell: bash + run: | + set -euo pipefail + test "$(id -u)" -ne 0 id uname -a systemd-detect-virt --container || true bwrap --version cc --version | head -1 + rustc -vV cat /proc/self/uid_map findmnt -t cgroup2 || true + - name: Run G-C matrix - run: ./tests/security/run-lane.sh + shell: bash + env: + MARQDO_STRICT_EVIDENCE: ${{ runner.temp }}/strict-evidence.json + MARQDO_STRICT_LANE_LOG: ${{ runner.temp }}/gc-lane.log + MARQDO_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + MARQDO_SOURCE_ARCHIVE: ${{ runner.temp }}/marqdo-source-${{ env.TESTED_SHA }}.tar + MARQDO_RUNNER_KIND: disposable-vm + run: | + set -euo pipefail + ./tests/security/run-lane.sh 2>&1 | tee "$MARQDO_STRICT_LANE_LOG" + + - name: Upload strict evidence + if: always() + uses: actions/upload-artifact@v4 + with: + name: strict-evidence-${{ env.TESTED_SHA || github.sha }} + path: | + ${{ runner.temp }}/strict-evidence.json + ${{ runner.temp }}/gc-lane.log + if-no-files-found: warn + retention-days: 30 diff --git a/tests/security/run-lane.sh b/tests/security/run-lane.sh index dac4f91..6974c20 100755 --- a/tests/security/run-lane.sh +++ b/tests/security/run-lane.sh @@ -12,11 +12,35 @@ set -euo pipefail repo="$(cd "$(dirname "$0")/../.." && pwd)" cd "$repo" +started_utc="$(date -u +%Y-%m-%dT%H:%M:%SZ)" +evidence="${MARQDO_STRICT_EVIDENCE:-}" +finish_lane() { + status=$? + trap - EXIT + set +e + if [ -n "$evidence" ]; then + outcome="FAIL" + if [ "$status" -eq 0 ]; then + outcome="PASS" + fi + python3 tools/ci/strict-evidence.py \ + --repo "$repo" \ + --out "$evidence" \ + --outcome "$outcome" \ + --started "$started_utc" \ + --finished "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ + --log "${MARQDO_STRICT_LANE_LOG:-}" \ + || echo "warning: could not write strict evidence" >&2 + fi + exit "$status" +} +trap finish_lane EXIT + if [ "$(id -u)" -eq 0 ]; then echo "error: the security lane must run as a non-root user" >&2 exit 1 fi -for tool in bwrap cc cargo; do +for tool in bwrap cc cargo python3; do if ! command -v "$tool" >/dev/null 2>&1; then echo "error: required tool '$tool' is missing (a missing mechanism must fail G-C)" >&2 exit 1 @@ -28,10 +52,10 @@ export MARQDO_REQUIRE_STRICT_SECURITY=1 export CARGO_TERM_COLOR=never echo "== adversarial helper matrix ==" -cargo test -p marqdo-sandbox --test security_linux -- --test-threads=1 +cargo test -p marqdo-sandbox --test security_linux --locked -- --test-threads=1 echo "== build CLI ==" -cargo build -p marqdo-cli +cargo build -p marqdo-cli --locked target_dir="${CARGO_TARGET_DIR:-$repo/target}" bin="$target_dir/debug/marqdo" @@ -115,6 +139,23 @@ reject --trust-native-plugin --plugin examples/plugin-demo/build reject --kernel examples/kernel-mock/kernel.json reject --trace-file /tmp/marqdo-trace.json +echo "== @knowledge payloads stay inert under strict ==" +rm -f .cache/security/knowledge-side-effect.txt +cat > .cache/security/knowledge-inert.mq.md <<'MQ' +## @exec main + +`return None` + +### @knowledge 说明 + +`resource.open("file", "knowledge-side-effect.txt", ["write"])` +MQ +"$bin" run .cache/security/knowledge-inert.mq.md --mode strict >/dev/null +if [ -e .cache/security/knowledge-side-effect.txt ]; then + echo "error: a @knowledge payload produced a host side effect in strict mode" >&2 + exit 1 +fi + echo "== forged active marker is rejected ==" set +e MARQDO_STRICT_ACTIVE=1 "$bin" run examples/hello.mq.md --mode strict >/dev/null 2>&1 @@ -125,4 +166,32 @@ if [ "$code" -ne 1 ]; then exit 1 fi +# The JSON contract is part of the gate: a rejected marker still produces +# exactly one parseable document with the stable code. +forged_json="$(mktemp)" +set +e +MARQDO_STRICT_ACTIVE=1 "$bin" run examples/hello.mq.md --mode strict --json >"$forged_json" 2>/dev/null +code=$? +set -e +if [ "$code" -ne 1 ]; then + echo "error: forged strict marker JSON path had unexpected exit $code" >&2 + rm -f "$forged_json" + exit 1 +fi +python3 - "$forged_json" <<'PY' +import json +import sys + +with open(sys.argv[1], encoding="utf-8") as handle: + document = json.load(handle) +if document.get("success") is not False: + raise SystemExit(f"forged marker JSON is not a failure: {document}") +if document.get("code") != "strict.identity_invalid": + raise SystemExit(f"forged marker JSON code drifted: {document}") +PY +rm -f "$forged_json" + echo "G-C security lane: PASS" +if [ -n "$evidence" ]; then + echo "strict evidence: $evidence" +fi diff --git a/tools/ci/strict-evidence.py b/tools/ci/strict-evidence.py new file mode 100755 index 0000000..ef6b63f --- /dev/null +++ b/tools/ci/strict-evidence.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python3 +"""Emit a machine-checkable G-C strict-security evidence document. + +The script never invents measurements. Every field comes from the running +host, the checked-out (or archived) source, or an operator-supplied value that +is recorded as such. It is called by `tests/security/run-lane.sh` and by the +self-hosted security workflow. + +Schema: marqdo.strict-evidence.v1 (fields may only be added). +""" + +import argparse +import datetime +import hashlib +import json +import os +import platform +import shutil +import subprocess +import sys +from pathlib import Path + +SCHEMA = "marqdo.strict-evidence.v1" + + +def utc_now() -> str: + return datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat().replace( + "+00:00", "Z" + ) + + +def command_output(command: list[str]) -> str: + try: + return subprocess.run( + command, + check=False, + capture_output=True, + text=True, + timeout=20, + ).stdout.strip() + except (OSError, subprocess.SubprocessError): + return "" + + +def sha256_file(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as handle: + for chunk in iter(lambda: handle.read(1024 * 1024), b""): + digest.update(chunk) + return f"sha256:{digest.hexdigest()}" + + +def git_head(repo: Path) -> str | None: + if not (repo / ".git").exists(): + return None + value = command_output(["git", "-C", str(repo), "rev-parse", "HEAD"]) + return value or None + + +def cgroup2(repo: Path) -> str: + for candidate in (Path("/proc/self/mountinfo"),): + try: + for line in candidate.read_text().splitlines(): + if " - cgroup2 " in line: + return line.split(" - ", 1)[1].strip() + except OSError: + pass + return "unavailable" + + +def parse_args(argv: list[str]) -> argparse.Namespace: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--repo", default=".", help="source checkout root") + parser.add_argument("--out", required=True, help="evidence JSON output path") + parser.add_argument("--outcome", required=True, choices=["PASS", "FAIL"]) + parser.add_argument("--runner-kind", default=os.environ.get("MARQDO_RUNNER_KIND", "disposable-vm")) + parser.add_argument("--run-url", default=os.environ.get("MARQDO_RUN_URL", "")) + parser.add_argument( + "--image-sha256", + default=os.environ.get("MARQDO_RUNNER_IMAGE_SHA256", ""), + help="immutable VM/runner image digest, recorded verbatim", + ) + parser.add_argument("--started", default="", help="UTC start stamp") + parser.add_argument("--finished", default="", help="UTC finish stamp") + parser.add_argument("--log", default="", help="lane log path or URL") + parser.add_argument( + "--source-archive", + default=os.environ.get("MARQDO_SOURCE_ARCHIVE", ""), + help="source archive used by the lane, when not a git checkout", + ) + parser.add_argument( + "--source-archive-sha256", + default=os.environ.get("MARQDO_SOURCE_ARCHIVE_SHA256", ""), + help="expected sha256 of the source archive", + ) + parser.add_argument( + "--tested-sha", + default=os.environ.get("MARQDO_TESTED_SHA", ""), + help="expected commit SHA (required when the checkout has no .git)", + ) + parser.add_argument("--command", default="./tests/security/run-lane.sh") + return parser.parse_args(argv) + + +def main(argv: list[str]) -> int: + args = parse_args(argv) + repo = Path(args.repo).resolve() + head = git_head(repo) + tested_sha = head or args.tested_sha + verified = head is not None + if not tested_sha: + print("error: no git checkout and no MARQDO_TESTED_SHA/--tested-sha", file=sys.stderr) + return 2 + if head and args.tested_sha and head != args.tested_sha: + print( + f"error: checkout HEAD {head} does not match expected {args.tested_sha}", + file=sys.stderr, + ) + return 2 + + archive_sha = "" + if args.source_archive: + archive = Path(args.source_archive) + if not archive.is_file(): + print(f"error: source archive does not exist: {archive}", file=sys.stderr) + return 2 + archive_sha = sha256_file(archive) + if args.source_archive_sha256 and args.source_archive_sha256 != archive_sha: + print( + f"error: source archive digest {archive_sha} does not match " + f"the expected {args.source_archive_sha256}", + file=sys.stderr, + ) + return 2 + + uid = os.getuid() if hasattr(os, "getuid") else -1 + document = { + "schema": SCHEMA, + "tested_sha": tested_sha, + "tested_sha_source": "git-rev-parse" if verified else "operator-env", + "tested_sha_verified": verified, + "source_archive_sha256": archive_sha, + "run": { + "command": args.command, + "outcome": args.outcome, + "runner_kind": args.runner_kind, + "run_url": args.run_url, + "runner_image_sha256": args.image_sha256, + "lane_log": args.log, + "started_utc": args.started or utc_now(), + "finished_utc": args.finished or utc_now(), + }, + "environment": { + "uname": platform.platform(), + "kernel": platform.release(), + "machine": platform.machine(), + "cgroup2": cgroup2(repo), + "container": command_output(["systemd-detect-virt", "--container"]) or "none-detected", + "uid": uid, + "user": os.environ.get("USER", ""), + "root": uid == 0, + "bwrap_version": command_output(["bwrap", "--version"]), + "rustc": command_output(["rustc", "-V"]), + "cargo": command_output(["cargo", "-V"]), + "python": platform.python_version(), + "cc": command_output(["cc", "--version"]).splitlines()[:1], + "source_archive_sha256_matches": not ( + args.source_archive_sha256 + and archive_sha + and args.source_archive_sha256 != archive_sha + ), + }, + } + out = Path(args.out) + out.parent.mkdir(parents=True, exist_ok=True) + temporary = out.with_suffix(out.suffix + ".tmp") + temporary.write_text(json.dumps(document, ensure_ascii=False, indent=2) + "\n") + temporary.replace(out) + print(f"strict evidence: {out}") + if uid == 0 and args.outcome == "PASS": + print( + "error: a root-only PASS is not valid G-C evidence", + file=sys.stderr, + ) + return 1 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv[1:]))