From 2bd7274355169100a5e3a0ae2ef8a4cc14378d48 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 11:37:52 -0700 Subject: [PATCH 1/9] Run the worker's tools as another user: --worker-user The worker's tools ran as Maat's own uid, so whatever Maat could read the worker could read: the reference program in /tmp, a mission contract in the task folder, the judge's session store under the same HOME, hidden check commands in ps, Maat's memory, earlier runs' logs in /work. A Grok worker was seen grepping /, /opt, ~/.grok, Maat's dist and .maat/ for its checks. --worker-user (MAAT_WORKER_USER), opt-in and Linux-first, for containers and unattended runs; Maat runs as root or with passwordless sudo: - bash, background jobs and an ACP worker agent (with every tool it runs itself) run as the worker uid/gid with a scrubbed environment, its own HOME and /tmp. - read/write/edit/list/grep/inspect are done by a helper process running as the worker (dist/fs-helper.js), so the kernel decides what they reach. - Maat's records (journal, receipts, integrity, out/, mission contract and state, reference and check temp dirs via TMPDIR) live in a 700 state dir outside the project (MAAT_STATE_DIR, default /var/lib/maat/ or ~/.local/state/maat/) and are copied into .maat/ when the job ends. done.yml, bg/ and the mission library stay in the project. - Judge and ask subprocesses stay Maat's, with a HOME the worker cannot read (tightened to 700 or refused). Hidden checks run as Maat in the copy-on-run tree; files a check leaves in place are handed back. - With root and CAP_SYS_ADMIN the worker's commands share one PID namespace with a private /proc and no capabilities; otherwise Maat says ps is visible. docs/privilege-separation.md. Bench: the container runs Maat as root with --worker-user agent and --cap-add=SYS_ADMIN (BENCH_PRIVSEP=0 / BENCH_PIDNS=0 turn them off); judge credentials go to /root, /work is 711, logs are 600 and finished task folders are locked to root. A Linux CI job runs the root-only tests. --- .github/workflows/check.yml | 14 + bench/local/container/Dockerfile | 6 +- bench/local/container/run-in-container.sh | 29 +- bench/local/run.py | 39 +- bench/local/tests/test_privsep_cmd.py | 35 ++ docs/privilege-separation.md | 164 +++++ src/acp.ts | 38 +- src/background.ts | 33 +- src/bar.ts | 12 + src/cli.tsx | 45 ++ src/engine.ts | 93 ++- src/fs-helper.ts | 71 +++ src/mission.ts | 7 +- src/privsep.ts | 704 ++++++++++++++++++++++ src/run.ts | 36 +- src/statedir.ts | 47 +- test/privsep.test.ts | 224 +++++++ 17 files changed, 1527 insertions(+), 70 deletions(-) create mode 100644 bench/local/tests/test_privsep_cmd.py create mode 100644 docs/privilege-separation.md create mode 100644 src/fs-helper.ts create mode 100644 src/privsep.ts create mode 100644 test/privsep.test.ts diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index cbd429e..f9e641e 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -30,3 +30,17 @@ jobs: node dist/cli.js --version # doctor may exit >1 when default localhost endpoint is down (no Ollama in CI) node dist/cli.js doctor || true + + # Privilege separation (--worker-user) is Linux-first and needs root to + # switch users: its tests skip on macOS and run here, in a root container. + # The test creates its own worker user. + privsep-linux: + runs-on: ubuntu-latest + container: node:22-bookworm + env: + ELECTRON_SKIP_BINARY_DOWNLOAD: "1" + steps: + - uses: actions/checkout@v4 + - run: npm ci + - run: npm run test:build && npm run build + - run: node --test --test-reporter=spec dist-test/test/privsep.test.js diff --git a/bench/local/container/Dockerfile b/bench/local/container/Dockerfile index 2816c8f..b0caf55 100644 --- a/bench/local/container/Dockerfile +++ b/bench/local/container/Dockerfile @@ -7,8 +7,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && rm -rf /var/lib/apt/lists/* \ && git config --system user.name "bench" && git config --system user.email "bench@localhost" \ && git config --system init.defaultBranch master -# The agent under test runs as this user; run-in-container.sh keeps the graders and the -# reference solutions in root-only places it cannot read. +# The worker's tools run as this user: Maat runs as root with --worker-user agent (privilege +# separation, src/privsep.ts), or wholly as this user with BENCH_PRIVSEP=0. run-in-container.sh +# keeps the graders and the reference solutions in root-only places it cannot read; /root (the +# judge's HOME) is 700. RUN useradd -m -u 1001 -s /bin/bash agent && chmod 700 /root ENV BENCH_AGENT_USER=agent WORKDIR /opt diff --git a/bench/local/container/run-in-container.sh b/bench/local/container/run-in-container.sh index c41c38c..24eeb4d 100755 --- a/bench/local/container/run-in-container.sh +++ b/bench/local/container/run-in-container.sh @@ -21,6 +21,13 @@ # there. It is for the judge only: # ARMS="oc:MAAT_JUDGE_MODEL=opencode/big-pickle,MAAT_JUDGE_URL=opencode://zen" # The key is never printed. +# Privilege separation (default; BENCH_PRIVSEP=0 turns it off): Maat runs as root and only the +# worker's tools run as `agent` (--worker-user agent). Maat's records sit in /var/lib/maat (700, +# root) until each job ends; the judge's logins are in /root (700); finished task folders and +# logs in /work are locked to root. BENCH_PIDNS=1 (default) also gives the container +# CAP_SYS_ADMIN so Maat can put the worker's commands in their own PID namespace with a private +# /proc (the worker then sees none of Maat's processes; it gets no capability itself). +# BENCH_PIDNS=0: no extra capability, and the worker can read the process list. set -e name=$1; tgz=${2:A}; shift 2; [ "$1" = "--" ] && shift here=${0:A:h}; local_dir=${here:h} @@ -62,11 +69,15 @@ printf 'OPENROUTER_API_KEY=%s\n' "$key" > "$envf" # The graders and reference solutions are mounted where only root can reach (/root is 700), # copied to a root-only /opt/bench, and run.py runs from there as root; it runs the agent # as the unprivileged `agent` user (BENCH_AGENT_USER) and grades as root afterwards. -credmount=; startcmd='export MOLT_DIST_ABS=$(npm root -g)/@solvyx/molt/dist; mkdir -p /work && chmod 755 /work && rm -rf /opt/bench && cp -a /root/bench-src /opt/bench && chmod -R go-rwx /opt/bench && cd /opt/bench && python3 run.py "$@"' +# /work is 711: the worker can reach its own task folder by name but cannot list the others. +privsep=${BENCH_PRIVSEP:-1} +credmount=; startcmd='export MOLT_DIST_ABS=$(npm root -g)/@solvyx/molt/dist; mkdir -p /work && chmod 711 /work && rm -rf /opt/bench && cp -a /root/bench-src /opt/bench && chmod -R go-rwx /opt/bench && cd /opt/bench && python3 run.py "$@"' if [ "$SUBSCRIPTION" = grok ]; then credmount="-v $HOME/.grok/auth.json:/root/grok-cred/auth.json:ro" - # grok runs as `agent`, so the credential copy lives in ITS home, owned by it, mode 600. + # The worker's grok runs as `agent`, so its credential copy lives in ITS home, owned by it, + # mode 600. A grok judge runs as Maat (root) with HOME=/root, so it gets its own copy there. startcmd='install -d -o agent -g agent -m 700 /home/agent/.grok && install -o agent -g agent -m 600 /root/grok-cred/auth.json /home/agent/.grok/auth.json && '$startcmd + [ "$privsep" = 0 ] || startcmd='install -d -m 700 /root/.grok && install -m 600 /root/grok-cred/auth.json /root/.grok/auth.json && '$startcmd # GROK_OWN_TOOLS=1: let Grok Build use its own shell/edit tools (inside the container only). # Needed because Grok hides Maat's MCP tools behind its search_tool/use_tool, so by default # the model asks for run_terminal_command, Maat refuses, and the turn ends with no work done. @@ -78,12 +89,20 @@ if [ "$OPENCODE" = 1 ]; then ocauth=$(mktemp); chmod 600 "$ocauth"; trap 'rm -f "$envf" "$ocauth"' EXIT python3 -c 'import json,sys;a=json.load(open(sys.argv[1]));json.dump({k:v for k,v in a.items() if k=="opencode"},open(sys.argv[2],"w"))' ~/.local/share/opencode/auth.json "$ocauth" credmount="$credmount -v $ocauth:/root/oc-cred/auth.json:ro" - startcmd='install -d -o agent -g agent -m 700 /home/agent/.local /home/agent/.local/share /home/agent/.local/share/opencode && install -o agent -g agent -m 600 /root/oc-cred/auth.json /home/agent/.local/share/opencode/auth.json && '$startcmd + # The OpenCode judge runs as Maat: with privilege separation that is root, HOME=/root, which + # the worker cannot read; without it, as `agent`. + if [ "$privsep" = 0 ]; then + startcmd='install -d -o agent -g agent -m 700 /home/agent/.local /home/agent/.local/share /home/agent/.local/share/opencode && install -o agent -g agent -m 600 /root/oc-cred/auth.json /home/agent/.local/share/opencode/auth.json && '$startcmd + else + startcmd='install -d -m 700 /root/.local /root/.local/share /root/.local/share/opencode && install -m 600 /root/oc-cred/auth.json /root/.local/share/opencode/auth.json && '$startcmd + fi fi +caps= +[ "$privsep" = 0 ] || [ "${BENCH_PIDNS:-1}" = 0 ] || caps=--cap-add=SYS_ADMIN echo "maat $img → $out/${RESULTS:-results.jsonl}" -docker run --rm --name "maat-bench-$name" \ +docker run --rm --name "maat-bench-$name" ${=caps} \ -v "$local_dir":/root/bench-src:ro -v "$out":/results -v "$work":/work ${=credmount} \ --env-file "$envf" -e REFERENCE="${REFERENCE:-0}" -e BENCH_MODEL="${BENCH_MODEL:-}" \ -e ARMS="${ARMS:-}" -e BENCH_TASKS="${BENCH_TASKS:-}" -e BENCH_URL="$url" -e BENCH_LIMIT="${BENCH_LIMIT:-600}" -e BENCH_REASONING="${BENCH_REASONING:-}" -e BENCH_GATE="${BENCH_GATE:-}" -e RESULTS="${RESULTS:-results.jsonl}" \ - -e MAAT_BUILD="$sha" -e PYTHONDONTWRITEBYTECODE=1 \ + -e MAAT_BUILD="$sha" -e PYTHONDONTWRITEBYTECODE=1 -e BENCH_PRIVSEP="$privsep" \ "$img" sh -c "$startcmd" run "$@" diff --git a/bench/local/run.py b/bench/local/run.py index a3e5e1b..e7c5654 100644 --- a/bench/local/run.py +++ b/bench/local/run.py @@ -54,13 +54,21 @@ def openrouter_key() -> str: # denied calls per 60 runs: writing the deliverable through a heredoc, removing # the model's own scratch files. BENCH_GATE=yes reproduces the old runs. AGENT_USER = os.environ.get("BENCH_AGENT_USER") # set in the containers: the agent runs unprivileged +# Privilege separation (default in the containers): Maat runs as root and only the worker's +# tools run as BENCH_AGENT_USER (--worker-user), so the worker cannot read Maat's state dir, +# the reference program, the judge's HOME or its process list either. BENCH_PRIVSEP=0 runs the +# whole of Maat as the agent user, as before 2026-10-07. +PRIVSEP = bool(AGENT_USER) and os.environ.get("BENCH_PRIVSEP", "1") != "0" and os.geteuid() == 0 def as_agent(cmd: list, env: dict | None = None) -> tuple[list, dict | None]: """With BENCH_AGENT_USER set, run the agent as that user: it cannot read the graders - or the reference solutions, which stay root-only. Unset (Mac host lanes): unchanged.""" + or the reference solutions, which stay root-only. Unset (Mac host lanes): unchanged. + With privilege separation Maat stays root and is told which user the worker is.""" if not AGENT_USER: return cmd, env + if PRIVSEP: + return [*cmd[:3], "--worker-user", AGENT_USER, *cmd[3:]], env import pwd home = pwd.getpwnam(AGENT_USER).pw_dir return ["runuser", "-u", AGENT_USER, "--", "env", f"HOME={home}", *cmd], env @@ -83,6 +91,26 @@ def hand_over(d: Path) -> None: subprocess.run(["chown", "-R", AGENT_USER, str(d)], check=True) +def lock(p: Path) -> None: + """A finished task's folder and logs: root-only, so a later task's worker cannot read an + earlier run's released checks, receipts or Maat's log (/work is shared by every task).""" + if not AGENT_USER or not p.exists(): + return + try: + os.chown(p, 0, 0, follow_symlinks=False) + os.chmod(p, 0o700 if p.is_dir() else 0o600) + except (PermissionError, FileNotFoundError): + pass + + +def write_private(p: Path, text: str) -> None: + """Written mode 600 from the start: the logs carry every hidden check once a job ends.""" + fd = os.open(p, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) + with os.fdopen(fd, "w") as f: + f.write(text) + lock(p) + + def provider_capped(out: str, steps: int) -> bool: """The provider's cap (OpenRouter daily limit, OpenCode free-usage limit), not the work.""" return ( @@ -121,9 +149,9 @@ def run_molt(d: Path, prompt: str, log: Path) -> dict: out, err = "", "" timed_out = True secs = time.time() - t0 - log.write_text(out) + write_private(log, out) # Maat's own notices (criteria not drafted, dropped checks, refusals) go to stderr. - log.with_suffix(".err").write_text(err or "") + write_private(log.with_suffix(".err"), err or "") steps = 0; per = []; outcome = None; spend = {}; review = None; disagree = []; extra = {} for line in out.splitlines(): if not line.startswith("{"): @@ -210,6 +238,10 @@ def main(which: str, repeats: int, task_filter: str | None) -> None: tasks = [T for T in tasks if T.name in want] arms = parse_arms(os.environ.get("ARMS")) out = Path(os.environ.get("RESULTS_DIR", HERE)) / os.environ.get("RESULTS", f"results-{which}-x{repeats}.jsonl") + # Earlier runs' folders and logs (a resumed run, an earlier lane): out of the worker's reach. + if AGENT_USER and WORK.exists(): + for p in WORK.iterdir(): + lock(p) done = set() if out.exists(): # resume: skip runs already recorded for line in out.read_text().splitlines(): @@ -242,6 +274,7 @@ def main(which: str, repeats: int, task_filter: str | None) -> None: print(f"STOPPED: the provider's daily limit / quota is reached ({tag} not recorded)", flush=True) return ok, why = T.grade(d) + lock(d) final = "" r.update(task=T.name, agent=a, rep=rep, passed=ok, why=why, final=final[:300]) if arm: diff --git a/bench/local/tests/test_privsep_cmd.py b/bench/local/tests/test_privsep_cmd.py new file mode 100644 index 0000000..68ecba6 --- /dev/null +++ b/bench/local/tests/test_privsep_cmd.py @@ -0,0 +1,35 @@ +import getpass +import os +import stat +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +import run # noqa: E402 + + +def test_privsep_keeps_maat_root_and_names_the_worker(monkeypatch): + monkeypatch.setattr(run, "AGENT_USER", "agent") + monkeypatch.setattr(run, "PRIVSEP", True) + cmd, _ = run.as_agent(["node", "cli.js", "run", "--url", "u", "the task"]) + assert cmd == ["node", "cli.js", "run", "--worker-user", "agent", "--url", "u", "the task"] + + +def test_without_privsep_the_whole_agent_runs_as_the_user(monkeypatch): + me = getpass.getuser() + monkeypatch.setattr(run, "AGENT_USER", me) + monkeypatch.setattr(run, "PRIVSEP", False) + cmd, _ = run.as_agent(["node", "cli.js", "run", "t"]) + assert cmd[:4] == ["runuser", "-u", me, "--"] + + +def test_no_agent_user_changes_nothing(monkeypatch): + monkeypatch.setattr(run, "AGENT_USER", None) + assert run.as_agent(["node", "cli.js", "run", "t"])[0] == ["node", "cli.js", "run", "t"] + + +def test_logs_are_written_owner_only(tmp_path): + p = tmp_path / "task.log" + run.write_private(p, "hidden: grep -q secret out.txt\n") + assert stat.S_IMODE(os.stat(p).st_mode) == 0o600 + assert p.read_text().startswith("hidden") diff --git a/docs/privilege-separation.md b/docs/privilege-separation.md new file mode 100644 index 0000000..e8c637c --- /dev/null +++ b/docs/privilege-separation.md @@ -0,0 +1,164 @@ +# Privilege separation: `--worker-user` + +Unattended, with hidden checks, the worker model's tools used to run as the +same user as Maat. Whatever Maat could read, the worker could read. On +2026-10-07 a Grok worker was seen hunting for its hidden checks with +`grep -r` over `/`, `/opt`, `~/.grok`, Maat's own `dist/` and `.maat/`. +Masking what Maat writes (hidden checks withheld from receipts, the journal +and `out/`) closes the files Maat knows about. Several leaks remained, and +all of them come from the worker and Maat being the same user: + +| leak | where | +|---|---| +| the reference program | a `ref-try-*` dir under `/tmp` | +| mission contracts | `.maat/mission/contract.yml` in the task folder | +| the judge's session store | `~/.local/share/opencode`, `~/.grok` under the same HOME | +| hidden check commands | `ps`, `/proc//cmdline` | +| everything Maat holds | Maat's process memory | +| earlier runs | bench logs and task folders in `/work` | + +`--worker-user ` (or `MAAT_WORKER_USER`) runs the worker as another +Linux user, so the kernel enforces the boundary and Maat no longer has to +guess which paths matter. It is opt-in and meant for containers, CI and +unattended runs. The desktop app and a plain `maat run` behave exactly as +before. + +```sh +# as root (a container, a CI job) +maat run --worker-user agent --sandbox --criteria auto --cwd /work/task "fix the failing test" +``` + +Maat must run as root, or as a user with passwordless `sudo -n -u `. +The worker user must be a different, non-root user, and it must be able to +read and write the project. + +## What changes when it is on + +**The worker's commands run as the worker.** That covers `bash`, background +jobs (`background=true`), and an ACP worker agent (Grok Build, OpenCode) +together with every tool that agent runs itself. Grok auto-approves its own +`read_file`, `grep` and `list_dir` without asking Maat, so only the uid can +bound those. As root, Maat spawns them with the worker's uid and gid and no +supplementary groups. With sudo, it uses `sudo -n -u env -i ...`. The +environment is scrubbed: every variable whose name looks like a credential is +removed, along with Maat's own `MAAT_*` and `MOLT_*` settings. HOME, USER and +LOGNAME are set to the worker's, and TMPDIR is `/tmp`. + +**The file tools run as the worker.** `read_file`, `write_file`, `edit_file`, +`list_dir`, `grep` and `inspect` are carried out by a small helper process +(`dist/fs-helper.js`) that runs as the worker user. A read the worker could +not do itself fails with `EACCES`, symlinks included, because the kernel +checks the path as that user rather than Maat checking the name. Files the +worker writes are owned by the worker. + +**Maat's records leave the project while the job runs.** The journal, +receipts (including the full twins), the integrity ledger, `out/`, exuviae, +judgment cases, the mission contract and state, and Maat's temp files all +move to a state dir: + +- `MAAT_STATE_DIR` if set, otherwise +- `/var/lib/maat/` when Maat runs as root, otherwise +- `~/.local/state/maat/` + +The dir is mode 700 and owned by Maat. `` includes 48 random bits. +`TMPDIR` points into it, so reference tries (`ref-try-*`), the drafter's +scratch dirs, the copy-on-run trees for hidden checks (`maat-check-*`) and +the judge's own temp files are private too. When the job starts, records +from earlier jobs are copied out of the project's `.maat/` so the hash chains +continue, and a mission's contract, features and state are moved out of the +task folder. + +These stay in the project: `.maat/done.yml`, because a person wrote it, and +`.maat/bg/`, which holds the worker's own background-job logs. +`.maat/mission/library/` also stays, because the worker writes it. A spilled +output (`.maat/out/.txt`) is still readable through `read_file`: Maat +serves its own masked copy of the worker's output. The worker cannot `cat` +it from bash. + +**When the job ends, the records come back.** `maat run`, `maat ask` and +`maat mission run` copy the state dir (except `tmp/`) into the project's +`.maat/`. The copies are owned by the project's owner, so receipts are where +people always found them and `maat verify` checks them there. The state dir +itself is kept. + +**The judge and the checks stay Maat's.** Judge and ask subprocesses +(OpenCode, a Grok judge, an HTTP judge) run as Maat with Maat's HOME. If that +HOME is readable by the worker, Maat tightens it to 700 when it owns it and +refuses to start otherwise. Hidden checks run as Maat in the copy-on-run +tree, never as the worker. A check that ran in place (a project check, or a +tree too large to copy) can leave files owned by Maat in the project. After +every bar run, those are handed back to the worker, so the worker can keep +editing its own tree. Git, running as Maat on a repository the worker owns, +is told `safe.directory=*` through `GIT_CONFIG_*`. The worker's environment +does not get that setting. + +## The process list + +`ps` shows every process's command line to every user unless `/proc` is +mounted with `hidepid=2` (`hidepid=invisible`). Hidden checks are commands, +so while one runs as Maat, a worker that can list processes can read it. +`hidepid` cannot be relied on: it is a mount option of the host or +container, and most container runtimes do not set it. + +Maat's own command line is plain (`maat`), because the process title is +replaced at startup. + +When Maat runs as root with `CAP_SYS_ADMIN` and `unshare`, `nsenter` and +`setpriv` are installed (util-linux), it starts one PID namespace with a +private `/proc` for the job. Every command the worker triggers runs inside +it, and so does the ACP agent with everything it starts. Inside, the worker +sees only its own processes. Background servers it starts keep running +between calls, because the namespace lives as long as Maat does. The worker +enters the namespace with `--no-new-privs` and an empty capability bounding +set, so the capability that created the namespace never reaches it, not even +through a setuid binary. + +- `MAAT_WORKER_PIDNS=0` turns it off; `=1` requires it (Maat refuses to + start without it). +- Without it, Maat says so on stderr. The fix is either to mount `/proc` + with `hidepid=2` (the host's or the container's), or to give Maat root and + `CAP_SYS_ADMIN`. For Docker: `docker run --cap-add=SYS_ADMIN ...`. Docker's + default seccomp and AppArmor profiles allow it on OrbStack and on current + Docker Engine. + +Process memory needs nothing extra. `/proc//mem` and ptrace need the +same uid (and `CAP_SYS_PTRACE`), and a separate PID namespace hides the +process entirely. + +## The bench + +`bench/local/container/run-in-container.sh` turns this on by default: + +- `run.py` runs Maat as root with `--worker-user agent`. `BENCH_PRIVSEP=0` + runs the whole of Maat as `agent`, as before. +- The container gets `--cap-add=SYS_ADMIN` for the PID namespace. + `BENCH_PIDNS=0` drops it. +- The worker's Grok credential is copied to `/home/agent/.grok`. A Grok + judge's credential is copied to `/root/.grok` and the OpenCode judge's to + `/root/.local/share/opencode`. `/root` is mode 700. +- `/work` is mode 711, so a worker can reach its own task folder by name but + cannot list the others. Each task's log is written mode 600, and after + grading the task folder is locked to root (700). This stops a later task + (a repeat of the same task, the other arm of a pair) from reading the + checks an earlier one released. +- The graders and reference solutions stay root-only, as before. + +To check it from outside a running job: + +```sh +docker exec maat-bench- sh -c 'ls /var/lib/maat' +docker exec maat-bench- runuser -u agent -- cat /var/lib/maat//log/ # Permission denied +``` + +## What is still open + +- A worker with network access can still reach whatever the network lets it + reach. Privilege separation is about the local machine. +- In sudo mode (Maat not root) there is no PID namespace. The worker's + environment is passed on sudo's command line (it is already scrubbed of + credentials). +- The ACP worker's MCP connection to Maat's tools is a loopback port behind a + per-session bearer token held by the worker's own agent. Through it the + worker reaches only Maat's tools, which run as the worker. +- Off Linux (macOS desktop) nothing changes. `--worker-user` works on any + POSIX system where Maat is root, but without a PID namespace. diff --git a/src/acp.ts b/src/acp.ts index 16de030..a6f2a47 100644 --- a/src/acp.ts +++ b/src/acp.ts @@ -78,6 +78,7 @@ import { homedir } from "node:os"; import { join } from "node:path"; import { randomBytes } from "node:crypto"; import { execFile } from "node:child_process"; +import { privSep } from "./privsep.js"; import { promisify } from "node:util"; import { errorText } from "./format.js"; @@ -499,6 +500,8 @@ export class AcpConnection { onRequest?: ClientHandler; /** Anything at all arrived from the agent: the stall watchdog's clock. */ onActivity?: () => void; + /** The agent is the worker: under privilege separation it runs as the worker user (src/privsep.ts). */ + asWorker?: boolean; } = {}, ) { const onRequest = opts.onRequest; @@ -521,19 +524,27 @@ export class AcpConnection { async start(): Promise { const spawnFn = this.opts.spawnFn ?? spawn; - const child = spawnFn(this.spec.bin, [...this.spec.args], { - cwd: this.opts.cwd ?? process.cwd(), + const cwd = this.opts.cwd ?? process.cwd(); + // The subprocess environment REPLACES rather than merges, so the spread + // is load-bearing: without it a Finder-launched molt hands the CLI an + // empty PATH and it cannot find its own helpers. + // `electron/login-path.ts` has already repaired process.env.PATH by the + // time anything gets here. + const env = { + ...(this.spec.childEnv ? this.spec.childEnv(process.env) : process.env), + ...this.spec.env, + ...(this.spec.env ? { PWD: cwd } : {}), + }; + // The worker's agent, and every tool it runs itself, as the worker user: + // its own reads (Grok auto-approves read_file, grep, list_dir) never + // reach Maat, so only the uid can bound them. + const ps = this.opts.asWorker ? privSep() : undefined; + const spec = ps?.execSpec(this.spec.bin, this.spec.args, cwd, env); + const child = spawnFn(spec?.file ?? this.spec.bin, spec?.args ?? [...this.spec.args], { + cwd, stdio: ["pipe", "pipe", "pipe"], - // The subprocess environment REPLACES rather than merges, so the spread - // is load-bearing: without it a Finder-launched molt hands the CLI an - // empty PATH and it cannot find its own helpers. - // `electron/login-path.ts` has already repaired process.env.PATH by the - // time anything gets here. - env: { - ...(this.spec.childEnv ? this.spec.childEnv(process.env) : process.env), - ...this.spec.env, - ...(this.spec.env ? { PWD: this.opts.cwd ?? process.cwd() } : {}), - }, + env: spec?.env ?? env, + ...(spec?.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), // Its own process group, so ending it ends what it started (src/proctree.ts). ...groupSpawn(), }); @@ -835,6 +846,8 @@ export type AcpOptions = { abortTools?: () => void; /** Injected in tests, which drive a scripted agent rather than a real one. */ spawnFn?: typeof spawn; + /** This agent is the worker (not a judge): run it as the worker user when privilege separation is on. */ + asWorker?: boolean; }; /** How long an interrupted turn waits for its aborted tool calls to end. */ @@ -954,6 +967,7 @@ export class AcpSession { const conn = new AcpConnection(spec, { cwd, ...(this.opts.spawnFn ? { spawnFn: this.opts.spawnFn } : {}), + ...(this.opts.asWorker ? { asWorker: true } : {}), onNotify: (method, params) => this.onNotify(method, params), onRequest: (method, params) => this.onRequest(method, params), onActivity: () => this.touch(), diff --git a/src/background.ts b/src/background.ts index 6244801..adb91f0 100644 --- a/src/background.ts +++ b/src/background.ts @@ -26,6 +26,7 @@ import { spawn } from "node:child_process"; import { closeSync, mkdirSync, openSync } from "node:fs"; import { join } from "node:path"; import { stateDirName } from "./statedir.js"; +import { privSep } from "./privsep.js"; export type BackgroundProcess = { id: number; @@ -91,7 +92,7 @@ function killGroup(pid: number, signal: NodeJS.Signals): boolean { */ export function startBackground( command: string, - opts: { cwd: string; env?: NodeJS.ProcessEnv }, + opts: { cwd: string; env?: NodeJS.ProcessEnv; asWorker?: boolean }, ): BackgroundProcess { hookExit(); const id = nextId++; @@ -100,13 +101,29 @@ export function startBackground( mkdirSync(dir, { recursive: true }); const log = `${bgRel}/${id}.log`; const fd = openSync(join(opts.cwd, log), "w"); - const child = spawn(command, { - cwd: opts.cwd, - shell: true, - env: opts.env, - detached: true, - stdio: ["ignore", fd, fd], - }); + // Under privilege separation the job, and its log, are the worker's. + const ps = opts.asWorker ? privSep() : undefined; + if (ps) { + ps.giveToWorker(join(opts.cwd, stateDirName(opts.cwd))); + ps.giveToWorker(dir); + ps.giveToWorker(join(opts.cwd, log)); + } + const spec = ps?.commandSpec(command, true, opts.cwd, opts.env); + const child = spec + ? spawn(spec.file, spec.args, { + cwd: opts.cwd, + env: spec.env, + detached: true, + stdio: ["ignore", fd, fd], + ...(spec.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), + }) + : spawn(command, { + cwd: opts.cwd, + shell: true, + env: opts.env, + detached: true, + stdio: ["ignore", fd, fd], + }); closeSync(fd); const entry: BackgroundProcess = { id, diff --git a/src/bar.ts b/src/bar.ts index 8425eb1..9e379b3 100644 --- a/src/bar.ts +++ b/src/bar.ts @@ -21,6 +21,7 @@ import { execFileSync } from "node:child_process"; import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; import { copyTreeOrWhy, runsInCopy } from "./scratch.js"; import { maskText } from "./withhold.js"; +import { privSep } from "./privsep.js"; import { dirname, join, resolve, sep } from "node:path"; import { parse as parseYaml } from "yaml"; import type { ArchiveLike } from "./archive.js"; @@ -2308,6 +2309,17 @@ export class CheckCache { } export async function runBar(bar: Bar, ctx: BarContext): Promise { + try { + return await runBarAs(bar, ctx); + } finally { + // Checks run as Maat. Under privilege separation, whatever one left in the + // project (a build in place, a cache) goes back to the worker, so the + // worker can still edit its own tree (src/privsep.ts). + privSep()?.handBack(ctx.cwd); + } +} + +async function runBarAs(bar: Bar, ctx: BarContext): Promise { const t0 = Date.now(); // In order, one at a time. Checks share a working directory and routinely // build into it; running them concurrently would have them tripping over diff --git a/src/cli.tsx b/src/cli.tsx index 3ad7345..78f16ff 100644 --- a/src/cli.tsx +++ b/src/cli.tsx @@ -53,6 +53,8 @@ import type { BarResult, Check, EngineEvent } from "./types.js"; import { stateDir } from "./statedir.js"; import { draftReference, snapshotProject } from "./reference.js"; import { env } from "./env.js"; +import { fileURLToPath } from "node:url"; +import { disablePrivSep, enablePrivSep, workerUserFrom, type PrivSep } from "./privsep.js"; /** * The version, from the manifest that npm actually publishes. @@ -232,6 +234,11 @@ options what high autonomy would still ask about runs (rm, sudo, python -c, a write outside the directory). For a container or a throwaway VM, never for a machine you keep. + --worker-user run the worker's tools as user (Linux; Maat runs as + root or with passwordless sudo to ). Maat's records go + to a private state dir (MAAT_STATE_DIR) and are copied + into .maat/ when the job ends. For containers and + unattended runs. (MAAT_WORKER_USER) --json machine-readable output (run/prove/stats/receipts) --version print the version and exit --no-stream disable token streaming (default: streaming on) @@ -306,6 +313,8 @@ type Args = { * boundary, and approves every gated call instead of refusing it. */ sandbox?: boolean; + /** `--worker-user `: the worker's tools run as that user (src/privsep.ts). */ + workerUser?: string; /** `molt mission run --features n`: stop after n worker runs. */ features?: number; /** Files the model may read and never write. `--read`, repeatable. */ @@ -686,6 +695,9 @@ export function parseArgs(argv: string[], stored: StoredEndpoint = {}): Args { out.sandbox = true; out.yes = true; break; + case "--worker-user": + out.workerUser = next(); + break; case "--json": out.json = true; break; @@ -2223,6 +2235,39 @@ export async function main(argv = process.argv.slice(2)): Promise { // Before anything else can print: from here on stdout is the protocol. if (args.acp || args.cmd === "acp") return cmdAcp(args); + // Privilege separation, when asked for: before any record is opened, so + // every one of them is made in the private state dir (src/privsep.ts). + const worker = workerUserFrom(args.workerUser); + const separates = args.cmd === "run" || args.cmd === "ask" || (args.cmd === "mission" && (args.task ?? "").split(" ")[0] === "run"); + if (worker && separates) { + let ps: PrivSep; + try { + ps = enablePrivSep({ + user: worker, + project: args.cwd, + helper: fileURLToPath(new URL("./fs-helper.js", import.meta.url)), + notice: (t) => process.stderr.write(`maat: ${t}\n`), + }); + } catch (e) { + process.stderr.write(`maat: ${(e as Error).message}\n`); + return 2; + } + process.stderr.write( + `maat: worker tools run as ${ps.worker.name} (uid ${ps.worker.uid})` + + `${ps.pidns ? " in their own PID namespace" : ""}; Maat's records are in ${ps.stateRoot} until the job ends\n`, + ); + try { + return args.cmd === "mission" ? await cmdMission(args) : await cmdRun(args, args.cmd === "ask"); + } finally { + try { + const dest = ps.publish(); + process.stderr.write(`maat: records copied to ${dest}\n`); + } catch (e) { + process.stderr.write(`maat: could not copy the records from ${ps.stateRoot} into the project: ${(e as Error).message}\n`); + } + disablePrivSep(); + } + } switch (args.cmd) { case "run": return cmdRun(args); diff --git a/src/engine.ts b/src/engine.ts index 97b497e..cd7add4 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -131,7 +131,8 @@ import { type Msg, type Spend, } from "./types.js"; -import { stateDir, stateDirName } from "./statedir.js"; +import { stateDir, stateDirName, stateRedirect } from "./statedir.js"; +import { privSep, type WorkerFs } from "./privsep.js"; import { env } from "./env.js"; import { Judgments, caseReason, reasonText } from "./judgment.js"; @@ -2842,9 +2843,11 @@ export class Engine { */ private spill(text: string, callId: string): string | null { try { - const rel = `${stateDirName(this.cwd)}/out/${callId.replace(/[^\w-]/g, "_")}.txt`; + const name = `${callId.replace(/[^\w-]/g, "_")}.txt`; + const rel = `${stateDirName(this.cwd)}/out/${name}`; mkdirSync(stateDir(this.cwd, "out"), { recursive: true }); - writeFileSync(join(this.cwd, rel), this.maskWithheld(redact(text, this.secrets())), "utf8"); + // In the state dir under privilege separation; read_file serves it back (spilledOutput). + writeFileSync(stateDir(this.cwd, "out", name), this.maskWithheld(redact(text, this.secrets())), "utf8"); return rel; } catch { return null; @@ -3125,6 +3128,9 @@ export class Engine { * editor is where the freshest copy lives, not the only copy there is. */ private async readText(abs: string): Promise { + // Under privilege separation the worker's helper reads it, as the worker. + const wfs = this.workerFs; + if (wfs) return wfs.read(abs); const read = this.cfg.files?.read; if (read) { try { @@ -3147,6 +3153,11 @@ export class Engine { * so the two agree and the ledger names a file that exists. */ private async writeText(abs: string, content: string): Promise { + const wfs = this.workerFs; + if (wfs) { + await wfs.write(abs, content); + return content; + } mkdirSync(dirname(abs), { recursive: true }); const write = this.cfg.files?.write; if (write) { @@ -3167,6 +3178,39 @@ export class Engine { return content; } + /** + * The file tools' hands under privilege separation (src/privsep.ts): a + * helper running as the worker user. Undefined when it is off, and the + * tools touch the disk as Maat, as they always did. + */ + private get workerFs(): WorkerFs | undefined { + return privSep()?.fs(); + } + + private async fileExists(abs: string): Promise { + const wfs = this.workerFs; + return wfs ? wfs.exists(abs) : existsSync(abs); + } + + private async fileSha(abs: string): Promise { + const wfs = this.workerFs; + return wfs ? wfs.sha256(abs) : sha256Of(abs); + } + + /** + * A spilled output (`.maat/out/.txt`) while Maat's records are out of + * the project: the worker cannot read the state dir, so Maat serves its own + * masked copy of the worker's own output. Undefined for any other path. + */ + private spilledOutput(rel: string): string | undefined { + const to = stateRedirect(this.cwd); + if (!to) return undefined; + const m = /^(?:\.\/)?\.(?:maat|molt)\/out\/([\w-]+\.txt)$/.exec(rel); + if (!m) return undefined; + const p = join(to, "out", m[1]!); + return existsSync(p) ? readFileSync(p, "utf8") : undefined; + } + private overBudget(): boolean { return this.budgetTokens !== undefined && this.sessionTokens >= this.budgetTokens; } @@ -3186,11 +3230,14 @@ export class Engine { case "inspect": { const rel = String(args.path ?? "."); const abs = resolve(this.cwd, rel); - if (!existsSync(abs)) return `${rel} does not exist`; + const wfs = this.workerFs; + if (!(await this.fileExists(abs))) return `${rel} does not exist`; try { + const part = { offset: num(args.offset, 0), length: num(args.length, 0) || undefined }; + if (wfs) return (await wfs.isDir(abs)) ? await wfs.inspectDir(abs, rel) : await wfs.inspectFile(abs, rel, part); return statSync(abs).isDirectory() ? inspectDir(abs, rel) - : inspectFile(abs, rel, { offset: num(args.offset, 0), length: num(args.length, 0) || undefined }); + : inspectFile(abs, rel, part); } catch (e) { return `could not inspect ${rel}: ${e instanceof Error ? e.message : String(e)}`; } @@ -3199,7 +3246,7 @@ export class Engine { case "read_file": this.readPaths.add(String(args.path ?? "")); return readPart( - await this.readText(resolve(this.cwd, String(args.path ?? ""))), + this.spilledOutput(String(args.path ?? "")) ?? (await this.readText(resolve(this.cwd, String(args.path ?? "")))), String(args.path ?? ""), num(args.offset, 0), num(args.limit, Number.MAX_SAFE_INTEGER), @@ -3213,11 +3260,11 @@ export class Engine { // promise to the person who made it, and a promise that holds only // when the model cooperates is not one. if (this.isReadOnly(rel, abs)) return readOnlyRefusal(rel); - const before = sha256Of(abs); + const before = await this.fileSha(abs); // The text as well as the hash: a hash proves the file changed, and // only the text can say whether the change was a comment. let priorText = ""; - const existed = existsSync(abs); + const existed = await this.fileExists(abs); if (existed) { try { priorText = await this.readText(abs); @@ -3266,14 +3313,9 @@ export class Engine { this.mustBeInside(abs, rel); // Bounded and off the main thread: a listing the model asks for can be // pointed at anything, including a home directory. - return formatListing( - rel, - await walkAsync(abs, { - depth: num(args.depth, 1), - glob: str(args.glob), - deadline: Date.now() + WALK_DEADLINE_MS, - }), - ); + const walkOpts = { depth: num(args.depth, 1), glob: str(args.glob), deadline: Date.now() + WALK_DEADLINE_MS }; + const wfs = this.workerFs; + return formatListing(rel, wfs ? await wfs.walk(abs, walkOpts) : await walkAsync(abs, walkOpts)); } case "grep": { @@ -3281,13 +3323,9 @@ export class Engine { const abs = resolve(this.cwd, rel); this.mustBeInside(abs, rel); const pattern = String(args.pattern ?? ""); - return formatMatches( - pattern, - await grepFiles(abs, pattern, { - glob: str(args.glob), - ignoreCase: args.ignore_case === true, - }), - ); + const grepOpts = { glob: str(args.glob), ignoreCase: args.ignore_case === true }; + const wfs = this.workerFs; + return formatMatches(pattern, wfs ? await wfs.grep(abs, pattern, grepOpts) : await grepFiles(abs, pattern, grepOpts)); } case "edit_file": { @@ -3295,8 +3333,8 @@ export class Engine { const abs = resolve(this.cwd, rel); this.mustBeInside(abs, rel); if (this.isReadOnly(rel, abs)) return readOnlyRefusal(rel); - if (!existsSync(abs)) return `no such file: ${rel} — write_file creates a new one`; - const before = sha256Of(abs); + if (!(await this.fileExists(abs))) return `no such file: ${rel} — write_file creates a new one`; + const before = await this.fileSha(abs); const current = await this.readText(abs); const edit = applyEdit( current, @@ -3353,7 +3391,7 @@ export class Engine { return `stopping job ${id} (pid ${p.pid}, "${p.command}"); its log is ${p.log}`; } if (args.background === true) { - const p = startBackground(command, { cwd: this.cwd, env: scrubbedEnv() }); + const p = startBackground(command, { cwd: this.cwd, env: scrubbedEnv(), asWorker: true }); // A process that died before the result was even composed is the // common failure — a typo in the command, a port already bound. Give // it a moment so that case is reported as what it is, not as a @@ -3382,6 +3420,8 @@ export class Engine { timeoutMs, maxBuffer: 1024 * 1024, env: scrubbedEnv(), + // As the worker user when privilege separation is on (src/privsep.ts). + asWorker: true, // A cancelled turn kills the command it is waiting on. Leaving a // build running after the turn that asked for it was called off is // the machine doing work nobody is going to read. @@ -4594,6 +4634,7 @@ export class Engine { // The per-call controller invokeTool sets: a deadline or a stall that // ends the agent's turn ends the command it was waiting on too. abortTools: () => this.running?.abort(), + asWorker: true, ...(this.cfg.acpSpawn ? { spawnFn: this.cfg.acpSpawn } : {}), }); } diff --git a/src/fs-helper.ts b/src/fs-helper.ts new file mode 100644 index 0000000..7975d4a --- /dev/null +++ b/src/fs-helper.ts @@ -0,0 +1,71 @@ +/** + * The worker's file tools, performed as the worker (src/privsep.ts). + * + * Maat starts this as the worker user and sends it one JSON request per line + * on stdin; it answers one JSON line per request on stdout. Every read, write, + * walk and grep the model asks for happens here, under the worker's uid, so + * the kernel — not a path check in Maat — decides what it may touch. + */ +import { createHash } from "node:crypto"; +import { accessSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; +import { dirname } from "node:path"; +import { grepFiles, walkAsync, type WalkOptions } from "./files.js"; +import { inspectDir, inspectFile } from "./inspect.js"; + +type Op = (...args: never[]) => unknown; + +const ops: Record = { + read: (p: string) => readFileSync(p, "utf8"), + write: (p: string, content: string) => { + mkdirSync(dirname(p), { recursive: true }); + writeFileSync(p, content, "utf8"); + return null; + }, + exists: (p: string) => existsSync(p), + isDir: (p: string) => statSync(p).isDirectory(), + sha256: (p: string) => (existsSync(p) ? createHash("sha256").update(readFileSync(p)).digest("hex") : null), + walk: (p: string, o: WalkOptions) => walkAsync(p, o), + grep: (p: string, pattern: string, o: { glob?: string; ignoreCase?: boolean }) => grepFiles(p, pattern, o), + inspectDir: (p: string, rel: string) => inspectDir(p, rel), + inspectFile: (p: string, rel: string, o: { offset?: number; length?: number }) => inspectFile(p, rel, o), + access: (p: string, mode: number) => { + try { + accessSync(p, mode); + return true; + } catch { + return false; + } + }, +}; + +const reply = (msg: unknown) => process.stdout.write(JSON.stringify(msg) + "\n"); + +async function handle(line: string): Promise { + let req: { id: number; op: string; args: unknown[] }; + try { + req = JSON.parse(line); + } catch { + return; + } + const op = ops[req.op]; + try { + if (!op) throw new Error(`unknown op ${req.op}`); + const value = await (op as (...a: unknown[]) => unknown)(...(req.args ?? [])); + reply({ id: req.id, ok: true, value: value === undefined ? null : value }); + } catch (e) { + const err = e as NodeJS.ErrnoException; + reply({ id: req.id, ok: false, error: { message: err?.message ?? String(e), ...(err?.code ? { code: err.code } : {}) } }); + } +} + +let buf = ""; +process.stdin.setEncoding("utf8"); +process.stdin.on("data", (d: string) => { + buf += d; + for (let i = buf.indexOf("\n"); i >= 0; i = buf.indexOf("\n")) { + const line = buf.slice(0, i); + buf = buf.slice(i + 1); + if (line.trim()) void handle(line); + } +}); +process.stdin.on("end", () => process.exit(0)); diff --git a/src/mission.ts b/src/mission.ts index bb505f6..78c0527 100644 --- a/src/mission.ts +++ b/src/mission.ts @@ -31,7 +31,7 @@ import { runCommand } from "./run.js"; import { diagnoseFailure } from "./bar.js"; import type { Check, Confirm, EngineEvent, JobOutcome, Spend } from "./types.js"; import { estTokens } from "./types.js"; -import { stateDir, stateDirName } from "./statedir.js"; +import { projectStateDir, stateDir, stateDirName } from "./statedir.js"; /** How the folder is named in messages; the real one is `missionRel(cwd)`. */ export const MISSION_DIR = ".maat/mission"; @@ -363,7 +363,8 @@ export function assertionNotes(contract: Contract, ids: readonly string[]): stri /** Every `.md` under the library, as the worker is shown it, within a budget. */ export function readLibrary(cwd: string, budgetTokens = LIBRARY_TOKENS): string { - const dir = join(missionDir(cwd), LIBRARY_DIR); + // In the project even under privilege separation: the worker writes it. + const dir = projectStateDir(cwd, "mission", LIBRARY_DIR); if (!existsSync(dir)) return ""; const files = readdirSync(dir).filter((f) => f.endsWith(".md")).sort(); const parts: string[] = []; @@ -874,7 +875,7 @@ export function writePlan(cwd: string, plan: MissionPlan, opts: { force?: boolea if (hasMission(cwd) && !opts.force) { throw new MissionError(`a mission already exists in ${MISSION_DIR}; pass --force to replace it`); } - mkdirSync(join(dir, LIBRARY_DIR), { recursive: true }); + mkdirSync(projectStateDir(cwd, "mission", LIBRARY_DIR), { recursive: true }); const written: string[] = []; const put = (name: string, text: string) => { writeFileSync(join(dir, name), text); diff --git a/src/privsep.ts b/src/privsep.ts new file mode 100644 index 0000000..a7d2978 --- /dev/null +++ b/src/privsep.ts @@ -0,0 +1,704 @@ +/** + * Privilege separation: the worker's tools run as another user. + * + * Unattended, with hidden checks, the worker model's tools ran as the same + * uid as Maat, so it could read everything Maat could. A Grok worker was seen + * hunting for its hidden checks — `grep -r` over `/`, `/opt`, `~/.grok`, + * Maat's own dist and `.maat/`. Masking what Maat writes (src/withhold.ts) + * closes the files Maat knows about; it cannot close the reference program in + * a world-readable temp dir, a mission contract in the task folder, the + * judge's session store under the same HOME, the commands in `ps`, Maat's + * process memory, or an earlier run's logs. Those share one root: the same + * user. This removes it. + * + * Opt-in (`--worker-user `, `MAAT_WORKER_USER`), Linux-first, for + * containers, CI and unattended runs. Maat runs as root (or as a user with + * passwordless sudo to the worker). When it is on: + * + * - every command the worker triggers (`bash`, background jobs, an ACP + * agent such as Grok Build and every tool it runs itself) runs as the + * worker's uid/gid, with the worker's HOME and a scrubbed environment; + * - the file tools (read/write/edit/list/grep/inspect) are performed by a + * small helper running as the worker (src/fs-helper.ts), so they can read + * and write exactly what that user can and nothing else; + * - Maat's records — journal, receipts, integrity, out/, mission contract, + * reference and check temp dirs — live in a state dir outside the project, + * mode 700, owned by Maat (MAAT_STATE_DIR, default /var/lib/maat/ + * as root, ~/.local/state/maat/ otherwise). At the end of the job + * they are copied into the project's `.maat/` as before; + * - the judge and ask subprocesses and the hidden checks stay Maat's: same + * uid, a HOME the worker cannot read, checks run in the copy-on-run tree; + * - where the kernel allows it (root with CAP_SYS_ADMIN), the worker's + * commands share one PID namespace of their own with a private /proc, so + * `ps` shows them nothing of Maat's. Without it the process list stays + * visible: see docs/privilege-separation.md. + * + * Off (the default) nothing here runs and nothing changes. + */ +import { spawn, spawnSync, type ChildProcess } from "node:child_process"; +import { randomBytes } from "node:crypto"; +import { + chmodSync, + cpSync, + existsSync, + lchownSync, + lstatSync, + mkdirSync, + readFileSync, + readdirSync, + realpathSync, + rmSync, + statSync, +} from "node:fs"; +import { homedir } from "node:os"; +import { dirname, join, relative, resolve, sep } from "node:path"; +import type { GrepResult, WalkOptions, WalkResult } from "./files.js"; +import { redirectState, stateDirName } from "./statedir.js"; + +export type WorkerUser = { name: string; uid: number; gid: number; home: string }; + +/** A process to start: what to exec, and as whom when Maat can say so directly. */ +export type SpawnSpec = { + file: string; + args: string[]; + env: NodeJS.ProcessEnv; + uid?: number; + gid?: number; +}; + +/** The file tools' view of the disk, as the worker sees it. */ +export type WorkerFs = { + read(abs: string): Promise; + write(abs: string, content: string): Promise; + exists(abs: string): Promise; + isDir(abs: string): Promise; + sha256(abs: string): Promise; + walk(abs: string, opts: WalkOptions): Promise; + grep(abs: string, pattern: string, opts: { glob?: string; ignoreCase?: boolean }): Promise; + inspectDir(abs: string, rel: string): Promise; + inspectFile(abs: string, rel: string, opts: { offset?: number; length?: number }): Promise; + /** access(2) as the worker: R_OK 4, W_OK 2, X_OK 1. */ + access(abs: string, mode: number): Promise; +}; + +export class PrivSepError extends Error {} + +/** Environment names that hold a credential, whatever the vendor. */ +const SECRETISH = /(KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL|COOKIE|AUTH)/i; + +/** Look a user up by name: NSS first (getent), then /etc/passwd. */ +export function lookupUser(name: string): WorkerUser { + const parse = (line: string): WorkerUser | null => { + const f = line.trim().split(":"); + if (f.length < 7 || f[0] !== name) return null; + const uid = Number(f[2]); + const gid = Number(f[3]); + if (!Number.isInteger(uid) || !Number.isInteger(gid)) return null; + return { name, uid, gid, home: f[5] || "/" }; + }; + const g = spawnSync("getent", ["passwd", name], { encoding: "utf8" }); + if (g.status === 0) { + const u = parse(g.stdout.split("\n")[0] ?? ""); + if (u) return u; + } + try { + for (const line of readFileSync("/etc/passwd", "utf8").split("\n")) { + const u = parse(line); + if (u) return u; + } + } catch { + /* no passwd file */ + } + throw new PrivSepError(`no such user: ${name}`); +} + +function which(bin: string): string | null { + for (const d of (process.env.PATH ?? "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin").split(":")) { + if (!d) continue; + const p = join(d, bin); + try { + if (statSync(p).isFile()) return p; + } catch { + /* not here */ + } + } + return null; +} + +function sleepSync(ms: number): void { + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); +} + +/** The pid of `parent`'s first child, as this PID namespace numbers it. */ +function childOf(parent: number): number | null { + try { + const kids = readFileSync(`/proc/${parent}/task/${parent}/children`, "utf8").trim().split(/\s+/).filter(Boolean); + if (kids.length) return Number(kids[0]); + } catch { + /* no children file: scan */ + } + try { + for (const n of readdirSync("/proc")) { + if (!/^\d+$/.test(n)) continue; + try { + const stat = readFileSync(`/proc/${n}/stat`, "utf8"); + const ppid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]); + if (ppid === parent) return Number(n); + } catch { + /* gone */ + } + } + } catch { + /* no /proc */ + } + return null; +} + +/** Where the state dir goes when MAAT_STATE_DIR does not say. */ +export function defaultStateRoot(session: string): string { + const root = process.getuid?.() === 0; + return root + ? join("/var/lib/maat", session) + : join(process.env.XDG_STATE_HOME || join(homedir(), ".local", "state"), "maat", session); +} + +export type PrivSepOptions = { + /** The worker user's name. */ + user: string; + /** The project folder the worker works in. */ + project: string; + /** Overrides MAAT_STATE_DIR and the default. */ + stateDir?: string; + /** false: never use a PID namespace; true: require one. Default: when available (MAAT_WORKER_PIDNS). */ + pidns?: boolean; + /** Said once on stderr: what was tightened, what stays visible. */ + notice?: (text: string) => void; + /** + * The file helper script (dist/fs-helper.js). The CLI passes it; left out, + * it is looked for beside the running script. + */ + helper?: string; +}; + +export class PrivSep { + readonly worker: WorkerUser; + readonly project: string; + readonly stateRoot: string; + readonly maatUid: number; + /** How the worker's processes are started: directly with a uid (root), or through sudo. */ + readonly mode: "root" | "sudo"; + readonly pidns: boolean; + readonly helperScript: string; + private nsLeader?: ChildProcess; + private nsInit?: number; + private helper?: FsHelper; + private closed = false; + private onExit = () => this.close(); + private readonly savedEnv: Record = {}; + + constructor(opts: PrivSepOptions) { + if (process.platform === "win32") throw new PrivSepError("--worker-user is not supported on Windows"); + const uid = process.geteuid?.() ?? process.getuid?.(); + if (uid === undefined) throw new PrivSepError("--worker-user needs a POSIX system"); + this.maatUid = uid; + this.worker = lookupUser(opts.user); + if (this.worker.uid === uid) { + throw new PrivSepError(`--worker-user ${opts.user} is the user Maat runs as; the worker must be a different user`); + } + if (this.worker.uid === 0) throw new PrivSepError("--worker-user must not be root"); + this.project = resolve(opts.project); + this.helperScript = opts.helper ?? join(dirname(realpathSync(process.argv[1] ?? ".")), "fs-helper.js"); + if (!existsSync(this.helperScript)) throw new PrivSepError(`the file helper is missing: ${this.helperScript}`); + if (uid === 0) this.mode = "root"; + else if (spawnSync("sudo", ["-n", "-u", this.worker.name, "--", "true"], { stdio: "ignore" }).status === 0) this.mode = "sudo"; + else { + throw new PrivSepError( + `--worker-user needs Maat to run as root, or passwordless sudo to ${this.worker.name} (sudo -n -u ${this.worker.name} true failed)`, + ); + } + + const session = `${new Date().toISOString().replace(/[:.]/g, "-")}-${randomBytes(6).toString("hex")}`; + const wanted = opts.stateDir ?? process.env.MAAT_STATE_DIR ?? defaultStateRoot(session); + this.stateRoot = resolve(wanted); + const inProject = relative(this.project, this.stateRoot); + if (!inProject || (!inProject.startsWith("..") && !inProject.startsWith(sep) && !/^[a-zA-Z]:/.test(inProject))) { + throw new PrivSepError(`the state dir ${this.stateRoot} is inside the project; it must be outside it`); + } + this.makePrivate(this.stateRoot); + this.makePrivate(join(this.stateRoot, "tmp")); + + const want = opts.pidns ?? envFlag("MAAT_WORKER_PIDNS"); + this.pidns = want === false ? false : this.startNamespace(); + if (want === true && !this.pidns) { + throw new PrivSepError("MAAT_WORKER_PIDNS=1 but a PID namespace could not be made (needs root with CAP_SYS_ADMIN, unshare, nsenter and setpriv)"); + } + process.on("exit", this.onExit); + + // Maat's own temp files (reference tries, check copies, drafts, judge + // scratch) land in the state dir; the worker's get /tmp (workerEnv). + this.setEnv("TMPDIR", join(this.stateRoot, "tmp")); + // Maat reads the worker's repository as another user; git refuses a + // repository owned by someone else unless told it is safe. + const n = Number(process.env.GIT_CONFIG_COUNT ?? 0) || 0; + this.setEnv(`GIT_CONFIG_KEY_${n}`, "safe.directory"); + this.setEnv(`GIT_CONFIG_VALUE_${n}`, "*"); + this.setEnv("GIT_CONFIG_COUNT", String(n + 1)); + + this.seed(); + redirectState(this.project, this.stateRoot); + + const notice = opts.notice ?? (() => {}); + this.guardHome(notice); + if (!this.pidns) { + notice( + "the worker can still list processes (ps): mount /proc with hidepid=2, or run Maat as root with CAP_SYS_ADMIN " + + "so its commands get their own PID namespace (docs/privilege-separation.md)", + ); + } + } + + private setEnv(k: string, v: string): void { + if (!(k in this.savedEnv)) this.savedEnv[k] = process.env[k]; + process.env[k] = v; + } + + /** A folder only Maat can read: created mode 700, or tightened to it. */ + private makePrivate(dir: string): void { + const parent = dirname(dir); + if (!existsSync(parent)) { + mkdirSync(parent, { recursive: true, mode: 0o700 }); + } + mkdirSync(dir, { recursive: true, mode: 0o700 }); + const st = statSync(dir); + if (st.uid !== this.maatUid) throw new PrivSepError(`${dir} is not owned by Maat's user (uid ${this.maatUid})`); + chmodSync(dir, 0o700); + } + + /** + * Maat's records from earlier jobs move to the state dir, so the chains + * continue; a mission contract leaves the project entirely until the job is + * over. The checks file, the worker's background logs and the mission + * library (which the worker writes) stay. + */ + private seed(): void { + const src = join(this.project, stateDirName(this.project)); + if (!existsSync(src)) return; + for (const name of readdirSync(src)) { + if (name === "done.yml" || name === "bg") continue; + const from = join(src, name); + cpSync(from, join(this.stateRoot, name), { + recursive: true, + force: true, + filter: (p) => relative(src, p) !== join("mission", "library") && !relative(src, p).startsWith(join("mission", "library") + sep), + }); + } + const mission = join(src, "mission"); + if (existsSync(mission)) { + for (const name of readdirSync(mission)) { + if (name !== "library") rmSync(join(mission, name), { recursive: true, force: true }); + } + } + } + + /** + * The judge's and ask's HOME (Maat's own) holds their logins and session + * stores. The worker must not be able to read it: tightened to 700 when it + * is Maat's, refused otherwise. + */ + private guardHome(notice: (t: string) => void): void { + const home = homedir(); + if (!home || !existsSync(home)) return; + const st = statSync(home); + const byOthers = (st.mode & 0o005) !== 0; + const byGroup = st.gid === this.worker.gid && (st.mode & 0o050) !== 0; + if (!byOthers && !byGroup) return; + if (st.uid !== this.maatUid) { + throw new PrivSepError(`Maat's HOME ${home} is readable by ${this.worker.name} and not Maat's to tighten; set HOME to a private folder`); + } + chmodSync(home, st.mode & 0o700); + notice(`tightened ${home} to mode 700 so ${this.worker.name} cannot read the judge's logins`); + } + + /** One PID namespace with a private /proc for all of the worker's processes. */ + private startNamespace(): boolean { + if (process.platform !== "linux" || this.mode !== "root") return false; + if (!which("unshare") || !which("nsenter") || !which("setpriv")) return false; + const probe = spawnSync("unshare", ["--pid", "--fork", "--mount-proc", "true"], { stdio: "ignore", timeout: 10_000 }); + if (probe.status !== 0) return false; + // A shell as the namespace's init: it reaps what the worker orphans, and + // when it dies everything in the namespace dies with it. `--kill-child` + // ends it when its unshare does, and unshare shares Maat's process group, + // so killing Maat's group (a harness backstop) ends the namespace too. + const leader = spawn( + "unshare", + ["--pid", "--fork", "--mount-proc", "--kill-child", "/bin/sh", "-c", "while :; do sleep 86400 & wait $!; done"], + { stdio: "ignore" }, + ); + leader.on("error", () => {}); + leader.unref(); + let init: number | null = null; + for (let i = 0; i < 200 && leader.pid && init === null; i++) { + init = childOf(leader.pid); + if (init === null) sleepSync(10); + } + if (!init) { + try { + leader.kill("SIGKILL"); + } catch { + /* gone */ + } + return false; + } + this.nsLeader = leader; + this.nsInit = init; + return true; + } + + /** The environment a worker process gets: no credentials, no Maat internals, its own HOME and /tmp. */ + workerEnv(base: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv { + const env: NodeJS.ProcessEnv = {}; + for (const [k, v] of Object.entries(base)) { + if (v === undefined) continue; + if (SECRETISH.test(k)) continue; + if (/^(MAAT|MOLT)_/.test(k) || /^GIT_CONFIG_/.test(k)) continue; + if (k === "ELECTRON_RUN_AS_NODE" || k === "TMP" || k === "TEMP" || k === "XDG_STATE_HOME" || k === "XDG_CONFIG_HOME" || k === "XDG_DATA_HOME" || k === "XDG_CACHE_HOME" || k === "XDG_RUNTIME_DIR") continue; + if (k === "SUDO_USER" || k === "SUDO_UID" || k === "SUDO_GID" || k === "SUDO_COMMAND" || k === "MAIL") continue; + env[k] = v; + } + env.HOME = this.worker.home; + env.USER = this.worker.name; + env.LOGNAME = this.worker.name; + env.TMPDIR = "/tmp"; + return env; + } + + /** A shell command, run as the worker. */ + commandSpec(command: string, shell: string | true, cwd: string, env?: NodeJS.ProcessEnv): SpawnSpec { + return this.execSpec(shell === true ? "/bin/sh" : shell, ["-c", command], cwd, env); + } + + /** A program, run as the worker (in its PID namespace when there is one). */ + execSpec(file: string, args: readonly string[], cwd: string, env?: NodeJS.ProcessEnv): SpawnSpec { + const wenv = this.workerEnv(env ?? process.env); + const { uid, gid, name } = this.worker; + if (this.mode === "sudo") { + const pairs = Object.entries(wenv).map(([k, v]) => `${k}=${v}`); + return { file: "sudo", args: ["-n", "-u", name, "--", "env", "-i", ...pairs, file, ...args], env: wenv }; + } + if (this.pidns && this.nsInit && this.nsAlive()) { + return { + file: "nsenter", + args: [ + `--target=${this.nsInit}`, + "--pid", + "--mount", + `--wd=${cwd}`, + "--", + "setpriv", + `--reuid=${uid}`, + `--regid=${gid}`, + "--clear-groups", + // Root in this container may hold CAP_SYS_ADMIN (it made the + // namespace); the worker gets no capability, and no setuid binary + // can hand it one. + "--no-new-privs", + "--bounding-set=-all", + "--", + file, + ...args, + ], + env: wenv, + }; + } + return { file, args: [...args], env: wenv, uid, gid }; + } + + private nsAlive(): boolean { + return this.nsInit !== undefined && existsSync(`/proc/${this.nsInit}`); + } + + /** The file tools, performed as the worker. Started on first use. */ + fs(): WorkerFs { + if (!this.helper) this.helper = new FsHelper(this); + return this.helper; + } + + /** + * After Maat ran something in the project as itself (a project check in + * place, a git commit), hand what it created back to the worker, so the + * worker can go on editing its own tree. Only entries Maat's user owns. + */ + handBack(dir: string = this.project, limit = 100_000): void { + if (this.mode !== "root") return; + const r = relative(this.project, resolve(dir)); + if (r.startsWith("..") || resolve(dir) !== join(this.project, r)) return; + let seen = 0; + const { uid, gid } = this.worker; + const walk = (d: string): void => { + let names: string[]; + try { + names = readdirSync(d); + } catch { + return; + } + for (const n of names) { + if (++seen > limit) return; + const p = join(d, n); + let st; + try { + st = lstatSync(p); + } catch { + continue; + } + if (st.uid === this.maatUid) { + try { + lchownSync(p, uid, gid); + } catch { + /* not ours to change */ + } + } + if (st.isDirectory() && !st.isSymbolicLink()) walk(p); + } + }; + try { + if (lstatSync(dir).uid === this.maatUid) lchownSync(dir, uid, gid); + } catch { + return; + } + walk(dir); + } + + /** Make a path Maat created for the worker (a background log) the worker's. */ + giveToWorker(path: string): void { + if (this.mode !== "root") return; + try { + lchownSync(path, this.worker.uid, this.worker.gid); + } catch { + /* best effort */ + } + } + + /** + * The job is over: copy Maat's records into the project's `.maat/`, where + * people have always found them, owned by whoever owns the project. The + * temp dir is not copied. Returns the folder they went to. + */ + publish(): string { + const dest = join(this.project, stateDirName(this.project)); + mkdirSync(dest, { recursive: true }); + for (const name of readdirSync(this.stateRoot)) { + if (name === "tmp") continue; + cpSync(join(this.stateRoot, name), join(dest, name), { recursive: true, force: true }); + } + if (this.mode === "root") { + let owner = { uid: this.worker.uid, gid: this.worker.gid }; + try { + const st = statSync(this.project); + owner = { uid: st.uid, gid: st.gid }; + } catch { + /* the worker's, then */ + } + const chown = (p: string): void => { + try { + lchownSync(p, owner.uid, owner.gid); + if (lstatSync(p).isDirectory()) for (const n of readdirSync(p)) chown(join(p, n)); + } catch { + /* best effort */ + } + }; + chown(dest); + this.handBack(this.project); + } + return dest; + } + + /** Stop the helper and the namespace, and put Maat's records back where they were looked for. */ + close(): void { + if (this.closed) return; + this.closed = true; + process.removeListener("exit", this.onExit); + this.helper?.stop(); + if (this.nsLeader?.pid) { + try { + process.kill(this.nsLeader.pid, "SIGKILL"); + } catch { + /* gone */ + } + } + redirectState(null); + for (const [k, v] of Object.entries(this.savedEnv)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + } +} + +function envFlag(name: string): boolean | undefined { + const v = process.env[name]; + if (v === undefined || v === "") return undefined; + return !/^(0|false|no|off)$/i.test(v); +} + +// --------------------------------------------------------------------------- +// The file helper: a node process running as the worker + +type Pending = { resolve: (v: unknown) => void; reject: (e: Error) => void }; + +class FsHelper implements WorkerFs { + private child?: ChildProcess; + private buf = ""; + private nextId = 1; + private pending = new Map(); + + constructor(private ps: PrivSep) {} + + private start(): ChildProcess { + if (this.child) return this.child; + const script = this.ps.helperScript; + const env = { ...this.ps.workerEnv(), ELECTRON_RUN_AS_NODE: "1" }; + // Not in the PID namespace: it is Maat's own tool and only needs the uid. + const spec: SpawnSpec = + this.ps.mode === "root" + ? { file: process.execPath, args: [script], env, uid: this.ps.worker.uid, gid: this.ps.worker.gid } + : this.ps.execSpec(process.execPath, [script], "/", env); + const child = spawn(spec.file, spec.args, { + cwd: "/", + env: { ...spec.env, ELECTRON_RUN_AS_NODE: "1" }, + stdio: ["pipe", "pipe", "pipe"], + ...(spec.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), + }); + this.child = child; + child.stdout?.setEncoding("utf8"); + child.stdout?.on("data", (d: string) => this.feed(d)); + let err = ""; + child.stderr?.setEncoding("utf8"); + child.stderr?.on("data", (d: string) => { + err = (err + d).slice(-2000); + }); + const fail = (e: Error) => { + for (const p of this.pending.values()) p.reject(e); + this.pending.clear(); + this.child = undefined; + }; + child.on("error", (e) => fail(e)); + child.on("exit", (code) => fail(new Error(`the worker file helper exited (${code ?? "signal"})${err.trim() ? `: ${err.trim()}` : ""}`))); + child.stdin?.on("error", () => {}); + this.idle(); + return child; + } + + /** Let Maat exit while nothing is asked of the helper. */ + private idle(): void { + const c = this.child; + if (!c) return; + const busy = this.pending.size > 0; + for (const s of [c, c.stdin, c.stdout, c.stderr] as ({ ref?: () => void; unref?: () => void } | null | undefined)[]) { + if (busy) s?.ref?.(); + else s?.unref?.(); + } + } + + private feed(chunk: string): void { + this.buf += chunk; + for (let i = this.buf.indexOf("\n"); i >= 0; i = this.buf.indexOf("\n")) { + const line = this.buf.slice(0, i); + this.buf = this.buf.slice(i + 1); + let msg: { id: number; ok: boolean; value?: unknown; error?: { message: string; code?: string } }; + try { + msg = JSON.parse(line); + } catch { + continue; + } + const p = this.pending.get(msg.id); + if (!p) continue; + this.pending.delete(msg.id); + if (msg.ok) p.resolve(msg.value); + else { + const e = new Error(msg.error?.message ?? "failed") as NodeJS.ErrnoException; + if (msg.error?.code) e.code = msg.error.code; + p.reject(e); + } + } + this.idle(); + } + + private call(op: string, ...args: unknown[]): Promise { + const child = this.start(); + const id = this.nextId++; + return new Promise((resolvePromise, reject) => { + this.pending.set(id, { resolve: resolvePromise as (v: unknown) => void, reject }); + this.idle(); + child.stdin?.write(JSON.stringify({ id, op, args }) + "\n"); + }); + } + + read(abs: string) { + return this.call("read", abs); + } + write(abs: string, content: string) { + return this.call("write", abs, content); + } + exists(abs: string) { + return this.call("exists", abs); + } + isDir(abs: string) { + return this.call("isDir", abs); + } + sha256(abs: string) { + return this.call("sha256", abs); + } + walk(abs: string, opts: WalkOptions) { + const { skip: _skip, ...plain } = opts; + return this.call("walk", abs, plain); + } + grep(abs: string, pattern: string, opts: { glob?: string; ignoreCase?: boolean }) { + return this.call("grep", abs, pattern, opts); + } + inspectDir(abs: string, rel: string) { + return this.call("inspectDir", abs, rel); + } + inspectFile(abs: string, rel: string, opts: { offset?: number; length?: number }) { + return this.call("inspectFile", abs, rel, opts); + } + access(abs: string, mode: number) { + return this.call("access", abs, mode); + } + + stop(): void { + try { + this.child?.kill("SIGKILL"); + } catch { + /* gone */ + } + this.child = undefined; + } +} + +// --------------------------------------------------------------------------- +// The one instance + +let active: PrivSep | undefined; + +/** The privilege separation in force, if any. */ +export function privSep(): PrivSep | undefined { + return active; +} + +/** Turn it on for this process. Throws PrivSepError when it cannot be made safe. */ +export function enablePrivSep(opts: PrivSepOptions): PrivSep { + active?.close(); + active = new PrivSep(opts); + return active; +} + +/** Turn it off (tests; the end of a run). */ +export function disablePrivSep(): void { + active?.close(); + active = undefined; +} + +/** The worker user asked for, from the flag or MAAT_WORKER_USER. */ +export function workerUserFrom(flag?: string): string | undefined { + const v = (flag ?? process.env.MAAT_WORKER_USER ?? "").trim(); + return v || undefined; +} diff --git a/src/run.ts b/src/run.ts index cba5922..7ee2d5a 100644 --- a/src/run.ts +++ b/src/run.ts @@ -18,6 +18,7 @@ */ import { spawn, type ChildProcess } from "node:child_process"; import { existsSync } from "node:fs"; +import { privSep } from "./privsep.js"; export type RunOptions = { cwd: string; @@ -30,6 +31,11 @@ export type RunOptions = { shell?: string | true; /** Kills the command when it aborts, so a turn can be cancelled mid-command. */ signal?: AbortSignal; + /** + * The worker asked for it: under privilege separation (src/privsep.ts) it + * runs as the worker user. Without privilege separation this changes nothing. + */ + asWorker?: boolean; }; export type RunResult = { @@ -91,15 +97,27 @@ export function runCommand(command: string, opts: RunOptions): Promise((resolve, reject) => { let child: ChildProcess; try { - child = spawn(command, { - cwd: opts.cwd, - shell: opts.shell ?? true, - env: opts.env, - stdio: ["ignore", "pipe", "pipe"], - // Its own process group, so a timeout can kill everything the command - // started and not just the shell (see `kill`). - detached: process.platform !== "win32", - }); + const ps = opts.asWorker ? privSep() : undefined; + if (ps) { + const spec = ps.commandSpec(command, opts.shell ?? true, opts.cwd, opts.env); + child = spawn(spec.file, spec.args, { + cwd: opts.cwd, + env: spec.env, + stdio: ["ignore", "pipe", "pipe"], + detached: true, + ...(spec.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), + }); + } else { + child = spawn(command, { + cwd: opts.cwd, + shell: opts.shell ?? true, + env: opts.env, + stdio: ["ignore", "pipe", "pipe"], + // Its own process group, so a timeout can kill everything the command + // started and not just the shell (see `kill`). + detached: process.platform !== "win32", + }); + } } catch (e) { reject(e as Error); return; diff --git a/src/statedir.ts b/src/statedir.ts index 48ce40f..1fc6165 100644 --- a/src/statedir.ts +++ b/src/statedir.ts @@ -12,8 +12,8 @@ * The checks file is looked up across both, because a person's * `.molt/done.yml` must never be ignored because a `.maat/` appeared later. */ -import { existsSync } from "node:fs"; -import { join } from "node:path"; +import { existsSync, realpathSync } from "node:fs"; +import { join, resolve } from "node:path"; export const STATE_DIR = ".maat"; export const LEGACY_STATE_DIR = ".molt"; @@ -27,13 +27,56 @@ export function stateDirName(root: string): string { return STATE_DIR; } +/** + * Privilege separation (src/privsep.ts) moves Maat's records out of the + * project, into a folder only Maat's user can read, for as long as a job + * runs. What stays in the project is what belongs to it: the checks file a + * person wrote, and the worker's own background-job logs. + */ +const KEPT_IN_PROJECT = new Set(["done.yml", "bg"]); +let redirect: { roots: Set; to: string } | undefined; + +/** Send this project's records to `to` (null: back to the project). */ +export function redirectState(project: string | null, to?: string): void { + if (project === null || !to) { + redirect = undefined; + return; + } + const roots = new Set([resolve(project)]); + try { + roots.add(realpathSync(project)); + } catch { + /* compare as given */ + } + redirect = { roots, to }; +} + +/** Where this project's records live while redirected, or undefined. */ +export function stateRedirect(root: string): string | undefined { + return redirect && redirect.roots.has(resolve(root)) ? redirect.to : undefined; +} + +function redirected(root: string, first: string | undefined): string | undefined { + if (first === undefined || KEPT_IN_PROJECT.has(first)) return undefined; + return stateRedirect(root); +} + /** The absolute folder, optionally with a path inside it. */ export function stateDir(root: string, ...parts: string[]): string { + const to = redirected(root, parts[0]); + if (to) return join(to, ...parts); + return join(root, stateDirName(root), ...parts); +} + +/** The folder inside the project itself, whatever privilege separation does. */ +export function projectStateDir(root: string, ...parts: string[]): string { return join(root, stateDirName(root), ...parts); } /** `.maat/` if it exists, else `.molt/` if that does, else where a new one goes. */ export function stateFile(root: string, file: string): string { + const to = redirected(root, file); + if (to) return join(to, file); for (const d of STATE_DIRS) { const p = join(root, d, file); if (existsSync(p)) return p; diff --git a/test/privsep.test.ts b/test/privsep.test.ts new file mode 100644 index 0000000..492bea5 --- /dev/null +++ b/test/privsep.test.ts @@ -0,0 +1,224 @@ +/** + * Privilege separation: the worker's tools run as another user. + * + * 2026-10-07: a Grok worker in an unattended run grepped `/`, `/opt`, + * `~/.grok`, Maat's dist and `.maat/` for its hidden checks. Its tools ran as + * Maat's own uid, so it could read the reference program in /tmp, the mission + * contract, the judge's session store and Maat's records. With + * `--worker-user` the kernel says no. + * + * The Linux tests need root (to switch users) and run in CI's Linux job; they + * skip elsewhere. The default-unchanged tests run everywhere. + */ +import assert from "node:assert/strict"; +import { execFileSync, spawnSync } from "node:child_process"; +import { chmodSync, chownSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { homedir, tmpdir } from "node:os"; +import { join } from "node:path"; +import { after, before, describe, it } from "node:test"; +import { Engine } from "../src/engine.js"; +import { Journal } from "../src/journal.js"; +import { Receipts } from "../src/receipts.js"; +import { Integrity } from "../src/integrity.js"; +import { runCommand } from "../src/run.js"; +import { stateDir } from "../src/statedir.js"; +import { disablePrivSep, enablePrivSep, privSep, type PrivSep } from "../src/privsep.js"; +import type { Check, EngineEvent } from "../src/types.js"; +import { fileURLToPath } from "node:url"; +import { allowAll, scriptedProvider, workspace } from "./helpers.js"; + +const HELPER = fileURLToPath(new URL("../src/fs-helper.js", import.meta.url)); + +const linuxRoot = process.platform === "linux" && process.getuid?.() === 0 && spawnSync("runuser", ["--help"]).status === 0; +const WORKER = process.env.MAAT_TEST_WORKER_USER ?? "maattestw"; + +/** Run a shell command as the worker user, the way an attacker in the worker would. */ +function asWorker(cmd: string): { status: number | null; out: string } { + const r = spawnSync("runuser", ["-u", WORKER, "--", "sh", "-c", cmd], { encoding: "utf8" }); + return { status: r.status, out: `${r.stdout}${r.stderr}` }; +} + +function ensureWorker(): number { + if (spawnSync("id", ["-u", WORKER]).status !== 0) execFileSync("useradd", ["-m", "-s", "/bin/sh", WORKER]); + return Number(execFileSync("id", ["-u", WORKER], { encoding: "utf8" }).trim()); +} + +/** A project folder the worker owns, as the bench hands one over. */ +function project(uid: number): string { + const dir = mkdtempSync(join("/tmp", "maat-priv-proj-")); + chmodSync(dir, 0o755); + chownSync(dir, uid, uid); + return dir; +} + +function toolResults(provider: ReturnType): string[] { + const last = provider.requests().at(-1) as { messages: { role: string; content: string | null }[] } | undefined; + return (last?.messages ?? []).filter((m) => m.role === "tool").map((m) => String(m.content ?? "")); +} + +describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "needs Linux and root (CI's Linux job)" }, () => { + let uid = 0; + let dir = ""; + let ps: PrivSep; + const savedState = process.env.MAAT_STATE_DIR; + + before(() => { + uid = ensureWorker(); + dir = project(uid); + // A mission contract waiting in the task folder before the job starts. + mkdirSync(join(dir, ".maat", "mission", "library"), { recursive: true }); + writeFileSync(join(dir, ".maat", "mission", "contract.yml"), "assertions: [{id: a, run: 'grep -q sealed-assertion-77 out.txt'}]\n"); + writeFileSync(join(dir, ".maat", "done.yml"), "checks: []\n"); + execFileSync("chown", ["-R", `${uid}:${uid}`, dir]); + delete process.env.MAAT_STATE_DIR; + ps = enablePrivSep({ user: WORKER, project: dir, helper: HELPER }); + }); + + after(() => { + disablePrivSep(); + rmSync(ps.stateRoot, { recursive: true, force: true }); + rmSync(join(homedir(), ".grok", "judge-session.json"), { force: true }); + if (savedState !== undefined) process.env.MAAT_STATE_DIR = savedState; + rmSync(dir, { recursive: true, force: true }); + }); + + it("puts Maat's records, temp dirs and the mission contract out of the worker's reach", () => { + assert.ok(!ps.stateRoot.startsWith(dir), "the state dir is outside the project"); + assert.equal(statSync(ps.stateRoot).mode & 0o777, 0o700); + assert.equal(statSync(ps.stateRoot).uid, process.getuid!()); + + // Records are redirected; the checks file stays in the project. + assert.ok(stateDir(dir, "receipts").startsWith(ps.stateRoot)); + assert.equal(stateDir(dir, "done.yml"), join(dir, ".maat", "done.yml")); + mkdirSync(stateDir(dir, "receipts"), { recursive: true }); + writeFileSync(stateDir(dir, "receipts", "0000-refused.md"), "hidden: grep -q sealed-check-42 out.txt\n"); + const r1 = asWorker(`cat '${stateDir(dir, "receipts", "0000-refused.md")}'`); + assert.notEqual(r1.status, 0, "the worker read a receipt in the state dir"); + assert.match(r1.out, /Permission denied/); + assert.notEqual(asWorker(`ls '${ps.stateRoot}'`).status, 0, "the worker listed the state dir"); + + // The contract left the task folder; the library (the worker's) stayed. + assert.ok(!existsSync(join(dir, ".maat", "mission", "contract.yml")), "the contract is still in the task folder"); + assert.ok(existsSync(join(ps.stateRoot, "mission", "contract.yml"))); + assert.ok(existsSync(join(dir, ".maat", "mission", "library"))); + assert.notEqual(asWorker(`cat '${join(ps.stateRoot, "mission", "contract.yml")}'`).status, 0); + + // A reference try made the way reference.ts makes one. + assert.ok(tmpdir().startsWith(ps.stateRoot), `Maat's temp dir ${tmpdir()} is not in the state dir`); + const refTry = mkdtempSync(join(tmpdir(), "ref-try-")); + writeFileSync(join(refTry, "reference.py"), "print('the answer')\n"); + chmodSync(join(refTry, "reference.py"), 0o644); + assert.notEqual(asWorker(`cat '${join(refTry, "reference.py")}'`).status, 0, "the worker read the reference program"); + + // The judge's HOME: its logins and its session store. + const home = homedir(); + mkdirSync(join(home, ".grok"), { recursive: true }); + writeFileSync(join(home, ".grok", "judge-session.json"), "{}"); + assert.notEqual(asWorker(`ls '${home}'`).status, 0, "the worker listed the judge's HOME"); + assert.notEqual(asWorker(`cat '${join(home, ".grok", "judge-session.json")}'`).status, 0); + }); + + it("runs the worker's bash as the worker uid, and its file tools as the worker", async () => { + const secret = join(ps.stateRoot, "receipts", "0000-refused.md"); + const provider = scriptedProvider([ + { + calls: [ + { name: "bash", args: { command: "id -u > who.txt; id -u" } }, + { name: "read_file", args: { path: secret } }, + { name: "write_file", args: { path: "made.txt", content: "mine\n" } }, + { name: "grep", args: { path: ".", pattern: "sealed" } }, + { name: "bash", args: { command: `cat '${secret}'; echo exit=$?` } }, + { name: "bash", args: { command: "ls /proc | grep -E '^[0-9]+$' | tr '\\n' ' '" } }, + ], + }, + { text: "Done." }, + ]); + const engine = new Engine({ baseUrl: "http://provider.test/v1", model: "m", cwd: dir, fetchFn: provider.fetchFn, bar: null, stream: false, autonomy: "high", sandbox: true }); + for await (const _ of engine.run("look around", allowAll, { ask: true })) { + /* drain */ + } + assert.equal(readFileSync(join(dir, "who.txt"), "utf8").trim(), String(uid), "bash ran as Maat's uid"); + assert.equal(statSync(join(dir, "who.txt")).uid, uid); + assert.equal(statSync(join(dir, "made.txt")).uid, uid, "write_file wrote as Maat"); + const results = toolResults(provider); + const joined = results.join("\n---\n"); + assert.ok(!joined.includes("sealed-check-42"), `a tool read the state dir:\n${joined}`); + assert.match(joined, /EACCES|permission denied/i, "read_file was refused by the kernel"); + if (ps.pidns) { + const pids = (results.at(-1) ?? "").trim().split(/\s+/).filter(Boolean).map(Number); + assert.ok(pids.length > 0 && pids.length < 10, `the worker's /proc: ${pids.join(" ")}`); + assert.ok(!pids.includes(process.pid), "the worker can see Maat in its process list"); + } + }); + + it("copies the receipts into the project when the job ends", async () => { + const ws = project(uid); + // A second project: the state dir follows whichever project was enabled. + disablePrivSep(); + const ps2 = enablePrivSep({ user: WORKER, project: ws, helper: HELPER }); + try { + const provider = scriptedProvider([{ calls: [{ name: "write_file", args: { path: "out.txt", content: "hello\n" } }] }, { text: "Done." }]); + const journal = new Journal(ws); + const receipts = new Receipts(ws); + const integrity = new Integrity(ws); + const engine = new Engine({ baseUrl: "http://provider.test/v1", model: "m", cwd: ws, fetchFn: provider.fetchFn, bar: null, stream: false, autonomy: "high", journal, receipts, integrity }); + const check: Check = { name: "task:out", kind: "command", run: "grep -q hello out.txt", timeoutMs: 5_000, expectExit: 0, tags: ["task", "value"], hidden: true }; + const events: EngineEvent[] = []; + for await (const ev of engine.run("write out.txt saying hello", allowAll, { taskChecks: [check] })) events.push(ev); + const end = events.find((e) => e.kind === "job_end"); + assert.ok(end && end.kind === "job_end" && end.outcome === "verified", `outcome ${end && end.kind === "job_end" ? end.outcome : "none"}`); + assert.ok(!existsSync(join(ws, ".maat", "receipts")), "receipts were written into the project during the job"); + assert.ok(readdirSync(join(ps2.stateRoot, "receipts")).some((f) => f.endsWith(".md"))); + + const dest = ps2.publish(); + assert.equal(dest, join(ws, ".maat")); + const mds = readdirSync(join(ws, ".maat", "receipts")).filter((f) => f.endsWith(".md")); + assert.ok(mds.length > 0, "no receipt in the project after the job"); + assert.ok(existsSync(join(ws, ".maat", "log")), "the journal was not copied"); + assert.equal(statSync(join(ws, ".maat", "receipts", mds[0]!)).uid, uid, "the copied receipt is not the project owner's"); + assert.ok(!existsSync(join(ws, ".maat", "tmp")), "Maat's temp dir was copied into the project"); + disablePrivSep(); + const v = Integrity.verify(ws); + assert.equal(v.ok, true, v.reason); + } finally { + disablePrivSep(); + rmSync(ws, { recursive: true, force: true }); + rmSync(ps2.stateRoot, { recursive: true, force: true }); + } + }); +}); + +describe("privilege separation off (the default)", () => { + it("changes nothing: records in the project, commands and files as Maat's own user", async () => { + assert.equal(privSep(), undefined); + const ws = workspace(); + try { + assert.equal(stateDir(ws.dir, "receipts"), join(ws.dir, ".maat", "receipts")); + if (process.platform !== "win32") { + const r = await runCommand("id -u", { cwd: ws.dir, asWorker: true }); + assert.equal(r.stdout.trim(), String(process.getuid!())); + } + const provider = scriptedProvider([ + { calls: [{ name: "write_file", args: { path: "a.txt", content: "x\n" } }, { name: "bash", args: { command: "echo hi > b.txt" } }] }, + { text: "Done." }, + ]); + const engine = new Engine({ baseUrl: "http://provider.test/v1", model: "m", cwd: ws.dir, fetchFn: provider.fetchFn, bar: null, stream: false, autonomy: "high" }); + for await (const _ of engine.run("make two files", allowAll, { ask: true })) { + /* drain */ + } + if (process.platform !== "win32") { + assert.equal(statSync(join(ws.dir, "a.txt")).uid, process.getuid!()); + assert.equal(statSync(join(ws.dir, "b.txt")).uid, process.getuid!()); + } + assert.equal(readFileSync(join(ws.dir, "b.txt"), "utf8"), "hi\n"); + } finally { + ws.cleanup(); + } + }); + + it("refuses a worker user that cannot be switched to", () => { + if (process.platform === "win32") return; + assert.throws(() => enablePrivSep({ user: "maat-no-such-user-xyz", project: tmpdir(), helper: HELPER }), /no such user/); + assert.equal(privSep(), undefined); + }); +}); From da08ea11cbe329abdee1063a3a8ca3321c4e5d9f Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 12:03:00 -0700 Subject: [PATCH 2/9] Worker cwd inside the PID namespace; bench tasks off the host mount The first container run showed every git command of the worker failing with "getcwd() failed: Permission denied". nsenter --wd opens the directory before entering the mount namespace, so inside it the cwd is unreachable and getcwd() falls back to walking the parents, which the worker cannot read under /var/lib/bench-work (711). The worker now enters the directory by path inside the namespace. The test project sits under a 711 parent and runs git, so this cannot pass by luck again. /work is a host bind mount: on OrbStack chown is ignored and modes are not enforced for other users, so locking finished tasks there did nothing. Tasks run in a container-local /var/lib/bench-work (711, finished folders and logs root-only) and are copied to /work (BENCH_EXPORT) when run.py ends. --- bench/local/container/run-in-container.sh | 7 +++++-- bench/local/run.py | 13 +++++++++++++ docs/privilege-separation.md | 14 +++++++++----- src/privsep.ts | 10 +++++++++- test/privsep.test.ts | 11 +++++++++-- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/bench/local/container/run-in-container.sh b/bench/local/container/run-in-container.sh index 24eeb4d..3bbc5c4 100755 --- a/bench/local/container/run-in-container.sh +++ b/bench/local/container/run-in-container.sh @@ -69,9 +69,12 @@ printf 'OPENROUTER_API_KEY=%s\n' "$key" > "$envf" # The graders and reference solutions are mounted where only root can reach (/root is 700), # copied to a root-only /opt/bench, and run.py runs from there as root; it runs the agent # as the unprivileged `agent` user (BENCH_AGENT_USER) and grades as root afterwards. -# /work is 711: the worker can reach its own task folder by name but cannot list the others. +# Tasks run in a container-local /var/lib/bench-work (711: the worker reaches its own task folder +# by name but cannot list the others) and are copied to /work when run.py ends. /work is a host +# bind mount, where chown is ignored and modes are not enforced, so nothing locked there would +# stay locked from a later task's worker. privsep=${BENCH_PRIVSEP:-1} -credmount=; startcmd='export MOLT_DIST_ABS=$(npm root -g)/@solvyx/molt/dist; mkdir -p /work && chmod 711 /work && rm -rf /opt/bench && cp -a /root/bench-src /opt/bench && chmod -R go-rwx /opt/bench && cd /opt/bench && python3 run.py "$@"' +credmount=; startcmd='export MOLT_DIST_ABS=$(npm root -g)/@solvyx/molt/dist BENCH_WORK=/var/lib/bench-work BENCH_EXPORT=/work; mkdir -p /work $BENCH_WORK && chmod 711 $BENCH_WORK && rm -rf /opt/bench && cp -a /root/bench-src /opt/bench && chmod -R go-rwx /opt/bench && cd /opt/bench && python3 run.py "$@"' if [ "$SUBSCRIPTION" = grok ]; then credmount="-v $HOME/.grok/auth.json:/root/grok-cred/auth.json:ro" # The worker's grok runs as `agent`, so its credential copy lives in ITS home, owned by it, diff --git a/bench/local/run.py b/bench/local/run.py index e7c5654..290f56b 100644 --- a/bench/local/run.py +++ b/bench/local/run.py @@ -28,6 +28,11 @@ # Outside every git repository: a task folder inside this one let agents' # `git commit` walk up and commit task files into molt-desktop's main. WORK = Path(os.environ.get("BENCH_WORK", Path.home() / ".cache/maat-bench/work")) +# BENCH_EXPORT: where the task folders and logs are copied when the whole run ends. The +# container works in a container-local BENCH_WORK because a host bind mount (OrbStack, Docker +# Desktop) ignores chown and does not enforce modes for other users: on /work a later task's +# worker could read every earlier task's log and released checks however they were locked. +EXPORT = os.environ.get("BENCH_EXPORT") # MOLT_DIST_ABS: an absolute path to the built Maat (the container mounts it at /maat). MOLT = (Path(os.environ["MOLT_DIST_ABS"]) if os.environ.get("MOLT_DIST_ABS") else Path.home() / os.environ.get("MOLT_DIST", "Documents/molt-desktop/dist-compare")) / "cli.js" LIMIT = int(os.environ.get("BENCH_LIMIT", "600")) # seconds per task @@ -225,6 +230,14 @@ def parse_arms(spec: str | None) -> list[tuple[str | None, dict]]: def main(which: str, repeats: int, task_filter: str | None) -> None: + try: + run_all(which, repeats, task_filter) + finally: + if EXPORT and Path(EXPORT).resolve() != WORK.resolve() and WORK.exists(): + shutil.copytree(WORK, EXPORT, symlinks=True, dirs_exist_ok=True) + + +def run_all(which: str, repeats: int, task_filter: str | None) -> None: from tasks2 import TASKS2 # noqa: PLC0415 from tasks3 import TASKS3 # noqa: PLC0415 agents = {"molt": run_molt} diff --git a/docs/privilege-separation.md b/docs/privilege-separation.md index e8c637c..f3c3aaf 100644 --- a/docs/privilege-separation.md +++ b/docs/privilege-separation.md @@ -136,11 +136,15 @@ process entirely. - The worker's Grok credential is copied to `/home/agent/.grok`. A Grok judge's credential is copied to `/root/.grok` and the OpenCode judge's to `/root/.local/share/opencode`. `/root` is mode 700. -- `/work` is mode 711, so a worker can reach its own task folder by name but - cannot list the others. Each task's log is written mode 600, and after - grading the task folder is locked to root (700). This stops a later task - (a repeat of the same task, the other arm of a pair) from reading the - checks an earlier one released. +- Tasks run in a container-local `/var/lib/bench-work` (mode 711), so a + worker can reach its own task folder by name but cannot list the others. + Each task's log is written mode 600, and after grading the task folder is + locked to root (700). This stops a later task (a repeat of the same task, + the other arm of a pair) from reading the checks an earlier one released. + Everything is copied to `/work` (`BENCH_EXPORT`) when `run.py` ends. `/work` + is a host bind mount, and on OrbStack and Docker Desktop a bind mount + ignores `chown` and does not enforce file modes for other users, so a lock + placed there would not hold. - The graders and reference solutions stay root-only, as before. To check it from outside a running job: diff --git a/src/privsep.ts b/src/privsep.ts index a7d2978..2d93644 100644 --- a/src/privsep.ts +++ b/src/privsep.ts @@ -392,7 +392,6 @@ export class PrivSep { `--target=${this.nsInit}`, "--pid", "--mount", - `--wd=${cwd}`, "--", "setpriv", `--reuid=${uid}`, @@ -404,6 +403,15 @@ export class PrivSep { "--no-new-privs", "--bounding-set=-all", "--", + // The working directory is entered inside the namespace, by path. + // nsenter --wd opens it before entering, and a cwd from the outer + // mount namespace is "unreachable" inside: getcwd() then fails with + // EACCES whenever a parent is not readable by the worker (bench: + // /var/lib/bench-work is 711), and git refuses to run at all. + "/bin/sh", + "-c", + 'cd -- "$0" && exec "$@"', + cwd, file, ...args, ], diff --git a/test/privsep.test.ts b/test/privsep.test.ts index 492bea5..da02fa4 100644 --- a/test/privsep.test.ts +++ b/test/privsep.test.ts @@ -45,7 +45,13 @@ function ensureWorker(): number { /** A project folder the worker owns, as the bench hands one over. */ function project(uid: number): string { - const dir = mkdtempSync(join("/tmp", "maat-priv-proj-")); + // Under a parent the worker may pass through but not read, as the bench's + // /var/lib/bench-work: a cwd that only works because "/tmp" is readable + // hid a getcwd() failure that broke every git command. + const parent = "/var/tmp/maat-priv-711"; + mkdirSync(parent, { recursive: true }); + chmodSync(parent, 0o711); + const dir = mkdtempSync(join(parent, "proj-")); chmodSync(dir, 0o755); chownSync(dir, uid, uid); return dir; @@ -123,7 +129,7 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need const provider = scriptedProvider([ { calls: [ - { name: "bash", args: { command: "id -u > who.txt; id -u" } }, + { name: "bash", args: { command: "id -u > who.txt; pwd -P > where.txt; git init -q . && git status --short >/dev/null && echo git-ok >> where.txt" } }, { name: "read_file", args: { path: secret } }, { name: "write_file", args: { path: "made.txt", content: "mine\n" } }, { name: "grep", args: { path: ".", pattern: "sealed" } }, @@ -139,6 +145,7 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need } assert.equal(readFileSync(join(dir, "who.txt"), "utf8").trim(), String(uid), "bash ran as Maat's uid"); assert.equal(statSync(join(dir, "who.txt")).uid, uid); + assert.equal(readFileSync(join(dir, "where.txt"), "utf8"), `${dir}\ngit-ok\n`, "the worker's commands cannot see their own cwd"); assert.equal(statSync(join(dir, "made.txt")).uid, uid, "write_file wrote as Maat"); const results = toolResults(provider); const joined = results.join("\n---\n"); From 5205f0ab3a0554fada1f92dc9c4670ba911a77e9 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 12:05:21 -0700 Subject: [PATCH 3/9] Bench graders run git in agent-owned repositories: mark them safe On the container's own filesystem the task folder really is owned by the agent user, and the root graders' git refused it as dubious ownership, so fix-git graded a correct merge as a failure. The old /work bind mount ignored chown, which hid this. run.py sets safe.directory for itself and the graders through GIT_CONFIG_*; Maat strips it from the worker. --- bench/local/run.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bench/local/run.py b/bench/local/run.py index 290f56b..3c46b00 100644 --- a/bench/local/run.py +++ b/bench/local/run.py @@ -65,6 +65,17 @@ def openrouter_key() -> str: # whole of Maat as the agent user, as before 2026-10-07. PRIVSEP = bool(AGENT_USER) and os.environ.get("BENCH_PRIVSEP", "1") != "0" and os.geteuid() == 0 +if AGENT_USER and os.geteuid() == 0: + # The graders (root) run git in task repositories the agent user owns. On the container's + # own filesystem ownership is real, and git refuses a repository owned by someone else + # ("dubious ownership"): fix-git graded every correct merge as a failure. On the old /work + # bind mount chown was ignored, so this never showed. Maat passes the same setting to its + # own checks and strips it from the worker's environment. + _n = int(os.environ.get("GIT_CONFIG_COUNT") or 0) + os.environ[f"GIT_CONFIG_KEY_{_n}"] = "safe.directory" + os.environ[f"GIT_CONFIG_VALUE_{_n}"] = "*" + os.environ["GIT_CONFIG_COUNT"] = str(_n + 1) + def as_agent(cmd: list, env: dict | None = None) -> tuple[list, dict | None]: """With BENCH_AGENT_USER set, run the agent as that user: it cannot read the graders From 8d159f790dec3e99b8a7b840c092ad22ce8a8b32 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 14:24:03 -0700 Subject: [PATCH 4/9] Privilege separation: root never follows paths the worker controls - Background job logs are created by the worker: its own shell makes .maat/bg and opens .log, so a planted symlink only redirects the worker's own write. Maat no longer opens or chowns anything there. - handBack walks by descriptor (/proc/self/fd// with O_NOFOLLOW|O_DIRECTORY, fchown on the open fd), checks the project is the same folder by device and inode, skips anything that moved out of it, multiply-linked files, symlinks, special files and other file systems. - seed reads the project's .maat/ through the file helper as the worker (fs-helper pack) and gets back only regular files and folders; the mission contract is removed by the worker too. Symlinks are skipped and reported, so none can appear in the state dir. - publish reads Maat's state dir and has the helper write the copies as the worker (fs-helper unpack), so Maat writes nothing into the worker's tree. - ACP specs name the worker agent's own credential (OpenCode: OPENCODE_API_KEY), which alone passes the worker env scrub for that agent. - CI runs on pull requests into claude/** as well as main. - Root-only tests: a planted .maat/bg log link, a folder flipped to a symlink during handBack, a hard link, seed with symlinked records. --- .github/workflows/check.yml | 3 +- docs/privilege-separation.md | 37 ++++- src/acp.ts | 13 +- src/background.ts | 53 ++++--- src/fs-helper.ts | 151 ++++++++++++++++-- src/privsep.ts | 298 +++++++++++++++++++++++++++-------- test/privsep.test.ts | 246 ++++++++++++++++++++++++++++- 7 files changed, 682 insertions(+), 119 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index f9e641e..dfe615f 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -3,7 +3,8 @@ name: check on: push: branches: [main] - # Stacked work is reviewed as PRs into claude/* branches, not only main. + # Stacked PRs target claude/* branches, not main; both get the checks + # (privsep-linux included). pull_request: branches: [main, "claude/**"] diff --git a/docs/privilege-separation.md b/docs/privilege-separation.md index f3c3aaf..3383acb 100644 --- a/docs/privilege-separation.md +++ b/docs/privilege-separation.md @@ -42,7 +42,12 @@ bound those. As root, Maat spawns them with the worker's uid and gid and no supplementary groups. With sudo, it uses `sudo -n -u env -i ...`. The environment is scrubbed: every variable whose name looks like a credential is removed, along with Maat's own `MAAT_*` and `MOLT_*` settings. HOME, USER and -LOGNAME are set to the worker's, and TMPDIR is `/tmp`. +LOGNAME are set to the worker's, and TMPDIR is `/tmp`. The one exception is +the worker agent's own login: an ACP spec names it (`workerCredentialEnv`; +for OpenCode, `OPENCODE_API_KEY`), and that name, and no other, passes the +scrub for that agent's process only, never for the worker's shell commands. +Grok has none: its login must be in the worker user's HOME +(`~/.grok/auth.json`), as the bench sets it up. **The file tools run as the worker.** `read_file`, `write_file`, `edit_file`, `list_dir`, `grep` and `inspect` are carried out by a small helper process @@ -66,10 +71,18 @@ scratch dirs, the copy-on-run trees for hidden checks (`maat-check-*`) and the judge's own temp files are private too. When the job starts, records from earlier jobs are copied out of the project's `.maat/` so the hash chains continue, and a mission's contract, features and state are moved out of the -task folder. +task folder. That copy is read, and the contract removed, by the file helper +running as the worker (`fs-helper.js pack`); Maat gets back regular files and +folders by name and writes them into its own state dir. Symlinks and other +special files in the project's `.maat/` are skipped, so nothing in the state +dir can point back into the worker's reach or out to a file of root's. These stay in the project: `.maat/done.yml`, because a person wrote it, and -`.maat/bg/`, which holds the worker's own background-job logs. +`.maat/bg/`, which holds the worker's own background-job logs. The worker +makes those itself: a background job starts as the worker with a shell that +creates the folder and opens its log, so Maat never opens a path in the +worker's tree (a planted `.maat/bg/.log` symlink only redirects the +worker's own write). `.maat/mission/library/` also stays, because the worker writes it. A spilled output (`.maat/out/.txt`) is still readable through `read_file`: Maat serves its own masked copy of the worker's output. The worker cannot `cat` @@ -77,9 +90,12 @@ it from bash. **When the job ends, the records come back.** `maat run`, `maat ask` and `maat mission run` copy the state dir (except `tmp/`) into the project's -`.maat/`. The copies are owned by the project's owner, so receipts are where -people always found them and `maat verify` checks them there. The state dir -itself is kept. +`.maat/`, so receipts are where people always found them and `maat verify` +checks them there. Maat reads its own state dir and the file helper, as the +worker, writes the copies (`fs-helper.js unpack`), so they are owned by the +worker and Maat writes nothing into the worker's tree. If a copy cannot be +written (the worker planted a link to somewhere it cannot write), Maat says +so and the records stay in the state dir, which is always kept. **The judge and the checks stay Maat's.** Judge and ask subprocesses (OpenCode, a Grok judge, an HTTP judge) run as Maat with Maat's HOME. If that @@ -88,7 +104,14 @@ refuses to start otherwise. Hidden checks run as Maat in the copy-on-run tree, never as the worker. A check that ran in place (a project check, or a tree too large to copy) can leave files owned by Maat in the project. After every bar run, those are handed back to the worker, so the worker can keep -editing its own tree. Git, running as Maat on a repository the worker owns, +editing its own tree. The worker may be changing the tree while that runs, +so it is never walked by path: each folder is opened relative to its parent +with `O_NOFOLLOW|O_DIRECTORY` (`/proc/self/fd//`), ownership is +changed with `fchown` on the open descriptor, and the project itself must be +the folder (device and inode) the job started in. A folder swapped for a +symlink mid-walk fails to open and is skipped; files with more than one hard +link, symlinks, special files and other file systems are left alone. Hand-back +is Linux-only; elsewhere root-made files stay Maat's. Git, running as Maat on a repository the worker owns, is told `safe.directory=*` through `GIT_CONFIG_*`. The worker's environment does not get that setting. diff --git a/src/acp.ts b/src/acp.ts index a6f2a47..5abd466 100644 --- a/src/acp.ts +++ b/src/acp.ts @@ -130,6 +130,15 @@ export type AcpAgentSpec = { readonly loginHint: string; /** Where the CLI keeps the credential, so health can say "logged out". */ readonly credentialPath: string; + /** + * Environment variables that carry this agent's own login. Under privilege + * separation (src/privsep.ts) the worker's environment loses every + * credential-looking name; these, and only for this agent's process when it + * is the worker, pass through. Anything else (another provider's key, a + * token the worker's shell commands would see) stays out. An agent without + * one must find its login in the worker user's HOME. + */ + readonly workerCredentialEnv?: readonly string[]; /** * How this agent can be handed Maat's tool server. * @@ -253,6 +262,8 @@ export const ACP_AGENTS: readonly AcpAgentSpec[] = [ installHint: "npm install -g opencode-ai", loginHint: "opencode auth login", credentialPath: ".local/share/opencode/auth.json", + // The Zen account's key, when it arrives through the environment rather than auth.json. + workerCredentialEnv: ["OPENCODE_API_KEY"], mcpTransport: "http", sessionMeta: () => ({}), }, @@ -539,7 +550,7 @@ export class AcpConnection { // its own reads (Grok auto-approves read_file, grep, list_dir) never // reach Maat, so only the uid can bound them. const ps = this.opts.asWorker ? privSep() : undefined; - const spec = ps?.execSpec(this.spec.bin, this.spec.args, cwd, env); + const spec = ps?.execSpec(this.spec.bin, this.spec.args, cwd, env, this.spec.workerCredentialEnv); const child = spawnFn(spec?.file ?? this.spec.bin, spec?.args ?? [...this.spec.args], { cwd, stdio: ["pipe", "pipe", "pipe"], diff --git a/src/background.ts b/src/background.ts index adb91f0..36ff280 100644 --- a/src/background.ts +++ b/src/background.ts @@ -98,33 +98,40 @@ export function startBackground( const id = nextId++; const bgRel = `${stateDirName(opts.cwd)}/bg`; const dir = join(opts.cwd, bgRel); - mkdirSync(dir, { recursive: true }); const log = `${bgRel}/${id}.log`; - const fd = openSync(join(opts.cwd, log), "w"); - // Under privilege separation the job, and its log, are the worker's. const ps = opts.asWorker ? privSep() : undefined; + let child; if (ps) { - ps.giveToWorker(join(opts.cwd, stateDirName(opts.cwd))); - ps.giveToWorker(dir); - ps.giveToWorker(join(opts.cwd, log)); + // Under privilege separation the job is the worker's, and so is its log: + // the worker's own shell makes the folder and opens the file. Maat (root) + // never creates or opens a path in the worker's tree, where a planted + // `.maat/bg/.log -> /etc/...` would have made root truncate the + // target and the worker's command write into it. + const spec = ps.execSpec( + "/bin/sh", + ["-c", 'mkdir -p -- "$1" && exec >"$2" 2>&1 = { }, }; +// --------------------------------------------------------------------------- +// One-shot modes, run as the worker so Maat never walks the worker's tree. +// +// fs-helper.js pack stdin {src, skip, clear} stdout {entries, skipped} +// fs-helper.js unpack stdin {dest, entries} stdout {written, failed} +// +// `pack` reads the project's `.maat/` for Maat to seed its private state dir +// from; `unpack` writes Maat's records back into it when the job ends. The +// worker owns the tree, so whatever a symlink in it reaches is what the worker +// could reach anyway; Maat, on its side, only ever gets regular files and +// folders by name (src/privsep.ts). + +export type PackEntry = { rel: string; kind: "dir" | "file"; mode: number; data?: string }; + +function pack(req: { src: string; skip?: string[]; clear?: { dir: string; keep: string[] } }): { entries: PackEntry[]; skipped: string[] } { + const entries: PackEntry[] = []; + const skipped: string[] = []; + const skip = new Set(req.skip ?? []); + let top; + try { + top = lstatSync(req.src); + } catch { + return { entries, skipped }; + } + if (!top.isDirectory()) return { entries, skipped: ["."] }; + const walk = (rel: string): void => { + for (const name of readdirSync(rel ? join(req.src, rel) : req.src)) { + const r = rel ? `${rel}/${name}` : name; + if (skip.has(r)) continue; + const p = join(req.src, r); + const st = lstatSync(p); + if (st.isDirectory()) { + entries.push({ rel: r, kind: "dir", mode: st.mode & 0o777 }); + walk(r); + } else if (st.isFile()) { + // O_NOFOLLOW: what was a file at lstat is still not a symlink at open. + const fd = openSync(p, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); + try { + if (!fstatSync(fd).isFile()) { + skipped.push(r); + continue; + } + entries.push({ rel: r, kind: "file", mode: st.mode & 0o777, data: readFileSync(fd).toString("base64") }); + } finally { + closeSync(fd); + } + } else { + // Symlinks, FIFOs, sockets, devices: never carried into Maat's state dir. + skipped.push(r); + } + } + }; + walk(""); + if (req.clear) { + const d = join(req.src, req.clear.dir); + try { + if (lstatSync(d).isDirectory()) { + for (const name of readdirSync(d)) if (!req.clear.keep.includes(name)) rmSync(join(d, name), { recursive: true, force: true }); + } + } catch { + /* nothing to clear */ + } + } + return { entries, skipped }; +} + +function unpack(req: { dest: string; entries: PackEntry[] }): { written: number; failed: string[] } { + let written = 0; + const failed: string[] = []; + mkdirSync(req.dest, { recursive: true }); + for (const e of req.entries) { + const p = join(req.dest, e.rel); + try { + if (e.kind === "dir") { + mkdirSync(p, { recursive: true }); + continue; + } + mkdirSync(dirname(p), { recursive: true }); + try { + if (!lstatSync(p).isFile()) rmSync(p, { recursive: true, force: true }); + } catch { + /* not there yet */ + } + writeFileSync(p, Buffer.from(e.data ?? "", "base64"), { mode: e.mode & 0o777 }); + written += 1; + } catch { + failed.push(e.rel); + } + } + return { written, failed }; +} + +function oneShot(mode: string): void { + const chunks: Buffer[] = []; + process.stdin.on("data", (d: Buffer) => chunks.push(d)); + process.stdin.on("end", () => { + try { + const req = JSON.parse(Buffer.concat(chunks).toString("utf8")); + const out = mode === "pack" ? pack(req) : unpack(req); + process.stdout.write(JSON.stringify(out), () => process.exit(0)); + } catch (e) { + process.stderr.write(`${(e as Error)?.message ?? String(e)}\n`); + process.exit(1); + } + }); +} + const reply = (msg: unknown) => process.stdout.write(JSON.stringify(msg) + "\n"); async function handle(line: string): Promise { @@ -58,14 +179,18 @@ async function handle(line: string): Promise { } } -let buf = ""; -process.stdin.setEncoding("utf8"); -process.stdin.on("data", (d: string) => { - buf += d; - for (let i = buf.indexOf("\n"); i >= 0; i = buf.indexOf("\n")) { - const line = buf.slice(0, i); - buf = buf.slice(i + 1); - if (line.trim()) void handle(line); - } -}); -process.stdin.on("end", () => process.exit(0)); +const mode = process.argv[2]; +if (mode === "pack" || mode === "unpack") oneShot(mode); +else { + let buf = ""; + process.stdin.setEncoding("utf8"); + process.stdin.on("data", (d: string) => { + buf += d; + for (let i = buf.indexOf("\n"); i >= 0; i = buf.indexOf("\n")) { + const line = buf.slice(0, i); + buf = buf.slice(i + 1); + if (line.trim()) void handle(line); + } + }); + process.stdin.on("end", () => process.exit(0)); +} diff --git a/src/privsep.ts b/src/privsep.ts index 2d93644..8b96a60 100644 --- a/src/privsep.ts +++ b/src/privsep.ts @@ -39,19 +39,23 @@ import { spawn, spawnSync, type ChildProcess } from "node:child_process"; import { randomBytes } from "node:crypto"; import { chmodSync, - cpSync, + closeSync, + constants, existsSync, - lchownSync, + fchownSync, + fstatSync, lstatSync, mkdirSync, + openSync, readFileSync, readdirSync, + readlinkSync, realpathSync, - rmSync, statSync, + writeFileSync, } from "node:fs"; import { homedir } from "node:os"; -import { dirname, join, relative, resolve, sep } from "node:path"; +import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import type { GrepResult, WalkOptions, WalkResult } from "./files.js"; import { redirectState, stateDirName } from "./statedir.js"; @@ -83,7 +87,23 @@ export type WorkerFs = { export class PrivSepError extends Error {} -/** Environment names that hold a credential, whatever the vendor. */ +/** One entry `fs-helper.js pack` reads or `unpack` writes (src/fs-helper.ts). */ +export type PackEntry = { rel: string; kind: "dir" | "file"; mode: number; data?: string }; + +/** A relative path from the file helper, or null if it could leave its root. */ +export function safeRel(rel: string): string | null { + if (typeof rel !== "string" || !rel || rel.includes("\0") || isAbsolute(rel)) return null; + const parts = rel.split("/"); + if (parts.some((p) => p === "" || p === "." || p === ".." || p.includes(sep))) return null; + return parts.join(sep); +} + +/** + * Environment names that hold a credential, whatever the vendor. Broad on + * purpose: it also drops harmless names such as AUTHOR_NAME, which costs the + * worker nothing it needs. A worker agent's own credential gets through only + * by name, from the agent's allowlist (`execSpec`'s `keep`). + */ const SECRETISH = /(KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL|COOKIE|AUTH)/i; /** Look a user up by name: NSS first (getent), then /etc/passwd. */ @@ -189,6 +209,9 @@ export class PrivSep { readonly mode: "root" | "sudo"; readonly pidns: boolean; readonly helperScript: string; + /** The project as it was when the job started, so a swapped folder is never walked. */ + private readonly projectReal: string; + private readonly projectId: { dev: number; ino: number }; private nsLeader?: ChildProcess; private nsInit?: number; private helper?: FsHelper; @@ -207,6 +230,9 @@ export class PrivSep { } if (this.worker.uid === 0) throw new PrivSepError("--worker-user must not be root"); this.project = resolve(opts.project); + this.projectReal = realpathSync(this.project); + const pst = statSync(this.projectReal); + this.projectId = { dev: pst.dev, ino: pst.ino }; this.helperScript = opts.helper ?? join(dirname(realpathSync(process.argv[1] ?? ".")), "fs-helper.js"); if (!existsSync(this.helperScript)) throw new PrivSepError(`the file helper is missing: ${this.helperScript}`); if (uid === 0) this.mode = "root"; @@ -279,25 +305,90 @@ export class PrivSep { * continue; a mission contract leaves the project entirely until the job is * over. The checks file, the worker's background logs and the mission * library (which the worker writes) stay. + * + * The project's `.maat/` is the worker's: it is read, and the contract + * removed, by the file helper running as the worker (`fs-helper.js pack`), + * never by Maat walking it. Only regular files and folders come back, by + * name, and Maat writes them into its own state dir: a symlink in the + * project (`.maat/receipts -> /root/...`) is skipped by the helper and could + * not be created here anyway, so Maat never writes through one later. */ private seed(): void { const src = join(this.project, stateDirName(this.project)); - if (!existsSync(src)) return; - for (const name of readdirSync(src)) { - if (name === "done.yml" || name === "bg") continue; - const from = join(src, name); - cpSync(from, join(this.stateRoot, name), { - recursive: true, - force: true, - filter: (p) => relative(src, p) !== join("mission", "library") && !relative(src, p).startsWith(join("mission", "library") + sep), - }); + const out = this.runHelper<{ entries: PackEntry[]; skipped: string[] }>("pack", { + src, + skip: ["done.yml", "bg", "mission/library"], + clear: { dir: "mission", keep: ["library"] }, + }); + for (const e of out.entries) { + const rel = safeRel(e.rel); + if (!rel) continue; + const to = join(this.stateRoot, rel); + if (!this.privateParent(to)) continue; + try { + if (e.kind === "dir") mkdirSync(to, { mode: 0o700 }); + else writeFileSync(to, Buffer.from(e.data ?? "", "base64"), { mode: 0o600, flag: "w" }); + } catch { + /* an entry that clashes with the state dir's own is not copied */ + } } - const mission = join(src, "mission"); - if (existsSync(mission)) { - for (const name of readdirSync(mission)) { - if (name !== "library") rmSync(join(mission, name), { recursive: true, force: true }); + this.skippedOnSeed = out.skipped; + } + + /** What `seed` refused to carry over (symlinks and other non-files), project-relative to `.maat/`. */ + skippedOnSeed: readonly string[] = []; + + /** + * True when every folder from the state root down to `p`'s parent exists + * and is a real folder (not a symlink). The state dir is Maat's and mode + * 700, so this holds unless something went badly wrong; it is checked + * anyway, because a write here is a write as Maat. + */ + private privateParent(p: string): boolean { + const rel = relative(this.stateRoot, dirname(p)); + if (rel.startsWith("..") || isAbsolute(rel)) return false; + let cur = this.stateRoot; + for (const part of rel ? rel.split(sep) : []) { + cur = join(cur, part); + try { + const st = lstatSync(cur); + if (!st.isDirectory() || st.uid !== this.maatUid) return false; + } catch { + return false; } } + try { + const st = lstatSync(p); + if (st.isSymbolicLink()) return false; + } catch { + /* not there yet: fine */ + } + return true; + } + + /** + * Run the file helper once, as the worker, with `input` on stdin and JSON + * on stdout (`pack`, `unpack`; see src/fs-helper.ts). + */ + private runHelper(mode: "pack" | "unpack", input: unknown): T { + const env = { ...this.workerEnv(), ELECTRON_RUN_AS_NODE: "1" }; + const spec: SpawnSpec = + this.mode === "root" + ? { file: process.execPath, args: [this.helperScript, mode], env, uid: this.worker.uid, gid: this.worker.gid } + : this.execSpec(process.execPath, [this.helperScript, mode], "/", env); + const r = spawnSync(spec.file, spec.args, { + cwd: "/", + env: { ...spec.env, ELECTRON_RUN_AS_NODE: "1" }, + input: JSON.stringify(input), + encoding: "utf8", + maxBuffer: 1 << 30, + timeout: 120_000, + ...(spec.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), + }); + if (r.status !== 0) { + throw new PrivSepError(`the worker file helper (${mode}) failed: ${(r.error?.message ?? r.stderr ?? "").toString().trim() || `exit ${r.status}`}`); + } + return JSON.parse(r.stdout) as T; } /** @@ -355,11 +446,11 @@ export class PrivSep { } /** The environment a worker process gets: no credentials, no Maat internals, its own HOME and /tmp. */ - workerEnv(base: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv { + workerEnv(base: NodeJS.ProcessEnv = process.env, keep: readonly string[] = []): NodeJS.ProcessEnv { const env: NodeJS.ProcessEnv = {}; for (const [k, v] of Object.entries(base)) { if (v === undefined) continue; - if (SECRETISH.test(k)) continue; + if (SECRETISH.test(k) && !keep.includes(k)) continue; if (/^(MAAT|MOLT)_/.test(k) || /^GIT_CONFIG_/.test(k)) continue; if (k === "ELECTRON_RUN_AS_NODE" || k === "TMP" || k === "TEMP" || k === "XDG_STATE_HOME" || k === "XDG_CONFIG_HOME" || k === "XDG_DATA_HOME" || k === "XDG_CACHE_HOME" || k === "XDG_RUNTIME_DIR") continue; if (k === "SUDO_USER" || k === "SUDO_UID" || k === "SUDO_GID" || k === "SUDO_COMMAND" || k === "MAIL") continue; @@ -377,9 +468,14 @@ export class PrivSep { return this.execSpec(shell === true ? "/bin/sh" : shell, ["-c", command], cwd, env); } - /** A program, run as the worker (in its PID namespace when there is one). */ - execSpec(file: string, args: readonly string[], cwd: string, env?: NodeJS.ProcessEnv): SpawnSpec { - const wenv = this.workerEnv(env ?? process.env); + /** + * A program, run as the worker (in its PID namespace when there is one). + * `keep` names credential variables that pass the scrub: only the worker + * agent's own login (an ACP spec's `workerCredentialEnv`), never anything + * the worker's shell commands get. + */ + execSpec(file: string, args: readonly string[], cwd: string, env?: NodeJS.ProcessEnv, keep: readonly string[] = []): SpawnSpec { + const wenv = this.workerEnv(env ?? process.env, keep); const { uid, gid, name } = this.worker; if (this.mode === "sudo") { const pairs = Object.entries(wenv).map(([k, v]) => `${k}=${v}`); @@ -435,88 +531,150 @@ export class PrivSep { * After Maat ran something in the project as itself (a project check in * place, a git commit), hand what it created back to the worker, so the * worker can go on editing its own tree. Only entries Maat's user owns. + * + * The tree is the worker's and the worker may be changing it while this + * runs, so it is never walked by path. Each folder is opened relative to + * the one before (`/proc/self/fd//`, the openat(2) of Linux) with + * O_NOFOLLOW|O_DIRECTORY, files with O_NOFOLLOW, and ownership is changed + * on the open descriptor (fchown). A folder swapped for a symlink fails to + * open and is skipped; one moved out of the project is noticed (its path + * no longer starts with the project's) and skipped. The project itself + * must be the folder, by device and inode, that the job started in. Files + * with more than one link are left alone (a hard link to a file outside), + * as are symlinks and anything on another file system. Linux only: elsewhere + * nothing is handed back. */ handBack(dir: string = this.project, limit = 100_000): void { - if (this.mode !== "root") return; + if (this.mode !== "root" || process.platform !== "linux" || !existsSync("/proc/self/fd")) return; const r = relative(this.project, resolve(dir)); - if (r.startsWith("..") || resolve(dir) !== join(this.project, r)) return; - let seen = 0; + if (r.startsWith("..") || isAbsolute(r)) return; const { uid, gid } = this.worker; - const walk = (d: string): void => { + const DIR = constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW; + const FILE = constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK | constants.O_NOCTTY; + const at = (fd: number, name: string) => `/proc/self/fd/${fd}/${name}`; + const inside = (fd: number): boolean => { + try { + const p = readlinkSync(`/proc/self/fd/${fd}`); + return p === this.projectReal || p.startsWith(this.projectReal + sep); + } catch { + return false; + } + }; + let fd: number; + try { + fd = openSync(this.projectReal, DIR); + } catch { + return; + } + try { + const st = fstatSync(fd); + if (st.dev !== this.projectId.dev || st.ino !== this.projectId.ino || !inside(fd)) return; + for (const part of r ? r.split(sep) : []) { + const next = openSync(at(fd, part), DIR); + closeSync(fd); + fd = next; + } + } catch { + closeSync(fd); + return; + } + let seen = 0; + const walk = (dfd: number, depth: number): void => { + if (!inside(dfd)) return; + const st = fstatSync(dfd); + if (st.dev !== this.projectId.dev) return; + if (st.uid === this.maatUid) fchownSync(dfd, uid, gid); + if (depth > 256) return; let names: string[]; try { - names = readdirSync(d); + names = readdirSync(`/proc/self/fd/${dfd}`); } catch { return; } for (const n of names) { if (++seen > limit) return; - const p = join(d, n); - let st; + let lst; try { - st = lstatSync(p); + lst = lstatSync(at(dfd, n)); } catch { continue; } - if (st.uid === this.maatUid) { + if (lst.isDirectory()) { + let sub: number; + try { + sub = openSync(at(dfd, n), DIR); + } catch { + continue; + } try { - lchownSync(p, uid, gid); + walk(sub, depth + 1); + } catch { + /* best effort */ + } finally { + closeSync(sub); + } + } else if (lst.isFile() && lst.uid === this.maatUid) { + let f: number; + try { + f = openSync(at(dfd, n), FILE); + } catch { + continue; + } + try { + const fst = fstatSync(f); + if (fst.isFile() && fst.uid === this.maatUid && fst.nlink === 1 && fst.dev === this.projectId.dev) fchownSync(f, uid, gid); } catch { /* not ours to change */ + } finally { + closeSync(f); } } - if (st.isDirectory() && !st.isSymbolicLink()) walk(p); + // Symlinks, FIFOs, sockets and devices are left as they are. } }; try { - if (lstatSync(dir).uid === this.maatUid) lchownSync(dir, uid, gid); - } catch { - return; - } - walk(dir); - } - - /** Make a path Maat created for the worker (a background log) the worker's. */ - giveToWorker(path: string): void { - if (this.mode !== "root") return; - try { - lchownSync(path, this.worker.uid, this.worker.gid); + walk(fd, 0); } catch { /* best effort */ + } finally { + closeSync(fd); } } /** * The job is over: copy Maat's records into the project's `.maat/`, where - * people have always found them, owned by whoever owns the project. The - * temp dir is not copied. Returns the folder they went to. + * people have always found them. The temp dir is not copied. Returns the + * folder they went to. + * + * Maat reads its own state dir (regular files and folders only) and the + * file helper, running as the worker, writes them into the project, so the + * copies are the worker's and Maat writes nothing in the worker's tree: a + * symlink planted at `.maat/receipts` sends the worker's write where the + * worker could write anyway. */ publish(): string { const dest = join(this.project, stateDirName(this.project)); - mkdirSync(dest, { recursive: true }); - for (const name of readdirSync(this.stateRoot)) { - if (name === "tmp") continue; - cpSync(join(this.stateRoot, name), join(dest, name), { recursive: true, force: true }); - } - if (this.mode === "root") { - let owner = { uid: this.worker.uid, gid: this.worker.gid }; - try { - const st = statSync(this.project); - owner = { uid: st.uid, gid: st.gid }; - } catch { - /* the worker's, then */ - } - const chown = (p: string): void => { - try { - lchownSync(p, owner.uid, owner.gid); - if (lstatSync(p).isDirectory()) for (const n of readdirSync(p)) chown(join(p, n)); - } catch { - /* best effort */ + const entries: PackEntry[] = []; + const walk = (rel: string): void => { + for (const name of readdirSync(rel ? join(this.stateRoot, rel) : this.stateRoot)) { + if (!rel && name === "tmp") continue; + const r = rel ? `${rel}/${name}` : name; + const p = join(this.stateRoot, r); + const st = lstatSync(p); + if (st.isDirectory()) { + entries.push({ rel: r, kind: "dir", mode: 0o755 }); + walk(r); + } else if (st.isFile()) { + entries.push({ rel: r, kind: "file", mode: 0o644, data: readFileSync(p).toString("base64") }); } - }; - chown(dest); - this.handBack(this.project); + } + }; + walk(""); + const out = this.runHelper<{ written: number; failed: string[] }>("unpack", { dest, entries }); + if (out.failed.length) { + throw new PrivSepError(`${out.failed.length} record(s) could not be written into ${dest} (${out.failed.slice(0, 3).join(", ")}); they are still in ${this.stateRoot}`); } + this.handBack(this.project); return dest; } diff --git a/test/privsep.test.ts b/test/privsep.test.ts index da02fa4..d61659e 100644 --- a/test/privsep.test.ts +++ b/test/privsep.test.ts @@ -11,8 +11,8 @@ * skip elsewhere. The default-unchanged tests run everywhere. */ import assert from "node:assert/strict"; -import { execFileSync, spawnSync } from "node:child_process"; -import { chmodSync, chownSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { execFileSync, spawn, spawnSync } from "node:child_process"; +import { chmodSync, chownSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { homedir, tmpdir } from "node:os"; import { join } from "node:path"; import { after, before, describe, it } from "node:test"; @@ -22,7 +22,9 @@ import { Receipts } from "../src/receipts.js"; import { Integrity } from "../src/integrity.js"; import { runCommand } from "../src/run.js"; import { stateDir } from "../src/statedir.js"; -import { disablePrivSep, enablePrivSep, privSep, type PrivSep } from "../src/privsep.js"; +import { disablePrivSep, enablePrivSep, privSep, safeRel, type PrivSep } from "../src/privsep.js"; +import { listBackground, resetBackgroundRegistry, startBackground, type BackgroundProcess } from "../src/background.js"; +import { ACP_AGENTS } from "../src/acp.js"; import type { Check, EngineEvent } from "../src/types.js"; import { fileURLToPath } from "node:url"; import { allowAll, scriptedProvider, workspace } from "./helpers.js"; @@ -57,6 +59,46 @@ function project(uid: number): string { return dir; } +/** A root-owned folder of root-owned files, outside every project: what an attack aims at. */ +function victim(files = 20): string { + mkdirSync("/var/tmp", { recursive: true }); + const v = mkdtempSync("/var/tmp/maat-victim-"); + chmodSync(v, 0o755); + for (let i = 0; i < files; i++) writeFileSync(join(v, `f${i}`), "precious\n", { mode: 0o644 }); + return v; +} + +/** Every entry under `dir` (and `dir` itself) still root's, with the original content. */ +function assertUntouched(v: string): void { + assert.equal(statSync(v).uid, 0, `${v} was chowned`); + for (const n of readdirSync(v)) { + const st = lstatSync(join(v, n)); + assert.equal(st.uid, 0, `${join(v, n)} was chowned to ${st.uid}`); + if (st.isFile()) assert.equal(readFileSync(join(v, n), "utf8"), "precious\n", `${join(v, n)} was written`); + } +} + +/** Every path under `root` that is a symlink. */ +function symlinksUnder(root: string): string[] { + const out: string[] = []; + const walk = (d: string): void => { + for (const n of readdirSync(d)) { + const p = join(d, n); + const st = lstatSync(p); + if (st.isSymbolicLink()) out.push(p); + else if (st.isDirectory()) walk(p); + } + }; + walk(root); + return out; +} + +async function waitExit(p: BackgroundProcess, ms = 10_000): Promise { + const t0 = Date.now(); + while (!p.exit && Date.now() - t0 < ms) await new Promise((r) => setTimeout(r, 50)); + assert.ok(p.exit, `job ${p.id} did not exit`); +} + function toolResults(provider: ReturnType): string[] { const last = provider.requests().at(-1) as { messages: { role: string; content: string | null }[] } | undefined; return (last?.messages ?? []).filter((m) => m.role === "tool").map((m) => String(m.content ?? "")); @@ -67,6 +109,15 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need let dir = ""; let ps: PrivSep; const savedState = process.env.MAAT_STATE_DIR; + const roots: string[] = []; + /** Privilege separation on for the main project (a test before may have turned it off). */ + const active = (): PrivSep => { + if (privSep() !== ps || !privSep()) { + ps = enablePrivSep({ user: WORKER, project: dir, helper: HELPER }); + roots.push(ps.stateRoot); + } + return ps; + }; before(() => { uid = ensureWorker(); @@ -82,7 +133,7 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need after(() => { disablePrivSep(); - rmSync(ps.stateRoot, { recursive: true, force: true }); + for (const r of [ps.stateRoot, ...roots]) rmSync(r, { recursive: true, force: true }); rmSync(join(homedir(), ".grok", "judge-session.json"), { force: true }); if (savedState !== undefined) process.env.MAAT_STATE_DIR = savedState; rmSync(dir, { recursive: true, force: true }); @@ -193,6 +244,182 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need rmSync(ps2.stateRoot, { recursive: true, force: true }); } }); + + it("a symlinked .maat/bg/.log does not make root write the target", async () => { + const v = victim(1); + const target = join(v, "f0"); + const v2 = victim(0); + active(); + resetBackgroundRegistry(); + try { + // The worker plants the next job's log as a link to a root-owned file. + const bg = join(dir, ".maat", "bg"); + const r = asWorker(`mkdir -p '${bg}' && ln -sf '${target}' '${bg}/1.log' && echo ok`); + assert.equal(r.status, 0, r.out); + const j1 = startBackground("echo pwned", { cwd: dir, asWorker: true }); + await waitExit(j1); + assert.equal(readFileSync(target, "utf8"), "precious\n", "root truncated or wrote the link's target"); + assert.equal(statSync(target).uid, 0); + assert.notEqual(j1.exit?.code, 0, "the worker's own shell should have been refused the root-owned target"); + + // A normal job: its log is made by the worker and holds the output. + const j2 = startBackground("echo hello-bg", { cwd: dir, asWorker: true }); + await waitExit(j2); + assert.equal(readFileSync(join(dir, j2.log), "utf8"), "hello-bg\n"); + assert.equal(statSync(join(dir, j2.log)).uid, uid, "the log is not the worker's"); + + // The whole bg folder swapped for a link into a root-owned folder. + rmSync(bg, { recursive: true, force: true }); + assert.equal(asWorker(`ln -s '${v2}' '${bg}' && echo ok`).status, 0); + const j3 = startBackground("echo pwned", { cwd: dir, asWorker: true }); + await waitExit(j3); + assert.deepEqual(readdirSync(v2), [], "a log was created inside the root-owned folder"); + assert.ok(listBackground().length >= 3); + } finally { + resetBackgroundRegistry(); + rmSync(join(dir, ".maat", "bg"), { recursive: true, force: true }); + rmSync(v, { recursive: true, force: true }); + rmSync(v2, { recursive: true, force: true }); + } + }); + + it("handBack: a folder swapped for a symlink mid-walk never gets root to chown outside", async () => { + const v = victim(40); + active(); + const swap = join(dir, "swap"); + mkdirSync(swap); + for (let i = 0; i < 40; i++) writeFileSync(join(swap, `g${i}`), "x\n"); + chownSync(swap, uid, uid); + // Root-made files inside, so the walk has something to hand back each time. + const refill = () => { + for (let i = 0; i < 40; i++) { + try { + chownSync(join(swap, `g${i}`), 0, 0); + } catch { + /* swapped out right now */ + } + } + }; + // The worker flips `swap` between its real folder and a link to the victim, as fast as it can. + assert.equal(asWorker(`ln -s '${v}' '${join(dir, "lnk")}' && echo ok`).status, 0); + const stop = join("/tmp", `maat-flip-stop-${process.pid}`); + rmSync(stop, { force: true }); + const flipper = spawn( + "runuser", + [ + "-u", + WORKER, + "--", + process.execPath, + "-e", + `const fs=require("fs");process.chdir(${JSON.stringify(dir)});const end=Date.now()+15000;let i=0;` + + `while(Date.now() flipper.once("exit", r)); + const stopFlipper = async () => { + writeFileSync(stop, ""); + await flipped; + }; + try { + // A static link as well, the case without any race. + assert.equal(asWorker(`ln -sfn '${v}' '${join(dir, "static-link")}' && echo ok`).status, 0); + const t0 = Date.now(); + let runs = 0; + while (Date.now() - t0 < 4_000) { + refill(); + ps.handBack(dir); + runs += 1; + await new Promise((r) => setImmediate(r)); + } + assert.ok(runs > 10, `only ${runs} hand-backs ran`); + assertUntouched(v); + // The state dir is Maat's and stays so. + assert.equal(statSync(ps.stateRoot).uid, 0); + // And hand-back still does its job on the real tree. + await stopFlipper(); + const real = existsSync(join(dir, "real")) ? join(dir, "real") : swap; + writeFileSync(join(real, "made-by-root.txt"), "x\n"); + ps.handBack(dir); + assert.equal(statSync(join(real, "made-by-root.txt")).uid, uid, "hand-back did not give back a root-made file"); + } finally { + await stopFlipper(); + rmSync(stop, { force: true }); + for (const n of ["swap", "real", "lnk", "static-link"]) rmSync(join(dir, n), { recursive: true, force: true }); + rmSync(v, { recursive: true, force: true }); + } + }); + + it("handBack leaves a hard link to a root-owned file alone", () => { + // Root-made, with a second link elsewhere in the tree: nlink 2 is refused. + active(); + const a = join(dir, "hl-a.txt"); + writeFileSync(a, "x\n"); + execFileSync("ln", [a, join(dir, "hl-b.txt")]); + try { + ps.handBack(dir); + assert.equal(statSync(a).uid, 0, "a multiply-linked root file was chowned"); + } finally { + rmSync(a, { force: true }); + rmSync(join(dir, "hl-b.txt"), { force: true }); + } + }); + + it("seed refuses symlinks: nothing from the project becomes a link in the state dir", () => { + const ws = project(uid); + const v = victim(1); + const secret = "/root/maat-seed-secret.txt"; + writeFileSync(secret, "root-only-secret-81\n", { mode: 0o600 }); + // The worker leaves links where Maat's records go, and one real record. + const r = asWorker( + `mkdir -p '${ws}/.maat/integrity' '${ws}/.maat/mission' && echo kept > '${ws}/.maat/integrity/chain' && ` + + `ln -s '${v}' '${ws}/.maat/receipts' && ln -s '${secret}' '${ws}/.maat/secret.txt' && ` + + `ln -s '${v}' '${ws}/.maat/integrity/sub' && ln -s '${secret}' '${ws}/.maat/mission/contract.yml' && echo ok`, + ); + assert.equal(r.status, 0, r.out); + disablePrivSep(); + const ps2 = enablePrivSep({ user: WORKER, project: ws, helper: HELPER }); + try { + assert.deepEqual(symlinksUnder(ps2.stateRoot), [], "a symlink was carried into the state dir"); + assert.equal(readFileSync(join(ps2.stateRoot, "integrity", "chain"), "utf8"), "kept\n", "a real record was not seeded"); + assert.ok(!existsSync(join(ps2.stateRoot, "secret.txt")), "a link to a root file was read into the state dir"); + for (const s of ["receipts", "secret.txt", "integrity/sub", "mission/contract.yml"]) { + assert.ok(ps2.skippedOnSeed.includes(s), `${s} was not reported as skipped: ${ps2.skippedOnSeed.join(", ")}`); + } + // Maat now writes its receipts into its own folder, not through the link. + mkdirSync(stateDir(ws, "receipts"), { recursive: true }); + writeFileSync(stateDir(ws, "receipts", "0001.md"), "receipt\n"); + assert.ok(stateDir(ws, "receipts").startsWith(ps2.stateRoot)); + assertUntouched(v); + + // Publishing goes back as the worker: the link at .maat/receipts gets the + // worker's write, which the root-owned target refuses. Nothing of root's is + // written, and the secret never reaches the project. + assert.throws(() => ps2.publish(), /could not be written/); + assertUntouched(v); + assert.equal(readFileSync(secret, "utf8"), "root-only-secret-81\n"); + assert.notEqual(asWorker(`grep -r root-only-secret-81 '${ws}'`).status, 0, "the root-only secret reached the project"); + } finally { + disablePrivSep(); + rmSync(ws, { recursive: true, force: true }); + rmSync(ps2.stateRoot, { recursive: true, force: true }); + rmSync(v, { recursive: true, force: true }); + rmSync(secret, { force: true }); + } + }); + + it("passes the worker agent's own credential through, and only that", () => { + const base = { OPENCODE_API_KEY: "zen-key", OPENAI_API_KEY: "other", GITHUB_TOKEN: "gh", PATH: "/usr/bin" }; + active(); + const oc = ACP_AGENTS.find((a) => a.name === "opencode")!; + const spec = ps.execSpec("opencode", ["acp"], dir, base, oc.workerCredentialEnv); + assert.equal(spec.env.OPENCODE_API_KEY, "zen-key"); + assert.equal(spec.env.OPENAI_API_KEY, undefined); + assert.equal(spec.env.GITHUB_TOKEN, undefined); + // A plain worker command never gets it. + assert.equal(ps.commandSpec("env", true, dir, base).env.OPENCODE_API_KEY, undefined); + }); }); describe("privilege separation off (the default)", () => { @@ -223,6 +450,17 @@ describe("privilege separation off (the default)", () => { } }); + it("accepts only plain relative paths back from the file helper", () => { + assert.equal(safeRel("receipts/0001.md"), join("receipts", "0001.md")); + for (const bad of ["", "/etc/passwd", "../x", "a/../../x", "a//b", "./a", "a/\0b"]) assert.equal(safeRel(bad), null, bad); + }); + + it("names the OpenCode worker's own credential, and no agent names another's", () => { + const oc = ACP_AGENTS.find((a) => a.name === "opencode"); + assert.deepEqual(oc?.workerCredentialEnv, ["OPENCODE_API_KEY"]); + for (const a of ACP_AGENTS) for (const k of a.workerCredentialEnv ?? []) assert.match(k, new RegExp(`^${a.name}_`, "i")); + }); + it("refuses a worker user that cannot be switched to", () => { if (process.platform === "win32") return; assert.throws(() => enablePrivSep({ user: "maat-no-such-user-xyz", project: tmpdir(), helper: HELPER }), /no such user/); From 202d6dbcae6b7f5a53bc9c5bb8e07a61e5377362 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 14:31:56 -0700 Subject: [PATCH 5/9] Maat's git runs as the worker; graders grade a sanitized copy - Maat no longer sets safe.directory. Its own git calls in the project (snapshots, commits, ls-files, the local exclude file, brief probes) run as the worker under --worker-user, so the repository's config is only ever read with the worker's permissions. Maat's own git keeps refusing a repository another user owns. - bench run.py: graders run on a root-owned copy of the task folder with the repository config reduced to its format lines, hooks and attributes removed, special files skipped, and git started without system or global config and with hooks off. The agent user's processes are stopped first. - Tests: root container test that the worker's repository config runs, if at all, as the worker and that root git refuses the repository; bench tests that marker-writing config and hooks leave no marker on the copy. --- bench/local/run.py | 106 ++++++++++++++++++++++--- bench/local/tests/test_grading_copy.py | 100 +++++++++++++++++++++++ docs/privilege-separation.md | 17 ++-- src/background.ts | 7 +- src/bar.ts | 10 +-- src/brief.ts | 4 +- src/engine.ts | 5 +- src/git.ts | 5 +- src/leftovers.ts | 29 ++++--- src/privsep.ts | 65 ++++++++++++--- test/privsep.test.ts | 24 +++++- 11 files changed, 314 insertions(+), 58 deletions(-) create mode 100644 bench/local/tests/test_grading_copy.py diff --git a/bench/local/run.py b/bench/local/run.py index 3c46b00..f2e6c27 100644 --- a/bench/local/run.py +++ b/bench/local/run.py @@ -17,6 +17,7 @@ import json import os import shutil +import stat import subprocess import sys import time @@ -65,16 +66,99 @@ def openrouter_key() -> str: # whole of Maat as the agent user, as before 2026-10-07. PRIVSEP = bool(AGENT_USER) and os.environ.get("BENCH_PRIVSEP", "1") != "0" and os.geteuid() == 0 -if AGENT_USER and os.geteuid() == 0: - # The graders (root) run git in task repositories the agent user owns. On the container's - # own filesystem ownership is real, and git refuses a repository owned by someone else - # ("dubious ownership"): fix-git graded every correct merge as a failure. On the old /work - # bind mount chown was ignored, so this never showed. Maat passes the same setting to its - # own checks and strips it from the worker's environment. - _n = int(os.environ.get("GIT_CONFIG_COUNT") or 0) - os.environ[f"GIT_CONFIG_KEY_{_n}"] = "safe.directory" - os.environ[f"GIT_CONFIG_VALUE_{_n}"] = "*" - os.environ["GIT_CONFIG_COUNT"] = str(_n + 1) +# The graders run as root. They never run git in the agent's own repository: the agent owns its +# .git/config, and core.fsmonitor, hooks, filters and diff drivers there would run as root. Until +# 2026-10-07 run.py set safe.directory=* so git would grade an agent-owned repository at all; that +# setting is gone. The graders get a root-owned copy of the task folder instead, with the git +# config cut down to the repository format and the hooks removed (grading_copy), and git runs +# with no system or global config and hooks pointed at /dev/null (GRADER_GIT_ENV). +GRADER_GIT_ENV = { + "GIT_CONFIG_NOSYSTEM": "1", + "GIT_CONFIG_GLOBAL": "/dev/null", + "GIT_CONFIG_COUNT": "3", + "GIT_CONFIG_KEY_0": "core.hooksPath", "GIT_CONFIG_VALUE_0": "/dev/null", + "GIT_CONFIG_KEY_1": "core.fsmonitor", "GIT_CONFIG_VALUE_1": "false", + "GIT_CONFIG_KEY_2": "core.pager", "GIT_CONFIG_VALUE_2": "cat", + "GIT_TERMINAL_PROMPT": "0", +} + + +def sanitize_git(git_dir: Path) -> None: + """Replace a copied repository's config with the format lines alone, and drop its hooks + and attributes: nothing in it can name a program for git to run.""" + cfg = git_dir / "config" + version, objfmt = "0", None + try: + section = "" + for line in cfg.read_text(errors="replace").splitlines(): + t = line.strip() + if t.startswith("["): + section = t.strip("[]").strip().lower() + continue + k, _, v = t.partition("=") + k, v = k.strip().lower(), v.strip() + if section == "core" and k == "repositoryformatversion" and v.isdigit(): + version = v + elif section == "extensions" and k == "objectformat" and v in ("sha1", "sha256"): + objfmt = v + except OSError: + pass + text = f"[core]\n\trepositoryformatversion = {version}\n\tfilemode = true\n\tbare = false\n" + if objfmt: + text += f"[extensions]\n\tobjectformat = {objfmt}\n" + if cfg.is_symlink() or cfg.exists(): + cfg.unlink() + cfg.write_text(text) + for p in (git_dir / "hooks", git_dir / "info" / "attributes", git_dir / "config.worktree"): + if p.is_symlink() or p.is_file(): + p.unlink() + elif p.is_dir(): + shutil.rmtree(p) + + +def grading_copy(d: Path) -> Path: + """A root-owned copy of a finished task folder, for the graders. Regular files, folders and + symlinks (as links) only: a FIFO or device the agent left is skipped, not opened. Every + repository in it (.git folders) is sanitized. Call with the agent's processes stopped.""" + import tempfile + + def copy_regular(src, dst, *, follow_symlinks=True): + st = os.lstat(src) + if not stat.S_ISREG(st.st_mode): + return dst + fd = os.open(src, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)) + with os.fdopen(fd, "rb") as fsrc: + if not stat.S_ISREG(os.fstat(fsrc.fileno()).st_mode): + return dst + with open(dst, "wb") as fdst: + shutil.copyfileobj(fsrc, fdst) + shutil.copystat(src, dst, follow_symlinks=False) + return dst + + dst = Path(tempfile.mkdtemp(prefix="maat-grade-")) / d.name + shutil.copytree(d, dst, symlinks=True, copy_function=copy_regular) + for root, dirs, _files in os.walk(dst): + if ".git" in dirs and not os.path.islink(os.path.join(root, ".git")): + sanitize_git(Path(root) / ".git") + return dst + + +def grade_safely(T, d: Path): + """T.grade on a sanitized root-owned copy (grading_copy), with GRADER_GIT_ENV set.""" + if AGENT_USER and shutil.which("pkill"): + subprocess.run(["pkill", "-KILL", "-u", AGENT_USER], check=False) + copy = grading_copy(d) + saved = {k: os.environ.get(k) for k in GRADER_GIT_ENV} + os.environ.update(GRADER_GIT_ENV) + try: + return T.grade(copy) + finally: + for k, v in saved.items(): + if v is None: + os.environ.pop(k, None) + else: + os.environ[k] = v + shutil.rmtree(copy.parent, ignore_errors=True) def as_agent(cmd: list, env: dict | None = None) -> tuple[list, dict | None]: @@ -297,7 +381,7 @@ def run_all(which: str, repeats: int, task_filter: str | None) -> None: # Not recorded, so a resume runs this task again. print(f"STOPPED: the provider's daily limit / quota is reached ({tag} not recorded)", flush=True) return - ok, why = T.grade(d) + ok, why = grade_safely(T, d) lock(d) final = "" r.update(task=T.name, agent=a, rep=rep, passed=ok, why=why, final=final[:300]) diff --git a/bench/local/tests/test_grading_copy.py b/bench/local/tests/test_grading_copy.py new file mode 100644 index 0000000..2193666 --- /dev/null +++ b/bench/local/tests/test_grading_copy.py @@ -0,0 +1,100 @@ +"""The graders never run git on the agent's own repository config (2026-10-07 review). + +An agent that owns the task folder owns its .git/config, and core.fsmonitor, +hooks and filters there name programs git runs. Graders run as root, so they +grade a sanitized root-owned copy instead. +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +import run # noqa: E402 + +pytestmark = pytest.mark.skipif(not shutil.which("git"), reason="needs git") + + +def sh(cmd, cwd, env=None): + return subprocess.run(cmd, cwd=cwd, shell=True, capture_output=True, text=True, env=env) + + +def hostile_repo(tmp_path: Path) -> tuple[Path, Path]: + d = tmp_path / "task" + d.mkdir() + marker = tmp_path / "pwned" + sh("git init -q -b master && git config user.email t@t && git config user.name t", d) + (d / "a.txt").write_text("one\n") + (d / ".gitattributes").write_text("*.txt filter=evil diff=evil\n") + sh("git add -A && git commit -qm init", d) + (d / "a.txt").write_text("two\n") + # Everything an agent could plant for git to run. + for k, v in [ + ("core.fsmonitor", f"touch {marker}.fsmonitor; false"), + ("filter.evil.clean", f"sh -c 'touch {marker}.filter; cat'"), + ("diff.evil.textconv", f"sh -c 'touch {marker}.textconv; cat \"$1\"' -"), + ("core.pager", f"touch {marker}.pager; cat"), + ("alias.status", f"!touch {marker}.alias"), + ("include.path", str(tmp_path / "more.cfg")), + ]: + subprocess.run(["git", "config", k, v], cwd=d, check=True) + (tmp_path / "more.cfg").write_text(f"[core]\n\thooksPath = {d}/.git/myhooks\n") + hooks = d / ".git" / "hooks" + hooks.mkdir(exist_ok=True) + for h in ("post-checkout", "pre-commit", "post-index-change"): + p = hooks / h + p.write_text(f"#!/bin/sh\ntouch {marker}.{h}\n") + p.chmod(0o755) + return d, marker + + +def planted(tmp_path: Path) -> list[str]: + return sorted(p.name for p in tmp_path.glob("pwned*")) + + +def grader_git(copy: Path, env=None) -> None: + env = env or (os.environ | run.GRADER_GIT_ENV) + for cmd in ("git status --porcelain", "git diff", "git show master:a.txt", "git log -1", + "git rev-parse --abbrev-ref HEAD", "git checkout -q master", "git add -A"): + sh(cmd, copy, env) + + +def test_the_planted_config_does_run_on_the_original(tmp_path): + # The control: the same grader commands on the agent's own repository fire the traps, + # so the test below would see them if the copy kept any. + d, _ = hostile_repo(tmp_path) + grader_git(d, env=dict(os.environ)) + assert planted(tmp_path), "the control should have fired at least one planted command" + + +def test_graders_git_on_the_copy_runs_nothing_the_agent_planted(tmp_path): + d, _ = hostile_repo(tmp_path) + copy = run.grading_copy(d) + try: + grader_git(copy) + assert planted(tmp_path) == [] + # And the copy still grades: history and the working tree are intact. + env = os.environ | run.GRADER_GIT_ENV + assert sh("git show HEAD:a.txt", copy, env).stdout == "one\n" + assert (copy / "a.txt").read_text() == "two\n" + assert "filter" not in (copy / ".git" / "config").read_text() + finally: + shutil.rmtree(copy.parent, ignore_errors=True) + + +def test_copy_skips_fifos_and_keeps_links_as_links(tmp_path): + d = tmp_path / "task" + d.mkdir() + (d / "f.txt").write_text("x") + os.mkfifo(d / "pipe") + (d / "link").symlink_to("/etc/hostname") + copy = run.grading_copy(d) + try: + assert (copy / "f.txt").read_text() == "x" + assert not (copy / "pipe").exists() + assert (copy / "link").is_symlink() + finally: + shutil.rmtree(copy.parent, ignore_errors=True) diff --git a/docs/privilege-separation.md b/docs/privilege-separation.md index 3383acb..1f5a184 100644 --- a/docs/privilege-separation.md +++ b/docs/privilege-separation.md @@ -80,9 +80,8 @@ dir can point back into the worker's reach or out to a file of root's. These stay in the project: `.maat/done.yml`, because a person wrote it, and `.maat/bg/`, which holds the worker's own background-job logs. The worker makes those itself: a background job starts as the worker with a shell that -creates the folder and opens its log, so Maat never opens a path in the -worker's tree (a planted `.maat/bg/.log` symlink only redirects the -worker's own write). +creates the folder and opens its log, so the log is opened with the +worker's permissions and Maat never opens a path in the worker's tree. `.maat/mission/library/` also stays, because the worker writes it. A spilled output (`.maat/out/.txt`) is still readable through `read_file`: Maat serves its own masked copy of the worker's output. The worker cannot `cat` @@ -94,8 +93,8 @@ it from bash. checks them there. Maat reads its own state dir and the file helper, as the worker, writes the copies (`fs-helper.js unpack`), so they are owned by the worker and Maat writes nothing into the worker's tree. If a copy cannot be -written (the worker planted a link to somewhere it cannot write), Maat says -so and the records stay in the state dir, which is always kept. +written with the worker's permissions, Maat says so and the records stay in +the state dir, which is always kept. **The judge and the checks stay Maat's.** Judge and ask subprocesses (OpenCode, a Grok judge, an HTTP judge) run as Maat with Maat's HOME. If that @@ -111,9 +110,11 @@ changed with `fchown` on the open descriptor, and the project itself must be the folder (device and inode) the job started in. A folder swapped for a symlink mid-walk fails to open and is skipped; files with more than one hard link, symlinks, special files and other file systems are left alone. Hand-back -is Linux-only; elsewhere root-made files stay Maat's. Git, running as Maat on a repository the worker owns, -is told `safe.directory=*` through `GIT_CONFIG_*`. The worker's environment -does not get that setting. +is Linux-only; elsewhere root-made files stay Maat's. Maat's own git calls in the project (snapshots, commits, +`ls-files`, the local exclude file, the brief's probes) run as the worker, +because the repository and its config belong to the worker. Maat does not set +`safe.directory`, so its own git keeps refusing a repository another user +owns. ## The process list diff --git a/src/background.ts b/src/background.ts index 36ff280..9b0b446 100644 --- a/src/background.ts +++ b/src/background.ts @@ -103,10 +103,9 @@ export function startBackground( let child; if (ps) { // Under privilege separation the job is the worker's, and so is its log: - // the worker's own shell makes the folder and opens the file. Maat (root) - // never creates or opens a path in the worker's tree, where a planted - // `.maat/bg/.log -> /etc/...` would have made root truncate the - // target and the worker's command write into it. + // the worker's own shell makes the folder and opens the file, so the + // log is created with the worker's permissions. Maat (root) never creates + // or opens a path in the worker's tree. const spec = ps.execSpec( "/bin/sh", ["-c", 'mkdir -p -- "$1" && exec >"$2" 2>&1 ; try { - const out = execFileSync("git", ["ls-files", "-z"], { - cwd: ctx.cwd, - encoding: "utf8", - maxBuffer: 8 * 1024 * 1024, - }); + // As the worker under privilege separation: never Maat's git on the worker's repository. + const out = gitSync(["ls-files", "-z"], ctx.cwd); tracked = new Set(out.split("\0").filter(Boolean)); } catch { return { diff --git a/src/brief.ts b/src/brief.ts index 6a49d32..206a5c9 100644 --- a/src/brief.ts +++ b/src/brief.ts @@ -103,7 +103,9 @@ export type Probe = (command: string, timeoutMs: number) => Promise { try { - const r = await runCommand(command, { cwd: cwd ?? process.cwd(), timeoutMs, maxBuffer: 16 * 1024 }); + // The worker's environment, as the worker sees it (src/privsep.ts): and + // git here reads the worker's repository config, which must not run as Maat. + const r = await runCommand(command, { cwd: cwd ?? process.cwd(), timeoutMs, maxBuffer: 16 * 1024, asWorker: true }); if (r.timedOut) return null; if (r.code !== 0) return null; const line = `${r.stdout}\n${r.stderr}` diff --git a/src/engine.ts b/src/engine.ts index cd7add4..1d32b27 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -25,7 +25,6 @@ import { judgeEffort, judgeTarget } from "./judge.js"; import { tierOf } from "./tiers.js"; import { arbitrate, parseDisputes, type Ruling } from "./dispute.js"; import { createHash } from "node:crypto"; -import { execFileSync } from "node:child_process"; import { mkdirSync, readFileSync, writeFileSync, existsSync, statSync } from "node:fs"; import { excludeMoltFromGit, listProject, removeNew, unnamedNewFiles, type ProjectListing } from "./leftovers.js"; import { inspectDir, inspectFile, namedInputs, profileLine } from "./inspect.js"; @@ -132,7 +131,7 @@ import { type Spend, } from "./types.js"; import { stateDir, stateDirName, stateRedirect } from "./statedir.js"; -import { privSep, type WorkerFs } from "./privsep.js"; +import { gitSync, privSep, type WorkerFs } from "./privsep.js"; import { env } from "./env.js"; import { Judgments, caseReason, reasonText } from "./judgment.js"; @@ -4490,7 +4489,7 @@ export class Engine { let tracked: Set; try { tracked = new Set( - execFileSync("git", ["ls-files"], { cwd: this.cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }) + gitSync(["ls-files"], this.cwd) .split("\n") .filter(Boolean), ); diff --git a/src/git.ts b/src/git.ts index d6b7611..ce3fa48 100644 --- a/src/git.ts +++ b/src/git.ts @@ -56,6 +56,9 @@ async function git(cwd: string, args: string[]): Promise { const r = await runCommand(`git ${args.map(shellQuote).join(" ")}`, { cwd, timeoutMs: GIT_TIMEOUT_MS, + // Under privilege separation the repository is the worker's, and so is + // its config: git runs as the worker, never as Maat (src/privsep.ts). + asWorker: true, }); return { ok: r.code === 0, stdout: r.stdout.trim(), stderr: r.stderr.trim() }; } @@ -220,7 +223,7 @@ export async function restore(cwd: string, ref: string, plan: RevertPlan): Promi else out.failed.push({ path, reason: r.stderr || "checkout failed" }); } for (const path of plan.remove) { - const r = await runCommand(`rm -f -- ${shellQuote(path)}`, { cwd, timeoutMs: GIT_TIMEOUT_MS }); + const r = await runCommand(`rm -f -- ${shellQuote(path)}`, { cwd, timeoutMs: GIT_TIMEOUT_MS, asWorker: true }); if (r.code === 0) out.removed.push(path); else out.failed.push({ path, reason: (r.stderr || "").trim() || "could not remove" }); } diff --git a/src/leftovers.ts b/src/leftovers.ts index 30af8fd..8da9bab 100644 --- a/src/leftovers.ts +++ b/src/leftovers.ts @@ -8,11 +8,11 @@ * the checks ran go — never a file that existed before them — and nothing at * all when the project could not be listed in full either time. */ -import { execFileSync } from "node:child_process"; import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, rmdirSync } from "node:fs"; import { dirname, isAbsolute, join } from "node:path"; import { TREE_SKIP, walk } from "./files.js"; import { STATE_DIRS } from "./statedir.js"; +import { gitSync, privSep } from "./privsep.js"; export type ProjectListing = { files: Set; dirs: Set }; @@ -66,19 +66,30 @@ export function removeNew(root: string, before: ProjectListing | null): string[] */ export function excludeMoltFromGit(cwd: string): boolean { try { - const path = execFileSync("git", ["rev-parse", "--git-path", "info/exclude"], { - cwd, - encoding: "utf8", - stdio: ["ignore", "pipe", "ignore"], - timeout: 5_000, - }).trim(); + const path = gitSync(["rev-parse", "--git-path", "info/exclude"], cwd, { timeout: 5_000 }).trim(); if (!path) return false; const file = isAbsolute(path) ? path : join(cwd, path); - const text = existsSync(file) ? readFileSync(file, "utf8") : ""; + const ps = privSep(); + // Under privilege separation the repository is the worker's: the file is + // read and appended to as the worker, never by Maat through a path the + // worker controls. + const text = ps + ? (() => { + const r = ps.workerSync("/bin/sh", ["-c", 'cat -- "$1" 2>/dev/null || true', "sh", file], cwd); + return r.status === 0 ? r.stdout : ""; + })() + : existsSync(file) + ? readFileSync(file, "utf8") + : ""; const missing = STATE_DIRS.filter((d) => !new RegExp(`^\\/?\\${d}\\/?$`, "m").test(text)); if (!missing.length) return false; + const add = `${text && !text.endsWith("\n") ? "\n" : ""}# Maat Agent's own records\n${missing.map((d) => `${d}/`).join("\n")}\n`; + if (ps) { + const r = ps.workerSync("/bin/sh", ["-c", 'mkdir -p -- "$(dirname -- "$1")" && printf %s "$2" >> "$1"', "sh", file, add], cwd); + return r.status === 0; + } mkdirSync(dirname(file), { recursive: true }); - appendFileSync(file, `${text && !text.endsWith("\n") ? "\n" : ""}# Maat Agent's own records\n${missing.map((d) => `${d}/`).join("\n")}\n`); + appendFileSync(file, add); return true; } catch { return false; diff --git a/src/privsep.ts b/src/privsep.ts index 8b96a60..637697e 100644 --- a/src/privsep.ts +++ b/src/privsep.ts @@ -35,7 +35,7 @@ * * Off (the default) nothing here runs and nothing changes. */ -import { spawn, spawnSync, type ChildProcess } from "node:child_process"; +import { execFileSync, spawn, spawnSync, type ChildProcess, type SpawnSyncReturns } from "node:child_process"; import { randomBytes } from "node:crypto"; import { chmodSync, @@ -263,12 +263,10 @@ export class PrivSep { // Maat's own temp files (reference tries, check copies, drafts, judge // scratch) land in the state dir; the worker's get /tmp (workerEnv). this.setEnv("TMPDIR", join(this.stateRoot, "tmp")); - // Maat reads the worker's repository as another user; git refuses a - // repository owned by someone else unless told it is safe. - const n = Number(process.env.GIT_CONFIG_COUNT ?? 0) || 0; - this.setEnv(`GIT_CONFIG_KEY_${n}`, "safe.directory"); - this.setEnv(`GIT_CONFIG_VALUE_${n}`, "*"); - this.setEnv("GIT_CONFIG_COUNT", String(n + 1)); + // No `safe.directory=*`: git's refusal of a repository someone else owns + // is what stops a worker-written .git/config (core.fsmonitor, hooks, + // filters, diff drivers) from running as Maat. Maat's own git runs as + // the worker instead (`gitSync`, src/git.ts), so it never needs it. this.seed(); redirectState(this.project, this.stateRoot); @@ -309,9 +307,9 @@ export class PrivSep { * The project's `.maat/` is the worker's: it is read, and the contract * removed, by the file helper running as the worker (`fs-helper.js pack`), * never by Maat walking it. Only regular files and folders come back, by - * name, and Maat writes them into its own state dir: a symlink in the - * project (`.maat/receipts -> /root/...`) is skipped by the helper and could - * not be created here anyway, so Maat never writes through one later. + * name, and Maat writes them into its own state dir. Symlinks in the + * project's `.maat/` are skipped by the helper, so the state dir holds no + * links. */ private seed(): void { const src = join(this.project, stateDirName(this.project)); @@ -521,6 +519,24 @@ export class PrivSep { return this.nsInit !== undefined && existsSync(`/proc/${this.nsInit}`); } + /** + * Run a program as the worker and wait for it (in its PID namespace when + * there is one). For Maat's own short git calls and small writes in the + * worker's tree, which must not run as Maat. + */ + workerSync(file: string, args: readonly string[], cwd: string, opts: { timeout?: number; maxBuffer?: number; input?: string } = {}): SpawnSyncReturns { + const spec = this.execSpec(file, args, cwd); + return spawnSync(spec.file, spec.args, { + cwd, + env: spec.env, + encoding: "utf8", + timeout: opts.timeout ?? 30_000, + maxBuffer: opts.maxBuffer ?? 8 * 1024 * 1024, + ...(opts.input !== undefined ? { input: opts.input } : {}), + ...(spec.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), + }); + } + /** The file tools, performed as the worker. Started on first use. */ fs(): WorkerFs { if (!this.helper) this.helper = new FsHelper(this); @@ -648,9 +664,8 @@ export class PrivSep { * * Maat reads its own state dir (regular files and folders only) and the * file helper, running as the worker, writes them into the project, so the - * copies are the worker's and Maat writes nothing in the worker's tree: a - * symlink planted at `.maat/receipts` sends the worker's write where the - * worker could write anyway. + * copies are the worker's and Maat writes nothing in the worker's tree: + * every write there happens with the worker's own permissions. */ publish(): string { const dest = join(this.project, stateDirName(this.project)); @@ -863,6 +878,30 @@ export function disablePrivSep(): void { active = undefined; } +/** + * git, synchronously, in the project. Under privilege separation it runs as + * the worker: the repository and its config are the worker's, and git reads + * that config (core.fsmonitor, hooks, filters) and may run what it names. + * Otherwise as Maat, as before. Returns stdout; throws on a non-zero exit, + * like execFileSync. + */ +export function gitSync(args: readonly string[], cwd: string, opts: { timeout?: number; maxBuffer?: number } = {}): string { + const ps = active; + if (!ps) { + return execFileSync("git", [...args], { + cwd, + encoding: "utf8", + stdio: ["ignore", "pipe", "ignore"], + ...(opts.timeout ? { timeout: opts.timeout } : {}), + maxBuffer: opts.maxBuffer ?? 8 * 1024 * 1024, + }); + } + const r = ps.workerSync("git", args, cwd, opts); + if (r.error) throw r.error; + if (r.status !== 0) throw new Error(`git ${args[0] ?? ""} exited ${r.status}: ${(r.stderr ?? "").trim().slice(0, 300)}`); + return r.stdout; +} + /** The worker user asked for, from the flag or MAAT_WORKER_USER. */ export function workerUserFrom(flag?: string): string | undefined { const v = (flag ?? process.env.MAAT_WORKER_USER ?? "").trim(); diff --git a/test/privsep.test.ts b/test/privsep.test.ts index d61659e..3b6b997 100644 --- a/test/privsep.test.ts +++ b/test/privsep.test.ts @@ -22,7 +22,7 @@ import { Receipts } from "../src/receipts.js"; import { Integrity } from "../src/integrity.js"; import { runCommand } from "../src/run.js"; import { stateDir } from "../src/statedir.js"; -import { disablePrivSep, enablePrivSep, privSep, safeRel, type PrivSep } from "../src/privsep.js"; +import { disablePrivSep, enablePrivSep, gitSync, privSep, safeRel, type PrivSep } from "../src/privsep.js"; import { listBackground, resetBackgroundRegistry, startBackground, type BackgroundProcess } from "../src/background.js"; import { ACP_AGENTS } from "../src/acp.js"; import type { Check, EngineEvent } from "../src/types.js"; @@ -409,6 +409,28 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need } }); + it("runs Maat's git as the worker: repository config never runs as root", () => { + active(); + const repo = join(dir, "gitrepo"); + const mark = join(dir, "git-marker"); + const r = asWorker( + `mkdir -p '${repo}' && cd '${repo}' && git init -q && git config core.fsmonitor 'touch ${mark}; false' && echo ok`, + ); + assert.equal(r.status, 0, r.out); + try { + assert.ok(!Object.values(process.env).includes("safe.directory"), "safe.directory is still set for Maat"); + gitSync(["status", "--porcelain"], repo); + if (existsSync(mark)) assert.equal(statSync(mark).uid, uid, "the repository's configured command ran as root"); + // And Maat's own (root) git, without safe.directory, refuses the worker's repository. + const own = spawnSync("git", ["status"], { cwd: repo, encoding: "utf8" }); + assert.notEqual(own.status, 0, "root git accepted a repository the worker owns"); + assert.ok(!existsSync(mark) || statSync(mark).uid === uid); + } finally { + rmSync(repo, { recursive: true, force: true }); + rmSync(mark, { force: true }); + } + }); + it("passes the worker agent's own credential through, and only that", () => { const base = { OPENCODE_API_KEY: "zen-key", OPENAI_API_KEY: "other", GITHUB_TOKEN: "gh", PATH: "/usr/bin" }; active(); From c18939184967eeb795da831c16e1ef1558bec21d Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 14:56:42 -0700 Subject: [PATCH 6/9] Task checks run as a check account; --worker-strict; isolation line - --check-user (MAAT_CHECK_USER), with --worker-user: hidden checks, drafted and --criterion task checks, the reference check's try, mission assertions and pre-work criterion tries run as that account in their copy of the tree. Environment is an allowlist: PATH, a fresh empty HOME per run, LANG, TERM. As root with CAP_SYS_ADMIN each run is the init of its own PID namespace with its own /proc and an empty tmpfs on /tmp, /var/tmp and /dev/shm, dropped to the account with setpriv --no-new-privs and an empty bounding set. - Copies and reference-check files live in .check (Maat, group the check account, 710): copies are handed to the check account, the reference check's files are group-readable (750/640). The worker cannot enter the folder. - Checks that run in the project itself (project checks, task checks too big to copy) and mutation runs run as the worker. - The check account must not be root, Maat or the worker, and its group must not contain the worker. - --worker-strict refuses to start (exit 2) unless the worker account, the check account and the PID namespace can all be set up. Without it Maat carries on, and every receipt and the journal carry one line with the isolation in effect, e.g. "isolation: worker uid 1001, check uid 1002, pid namespace on". - Docs: the check account, strict mode, and a Known limitations section. - Tests (root, Linux): a check runs with the check uid and no provider or Maat variables; a project check runs as the worker; a mission assertion runs as the check account; the worker cannot read or enter the reference check folder while the check account can; the isolation line is in every receipt and once in the journal; --worker-strict refuses with no PID namespace (library and CLI); a check account equal to the worker is refused; with CAP_SYS_ADMIN the check sees its own /proc and an empty /tmp. --- docs/privilege-separation.md | 137 +++++++++++-- src/bar.ts | 18 +- src/cli.tsx | 66 ++++-- src/criteria.ts | 2 + src/engine.ts | 13 +- src/mission.ts | 15 +- src/privsep.ts | 383 ++++++++++++++++++++++++++++++++++- src/receipts.ts | 5 + src/reference.ts | 14 +- src/run.ts | 30 ++- src/scratch.ts | 9 +- test/privsep.test.ts | 245 +++++++++++++++++++++- 12 files changed, 885 insertions(+), 52 deletions(-) diff --git a/docs/privilege-separation.md b/docs/privilege-separation.md index 1f5a184..451b12a 100644 --- a/docs/privilege-separation.md +++ b/docs/privilege-separation.md @@ -96,11 +96,13 @@ worker and Maat writes nothing into the worker's tree. If a copy cannot be written with the worker's permissions, Maat says so and the records stay in the state dir, which is always kept. -**The judge and the checks stay Maat's.** Judge and ask subprocesses -(OpenCode, a Grok judge, an HTTP judge) run as Maat with Maat's HOME. If that -HOME is readable by the worker, Maat tightens it to 700 when it owns it and -refuses to start otherwise. Hidden checks run as Maat in the copy-on-run -tree, never as the worker. A check that ran in place (a project check, or a +**The judge stays Maat's; the checks run as Maat or as the check account.** +Judge and ask subprocesses (OpenCode, a Grok judge, an HTTP judge) run as +Maat with Maat's HOME. If that HOME is readable by the worker, Maat tightens +it to 700 when it owns it and refuses to start otherwise. Without +`--check-user`, hidden checks run as Maat in the copy-on-run tree, never as +the worker; with it, they run as the check account (next section). A check +that ran in place as Maat (a project check, or a tree too large to copy) can leave files owned by Maat in the project. After every bar run, those are handed back to the worker, so the worker can keep editing its own tree. The worker may be changing the tree while that runs, @@ -116,6 +118,90 @@ because the repository and its config belong to the worker. Maat does not set `safe.directory`, so its own git keeps refusing a repository another user owns. +## The check account: `--check-user` + +```sh +maat run --worker-user agent --check-user checker --worker-strict ... +``` + +`--check-user ` (or `MAAT_CHECK_USER`) needs `--worker-user`. Task +checks then run as a third account instead of as Maat: hidden checks, drafted +task checks (`--criteria auto`), `--criterion` checks, the reference check +and its try on the untouched project (the compile and probe steps before it, +which run only the reference program, stay Maat's), mission assertions in the bar and at a +milestone seal, and the tries of drafted criteria before the work. A check is +model-written and runs the worker's code, so it no longer runs as root. + +What it guarantees: + +- **The account.** The check account must not be root, Maat's user or the + worker, and its primary group must not contain the worker. Maat runs as + root (it spawns the check with that uid and gid and no supplementary + groups), or has passwordless `sudo -n -u `. +- **The environment is an allowlist.** `PATH`, `HOME`, `LANG` and `TERM`, and + nothing else of Maat's. `HOME` is a fresh, empty folder made for that one + run and removed after it. No provider key, no `MAAT_*` setting, no `TMPDIR` + (the check uses `/tmp`). +- **Its own PID namespace and /tmp.** When Maat is root with `CAP_SYS_ADMIN`, + each check run is started with `unshare --pid --fork --mount-proc`, so it is + the init of a PID namespace with its own `/proc` (it sees none of Maat's or + the worker's processes, and everything it starts dies with it). Inside, an + empty tmpfs is mounted on `/tmp`, `/var/tmp` and `/dev/shm` (except one that + holds its working directory or Maat's state dir), and the check then drops to + its account with `setpriv --clear-groups --no-new-privs --bounding-set=-all`. + Nothing it writes to a temp dir outlives it, so it cannot leave the + reference check where the worker can read it. +- **Its copy is its own.** The copy-on-run tree is made in + `.check` (owner Maat, group the check account, mode 710) and + handed to the check account, so it can build, write caches and commit in it + as it would in the project. The worker is neither owner nor group of that + folder, so it cannot enter it. +- **The reference check is readable by the check account and not the + worker.** Its programs (`check.py`, `second.py`, `driver.py`) and its + snapshot of the untouched project live in the same check folder, group the + check account, folders 750 and files 640. + +Checks that must run in the project itself run as the **worker**, because the +tree is the worker's: a project's own `done.yml` command checks, a task check +whose tree was too large to copy, and the mutation builtin's runs. They get +the worker's scrubbed environment, and with `CAP_SYS_ADMIN` their own PID +namespace and private /tmp per run too (a hidden check that fell back to +running in place is not visible in the worker's process list). Nothing in the +project is then made by Maat, so there is nothing to hand back. + +The check account's folder must be reachable: with the default state dir +(`/var/lib/maat/`), Maat opens `/var/lib/maat` to 711 when it made +it (each session's own folder stays 700). With `MAAT_STATE_DIR`, a folder +above it closed to the check account means the check account cannot be used +(Maat says so; `--worker-strict` refuses). In sudo mode Maat cannot chown, so +the check gets read-only copies and the default state dir under Maat's HOME +(700) is not reachable: set `MAAT_STATE_DIR`. + +## `--worker-strict` and the isolation line + +`--worker-strict` refuses to start, with exit 2 and a message naming what is +missing, unless all three are in place: the worker account, the check account +(`--check-user` must be given and usable) and the PID namespace (for the +worker, and for each check run with its private /tmp). It also refuses +`MAAT_WORKER_PIDNS=0`. + +Without it, Maat carries on with whatever could be set up: a check account +that cannot be used leaves the checks running as Maat, a missing namespace +leaves the process list visible, and a worker account that cannot be used +leaves the whole job unseparated (unless `MAAT_WORKER_PIDNS=1`, which still +refuses). Each case is said on stderr. Every receipt and the journal (one +`note` entry with `kind: "isolation"`) then carry one line with the isolation +actually in effect, for example: + +``` +isolation: worker uid 1001, check uid 1002, pid namespace on +isolation: worker uid 1001, check uid none (task checks run as Maat, uid 0), pid namespace off +isolation: none (worker tools and checks run as Maat, uid 0: no such user: agent) +``` + +The line is there whenever `--worker-user` was given, strict or not, and +absent otherwise. + ## The process list `ps` shows every process's command line to every user unless `/proc` is @@ -153,8 +239,10 @@ process entirely. `bench/local/container/run-in-container.sh` turns this on by default: -- `run.py` runs Maat as root with `--worker-user agent`. `BENCH_PRIVSEP=0` - runs the whole of Maat as `agent`, as before. +- `run.py` runs Maat as root with `--worker-user agent --check-user checker`. + The image has both users: `agent` (uid 1001) and `checker` (uid 1002, its + own group, no home). `BENCH_PRIVSEP=0` runs the whole of Maat as `agent`, + as before. - The container gets `--cap-add=SYS_ADMIN` for the PID namespace. `BENCH_PIDNS=0` drops it. - The worker's Grok credential is copied to `/home/agent/.grok`. A Grok @@ -169,7 +257,10 @@ process entirely. is a host bind mount, and on OrbStack and Docker Desktop a bind mount ignores `chown` and does not enforce file modes for other users, so a lock placed there would not hold. -- The graders and reference solutions stay root-only, as before. +- The graders and reference solutions stay root-only, as before. The graders + (and `crosscheck.py`'s re-grades and check re-runs) work on a root-owned + copy whose repositories are cut to their format lines, without hooks or + attributes; nothing sets `safe.directory`. To check it from outside a running job: @@ -178,13 +269,31 @@ docker exec maat-bench- sh -c 'ls /var/lib/maat' docker exec maat-bench- runuser -u agent -- cat /var/lib/maat//log/ # Permission denied ``` -## What is still open +## Known limitations -- A worker with network access can still reach whatever the network lets it - reach. Privilege separation is about the local machine. -- In sudo mode (Maat not root) there is no PID namespace. The worker's - environment is passed on sudo's command line (it is already scrubbed of - credentials). +- **The network is shared between accounts.** The worker, the check account + and Maat use the same network. A check (which runs the worker's code) can + send what it can read, the reference check included, to anything it can + reach, and the worker can fetch it from there. Privilege separation is + about the local machine; run the container with no network, or a + restricted one, to close this. +- **Snapshot and mutation code still read worker files by path.** Maat, as + root, copies the worker's tree for each check (`src/scratch.ts`), snapshots + it for the reference check (`snapshotProject`), lists it to see what a + command created, and the mutation builtin reads and rewrites source files + in the project by path. Those reads follow what the worker left there (a + symlink is copied as a symlink, but a mutation target is opened by name). + Only the hand-back walk and the records copy work by descriptor or as the + worker. +- **The agent user's HOME and /tmp are shared between bench tasks in one + container.** Every task in a bench container runs its worker as the same + `agent` user, with the same `/home/agent` and the same `/tmp` (the worker's + own PID namespace has no private /tmp). A later task's worker can read what + an earlier one left there. Task folders and logs are locked after grading; + HOME and /tmp are not cleared between tasks. +- In sudo mode (Maat not root) there is no PID namespace, for the worker or + the checks. The environment is passed on sudo's command line (already + scrubbed for the worker, allowlisted for checks). - The ACP worker's MCP connection to Maat's tools is a loopback port behind a per-session bearer token held by the worker's own agent. Through it the worker reaches only Maat's tools, which run as the worker. diff --git a/src/bar.ts b/src/bar.ts index 935c773..2faa5ed 100644 --- a/src/bar.ts +++ b/src/bar.ts @@ -1006,7 +1006,8 @@ async function mutationCheck( }; } if (!prior) { - const baseline = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal }); + // Mutation runs are in the worker's own tree: with a check account they run as the worker. + const baseline = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal, asCheck: "in-place" }); if (baseline.code !== 0) { return { ok: false, @@ -1039,6 +1040,7 @@ async function mutationCheck( cwd: ctx.cwd, timeoutMs, signal: ctx.signal, + asCheck: "in-place", }); // A mutation the command still passes is a line nothing checks — unless // it was a boundary nudge and negating the same condition is caught. @@ -1052,7 +1054,7 @@ async function mutationCheck( let negKilled = false; if (neg && negText !== null && negText !== file.text) { writeFileSync(file.abs, negText, "utf8"); - const rn = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal }); + const rn = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal, asCheck: "in-place" }); negKilled = rn.code !== 0; } if (negKilled) boundaryOnly.push(`${m.path}:${m.line} (${m.operator}) — ${m.before.trim()}`); @@ -2193,6 +2195,10 @@ export async function runCheck(check: Check, ctx: BarContext): Promise { try { return await runBarAs(bar, ctx); } finally { - // Checks run as Maat. Under privilege separation, whatever one left in the - // project (a build in place, a cache) goes back to the worker, so the - // worker can still edit its own tree (src/privsep.ts). + // Without a check account, checks run as Maat. Under privilege + // separation, whatever one left in the project (a build in place, a + // cache) goes back to the worker, so the worker can still edit its own + // tree (src/privsep.ts). With one, checks in the project run as the + // worker and this finds nothing of Maat's to hand back. privSep()?.handBack(ctx.cwd); } } diff --git a/src/cli.tsx b/src/cli.tsx index 78f16ff..2479ddb 100644 --- a/src/cli.tsx +++ b/src/cli.tsx @@ -54,7 +54,7 @@ import { stateDir } from "./statedir.js"; import { draftReference, snapshotProject } from "./reference.js"; import { env } from "./env.js"; import { fileURLToPath } from "node:url"; -import { disablePrivSep, enablePrivSep, workerUserFrom, type PrivSep } from "./privsep.js"; +import { checkUserFrom, disablePrivSep, enablePrivSep, setIsolationLine, workerUserFrom, type PrivSep } from "./privsep.js"; /** * The version, from the manifest that npm actually publishes. @@ -239,6 +239,16 @@ options to a private state dir (MAAT_STATE_DIR) and are copied into .maat/ when the job ends. For containers and unattended runs. (MAAT_WORKER_USER) + --check-user with --worker-user: task checks (hidden, drafted, mission) + run as user in their copy of the tree, with only PATH, + a fresh HOME, LANG and TERM, and (root with CAP_SYS_ADMIN) + their own PID namespace and /tmp. Checks that run in the + project itself, and mutation runs, run as the worker. + (MAAT_CHECK_USER) + --worker-strict with --worker-user: refuse to start unless the worker + account, the check account and the PID namespace can all + be set up. Without it Maat carries on and every receipt + and the journal say which isolation was in effect. --json machine-readable output (run/prove/stats/receipts) --version print the version and exit --no-stream disable token streaming (default: streaming on) @@ -315,6 +325,10 @@ type Args = { sandbox?: boolean; /** `--worker-user `: the worker's tools run as that user (src/privsep.ts). */ workerUser?: string; + /** `--check-user `: task checks run as that account (src/privsep.ts). */ + checkUser?: string; + /** `--worker-strict`: refuse to start unless worker account, check account and PID namespace are all in place. */ + workerStrict?: boolean; /** `molt mission run --features n`: stop after n worker runs. */ features?: number; /** Files the model may read and never write. `--read`, repeatable. */ @@ -698,6 +712,12 @@ export function parseArgs(argv: string[], stored: StoredEndpoint = {}): Args { case "--worker-user": out.workerUser = next(); break; + case "--check-user": + out.checkUser = next(); + break; + case "--worker-strict": + out.workerStrict = true; + break; case "--json": out.json = true; break; @@ -2238,34 +2258,54 @@ export async function main(argv = process.argv.slice(2)): Promise { // Privilege separation, when asked for: before any record is opened, so // every one of them is made in the private state dir (src/privsep.ts). const worker = workerUserFrom(args.workerUser); + const checker = checkUserFrom(args.checkUser); const separates = args.cmd === "run" || args.cmd === "ask" || (args.cmd === "mission" && (args.task ?? "").split(" ")[0] === "run"); + if (!worker && separates && (checker || args.workerStrict)) { + process.stderr.write(`maat: ${args.workerStrict ? "--worker-strict" : "--check-user"} needs --worker-user (MAAT_WORKER_USER)\n`); + return 2; + } if (worker && separates) { - let ps: PrivSep; + let ps: PrivSep | undefined; try { ps = enablePrivSep({ user: worker, project: args.cwd, helper: fileURLToPath(new URL("./fs-helper.js", import.meta.url)), notice: (t) => process.stderr.write(`maat: ${t}\n`), + ...(checker ? { checkUser: checker } : {}), + strict: args.workerStrict === true, }); } catch (e) { - process.stderr.write(`maat: ${(e as Error).message}\n`); - return 2; + // --worker-strict, or MAAT_WORKER_PIDNS=1, means refuse. Otherwise the + // job goes on unseparated, and every receipt and the journal say so. + if (args.workerStrict || process.env.MAAT_WORKER_PIDNS === "1") { + process.stderr.write(`maat: ${(e as Error).message}\n`); + return 2; + } + process.stderr.write(`maat: ${(e as Error).message}; carrying on WITHOUT privilege separation (--worker-strict refuses instead)\n`); + setIsolationLine(`isolation: none (worker tools and checks run as Maat, uid ${process.getuid?.() ?? "?"}: ${(e as Error).message})`); + } + if (ps) { + process.stderr.write( + `maat: worker tools run as ${ps.worker.name} (uid ${ps.worker.uid})` + + `${ps.pidns ? " in their own PID namespace" : ""}` + + `${ps.check ? `; task checks run as ${ps.check.name} (uid ${ps.check.uid})` : ""}; Maat's records are in ${ps.stateRoot} until the job ends\n` + + `maat: ${ps.isolation()}\n`, + ); } - process.stderr.write( - `maat: worker tools run as ${ps.worker.name} (uid ${ps.worker.uid})` + - `${ps.pidns ? " in their own PID namespace" : ""}; Maat's records are in ${ps.stateRoot} until the job ends\n`, - ); try { return args.cmd === "mission" ? await cmdMission(args) : await cmdRun(args, args.cmd === "ask"); } finally { - try { - const dest = ps.publish(); - process.stderr.write(`maat: records copied to ${dest}\n`); - } catch (e) { - process.stderr.write(`maat: could not copy the records from ${ps.stateRoot} into the project: ${(e as Error).message}\n`); + if (ps) { + try { + const dest = ps.publish(); + process.stderr.write(`maat: records copied to ${dest}\n`); + } catch (e) { + process.stderr.write(`maat: could not copy the records from ${ps.stateRoot} into the project: ${(e as Error).message}\n`); + } } disablePrivSep(); + setIsolationLine(undefined); } } switch (args.cmd) { diff --git a/src/criteria.ts b/src/criteria.ts index 85e8083..221c883 100644 --- a/src/criteria.ts +++ b/src/criteria.ts @@ -321,6 +321,8 @@ export async function preflightCriteria( timeoutMs: opts.timeoutMs ?? 5_000, maxBuffer: 1024 * 1024, signal: opts.signal, + // As the check account in its copy, or as the worker in the project (--check-user). + asCheck: copy ? "copy" : "in-place", }); // One decision point, shared with the bar. A command that outlived the // timeout plainly ran, and a timeout's exit code is never one of the diff --git a/src/engine.ts b/src/engine.ts index 1d32b27..214a552 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -131,7 +131,10 @@ import { type Spend, } from "./types.js"; import { stateDir, stateDirName, stateRedirect } from "./statedir.js"; -import { gitSync, privSep, type WorkerFs } from "./privsep.js"; +import { gitSync, isolationLine, privSep, type WorkerFs } from "./privsep.js"; + +/** Journals that already carry the isolation line (one per journal, however many engines share it). */ +const isolationNoted = new WeakSet(); import { env } from "./env.js"; import { Judgments, caseReason, reasonText } from "./judgment.js"; @@ -2228,6 +2231,14 @@ export class Engine { journalRoot: cfg.journal.chainRoot(), }); } + // Under --worker-user, one line in the journal saying what isolation was + // actually in effect (src/privsep.ts), e.g. + // "isolation: worker uid 1001, check uid 1002, pid namespace on". + const isolation = isolationLine(); + if (isolation && cfg.journal && !isolationNoted.has(cfg.journal)) { + isolationNoted.add(cfg.journal); + cfg.journal.append("note", { kind: "isolation", text: isolation }); + } } get model(): string { diff --git a/src/mission.ts b/src/mission.ts index 78c0527..0b11e5c 100644 --- a/src/mission.ts +++ b/src/mission.ts @@ -28,6 +28,8 @@ import { parse as parseYaml, stringify as toYaml } from "yaml"; import { askModel, jsonIn, notJson, type AskOptions } from "./ask.js"; import type { Engine } from "./engine.js"; import { runCommand } from "./run.js"; +import { privSep } from "./privsep.js"; +import { copyTree } from "./scratch.js"; import { diagnoseFailure } from "./bar.js"; import type { Check, Confirm, EngineEvent, JobOutcome, Spend } from "./types.js"; import { estTokens } from "./types.js"; @@ -451,23 +453,32 @@ export async function runAssertions( const a = byId.get(id); if (!a?.run) continue; const t0 = Date.now(); + // With a check account (--check-user) an assertion is a task check like + // any other: it runs as that account in a copy of the tree, or as the + // worker in the tree itself when no copy can be taken. Without one it + // runs in place as Maat, as it always has. + const copy = privSep()?.check && process.env.MAAT_CHECK_COPY !== "0" ? await copyTree(cwd) : null; try { const r = await runCommand(a.run, { - cwd, + cwd: copy?.dir ?? cwd, timeoutMs: a.timeoutMs ?? DEFAULT_ASSERTION_TIMEOUT_MS, maxBuffer: 256 * 1024, signal, + asCheck: copy ? "copy" : "in-place", }); const d = diagnoseFailure(r.code ?? 0, r.stdout, r.stderr); + const text = `${r.stdout}${r.stderr}`; out.push({ id, ok: !r.timedOut && r.code === 0, - output: (r.timedOut ? "timed out\n" : "") + `${r.stdout}${r.stderr}`.slice(-4000), + output: (r.timedOut ? "timed out\n" : "") + (copy ? copy.unmap(text) : text).slice(-4000), didNotRun: d.didNotRun, ms: Date.now() - t0, }); } catch (e) { out.push({ id, ok: false, output: String(e), didNotRun: true, ms: Date.now() - t0 }); + } finally { + await copy?.cleanup(); } } return out; diff --git a/src/privsep.ts b/src/privsep.ts index 637697e..b3c6af3 100644 --- a/src/privsep.ts +++ b/src/privsep.ts @@ -26,13 +26,22 @@ * mode 700, owned by Maat (MAAT_STATE_DIR, default /var/lib/maat/ * as root, ~/.local/state/maat/ otherwise). At the end of the job * they are copied into the project's `.maat/` as before; - * - the judge and ask subprocesses and the hidden checks stay Maat's: same - * uid, a HOME the worker cannot read, checks run in the copy-on-run tree; + * - the judge and ask subprocesses stay Maat's: same uid, a HOME the worker + * cannot read. Task checks (hidden, drafted, mission) run in the + * copy-on-run tree as Maat, or, with `--check-user `, as that third + * account, with an allowlisted environment and (root with CAP_SYS_ADMIN) + * a PID namespace and private /tmp of their own per run. Checks that must + * run in the project itself, and mutation runs, then run as the worker; * - where the kernel allows it (root with CAP_SYS_ADMIN), the worker's * commands share one PID namespace of their own with a private /proc, so * `ps` shows them nothing of Maat's. Without it the process list stays * visible: see docs/privilege-separation.md. * + * `--worker-strict` refuses to start unless the worker account, the check + * account and the PID namespace are all in place. Without it a missing piece + * is reported, and `isolationLine()` names what is actually in effect; every + * receipt and the journal carry that line. + * * Off (the default) nothing here runs and nothing changes. */ import { execFileSync, spawn, spawnSync, type ChildProcess, type SpawnSyncReturns } from "node:child_process"; @@ -50,11 +59,14 @@ import { readFileSync, readdirSync, readlinkSync, + lchownSync, + mkdtempSync, realpathSync, + rmSync, statSync, writeFileSync, } from "node:fs"; -import { homedir } from "node:os"; +import { homedir, tmpdir } from "node:os"; import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import type { GrepResult, WalkOptions, WalkResult } from "./files.js"; import { redirectState, stateDirName } from "./statedir.js"; @@ -198,8 +210,57 @@ export type PrivSepOptions = { * it is looked for beside the running script. */ helper?: string; + /** + * The account task checks run as (`--check-user`, MAAT_CHECK_USER): not + * root, not Maat's user, not the worker, and in a group the worker is not + * in. Left out, task checks run as Maat, as before. + */ + checkUser?: string; + /** + * `--worker-strict`: throw unless the check account and the PID namespace + * can both be set up (a worker account that cannot be set up always throws + * here). Without it, a check account or namespace that cannot be made is + * reported through `notice` and in `isolation()`, and the job goes on. + */ + strict?: boolean; }; +/** Temp dirs a check run gets a private, empty tmpfs on, when it has a mount namespace. */ +const PRIVATE_TMP_DIRS = ["/tmp", "/var/tmp", "/dev/shm"]; + +/** True when `p` is `dir` or inside it. */ +function under(p: string, dir: string): boolean { + const r = relative(dir, p); + return r === "" || (!r.startsWith("..") && !isAbsolute(r)); +} + +/** Names of the users listed as members of group `gid` in /etc/group. */ +function groupMembers(gid: number): string[] { + try { + for (const line of readFileSync("/etc/group", "utf8").split("\n")) { + const f = line.trim().split(":"); + if (f.length >= 4 && Number(f[2]) === gid) return f[3]!.split(",").filter(Boolean); + } + } catch { + /* no group file */ + } + return []; +} + +/** + * The shell run as the init of a check's own PID namespace: it mounts an + * empty tmpfs on each temp dir it is given (a private /tmp), then drops to the + * check's account with no capabilities and no way to gain one, enters the + * working directory and runs the command. Arguments: the temp dirs as one + * colon-separated word, uid, gid, cwd, then the program and its arguments. + */ +const CHECK_INIT = [ + "set -e", + 'dirs=$1 uid=$2 gid=$3 wd=$4; shift 4', + 'IFS=:; for d in $dirs; do [ -n "$d" ] && mount -t tmpfs -o mode=1777,nosuid,nodev maat-check-tmp "$d"; done; unset IFS', + 'exec setpriv --reuid="$uid" --regid="$gid" --clear-groups --no-new-privs --bounding-set=-all -- /bin/sh -c \'cd -- "$0" && exec "$@"\' "$wd" "$@"', +].join("\n"); + export class PrivSep { readonly worker: WorkerUser; readonly project: string; @@ -209,6 +270,22 @@ export class PrivSep { readonly mode: "root" | "sudo"; readonly pidns: boolean; readonly helperScript: string; + /** The check account, when one was asked for and could be set up. */ + readonly check?: WorkerUser; + /** + * Where check copies and reference-check files go when there is a check + * account: `.check`, owned by Maat, group the check account, + * mode 710. The check account can pass through it to the folders made for + * it; the worker (neither owner nor group) cannot enter it at all. + */ + readonly checkRoot?: string; + /** Each check run gets its own PID namespace (and a private /tmp, see `checkTmpDirs`). */ + readonly checkNs: boolean; + /** The temp dirs each check run gets an empty tmpfs on. */ + readonly checkTmpDirs: readonly string[]; + /** Why a check account that was asked for is not in use. */ + readonly checkProblem?: string; + readonly strict: boolean; /** The project as it was when the job started, so a swapped folder is never walked. */ private readonly projectReal: string; private readonly projectId: { dev: number; ino: number }; @@ -243,6 +320,23 @@ export class PrivSep { ); } + this.strict = opts.strict === true; + if (this.strict && (opts.pidns === false || envFlag("MAAT_WORKER_PIDNS") === false)) { + throw new PrivSepError("--worker-strict needs a PID namespace, and MAAT_WORKER_PIDNS=0 turns it off"); + } + let check: WorkerUser | undefined; + let checkProblem: string | undefined; + if (opts.checkUser) { + try { + check = this.resolveCheck(opts.checkUser); + } catch (e) { + if (this.strict) throw e; + checkProblem = (e as Error).message; + } + } else if (this.strict) { + throw new PrivSepError("--worker-strict needs a check account (--check-user): without one, task checks would run as Maat"); + } + const session = `${new Date().toISOString().replace(/[:.]/g, "-")}-${randomBytes(6).toString("hex")}`; const wanted = opts.stateDir ?? process.env.MAAT_STATE_DIR ?? defaultStateRoot(session); this.stateRoot = resolve(wanted); @@ -253,11 +347,54 @@ export class PrivSep { this.makePrivate(this.stateRoot); this.makePrivate(join(this.stateRoot, "tmp")); - const want = opts.pidns ?? envFlag("MAAT_WORKER_PIDNS"); + if (check) { + const root = `${this.stateRoot}.check`; + const usedDefault = !opts.stateDir && !process.env.MAAT_STATE_DIR; + try { + this.makeCheckRoot(root, check, usedDefault); + this.checkRoot = root; + } catch (e) { + try { + rmSync(root, { recursive: true, force: true }); + } catch { + /* nothing made */ + } + if (this.strict) throw e; + checkProblem = (e as Error).message; + check = undefined; + } + } + this.check = check; + this.checkProblem = checkProblem; + + const want = this.strict ? true : (opts.pidns ?? envFlag("MAAT_WORKER_PIDNS")); this.pidns = want === false ? false : this.startNamespace(); if (want === true && !this.pidns) { - throw new PrivSepError("MAAT_WORKER_PIDNS=1 but a PID namespace could not be made (needs root with CAP_SYS_ADMIN, unshare, nsenter and setpriv)"); + if (this.checkRoot) rmSync(this.checkRoot, { recursive: true, force: true }); + throw new PrivSepError( + `${this.strict ? "--worker-strict" : "MAAT_WORKER_PIDNS=1"} but a PID namespace could not be made (needs root with CAP_SYS_ADMIN, unshare, nsenter and setpriv)`, + ); + } + // A check run's namespace is made per run (`checkSpec`); probed here, once, + // with the mount it needs for a private /tmp. + this.checkNs = !!this.check && this.pidns && this.probeCheckNamespace(); + if (this.strict && this.check && !this.checkNs) { + this.killNamespace(); + if (this.checkRoot) rmSync(this.checkRoot, { recursive: true, force: true }); + throw new PrivSepError("--worker-strict but a check run's PID namespace with a private /tmp could not be made (mount -t tmpfs failed inside unshare)"); } + this.checkTmpDirs = this.checkNs + ? PRIVATE_TMP_DIRS.filter((d) => { + try { + if (!statSync(d).isDirectory()) return false; + } catch { + return false; + } + // A tmpfs over a folder Maat's own records live in would hide them + // from the check (MAAT_STATE_DIR under /tmp, say): that dir is left shared. + return !under(this.stateRoot, d) && !(this.checkRoot && under(this.checkRoot, d)); + }) + : []; process.on("exit", this.onExit); // Maat's own temp files (reference tries, check copies, drafts, judge @@ -279,6 +416,186 @@ export class PrivSep { "so its commands get their own PID namespace (docs/privilege-separation.md)", ); } + if (opts.checkUser && !this.check) notice(`task checks run as Maat, not ${opts.checkUser}: ${this.checkProblem}`); + else if (this.check && !this.checkNs) { + notice("check runs share the PID namespace and /tmp with the rest of the machine (needs root with CAP_SYS_ADMIN)"); + } + } + + /** + * The one line every receipt and the journal carry under privilege + * separation: the isolation actually in effect, not the one asked for. + * For example "isolation: worker uid 1001, check uid 1002, pid namespace on". + */ + isolation(): string { + const parts = [`worker uid ${this.worker.uid}`]; + parts.push(this.check ? `check uid ${this.check.uid}` : `check uid none (task checks run as Maat, uid ${this.maatUid})`); + if (!this.pidns) parts.push("pid namespace off"); + else if (this.check && !this.checkNs) parts.push("pid namespace on (worker only; check runs have none)"); + else parts.push("pid namespace on"); + if (this.checkNs && !this.checkTmpDirs.includes("/tmp")) parts.push("private /tmp off (the state dir is under /tmp)"); + return `isolation: ${parts.join(", ")}`; + } + + /** The check account by name, refused unless it is a fourth party to Maat, root and the worker. */ + private resolveCheck(name: string): WorkerUser { + const c = lookupUser(name); + if (c.uid === 0) throw new PrivSepError("--check-user must not be root"); + if (c.uid === this.maatUid) throw new PrivSepError(`--check-user ${name} is the user Maat runs as; the check account must be a different user`); + if (c.uid === this.worker.uid) throw new PrivSepError(`--check-user ${name} is the worker; the check account must be a different user`); + // Reference-check files are shared with the check account through its + // primary group, so the worker must not be in that group. + if (c.gid === this.worker.gid || groupMembers(c.gid).includes(this.worker.name)) { + throw new PrivSepError(`--check-user ${name}: its group (gid ${c.gid}) includes the worker ${this.worker.name}; give it a group of its own`); + } + if (this.mode === "sudo" && spawnSync("sudo", ["-n", "-u", name, "--", "true"], { stdio: "ignore" }).status !== 0) { + throw new PrivSepError(`--check-user needs Maat to run as root, or passwordless sudo to ${name} (sudo -n -u ${name} true failed)`); + } + return c; + } + + /** + * The folder check copies and reference files go in: Maat's, group the + * check account, mode 710. Its parent must let the check account through; + * the default parent (/var/lib/maat), when Maat made it, is opened to 711 + * for that (each session's own dirs stay closed to everyone else). The + * check account is then asked to enter it; a refusal means checks cannot + * run as that account here. + */ + private makeCheckRoot(root: string, check: WorkerUser, usedDefault: boolean): void { + mkdirSync(root, { recursive: true, mode: 0o700 }); + const st = lstatSync(root); + if (!st.isDirectory() || st.uid !== this.maatUid) throw new PrivSepError(`${root} is not a folder of Maat's`); + if (this.mode === "root") lchownSync(root, this.maatUid, check.gid); + chmodSync(root, 0o710); + if (usedDefault) { + const parent = dirname(this.stateRoot); + const pst = statSync(parent); + if (pst.uid === this.maatUid && (pst.mode & 0o001) === 0) chmodSync(parent, (pst.mode & 0o7777) | 0o011); + } + const probe = + this.mode === "root" + ? spawnSync("/bin/sh", ["-c", 'cd -- "$0"', root], { stdio: "ignore", uid: check.uid, gid: check.gid, cwd: "/" }) + : spawnSync("sudo", ["-n", "-u", check.name, "--", "/bin/sh", "-c", 'cd -- "$0"', root], { stdio: "ignore", cwd: "/" }); + if (probe.status !== 0) { + throw new PrivSepError(`the check account ${check.name} cannot reach ${root} (a folder above it is closed to it; set MAAT_STATE_DIR under one it can pass through)`); + } + } + + /** Can a check run get its own PID namespace, /proc and an empty tmpfs? */ + private probeCheckNamespace(): boolean { + if (process.platform !== "linux" || this.mode !== "root" || !which("setpriv")) return false; + const r = spawnSync("unshare", ["--pid", "--fork", "--mount-proc", "/bin/sh", "-c", "mount -t tmpfs -o mode=1777 maat-check-tmp /tmp"], { + stdio: "ignore", + timeout: 10_000, + }); + return r.status === 0; + } + + /** + * The environment a check run as the check account gets: an allowlist, + * not a scrub. PATH, HOME (a fresh empty folder per run), LANG and TERM; + * nothing else of Maat's, so no provider key or Maat setting reaches it. + */ + checkEnv(home: string, base: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv { + const env: NodeJS.ProcessEnv = { PATH: base.PATH || "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", HOME: home }; + if (base.LANG) env.LANG = base.LANG; + if (base.TERM) env.TERM = base.TERM; + return env; + } + + /** + * A check, ready to spawn. `who` is "check" for a check in its copy (the + * check account, allowlisted environment, fresh HOME) and "worker" for one + * that must run in the project itself (a project check, a mutation run, a + * task check whose tree was too large to copy): the worker account and its + * scrubbed environment, because the tree is the worker's. + * + * With `checkNs` each run is the init of its own PID namespace, with its + * own /proc and an empty tmpfs on /tmp, /var/tmp and /dev/shm (except one + * that holds the working directory or Maat's state), and drops to the + * account with no capabilities and no-new-privs. Everything it starts dies + * with it, and nothing it leaves in a temp dir outlives it. Without a + * namespace the check account runs directly with its uid (root) or through + * `sudo -n -u` (sudo mode), and a worker-run check enters the worker's own + * namespace when there is one. + * + * Call `cleanup` when the run is over: it removes the fresh HOME. + */ + checkSpec(command: string, shell: string | true, cwd: string, who: "check" | "worker", base: NodeJS.ProcessEnv = process.env): { spec: SpawnSpec; cleanup: () => void } { + const sh = shell === true ? "/bin/sh" : shell; + const user = who === "check" ? this.check : this.worker; + if (!user) throw new PrivSepError("no check account"); + let runDir: string | undefined; + const cleanup = () => { + if (runDir) rmSync(runDir, { recursive: true, force: true }); + runDir = undefined; + }; + let env: NodeJS.ProcessEnv; + if (who === "check") { + runDir = mkdtempSync(join(this.checkRoot ?? tmpdir(), "run-")); + const home = join(runDir, "home"); + mkdirSync(home, { mode: 0o700 }); + if (this.mode === "root") { + lchownSync(runDir, this.maatUid, user.gid); + chmodSync(runDir, 0o710); + lchownSync(home, user.uid, user.gid); + } else { + // sudo mode: Maat cannot chown, so the check gets an empty HOME it + // can read but not write. + chmodSync(runDir, 0o711); + chmodSync(home, 0o711); + } + env = this.checkEnv(home, base); + } else { + env = this.workerEnv(base); + } + if (this.checkNs) { + const tmps = this.checkTmpDirs.filter((d) => !under(cwd, d)); + return { + spec: { + file: "unshare", + args: ["--pid", "--fork", "--mount-proc", "--kill-child", "/bin/sh", "-c", CHECK_INIT, "maat-check", tmps.join(":"), String(user.uid), String(user.gid), cwd, sh, "-c", command], + env, + }, + cleanup, + }; + } + if (who === "worker") return { spec: this.execSpec(sh, ["-c", command], cwd, base), cleanup }; + if (this.mode === "sudo") { + const pairs = Object.entries(env).map(([k, v]) => `${k}=${v}`); + return { spec: { file: "sudo", args: ["-n", "-u", user.name, "--", "env", "-i", ...pairs, sh, "-c", command], env }, cleanup }; + } + return { spec: { file: sh, args: ["-c", command], env, uid: user.uid, gid: user.gid }, cleanup }; + } + + /** + * Hand a folder Maat made for a check (a copy of the tree, a reference + * try's scratch dir) to the check account, so the check can write in it as + * it would in the project. Only folders Maat made under `checkRoot`, which + * the worker cannot enter; symlinks are re-owned, never followed. No-op + * without a check account, or when Maat cannot chown (sudo mode). + */ + giveToCheck(dir: string): void { + const c = this.check; + if (!c || !this.checkRoot || this.mode !== "root" || !under(resolve(dir), this.checkRoot)) return; + walkOwn(dir, (p) => lchownSync(p, c.uid, c.gid)); + } + + /** + * Let the check account read a folder of Maat's (the reference check's + * programs and its snapshot of the project): owner Maat, group the check + * account, folders 750 and files 640. The worker is neither, and cannot + * enter `checkRoot` in the first place. + */ + shareWithCheck(dir: string): void { + const c = this.check; + if (!c || !this.checkRoot || !under(resolve(dir), this.checkRoot)) return; + walkOwn(dir, (p, st) => { + if (this.mode === "root") lchownSync(p, this.maatUid, c.gid); + if (st.isSymbolicLink()) return; + chmodSync(p, st.isDirectory() ? (this.mode === "root" ? 0o750 : 0o755) : this.mode === "root" ? 0o640 : 0o644); + }); } private setEnv(k: string, v: string): void { @@ -408,6 +725,16 @@ export class PrivSep { notice(`tightened ${home} to mode 700 so ${this.worker.name} cannot read the judge's logins`); } + private killNamespace(): void { + if (this.nsLeader?.pid) { + try { + process.kill(this.nsLeader.pid, "SIGKILL"); + } catch { + /* gone */ + } + } + } + /** One PID namespace with a private /proc for all of the worker's processes. */ private startNamespace(): boolean { if (process.platform !== "linux" || this.mode !== "root") return false; @@ -699,11 +1026,12 @@ export class PrivSep { this.closed = true; process.removeListener("exit", this.onExit); this.helper?.stop(); - if (this.nsLeader?.pid) { + this.killNamespace(); + if (this.checkRoot) { try { - process.kill(this.nsLeader.pid, "SIGKILL"); + rmSync(this.checkRoot, { recursive: true, force: true }); } catch { - /* gone */ + /* left for the OS */ } } redirectState(null); @@ -714,6 +1042,14 @@ export class PrivSep { } } +/** Every entry under `dir`, `dir` included, by lstat: links are visited, never followed. */ +function walkOwn(dir: string, fn: (p: string, st: import("node:fs").Stats) => void): void { + const st = lstatSync(dir); + fn(dir, st); + if (!st.isDirectory()) return; + for (const n of readdirSync(dir)) walkOwn(join(dir, n), fn); +} + function envFlag(name: string): boolean | undefined { const v = process.env[name]; if (v === undefined || v === "") return undefined; @@ -868,7 +1204,10 @@ export function privSep(): PrivSep | undefined { /** Turn it on for this process. Throws PrivSepError when it cannot be made safe. */ export function enablePrivSep(opts: PrivSepOptions): PrivSep { active?.close(); + active = undefined; + isolation = undefined; active = new PrivSep(opts); + isolation = active.isolation(); return active; } @@ -876,6 +1215,28 @@ export function enablePrivSep(opts: PrivSepOptions): PrivSep { export function disablePrivSep(): void { active?.close(); active = undefined; + isolation = undefined; +} + +let isolation: string | undefined; + +/** + * The isolation line for receipts and the journal, when separation was asked + * for: what `PrivSep.isolation()` says, or what the CLI set when it carried on + * without it. Undefined when nobody asked (the default changes nothing). + */ +export function isolationLine(): string | undefined { + return isolation; +} + +/** Set (or clear) the isolation line; the CLI uses it when it carries on without separation. */ +export function setIsolationLine(line: string | undefined): void { + isolation = line; +} + +/** Where a check's copy or a reference check's files go: the check folder when there is a check account, else Maat's temp dir. */ +export function checkTmpDir(): string { + return active?.checkRoot ?? tmpdir(); } /** @@ -907,3 +1268,9 @@ export function workerUserFrom(flag?: string): string | undefined { const v = (flag ?? process.env.MAAT_WORKER_USER ?? "").trim(); return v || undefined; } + +/** The check account asked for, from the flag or MAAT_CHECK_USER. */ +export function checkUserFrom(flag?: string): string | undefined { + const v = (flag ?? process.env.MAAT_CHECK_USER ?? "").trim(); + return v || undefined; +} diff --git a/src/receipts.ts b/src/receipts.ts index 3a33953..3388c20 100644 --- a/src/receipts.ts +++ b/src/receipts.ts @@ -14,6 +14,7 @@ import { MIN_SECRET_CHARS, redact } from "./redact.js"; import type { BarResult } from "./types.js"; import { stateDir } from "./statedir.js"; import { WITHHELD, maskDeep, maskText } from "./withhold.js"; +import { isolationLine } from "./privsep.js"; /** Where a receipt's full text goes when hidden commands were masked in it (src/withhold.ts). */ export const FULL_DIR = "full"; @@ -531,6 +532,10 @@ export class Receipts { "", ] : []), + // Under --worker-user, the isolation that was actually in effect while + // this was judged: which uid ran the worker and the checks, and whether + // there was a PID namespace (src/privsep.ts). Absent when nobody asked. + ...(isolationLine() ? [isolationLine()!, ""] : []), "## What the model claimed", "", "> " + (args.claim.trim() || "(no final message)").split("\n").join("\n> "), diff --git a/src/reference.ts b/src/reference.ts index 8d55042..5e62d21 100644 --- a/src/reference.ts +++ b/src/reference.ts @@ -42,10 +42,10 @@ */ import { createHash } from "node:crypto"; import { cpSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; import { join, relative } from "node:path"; import { askModel, jsonIn, type AskOptions } from "./ask.js"; import { runCommand } from "./run.js"; +import { checkTmpDir, privSep } from "./privsep.js"; import { STATE_DIRS } from "./statedir.js"; import type { Check } from "./types.js"; @@ -250,7 +250,7 @@ export type Snapshot = { dir: string; files: number; bytes: number; hash: string * work. Null when the project is too large to copy quickly — the reference is * an extra, never a reason for a slow start. */ -export function snapshotProject(cwd: string, root = mkdtempSync(join(tmpdir(), "ref-"))): Snapshot | null { +export function snapshotProject(cwd: string, root = mkdtempSync(join(checkTmpDir(), "ref-"))): Snapshot | null { const files: string[] = []; let bytes = 0; const walk = (dir: string): boolean => { @@ -349,7 +349,7 @@ async function writeAndTry(opts: Ask, prompt: string): Promise { return new Promise((resolve, reject) => { let child: ChildProcess; + let cleanup: (() => void) | undefined; try { - const ps = opts.asWorker ? privSep() : undefined; - if (ps) { + const sep = privSep(); + const ps = opts.asWorker ? sep : undefined; + if (opts.asCheck && sep?.check) { + const made = sep.checkSpec(command, opts.shell ?? true, opts.cwd, opts.asCheck === "copy" ? "check" : "worker", opts.env ?? process.env); + cleanup = made.cleanup; + const spec = made.spec; + child = spawn(spec.file, spec.args, { + cwd: opts.cwd, + env: spec.env, + stdio: ["ignore", "pipe", "pipe"], + detached: true, + ...(spec.uid !== undefined ? { uid: spec.uid, gid: spec.gid } : {}), + }); + } else if (ps) { const spec = ps.commandSpec(command, opts.shell ?? true, opts.cwd, opts.env); child = spawn(spec.file, spec.args, { cwd: opts.cwd, @@ -119,6 +142,7 @@ export function runCommand(command: string, opts: RunOptions): Promise maxFiles) return { why: `the tree has over ${maxFiles} files` }; let tmp: string; try { - tmp = await mkdtemp(join(tmpdir(), "maat-check-")); + // With a check account (--check-user) the copy goes in its folder, which + // the worker cannot enter, and is handed to it below. + tmp = await mkdtemp(join(checkTmpDir(), "maat-check-")); } catch (e) { return { why: `no temporary directory (${e instanceof Error ? e.message : String(e)})` }; } @@ -286,6 +288,9 @@ export async function copyTreeOrWhy( }; try { await copyDir(root, dest, true, false); + // The check runs as the check account and may write in its copy (a + // build, a cache, a commit), as it would in the project. + privSep()?.giveToCheck(tmp); } catch (e) { await cleanup(); return { diff --git a/test/privsep.test.ts b/test/privsep.test.ts index 3b6b997..578d870 100644 --- a/test/privsep.test.ts +++ b/test/privsep.test.ts @@ -22,7 +22,10 @@ import { Receipts } from "../src/receipts.js"; import { Integrity } from "../src/integrity.js"; import { runCommand } from "../src/run.js"; import { stateDir } from "../src/statedir.js"; -import { disablePrivSep, enablePrivSep, gitSync, privSep, safeRel, type PrivSep } from "../src/privsep.js"; +import { checkUserFrom, disablePrivSep, enablePrivSep, gitSync, isolationLine, privSep, safeRel, type PrivSep } from "../src/privsep.js"; +import { runCheck, type BarContext } from "../src/bar.js"; +import { runAssertions, type Contract } from "../src/mission.js"; +import { snapshotProject } from "../src/reference.js"; import { listBackground, resetBackgroundRegistry, startBackground, type BackgroundProcess } from "../src/background.js"; import { ACP_AGENTS } from "../src/acp.js"; import type { Check, EngineEvent } from "../src/types.js"; @@ -33,6 +36,15 @@ const HELPER = fileURLToPath(new URL("../src/fs-helper.js", import.meta.url)); const linuxRoot = process.platform === "linux" && process.getuid?.() === 0 && spawnSync("runuser", ["--help"]).status === 0; const WORKER = process.env.MAAT_TEST_WORKER_USER ?? "maattestw"; +const CHECKER = process.env.MAAT_TEST_CHECK_USER ?? "maattestc"; +/** + * CI's Linux job runs with CAP_SYS_ADMIN and sets this, so the namespace + * assertions fail there instead of being skipped when no namespace was made. + */ +const REQUIRE_PIDNS = process.env.MAAT_TEST_REQUIRE_PIDNS === "1"; +/** Can this machine make a PID namespace with a private /proc and a tmpfs (root with CAP_SYS_ADMIN)? */ +const nsAvailable = + linuxRoot && spawnSync("unshare", ["--pid", "--fork", "--mount-proc", "/bin/sh", "-c", "mount -t tmpfs t /tmp"], { stdio: "ignore" }).status === 0; /** Run a shell command as the worker user, the way an attacker in the worker would. */ function asWorker(cmd: string): { status: number | null; out: string } { @@ -45,6 +57,18 @@ function ensureWorker(): number { return Number(execFileSync("id", ["-u", WORKER], { encoding: "utf8" }).trim()); } +/** The check account: its own user and its own group, as the bench image makes `checker`. */ +function ensureChecker(): number { + if (spawnSync("id", ["-u", CHECKER]).status !== 0) execFileSync("useradd", ["-M", "-U", "-s", "/bin/sh", CHECKER]); + return Number(execFileSync("id", ["-u", CHECKER], { encoding: "utf8" }).trim()); +} + +/** Run a shell command as the check account. */ +function asChecker(cmd: string): { status: number | null; out: string } { + const r = spawnSync("runuser", ["-u", CHECKER, "--", "sh", "-c", cmd], { encoding: "utf8" }); + return { status: r.status, out: `${r.stdout}${r.stderr}` }; +} + /** A project folder the worker owns, as the bench hands one over. */ function project(uid: number): string { // Under a parent the worker may pass through but not read, as the bench's @@ -202,6 +226,7 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need const joined = results.join("\n---\n"); assert.ok(!joined.includes("sealed-check-42"), `a tool read the state dir:\n${joined}`); assert.match(joined, /EACCES|permission denied/i, "read_file was refused by the kernel"); + if (REQUIRE_PIDNS) assert.ok(ps.pidns, "MAAT_TEST_REQUIRE_PIDNS=1 but the worker got no PID namespace"); if (ps.pidns) { const pids = (results.at(-1) ?? "").trim().split(/\s+/).filter(Boolean).map(Number); assert.ok(pids.length > 0 && pids.length < 10, `the worker's /proc: ${pids.join(" ")}`); @@ -444,6 +469,209 @@ describe("privilege separation (Linux, root)", { skip: linuxRoot ? false : "need }); }); +describe("check account: --check-user and --worker-strict (Linux, root)", { skip: linuxRoot ? false : "needs Linux and root (CI's Linux job)" }, () => { + let wuid = 0; + let cuid = 0; + let dir = ""; + let ps: PrivSep; + const roots: string[] = []; + const savedState = process.env.MAAT_STATE_DIR; + const saved: Record = {}; + /** Provider keys and Maat settings in Maat's own environment: none may reach a check. */ + const PLANTED = { OPENAI_API_KEY: "sk-planted-openai", OPENROUTER_API_KEY: "sk-or-planted", MAAT_JUDGE_MODEL: "planted-judge", MOLT_API_KEY: "planted-molt" }; + const hostTmpMarker = `/tmp/maat-host-tmp-marker-${process.pid}`; + + const enable = (extra: { strict?: boolean } = {}): PrivSep => { + disablePrivSep(); + ps = enablePrivSep({ user: WORKER, project: dir, helper: HELPER, checkUser: CHECKER, ...extra }); + roots.push(ps.stateRoot); + return ps; + }; + + before(() => { + wuid = ensureWorker(); + cuid = ensureChecker(); + dir = project(wuid); + writeFileSync(join(dir, "out.txt"), "marker-77\n"); + execFileSync("chown", ["-R", `${wuid}:${wuid}`, dir]); + delete process.env.MAAT_STATE_DIR; + for (const [k, v] of Object.entries(PLANTED)) { + saved[k] = process.env[k]; + process.env[k] = v; + } + writeFileSync(hostTmpMarker, "host\n"); + enable(); + }); + + after(() => { + disablePrivSep(); + for (const r of roots) { + rmSync(r, { recursive: true, force: true }); + rmSync(`${r}.check`, { recursive: true, force: true }); + } + for (const [k, v] of Object.entries(saved)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + if (savedState !== undefined) process.env.MAAT_STATE_DIR = savedState; + rmSync(hostTmpMarker, { force: true }); + rmSync(dir, { recursive: true, force: true }); + }); + + /** Run a check through the bar's own runCheck and return its output (the probe fails on purpose so the output is kept). */ + const probe = async (check: Partial): Promise => { + const ctx = { cwd: dir, record: [], ledger: [], archivedBatches: 0 } as unknown as BarContext; + const full: Check = { name: "task:probe", kind: "command", run: "true", timeoutMs: 20_000, expectExit: 0, tags: ["task"], hidden: true, ...check } as Check; + const r = await runCheck(full, ctx); + assert.equal(r.ok, false, "the probe check is built to fail so its output is kept"); + return r.output; + }; + + it("a task check runs as the check account, in its copy, with only PATH, HOME, LANG and TERM", async () => { + assert.ok(privSep()?.check, `no check account: ${privSep()?.checkProblem}`); + const out = await probe({ + run: + 'echo "uid=$(id -u)"; grep -q marker-77 out.txt && echo saw-work; echo "home=$HOME"; echo "homefiles=$(ls -A "$HOME" | wc -l)"; ' + + `test -e ${hostTmpMarker} && echo host-tmp-visible; echo "procs=$(ls /proc | grep -cE '^[0-9]+$')"; ` + + "echo ENV-BEGIN; env; echo ENV-END; exit 7", + }); + assert.match(out, new RegExp(`^uid=${cuid}$`, "m"), `the check did not run as the check account:\n${out}`); + assert.match(out, /^saw-work$/m, "the check could not read its copy of the work"); + assert.match(out, /^homefiles=0$/m, "HOME was not a fresh empty folder"); + assert.ok(!out.includes(`home=/home/${WORKER}`), "the check got the worker's HOME"); + const env = (out.split("ENV-BEGIN\n")[1] ?? "").split("\nENV-END")[0]!.split("\n").map((l) => l.split("=")[0]!).filter(Boolean); + // The shell itself adds PWD, SHLVL and _; nothing else may be there. + const allowed = new Set(["PATH", "HOME", "LANG", "TERM", "PWD", "OLDPWD", "SHLVL", "_"]); + assert.deepEqual(env.filter((k) => !allowed.has(k)), [], `variables beyond the allowlist reached the check: ${env.join(" ")}`); + for (const [k, v] of Object.entries(PLANTED)) { + assert.ok(!out.includes(k), `${k} reached the check`); + assert.ok(!out.includes(v), `the value of ${k} reached the check`); + } + if (REQUIRE_PIDNS) assert.ok(ps.checkNs, "MAAT_TEST_REQUIRE_PIDNS=1 but check runs got no namespace"); + if (ps.checkNs) { + assert.ok(!out.includes("host-tmp-visible"), "the check saw the host's /tmp: it has no private /tmp"); + const procs = Number(/^procs=(\d+)$/m.exec(out)?.[1]); + assert.ok(procs > 0 && procs < 10, `the check sees ${procs} processes: no PID namespace of its own`); + } + }); + + it("a project check, which runs in the project itself, runs as the worker", async () => { + const out = await probe({ name: "tests", tags: [], hidden: false, run: 'echo "uid=$(id -u)"; exit 7' }); + assert.match(out, new RegExp(`^uid=${wuid}$`, "m"), `a project check did not run as the worker:\n${out}`); + }); + + it("a mission assertion runs as the check account", async () => { + const contract = { version: 1, assertions: [{ id: "who", run: `test "$(id -u)" = ${cuid} && grep -q marker-77 out.txt` }] } as unknown as Contract; + const [r] = await runAssertions(contract, ["who"], dir); + assert.equal(r?.ok, true, r?.output); + }); + + it("the reference check's files are readable by the check account and not by the worker", () => { + const snap = snapshotProject(dir); + assert.ok(snap, "no snapshot"); + assert.ok(snap.dir.startsWith(ps.checkRoot!), `the reference folder ${snap.dir} is not in the check folder ${ps.checkRoot}`); + writeFileSync(join(snap.dir, "check.py"), "SEALED = 'reference-marker-99'\n"); + ps.shareWithCheck(snap.dir); + const read = asWorker(`cat '${join(snap.dir, "check.py")}'`); + assert.notEqual(read.status, 0, "the worker read the reference check"); + assert.ok(!read.out.includes("reference-marker-99"), "the worker read the reference check"); + assert.match(read.out, /Permission denied/); + assert.notEqual(asWorker(`ls '${snap.dir}'`).status, 0, "the worker listed the reference folder"); + assert.notEqual(asWorker(`ls '${ps.checkRoot}'`).status, 0, "the worker listed the check folder"); + assert.notEqual(asWorker(`cd '${ps.checkRoot}'`).status, 0, "the worker entered the check folder"); + const asC = asChecker(`cat '${join(snap.dir, "check.py")}'`); + assert.equal(asC.status, 0, asC.out); + assert.match(asC.out, /reference-marker-99/); + }); + + it("puts the isolation in effect in the receipt and the journal", async () => { + const ws = project(wuid); + disablePrivSep(); + const ps2 = enablePrivSep({ user: WORKER, project: ws, helper: HELPER, checkUser: CHECKER }); + roots.push(ps2.stateRoot); + try { + const line = isolationLine(); + assert.equal(line, ps2.isolation()); + assert.match(line!, new RegExp(`^isolation: worker uid ${wuid}, check uid ${cuid}, pid namespace (on|off)`)); + if (REQUIRE_PIDNS) assert.equal(line, `isolation: worker uid ${wuid}, check uid ${cuid}, pid namespace on`); + const provider = scriptedProvider([{ calls: [{ name: "write_file", args: { path: "out.txt", content: "hello\n" } }] }, { text: "Done." }]); + const journal = new Journal(ws); + const receipts = new Receipts(ws); + const engine = new Engine({ baseUrl: "http://provider.test/v1", model: "m", cwd: ws, fetchFn: provider.fetchFn, bar: null, stream: false, autonomy: "high", journal, receipts }); + const check: Check = { name: "task:out", kind: "command", run: `grep -q hello out.txt && test "$(id -u)" = ${cuid}`, timeoutMs: 10_000, expectExit: 0, tags: ["task", "value"], hidden: true }; + const events: EngineEvent[] = []; + for await (const ev of engine.run("write out.txt saying hello", allowAll, { taskChecks: [check] })) events.push(ev); + const end = events.find((e) => e.kind === "job_end"); + assert.ok(end && end.kind === "job_end" && end.outcome === "verified", `outcome ${end && end.kind === "job_end" ? end.outcome : "none"}`); + const rdir = join(ps2.stateRoot, "receipts"); + const md = readdirSync(rdir).filter((f) => f.endsWith(".md")); + assert.ok(md.length > 0, "no receipt"); + for (const f of md) assert.ok(readFileSync(join(rdir, f), "utf8").split("\n").includes(line!), `${f} does not carry the isolation line`); + const log = readFileSync(journal.path, "utf8").split("\n").filter((l) => l.includes(line!)); + assert.equal(log.length, 1, "the journal must carry the isolation line exactly once"); + } finally { + disablePrivSep(); + rmSync(ws, { recursive: true, force: true }); + } + }); + + it("has a PID namespace for the worker and each check run where the container allows it", { skip: REQUIRE_PIDNS || nsAvailable ? false : "no CAP_SYS_ADMIN here (CI's Linux job requires it)" }, () => { + const p = enable(); + assert.ok(p.pidns, "no PID namespace for the worker"); + assert.ok(p.checkNs, "no PID namespace for check runs"); + assert.match(p.isolation(), /pid namespace on$/); + }); + + it("--worker-strict refuses to start when no PID namespace can be made", () => { + // No unshare on PATH stands in for a container without CAP_SYS_ADMIN; + // without the capability, the plain call below refuses too. + const emptyBin = mkdtempSync(join(tmpdir(), "maat-nobin-")); + const path = process.env.PATH; + disablePrivSep(); + try { + process.env.PATH = emptyBin; + assert.throws(() => enablePrivSep({ user: WORKER, project: dir, helper: HELPER, checkUser: CHECKER, strict: true }), /--worker-strict.*PID namespace/); + assert.equal(privSep(), undefined); + assert.equal(isolationLine(), undefined); + // Without --worker-strict the same machine carries on and says so. + const p = enablePrivSep({ user: WORKER, project: dir, helper: HELPER, checkUser: CHECKER }); + roots.push(p.stateRoot); + assert.equal(p.pidns, false); + assert.equal(isolationLine(), `isolation: worker uid ${wuid}, check uid ${cuid}, pid namespace off`); + } finally { + process.env.PATH = path; + disablePrivSep(); + rmSync(emptyBin, { recursive: true, force: true }); + for (const d of readdirSync("/var/lib/maat")) if (d.endsWith(".check") && !existsSync(join("/var/lib/maat", d.slice(0, -6)))) rmSync(join("/var/lib/maat", d), { recursive: true, force: true }); + } + if (!nsAvailable) { + assert.throws(() => enablePrivSep({ user: WORKER, project: dir, helper: HELPER, checkUser: CHECKER, strict: true }), /--worker-strict.*PID namespace/); + } + // And it refuses without a check account at all. + assert.throws(() => enablePrivSep({ user: WORKER, project: dir, helper: HELPER, strict: true }), /--worker-strict needs a check account/); + // The CLI turns the refusal into exit 2 before any work starts. + const cli = fileURLToPath(new URL("../src/cli.js", import.meta.url)); + const r = spawnSync(process.execPath, [cli, "run", "--worker-user", WORKER, "--check-user", CHECKER, "--worker-strict", "--cwd", dir, "say hi"], { + encoding: "utf8", + env: { ...process.env, PATH: "/nonexistent-maat-bin", MOLT_API_KEY: "x" }, + timeout: 30_000, + }); + assert.equal(r.status, 2, `${r.stdout}${r.stderr}`); + assert.match(r.stderr, /--worker-strict.*PID namespace/); + }); + + it("refuses a check account that is the worker, or shares the worker's group", () => { + disablePrivSep(); + assert.throws(() => enablePrivSep({ user: WORKER, project: dir, helper: HELPER, checkUser: WORKER, strict: true }), /is the worker/); + // Not strict: the job goes on with checks as Maat, and the line says so. + const p = enablePrivSep({ user: WORKER, project: dir, helper: HELPER, checkUser: WORKER }); + roots.push(p.stateRoot); + assert.equal(p.check, undefined); + assert.match(p.isolation(), /check uid none \(task checks run as Maat, uid 0\)/); + disablePrivSep(); + }); +}); + describe("privilege separation off (the default)", () => { it("changes nothing: records in the project, commands and files as Maat's own user", async () => { assert.equal(privSep(), undefined); @@ -483,6 +711,21 @@ describe("privilege separation off (the default)", () => { for (const a of ACP_AGENTS) for (const k of a.workerCredentialEnv ?? []) assert.match(k, new RegExp(`^${a.name}_`, "i")); }); + it("reads the check account from --check-user or MAAT_CHECK_USER", () => { + const saved = process.env.MAAT_CHECK_USER; + try { + delete process.env.MAAT_CHECK_USER; + assert.equal(checkUserFrom(), undefined); + assert.equal(checkUserFrom(" checker "), "checker"); + process.env.MAAT_CHECK_USER = "from-env"; + assert.equal(checkUserFrom(), "from-env"); + assert.equal(isolationLine(), undefined, "no isolation line unless separation was asked for"); + } finally { + if (saved === undefined) delete process.env.MAAT_CHECK_USER; + else process.env.MAAT_CHECK_USER = saved; + } + }); + it("refuses a worker user that cannot be switched to", () => { if (process.platform === "win32") return; assert.throws(() => enablePrivSep({ user: "maat-no-such-user-xyz", project: tmpdir(), helper: HELPER }), /no such user/); From 2a87b03b8be02227335b6dcb287b42f85e73cb1a Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 14:56:42 -0700 Subject: [PATCH 7/9] CI: privsep-linux runs with CAP_SYS_ADMIN and requires the namespace tests The container gets --cap-add=SYS_ADMIN (and AppArmor unconfined, whose Docker default denies mount even with the capability), and MAAT_TEST_REQUIRE_PIDNS=1 makes the PID namespace assertions fail there instead of skipping. --- .github/workflows/check.yml | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index dfe615f..e7d1b46 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -32,14 +32,23 @@ jobs: # doctor may exit >1 when default localhost endpoint is down (no Ollama in CI) node dist/cli.js doctor || true - # Privilege separation (--worker-user) is Linux-first and needs root to - # switch users: its tests skip on macOS and run here, in a root container. - # The test creates its own worker user. + # Privilege separation (--worker-user, --check-user, --worker-strict) is + # Linux-first and needs root to switch users: its tests skip on macOS and + # run here, in a root container. The test creates its own worker and check + # users. The container gets CAP_SYS_ADMIN, so the worker's PID namespace and + # each check run's PID namespace and private /tmp can be made, and + # MAAT_TEST_REQUIRE_PIDNS=1 makes the namespace tests fail here rather than + # skip when they are not. Docker's default AppArmor profile on Ubuntu hosts + # denies mount(2) (needed for the namespace's own /proc and the check's + # tmpfs /tmp) even with the capability, so it is lifted for this container. privsep-linux: runs-on: ubuntu-latest - container: node:22-bookworm + container: + image: node:22-bookworm + options: --cap-add=SYS_ADMIN --security-opt apparmor=unconfined env: ELECTRON_SKIP_BINARY_DOWNLOAD: "1" + MAAT_TEST_REQUIRE_PIDNS: "1" steps: - uses: actions/checkout@v4 - run: npm ci From 47da67172a289c114f9adf5087ebdaa03476b4fa Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 14:56:42 -0700 Subject: [PATCH 8/9] Bench: checker account; crosscheck grades a sanitized copy - The bench image adds `checker` (uid 1002, own group, no home) beside `agent`; image tags move to :agentcu so older images without it are not reused. run.py passes --check-user checker (BENCH_CHECK_USER) when privilege separation is on. - crosscheck.py no longer sets safe.directory=*. The grader runs through run.grade_safely and each check on a copy from run.sanitized_copy (the helper grading_copy now uses), so a tree's planted git config never runs. - Test: a hostile repository's fsmonitor, filters, hooks, pager, alias and include fire nothing during a crosscheck grade and check run, and no git config is put in the environment. --- bench/local/container/Dockerfile | 6 +- bench/local/container/Dockerfile.grok | 2 +- bench/local/container/Dockerfile.opencode | 2 +- bench/local/container/run-in-container.sh | 12 ++-- bench/local/crosscheck.py | 21 ++++--- bench/local/run.py | 55 ++++++++++++------- .../local/tests/test_crosscheck_sanitized.py | 51 +++++++++++++++++ bench/local/tests/test_privsep_cmd.py | 5 +- 8 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 bench/local/tests/test_crosscheck_sanitized.py diff --git a/bench/local/container/Dockerfile b/bench/local/container/Dockerfile index b0caf55..b872ae4 100644 --- a/bench/local/container/Dockerfile +++ b/bench/local/container/Dockerfile @@ -12,7 +12,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ # keeps the graders and the reference solutions in root-only places it cannot read; /root (the # judge's HOME) is 700. RUN useradd -m -u 1001 -s /bin/bash agent && chmod 700 /root -ENV BENCH_AGENT_USER=agent +# Task checks (hidden, drafted, mission) run as this user (--check-user checker): its own group, +# no home (Maat gives each check run a fresh empty HOME). It can read the reference check's +# files and its own copy of the work; `agent` cannot read the reference check. +RUN useradd -M -U -u 1002 -s /bin/sh checker +ENV BENCH_AGENT_USER=agent BENCH_CHECK_USER=checker WORKDIR /opt # Maat itself is installed per build by run-in-container.sh (FROM maat-bench + npm i -g the packed tarball). diff --git a/bench/local/container/Dockerfile.grok b/bench/local/container/Dockerfile.grok index 8f7726e..20d0a37 100644 --- a/bench/local/container/Dockerfile.grok +++ b/bench/local/container/Dockerfile.grok @@ -2,7 +2,7 @@ # Installed with the official script (https://x.ai/cli/install.sh), pinned to the version the # owner's Mac runs so ACP behaviour matches. No credentials are baked in: run-in-container.sh # copies them in at start (SUBSCRIPTION=grok). -FROM maat-bench:agentu +FROM maat-bench:agentcu ARG GROK_VERSION=1.0.46 RUN curl -fsSL https://x.ai/cli/install.sh | HOME=/opt/grok-install GROK_BIN_DIR=/opt/grok-install/.grok/bin bash -s ${GROK_VERSION} \ && cp -L "$(readlink -f /opt/grok-install/.grok/bin/grok)" /tmp/grok.bin && rm -f /usr/local/bin/grok /usr/local/bin/agent && mv /tmp/grok.bin /usr/local/bin/grok && rm -rf /opt/grok-install \ diff --git a/bench/local/container/Dockerfile.opencode b/bench/local/container/Dockerfile.opencode index efc6e01..d475c2b 100644 --- a/bench/local/container/Dockerfile.opencode +++ b/bench/local/container/Dockerfile.opencode @@ -3,7 +3,7 @@ # (run-in-container.sh passes BASE), so OPENCODE=1 combines with SUBSCRIPTION=grok. Pinned to the # version the owner's Mac runs. No credentials are baked in: run-in-container.sh copies # auth.json in at start. -ARG BASE=maat-bench:agentu +ARG BASE=maat-bench:agentcu FROM ${BASE} ARG OPENCODE_VERSION=1.18.33 RUN npm install -g --no-fund --no-audit opencode-ai@${OPENCODE_VERSION} && opencode --version diff --git a/bench/local/container/run-in-container.sh b/bench/local/container/run-in-container.sh index 3bbc5c4..eae310d 100755 --- a/bench/local/container/run-in-container.sh +++ b/bench/local/container/run-in-container.sh @@ -22,7 +22,8 @@ # ARMS="oc:MAAT_JUDGE_MODEL=opencode/big-pickle,MAAT_JUDGE_URL=opencode://zen" # The key is never printed. # Privilege separation (default; BENCH_PRIVSEP=0 turns it off): Maat runs as root and only the -# worker's tools run as `agent` (--worker-user agent). Maat's records sit in /var/lib/maat (700, +# worker's tools run as `agent` (--worker-user agent); task checks run as `checker` +# (--check-user checker), which can read the reference check and `agent` cannot. Maat's records sit in /var/lib/maat (700, # root) until each job ends; the judge's logins are in /root (700); finished task folders and # logs in /work are locked to root. BENCH_PIDNS=1 (default) also gives the container # CAP_SYS_ADMIN so Maat can put the worker's commands in their own PID namespace with a private @@ -31,11 +32,12 @@ set -e name=$1; tgz=${2:A}; shift 2; [ "$1" = "--" ] && shift here=${0:A:h}; local_dir=${here:h} -# Image tags carry ":agentu": images with the unprivileged `agent` user (older ones lack it). -sha=$(shasum -a 256 "$tgz" | cut -c1-8); base=maat-bench:agentu; img=maat-bench:$sha-u +# Image tags carry ":agentcu": images with the unprivileged `agent` (worker) and `checker` (task +# checks, --check-user) users. Older ":agentu" images lack `checker`. +sha=$(shasum -a 256 "$tgz" | cut -c1-8); base=maat-bench:agentcu; img=maat-bench:$sha-cu docker image inspect $base >/dev/null 2>&1 || docker build -q -t $base "$here" >/dev/null if [ "$SUBSCRIPTION" = grok ]; then - base=maat-bench-grok:agentu; img=maat-bench-grok:$sha-u + base=maat-bench-grok:agentcu; img=maat-bench-grok:$sha-cu docker image inspect $base >/dev/null 2>&1 || docker build -q -t $base -f "$here/Dockerfile.grok" "$here" >/dev/null [ -f ~/.grok/auth.json ] || { echo "no ~/.grok/auth.json: run 'grok login' on the Mac first" >&2; exit 1; } # The token is short-lived (~6 h) and a refresh inside the container may rotate the refresh @@ -45,7 +47,7 @@ if [ "$SUBSCRIPTION" = grok ]; then fi if [ "$OPENCODE" = 1 ]; then [ -f ~/.local/share/opencode/auth.json ] || { echo "no ~/.local/share/opencode/auth.json: run 'opencode auth login' on the Mac first" >&2; exit 1; } - ocbase=${base%:*}-oc:agentu + ocbase=${base%:*}-oc:agentcu docker image inspect $ocbase >/dev/null 2>&1 || docker build -q -t $ocbase -f "$here/Dockerfile.opencode" --build-arg BASE=$base "$here" >/dev/null base=$ocbase; img=${img%:*}-oc:${img##*:} fi diff --git a/bench/local/crosscheck.py b/bench/local/crosscheck.py index b2b5f5f..018e248 100644 --- a/bench/local/crosscheck.py +++ b/bench/local/crosscheck.py @@ -54,28 +54,31 @@ # ------------------------------------------------------------------ inside the container -def inside() -> None: +def inside(job_path: str = "/job.json", src: str = "/src", work: str = "/work") -> None: sys.path.insert(0, str(HERE)) - job = json.load(open("/job.json")) + job = json.load(open(job_path)) from tasks import TASKS # noqa: PLC0415 from tasks2 import TASKS2 # noqa: PLC0415 + import run # noqa: PLC0415 (the grading helpers run.py grades with) + by = {T.name: T for T in TASKS + TASKS2} - os.environ["GIT_CONFIG_COUNT"] = "1" - os.environ["GIT_CONFIG_KEY_0"] = "safe.directory" - os.environ["GIT_CONFIG_VALUE_0"] = "*" - dst = Path("/work") / job["name"] + # No safe.directory=*: the trees are agent-written, and their .git/config (core.fsmonitor, + # hooks, filters, diff drivers) would run as root. The grader grades a sanitized root-owned + # copy exactly as run.py does (run.grade_safely: config cut to the format lines, hooks and + # attributes removed, git started without system or global config). The checks run on a + # root-owned copy whose repositories are sanitized the same way (run.sanitized_copy). + dst = Path(work) / job["name"] def fresh() -> Path: shutil.rmtree(dst, ignore_errors=True) dst.parent.mkdir(parents=True, exist_ok=True) - subprocess.run(["cp", "-R", "--preserve=mode,timestamps", "/src", str(dst)], check=True) - return dst + return run.sanitized_copy(Path(src), dst) res: dict = {"name": job["name"], "checks": {}} try: d = fresh() - ok, why = by[job["task"]].grade(d) + ok, why = run.grade_safely(by[job["task"]], d) res["grade"] = [bool(ok), str(why)[:300]] except Exception as e: # noqa: BLE001 res["grade"] = [None, f"grader crashed: {e}"[:300]] diff --git a/bench/local/run.py b/bench/local/run.py index f2e6c27..45c2a11 100644 --- a/bench/local/run.py +++ b/bench/local/run.py @@ -65,6 +65,9 @@ def openrouter_key() -> str: # the reference program, the judge's HOME or its process list either. BENCH_PRIVSEP=0 runs the # whole of Maat as the agent user, as before 2026-10-07. PRIVSEP = bool(AGENT_USER) and os.environ.get("BENCH_PRIVSEP", "1") != "0" and os.geteuid() == 0 +# With privilege separation, task checks (hidden, drafted, mission) run as a third account +# (--check-user): they can read the reference check, and the worker cannot. The image makes it. +CHECK_USER = os.environ.get("BENCH_CHECK_USER") or "checker" # The graders run as root. They never run git in the agent's own repository: the agent owns its # .git/config, and core.fsmonitor, hooks, filters and diff drivers there would run as root. Until @@ -116,33 +119,42 @@ def sanitize_git(git_dir: Path) -> None: shutil.rmtree(p) -def grading_copy(d: Path) -> Path: - """A root-owned copy of a finished task folder, for the graders. Regular files, folders and - symlinks (as links) only: a FIFO or device the agent left is skipped, not opened. Every - repository in it (.git folders) is sanitized. Call with the agent's processes stopped.""" - import tempfile - - def copy_regular(src, dst, *, follow_symlinks=True): - st = os.lstat(src) - if not stat.S_ISREG(st.st_mode): - return dst - fd = os.open(src, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)) - with os.fdopen(fd, "rb") as fsrc: - if not stat.S_ISREG(os.fstat(fsrc.fileno()).st_mode): - return dst - with open(dst, "wb") as fdst: - shutil.copyfileobj(fsrc, fdst) - shutil.copystat(src, dst, follow_symlinks=False) +def _copy_regular(src, dst, *, follow_symlinks=True): + """copytree's copy function: regular files only, opened without following a link and + without blocking on a FIFO; anything else is skipped.""" + st = os.lstat(src) + if not stat.S_ISREG(st.st_mode): return dst + fd = os.open(src, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)) + with os.fdopen(fd, "rb") as fsrc: + if not stat.S_ISREG(os.fstat(fsrc.fileno()).st_mode): + return dst + with open(dst, "wb") as fdst: + shutil.copyfileobj(fsrc, fdst) + shutil.copystat(src, dst, follow_symlinks=False) + return dst - dst = Path(tempfile.mkdtemp(prefix="maat-grade-")) / d.name - shutil.copytree(d, dst, symlinks=True, copy_function=copy_regular) + +def sanitized_copy(src: Path, dst: Path) -> Path: + """Copy an agent-written tree to `dst` (which must not exist), owned by whoever runs this: + regular files, folders and symlinks (as links) only, and every repository in it (.git + folders) sanitized (sanitize_git).""" + shutil.copytree(src, dst, symlinks=True, copy_function=_copy_regular) for root, dirs, _files in os.walk(dst): if ".git" in dirs and not os.path.islink(os.path.join(root, ".git")): sanitize_git(Path(root) / ".git") return dst +def grading_copy(d: Path) -> Path: + """A root-owned copy of a finished task folder, for the graders. Regular files, folders and + symlinks (as links) only: a FIFO or device the agent left is skipped, not opened. Every + repository in it (.git folders) is sanitized. Call with the agent's processes stopped.""" + import tempfile + + return sanitized_copy(d, Path(tempfile.mkdtemp(prefix="maat-grade-")) / d.name) + + def grade_safely(T, d: Path): """T.grade on a sanitized root-owned copy (grading_copy), with GRADER_GIT_ENV set.""" if AGENT_USER and shutil.which("pkill"): @@ -164,11 +176,12 @@ def grade_safely(T, d: Path): def as_agent(cmd: list, env: dict | None = None) -> tuple[list, dict | None]: """With BENCH_AGENT_USER set, run the agent as that user: it cannot read the graders or the reference solutions, which stay root-only. Unset (Mac host lanes): unchanged. - With privilege separation Maat stays root and is told which user the worker is.""" + With privilege separation Maat stays root and is told which user the worker is, and which + account runs the task checks.""" if not AGENT_USER: return cmd, env if PRIVSEP: - return [*cmd[:3], "--worker-user", AGENT_USER, *cmd[3:]], env + return [*cmd[:3], "--worker-user", AGENT_USER, "--check-user", CHECK_USER, *cmd[3:]], env import pwd home = pwd.getpwnam(AGENT_USER).pw_dir return ["runuser", "-u", AGENT_USER, "--", "env", f"HOME={home}", *cmd], env diff --git a/bench/local/tests/test_crosscheck_sanitized.py b/bench/local/tests/test_crosscheck_sanitized.py new file mode 100644 index 0000000..ed97904 --- /dev/null +++ b/bench/local/tests/test_crosscheck_sanitized.py @@ -0,0 +1,51 @@ +"""crosscheck.py grades and runs checks on sanitized copies, as run.py does (2026-10-07 review). + +The trees it re-grades are agent-written, so their .git/config can name programs for git to run +(core.fsmonitor, hooks, filters, an include). crosscheck used to set safe.directory=* and run +git on a plain copy. Now the grader goes through run.grade_safely and each check gets a copy +from run.sanitized_copy. The test plants every trap from test_grading_copy, runs a grader and a +check that both drive git, and asserts no trap fired and safe.directory was never set. +""" +import json +import os +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest + +HERE = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(HERE)) +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import crosscheck # noqa: E402 +import tasks # noqa: E402 +from test_grading_copy import hostile_repo, planted # noqa: E402 + +pytestmark = pytest.mark.skipif(not shutil.which("git"), reason="needs git") + +GIT = "git status --porcelain; git diff; git log -1; git add -A; git diff --cached; git checkout -q master" + + +class FakeTask: + name = "crosscheck-fake" + + def grade(self, d: Path): + subprocess.run(GIT, shell=True, cwd=d, capture_output=True) + return True, (Path(d) / "a.txt").read_text().strip() + + +def test_grader_and_checks_run_nothing_the_agent_planted(tmp_path, monkeypatch, capsys): + d, _ = hostile_repo(tmp_path) + monkeypatch.setattr(tasks, "TASKS", [*tasks.TASKS, FakeTask()]) + job = tmp_path / "job.json" + job.write_text(json.dumps({"name": "fake-molt-1", "task": FakeTask.name, "checks": [GIT + "; echo check-ran"]})) + before = {k: os.environ.get(k) for k in ("GIT_CONFIG_COUNT", "GIT_CONFIG_KEY_0", "GIT_CONFIG_VALUE_0")} + crosscheck.inside(str(job), str(d), str(tmp_path / "work")) + out = capsys.readouterr().out + res = json.loads(out.split("@@RESULT@@", 1)[1]) + assert res["grade"] == [True, "two"], res + assert "check-ran" in res["checks"][GIT + "; echo check-ran"]["out"] + assert planted(tmp_path) == [], f"a planted git command ran: {planted(tmp_path)}" + assert {k: os.environ.get(k) for k in before} == before, "inside() changed the git config environment" + assert "safe.directory" not in json.dumps(dict(os.environ)) diff --git a/bench/local/tests/test_privsep_cmd.py b/bench/local/tests/test_privsep_cmd.py index 68ecba6..9afcaa9 100644 --- a/bench/local/tests/test_privsep_cmd.py +++ b/bench/local/tests/test_privsep_cmd.py @@ -8,11 +8,12 @@ import run # noqa: E402 -def test_privsep_keeps_maat_root_and_names_the_worker(monkeypatch): +def test_privsep_keeps_maat_root_and_names_the_worker_and_the_check_account(monkeypatch): monkeypatch.setattr(run, "AGENT_USER", "agent") + monkeypatch.setattr(run, "CHECK_USER", "checker") monkeypatch.setattr(run, "PRIVSEP", True) cmd, _ = run.as_agent(["node", "cli.js", "run", "--url", "u", "the task"]) - assert cmd == ["node", "cli.js", "run", "--worker-user", "agent", "--url", "u", "the task"] + assert cmd == ["node", "cli.js", "run", "--worker-user", "agent", "--check-user", "checker", "--url", "u", "the task"] def test_without_privsep_the_whole_agent_runs_as_the_user(monkeypatch): From e7486c49beccfaea6397b439c5abee0931da7129 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 15:03:26 -0700 Subject: [PATCH 9/9] CI: run the privsep tests with HOME=/root The job container's HOME is /github/home, a runner bind mount readable by the test's worker user and not owned by root, which Maat refuses as the judge's HOME. Every root test in the job was cancelled by that refusal. --- .github/workflows/check.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index e7d1b46..b8f516a 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -53,4 +53,9 @@ jobs: - uses: actions/checkout@v4 - run: npm ci - run: npm run test:build && npm run build + # The job container's HOME is /github/home, a runner bind mount the + # worker can read and root does not own, so Maat (correctly) refuses it + # as the judge's HOME. root's own HOME is private. - run: node --test --test-reporter=spec dist-test/test/privsep.test.js + env: + HOME: /root