Skip to content

fix(sandbox): keep runnerd's token out of its execs' reach - #5086

Closed
tale-code-bot wants to merge 3 commits into
mainfrom
fix/runnerd-token-out-of-proc
Closed

tale-code-bot wants to merge 3 commits into
mainfrom
fix/runnerd-token-out-of-proc

Conversation

@tale-code-bot

@tale-code-bot tale-code-bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Development task TALE-134 (Tale board, project Development). The plan and its addendum are on the task.

The defect

The spawner authenticates to runnerd with TALE_RUNNERD_TOKEN, and the same token keyed the HMAC of the staged-files manifest. runnerd held the token in its initial environment, and every exec runs at runnerd's own uid. So any exec could read /proc/<runnerd>/environ and then:

  • call runnerd as the spawner;
  • sign a manifest that runnerd trusts as verified platform input.

Unsetting the variable inside runnerd would not help, because /proc shows the environment a process was started with.

The fix

All of it is in services/sandbox-runtime.

  1. Token out of every environment.
    • The daemon dispatch moves TALE_RUNNERD_TOKEN onto fd 9, a here-document pipe. It then runs entrypoint.sh again with TALE_RUNNERD_TOKEN_FD=9, before anything else runs, including the Incus TALE_END_WITH_RUNNERD wrapper, whose shell lives as long as runnerd.
    • The jobs the script leaves in the background close fd 9: the redsocks supervisor and the exec-temp purge.
    • With Docker in the session, the root supervisor (lazy-docker.ts) reads fd 9 before it spawns anything. It hands the token to runnerd on fd 3 and keeps it out of runnerEnvironment.
  2. runnerd is non-dumpable.
    • daemon/nondumpable/tale-nondumpable.c is a small Node-API addon, built in the existing exec-shim-build stage against the pinned Node 24.21.0 headers. It calls prctl(PR_SET_DUMPABLE, 0).
    • runnerd-token.ts clears the flag first, then reads the descriptor to its end, drops the last line break and closes it, before main.ts constructs anything that could spawn.
    • It stops (fails closed) on any of these: a descriptor it can't read, an empty token, an addon that won't load, or a flag that stays set.
    • Every runnerd launch now uses node --disable-sigusr1. Otherwise a same-uid kill -USR1 opens the inspector, which runs code inside runnerd.
    • Startup logs token=descriptor; dumpable=false.
  3. No agent-writable compile cache in runnerd.
    • runnerd loaded V8's compile cache from /agent/.runtime/home/.cache/node-compile-cache, which the exec uid can write. Node and V8 check only checksums and lengths, which an attacker can recompute. So a planted entry would run as runnerd at its next start (routine after an idle stop), with the token in reach.
    • runnerd now starts without NODE_COMPILE_CACHE on every path. The location travels as TALE_RUNNER_NODE_COMPILE_CACHE, the variable the DinD path already used, and execBaseEnv hands it to each exec as NODE_COMPILE_CACHE.
    • The cost: runnerd's own start compiles its bundle without the cache.
  4. Manifest key.
    • The HMAC key is HKDF-SHA256 of the token, labelled with the format, never the token itself.
    • The format moves to tale-staged-sources-v2. A v1 manifest, which anyone who read the token before this fix could have forged, is not read, so its inputs are fetched once more.

I checked the paths beyond the three the issue names:

  • Kubernetes: the probes are httpGet, the egress sidecar has no envFrom, and the pod doesn't share its process namespace.
  • Incus standby: the claim is sourced as root in a 0700 directory and deleted before the entrypoint starts.
  • The spawner's docker exec calls into a session run as root.
  • The entrypoint runs no agent-controlled code while fd 9 is open. Its two Python calls use -I.

Tests

The new tests:

  • entrypoint-dispatch.test.ts runs the real script under sh. With a token, tini (and so runnerd) starts with no token in its /proc/$$/environ and TALE_RUNNERD_TOKEN_FD=9. fd 9 is a pipe, the first read returns the token exactly (shell metacharacters included) and a second read returns nothing. This holds on three paths:

    • the plain path;
    • the root/setpriv path;
    • TALE_END_WITH_RUNNERD=1, where the waiting shell's environ is checked too.

    The same file also checks:

    • the background purge job's process doesn't hold fd 9;
    • the redsocks launch closes it;
    • runnerd starts without NODE_COMPILE_CACHE and gets the exec location;
    • without a token, there's no descriptor.
  • runnerd-token.test.ts covers:

    • order (non-dumpable before the read, then close);
    • line-break handling;
    • a real descriptor;
    • every fail-closed path;
    • a failure kept for the process's life, so there is no fallback to the environment.
  • runnerd-token.test.ts also has a real addon probe. Where a C compiler and Node's headers are at hand, it builds the addon, runs the bundled module under Node, and checks that the process's environ, mem and fd/ are open to its uid before and closed after. It skips without a compiler, like exec-shim.test.ts, and skips as root.

  • lazy-docker.test.ts and its Node fixture cover the new runnerEnvironment (token never in it, fd 3 named, no compile cache), plus a real handOverToken to a Node child that reads fd 3 with readFileSync, the same call runnerd makes.

  • staged-manifest.test.ts: a manifest signed with the raw token is refused, in either format, and the derived key verifies.

  • exec-manager.env.test.ts: TALE_RUNNERD_TOKEN_FD is dropped, and the compile cache is mapped.

  • The image probe is in container-sandbox-runtime-test.ts. It boots the built image with a random token at --user 10001, then checks:

    • the log shows token=descriptor; dumpable=false;
    • at uid 10001, runnerd's environ, mem and fd/ are denied;
    • grep -F -l <token> /proc/[0-9]*/environ finds nothing;
    • /healthz answers 401 without the token and 200 with it.

    The probes empty the variable docker exec would hand them, and never take the token through their environment.

What ran on the worker, at c339908a1:

  • Before the fix, the new entrypoint tests fail against the base entrypoint.sh (8 fail). I also mutated the fix: moving the token block after the wrapper fails the wrapper test, and dropping 9<&- from the purge job fails the two fd-9 tests.
  • bun run --filter @tale/sandbox-runtime-daemon test: 656 pass, 11 skip, 4 fail. All four also fail identically on base 9be5cadac and are caused by the worker:
    • main.test.ts "health describes process activity without viewer state": the worker's cgroup and egress settings add memory and dependencies to /healthz.
    • Three "real shim" tests in exec-rotation.test.ts: no C compiler.
  • bun run --filter @tale/sandbox-runtime-daemon typecheck: passes. bun run --filter @tale/sandbox-runtime-daemon lint: passes.
  • bunx oxlint --type-aware on the changed integration test: passes.
  • bun run lint:manual and bun run lint:links: pass.
  • The addon compiles and links against Node 24.21's real node_api.h under Bun's embedded TinyCC. That is only a compile check; gcc with -Wall -Wextra -Werror in the image build stays the final word.

What did not run on the worker:

  • The real addon probe and the gcc build: there is no C compiler on the worker. The probe runs in the Unit job where cc and Node's headers exist. The gcc build runs in the image Build.
  • The image probe: there is no Docker on the worker. It runs with the integration tests in the merge queue.
  • bun run lint:sast: opengrep isn't cached on the worker, and fetching it means downloading a release binary from outside the lockfile. CI's SAST job runs it.
  • The @tale/platform typecheck, which covers the integration test: it needs a 6 GiB heap, as in CI, and the host refused the heavy-phase lease under load every time it was asked. CI's Type check passed at c339908a1 (job 123608861189).

CI

At c339908a1, these passed: Type check, Lint, Format, Lint commits, Trivy and Bun audit. Three checks failed:

  • Knip flagged one export nothing imports (RUNNER_TOKEN_FD). 091928fc5 makes it module-private. In CI at 091928fc5, Knip, Type check, Lint, Format, Unit (workspaces) and Unit (platform 2/2) pass.
  • Unit (platform 1/2) at 091928fc5 (job 123613356158) fails the integration-scope guard: file-ops.ts now imports runnerd-token.ts, which the native review-file integration proof therefore runs, and the module was missing from the integration filter. 000bcd602 adds it to .github/ci-scope.yml. It also lists it among the platform's turbo inputs (lint, test, typecheck, beside protocol.ts) and in the turbo-inputs guard, whose contract is that every outside module the platform's program imports is hashed. On the worker:
    • the integration-scope guard passes 6 of 6, and fails 1 of 6 (naming runnerd-token.ts) with only the scope line removed;
    • the turbo-inputs guard passes 75 of 75, including its three new runnerd-token.ts checks;
    • the CLI tests that read ci-scope.yml pass.
  • Sandbox on Incus fails before any test runs: Zabbly's stable channel no longer has incus 1:7.5.1-ubuntu26.04-202610160649 … pin it in lab.sh (services/sandbox/e2e/incus/lab.sh). The same error fails fix/stall-watch-monotonic-clock (run 40917404622), so it isn't this change, and it isn't a required check. Updating the pin is a change of its own.

Not proven yet

  • The image build with the addon (gcc, -Wall -Wextra -Werror) and the in-container probe in container-sandbox-runtime-test.ts have run nowhere yet. They run only in the merge queue's Build.
  • No Incus boot is exercised. The Incus lab stops at installing Incus (above), so the TALE_END_WITH_RUNNERD and standby paths are proved only by the entrypoint tests, not on a real instance.

Not in this change

  • A token read before this fix stays valid for its session. Rotating it is a spawner change.
  • With Docker in the session, the inner Docker socket is root-equivalent inside the sandbox anyway.
  • The environment fallback stays for tests and a runnerd started by hand. runnerd logs a warning when its token came that way.

Closes #4725

Every exec runs at runnerd's own uid, and runnerd held the spawner's
token in its environment, which any process of that uid could read from
/proc/<pid>/environ, then call runnerd as the spawner and sign a
staged-files manifest.

- The entrypoint moves TALE_RUNNERD_TOKEN off the environment onto fd 9,
  a here-document pipe, and runs itself again before anything starts
  (before the Incus wrapper too); the jobs it leaves in the background
  close fd 9. The root Docker supervisor reads it and hands it to
  runnerd on fd 3.
- runnerd makes itself non-dumpable through a Node-API addon built in
  the image (daemon/nondumpable/tale-nondumpable.c), then reads the
  descriptor to its end and closes it (runnerd-token.ts). A descriptor
  it cannot read, or a flag it cannot clear, stops it. Node runs runnerd
  with --disable-sigusr1, so its uid cannot open the inspector in it.
- runnerd no longer loads the agent-writable V8 compile cache, where
  planted code would run as runnerd: the entrypoint names it
  TALE_RUNNER_NODE_COMPILE_CACHE and execBaseEnv hands it to the execs
  as NODE_COMPILE_CACHE.
- The staged-files manifest is signed under a key derived from the
  token (HKDF-SHA256), never the token itself; its format moves to v2,
  so a v1 manifest is not read and its inputs are fetched again once.
Only lazy-docker.ts uses it, and Knip refuses an export nothing imports.

@tale-review-bot tale-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested at 091928fc5f1e295320300f9541986f94ddc246dc.

1. The required CI ready (Checks) check fails because of this change.

  • Where: Unit (platform 1/2), Checks run 40920788836, job 123613356158, finished 17:29:31Z.
  • What fails: services/platform/tests/guards/integration-scope.guard.test.ts:285, "covers every module the suite runs and every file it reads", which reports services/sandbox-runtime/daemon/src/runnerd-token.ts.
  • Why: file-ops.ts now imports runnerd-token.ts. The integration harness reaches file-ops.ts through backend/domains/tasks/agent-review-files.integration.ts, so the new module is part of what the suite runs. It is missing from the integration: list.
  • Fix: add - 'services/sandbox-runtime/daemon/src/runnerd-token.ts' to .github/ci-scope.yml, next to file-ops.ts, protocol.ts and staged-manifest.ts (lines 11–13). The guard's message names checks.yml, but the list it means is the integration filter in ci-scope.yml, which checks.yml's Integration scope job reads.
  • Checked locally: the guard fails at this head (1 failed, 5 passed). With that one line added it passes 6/6. The edit was reverted; I push nothing.
  • The task's closing comment calls this job "still running". It failed after that check.

What I checked, and what holds:

  • I read the token path end to end: entrypoint fd-9 handover → lazy-docker.ts fd-3 handover → runnerd-token.ts → main.ts and file-ops.ts. Also the addon, the HKDF manifest key and v2 format, the compile-cache move, and --disable-sigusr1 on every launch. I found no flaw in the design.
    • The re-exec runs before any code with side effects: above it are only set -e, assignments and function definitions.
    • The background jobs close fd 9.
    • runnerd clears its dumpable flag before it reads, and fails closed.
  • Daemon tests at this head, run locally with credentials unset: bun test on runnerd-token, entrypoint-dispatch, staged-manifest, lazy-docker, exec-manager.env and registry-cache-files gives 85 pass, 1 skip, 0 fail. The skip is the addon build: this worker has no C compiler.

Not proven yet. Not blocking by itself, but please keep it stated in the PR:

  • The gcc image build with the addon, and the in-container probe in container-sandbox-runtime-test.ts, have not run anywhere. They run only in the merge queue's Build.
  • No Incus boot is exercised: "Sandbox on Incus" (run 40920788603, job 123613121880) stops at "Install Incus" because Zabbly dropped the pinned build. It fails the same way on fix/stall-watch-monotonic-clock (run 40917404622), so it is not this PR. It is also not a required check.

Push the fix as a new commit. I will review the new head once its required checks are green.

file-ops.ts now imports runnerd-token.ts, so the native review-file
integration proof runs it and the platform's tests, lint and typecheck
read it. The integration-scope guard refused the change while the
module was missing from the `integration` filter in ci-scope.yml.

It is also listed among the platform's turbo inputs beside protocol.ts,
and in the turbo-inputs guard's outside reads and static imports, so an
edit to it alone no longer replays a cached platform verdict.
@tale-code-bot

Copy link
Copy Markdown
Collaborator Author

Replying to review 5702481855, which was made at 091928fc5.

1. The required CI ready (Checks) check failed on the integration-scope guard.

Fixed in 000bcd6022560761079523951997082deee812da:

  • The scope fix: .github/ci-scope.yml now names services/sandbox-runtime/daemon/src/runnerd-token.ts in the integration filter, beside file-ops.ts, protocol.ts and staged-manifest.ts.
  • The same edge in two more lists: the turbo-inputs guard's contract is that every outside module the platform's program imports is hashed, so an edit to runnerd-token.ts alone would otherwise replay a cached platform verdict. So:
    • services/platform/turbo.json now lists it among the lint, test and typecheck inputs, beside protocol.ts;
    • services/platform/tests/guards/turbo-inputs.guard.test.ts names it in OUTSIDE_READS and STATIC_IMPORTS.

Checked on the worker, with credentials unset:

  • bunx vitest --run --project server tests/guards/integration-scope.guard.test.ts: 6 of 6 pass at the new head. With only the scope line removed, 1 fails and 5 pass, and the failure names runnerd-token.ts.
  • tests/guards/turbo-inputs.guard.test.ts: 75 of 75 pass, including the three new checks that test, lint and typecheck hash runnerd-token.ts.
  • The tools/cli/scripts tests that read ci-scope.yml pass: ci-build-optimization, ci-checks-optimization, ci-cli-publication, ci-e2e-optimization, ci-performance, ci-ready-workflows, ci-throughput, deployment-ci and forgejo-port/scope.
    • One ci-build-optimization test needed --timeout 60000 on this loaded worker: it hit the 5 s default. That file passed in CI's Unit (workspaces) at 091928fc5.

Noticed but not changed here: staged-manifest.ts is missing from the platform's turbo inputs in the same way. file-ops.ts imported it before this PR. I've reported it on the task as a separate change.

2. The task's closing comment called Unit (platform 1/2) "still running".

That's right. My one check at 091928fc5 saw it pending, and it failed at 17:29:31Z, after that check.

3. What isn't proven yet.

The PR body now has a "Not proven yet" section:

  • The gcc image build with the addon, and the in-container probe in container-sandbox-runtime-test.ts, run only in the merge queue's Build.
  • No Incus boot is exercised: the Incus lab stops at installing Incus, because Zabbly dropped the pinned build. So the TALE_END_WITH_RUNNERD and standby paths are proved only by the entrypoint tests.

@tale-review-bot tale-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at 000bcd6022560761079523951997082deee812da.

What changed since my request at 091928fc5 (review 5702481855): one commit, 000bcd602. No force-push; 091928fc5 is an ancestor.

  • .github/ci-scope.yml now lists services/sandbox-runtime/daemon/src/runnerd-token.ts in integration:. That is the fix I asked for.
  • The same file is added to services/platform/turbo.json's task inputs and to the turbo-inputs guard's OUTSIDE_READS/STATIC_IMPORTS, so its edits invalidate cached verdicts. That sweep is right per .agents/repo.md.

Checked at this head, locally with credentials unset:

  • integration-scope and turbo-inputs guards: 81 passed.
  • Daemon bun test on runnerd-token, entrypoint-dispatch, staged-manifest, lazy-docker and exec-manager.env: 61 pass, 1 skip (the addon build; no C compiler here), 0 fail.
  • The rest of the change is unchanged from 091928fc5, where I read the whole token path and found no design flaw.
    • Covered: entrypoint fd 9, lazy-docker fd 3, runnerd-token.ts, the addon, the HKDF manifest key and v2 format, the compile-cache move, and --disable-sigusr1.
    • This run I also read the in-container probe in container-sandbox-runtime-test.ts, which build.yml runs, plus the docs and manual references.

CI on this head: all seven required CI ready checks pass. Runs: Checks 40926614654, SAST 40926614688, Commitlint 40926614617, Build 40926614644, CLI 40926614629, Security 40926614702, E2E 40926614744.

Not run, still to prove:

  • The gcc image build and the in-container token probe. Only the merge queue's Build runs them, so nothing lands unless they pass.
  • The Incus boot path. "Sandbox on Incus" (run 40926614520, not a required check) still fails at "Install Incus" on the dropped Zabbly pin, which is unrelated to this PR.

@tale-review-bot
tale-review-bot added this pull request to the merge queue Oct 29, 2026
@yannickmonney
yannickmonney removed this pull request from the merge queue due to a manual request Oct 29, 2026
@yannickmonney yannickmonney reopened this Oct 29, 2026
@tale-review-bot

Copy link
Copy Markdown
Collaborator

Not landing yet. The green CI ready checks on the approved head 000bcd6022560761079523951997082deee812da came from the pre-0da08205 workflows. (tale-review-bot, Reviewer run b123c4de, read at 22:32 UTC)

  • The newest runs: they were created by the 20:13–20:15 UTC close/reopen. They checked out the merge into the old base: Checks run 40947272212, job 123705827113, logged HEAD is now at 831abc0 Merge 000bcd6022560761079523951997082deee812da into 9be5cadac.
  • What they skipped: in that run, UI (platform n/4) and UI read skipped. So the heavy suites that .github/CI.md ("Landing on main") now requires on the pull request never ran on this head.
  • My approval of 000bcd6022560761079523951997082deee812da stands. The head needs a fresh pull_request run first, from a close/reopen or a draft→ready, which now merges into 0da08205. Once every required check finishes green on that run, the Reviewer lands it with --match-head-commit 000bcd6022560761079523951997082deee812da.

@yannickmonney

yannickmonney commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Reopened so the checks run again. The 20:13–20:15 UTC reopen started its runs from a test merge into 9be5cada (before 0da08205), because GitHub had not refreshed it yet. This reopen's runs start from the test merge fad3b36421, into 0da08205. The head is unchanged (no new commit).

@yannickmonney

Copy link
Copy Markdown
Contributor

Landed on train as 369f037: squashed at the approved head after the fast gate (formatting, typecheck, build). It reaches main with the merge train's pull request, so this pull request closes without merging.

yannickmonney pushed a commit that referenced this pull request Oct 30, 2026
Reviewed and approved in pull request #5086 at 000bcd6.

Closes #4725
Task: TALE-134
@yannickmonney
yannickmonney deleted the fix/runnerd-token-out-of-proc branch October 30, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(sandbox): agent processes can read runnerd's token from /proc

3 participants