Public: anyone can read this; the crew can change it.

A check's sockets live under a short fixed directory, and a rule test bounds its length #15878

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A run's temp directory now moves to a short fixed directory when a check's scratch path is too long for Claude Code's sockets. The longest socket path the sandbox builds is now held to 100 bytes (#13752, #15832).

Notes

shipped production is serving it deploy 0adf720

opened this

A run's temp directory now moves to a short fixed directory when a check's scratch path is too long for Claude Code's sockets. The longest socket path the sandbox builds is now held to 100 bytes (#13752, #15832). Before, runTmpDir moved off a long data directory into the machine's temp without checking how long that temp was. Inside a hosted forge's check, that temp is the long scratch path itself, so the socket names were truncated and the sandbox failed to start.

What changed, in src/crew/sandbox.ts:

  • runTmpDir tries three places in order and keeps the first where longestSocketPath(dir) fits within SOCKET_PATH_BUDGET (100 bytes; Linux allows 107):
    1. the agent's own .t/ directory;
    2. the machine's temp, only when it is short enough;
    3. a short fixed root from shortRoot(): /run/forge if it can be created and written, else /tmp.

    The longest path counts the real nesting: <tmpDir>/claude-<uid>/claude-socks-<16 hex>.sock.

  • When a run's temp directory sits in a short root, shortRootDenied denies the other runs' fr- directories there, the cells' fc- directories and the shared claude-<uid> directory. It never denies the whole root, which may be a busy /tmp.
  • sweepRunTmp also clears stale fr-* directories in the short roots.

Tests:

  • test/sandbox-socket-path.test.ts is the new rule test. With a 120-character scratch path as both the data directory and the temp, it fails if the longest socket path is over 100 bytes. It also checks that the machine's temp is used only while it is short enough.
  • Two tests in test/crew-sandbox.test.ts were wrong and I changed them:
    • The #10541 test asserted a run temp inside an 80-character-plus temp, which puts the socket past 107 bytes, the bug itself. It now expects the short root's directory and asserts the socket fits.
    • The child-forge test assumed the machine's temp is always short. It now expects whatever runTmpDir returns and asserts the socket fits.

    Their deny assertions are unchanged.

Not done:

  • /run/forge is not created with a per-check owner or mode beyond 0700; whichever forge process gets there first owns it.
  • No test of the denial inside a short root, because this sandbox can write neither /run nor /tmp.

Checks run: gate --fix and tsc are clean. The tests fast.ts named (sandbox.ts is widely imported) gave 4291 pass, 0 fail. The real-sandbox suites (crew-sandbox-escape, crew-sandbox-real) are skipped here because user namespaces are refused; the forge's check runs them.

To try: bun test test/sandbox-socket-path.test.ts test/crew-sandbox.test.ts.

🤖 Generated with Claude Code

Closes #15832

1 earlier comment · 1 event
commented

A run's temp directory now moves to a short fixed directory when a check's scratch path is too long for Claude Code's sockets. The longest socket path the sandbox builds is now held to 100 bytes (#13752, #15832). Before, runTmpDir moved off a long data directory into the machine's temp without checking how long that temp was. Inside a hosted forge's check, that temp is the long scratch path itself, so the socket names were truncated and the sandbox failed to start.

What changed, in src/crew/sandbox.ts:

  • runTmpDir tries three places in order and keeps the first where longestSocketPath(dir) fits within SOCKET_PATH_BUDGET (100 bytes; Linux allows 107):
    1. the agent's own .t/ directory;
    2. the machine's temp, only when it is short enough;
    3. a short fixed root from shortRoot(): /run/forge if it can be created and written, else /tmp.

    The longest path counts the real nesting: <tmpDir>/claude-<uid>/claude-socks-<16 hex>.sock.

  • When a run's temp directory sits in a short root, shortRootDenied denies the other runs' fr- directories there, the cells' fc- directories and the shared claude-<uid> directory. It never denies the whole root, which may be a busy /tmp.
  • sweepRunTmp also clears stale fr-* directories in the short roots.

Tests:

  • test/sandbox-socket-path.test.ts is the new rule test. With a 120-character scratch path as both the data directory and the temp, it fails if the longest socket path is over 100 bytes. It also checks that the machine's temp is used only while it is short enough.
  • Two tests in test/crew-sandbox.test.ts were wrong and I changed them:
    • The #10541 test asserted a run temp inside an 80-character-plus temp, which puts the socket past 107 bytes, the bug itself. It now expects the short root's directory and asserts the socket fits.
    • The child-forge test assumed the machine's temp is always short. It now expects whatever runTmpDir returns and asserts the socket fits.

    Their deny assertions are unchanged.

Not done:

  • /run/forge is not created with a per-check owner or mode beyond 0700; whichever forge process gets there first owns it.
  • No test of the denial inside a short root, because this sandbox can write neither /run nor /tmp.

Checks run: gate --fix and tsc are clean. The tests fast.ts named (sandbox.ts is widely imported) gave 4291 pass, 0 fail. The real-sandbox suites (crew-sandbox-escape, crew-sandbox-real) are skipped here because user namespaces are refused; the forge's check runs them.

To try: bun test test/sandbox-socket-path.test.ts test/crew-sandbox.test.ts.

🤖 Generated with Claude Code

Changes requested — before the latest push

Boxes 1 and 2 are met, but two things need fixing before this lands.

  1. Isolation is weaker when the run lands in /tmp. SHORT_ROOTS falls back to the shared /tmp, and shortRootDenied denies only fr-, fc- and claude-<uid> there. Every other file in /tmp stays readable by the sandboxed run, whereas tempDenied denies the whole machine temp. That leaks other sessions data across runs. Deny everything in the short root except this run directory, or fail closed instead of using /tmp.
  2. Two existing tests were rewritten, so box 3 is not met. The child-forge test now asserts toBe(runTmpDir(...)), which is tautological. Restore a concrete expectation: pass a host with a short temp and expect it. The new shortRootDenied and the sweepRunTmp short-root sweep have no test at all. Add tests with an injectable root.

Smaller: /run/forge is created 0700 and shared, and the first process to create it owns it. A /tmp/fr-<id> path is predictable, so mkdir with a non-recursive create and verify ownership and that it is not a symlink.

commented

A check's sockets now live under a short fixed directory that belongs to this forge alone, and a rule test keeps their path within 100 bytes (#15832). When neither the agent's own temp nor the machine's leaves room for Claude Code's bridge sockets, runTmpDir falls back to a short root.

This push addresses the review:

  • The shared /tmp is no longer a short root. shortRoots() is /run/forge, else /tmp/forge-<uid>. shortRoot creates each one without the recursive flag, then checks it with lstat. It passes over a root that is a symlink, is not a directory, is owned by another uid, or can be read by group or others. If no root qualifies, the run is refused (SandboxListingError) rather than placed in a shared directory.
  • Everything else in the short root is denied. shortRootDenied now denies every sibling of the run's directory, not only fr-, fc- and claude-<uid>. The only entry kept is one that holds the data directory, the same exception tempDenied makes. Because the root is private to this forge, the list stays short.
  • Predictable run directories outside the data directory: the new makeRunTmp (called in src/crew/runner.ts) creates /tmp/fr-<id> or <root>/fr-<id> on its own, mode 0700, and runs the same checks. A directory or symlink someone planted there first refuses the run.
  • The two rewritten tests assert real paths again. sandboxPolicy takes an optional tmpHost, and tempDenied reads its temp from that host instead of tmpdir(). The child-forge test passes a short temp and expects <temp>/fr-arr359fkygy5. The #10541 test passes an injected root and expects <root>/fr-kmnpqrstvwxy, with a sibling there denied.
  • New test/sandbox-short-root.test.ts covers, with injected roots:
    • shortRoot: creates a missing root, skips a symlink, an open mode and a missing parent, and refuses when nothing fits.
    • shortRootDenied: denies siblings whatever their name and keeps the data directory.
    • The short-root sweep in sweepRunTmp.
    • makeRunTmp: refuses a planted symlink.

    The test helper test/helpers/short-temp.ts creates a short scratch directory for these tests.

Behaviour change to look at: a host where neither /run/forge nor /tmp/forge-<uid> can be made private now refuses runs whose scratch path is too long. Before, it used the shared /tmp.

Checks: bun scripts/gate.ts --fix reported nothing and bun x tsc --noEmit is clean. scripts/fast.ts named 850 files, about 71 minutes, because runner.ts is imported widely. So I ran these instead, all passing:

  • the 29 test files that touch crew/sandbox.ts or a run's temp: 202 pass, 45 skipped by the sandbox (unix-socket and cell suites)
  • every test/crew-*.test.ts: 671 pass, 0 fail

🤖 Generated with Claude Code

Approved

Earlier asks are met. The shared /tmp is no longer a short root: roots are /run/forge or /tmp/forge-<uid>, made non-recursively and lstat-checked (not a symlink, owned by this uid, mode 0700), and the run is refused if none fits. Every sibling in the short root is denied. makeRunTmp refuses a planted run directory. The rewritten tests now assert real paths with an injected host, and the new tests cover each of these. Non-blocking: scripts/fast.ts was too broad, so only the sandbox and crew suites were run; the forge check runs the rest.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs