Skip to content

fix(cli): link-safe writes in a seat's Claude state directory (F153) - #3479

Merged
lilyshen0722 merged 4 commits into
mainfrom
cto/f153-seatbelt-state
Nov 5, 2026
Merged

lilyshen0722 merged 4 commits into
mainfrom
cto/f153-seatbelt-state

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

@coderabbitai ignore

What

F153. On both Seatbelt and bwrap, the host now uses link-safe writes and removals in a seat's Claude state directory.

  • Seatbelt.
    • .claude.json is written with replacePrivateFile (temp file, then rename), as bwrap already did.
    • tmp/ and Library/ go through a shared ensurePrivateDir, which refuses anything that isn't a real directory.
  • bwrap. Removing the access-only login copy goes through a new removePrivateFile. It checks the directory first and removes nothing through a link. The other two .credentials.json removals use it too.
  • Hint. A keyless Seatbelt "Not logged in" failure now carries the same hint bwrap gives.

Tests

adapters.claude.state-symlinks is new, with 13 tests. 8 of them failed before the change, and all 13 pass after it.

Reverse checks:

  • Putting writeFile back for .claude.json fails both Seatbelt link tests.
  • Putting the plain rm back fails the link-removal test.

The existing adapter, Seatbelt and bwrap suites pass, including the real sandbox-exec smoke. adapters.claude.environment has one failure, and it fails the same way on main (the #3457 note).

The cli/package.json version line is unchanged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MDs9DPFVAooJnqCSwrdxVw

lilyshen0722 and others added 2 commits November 4, 2026 20:13
…the seat planted (F153)

A confined child can write anywhere in its state dir, so every host-side write
or removal there must refuse a planted symlink.

- Seatbelt: .claude.json now goes through replacePrivateFile (temp + rename),
  as bwrap's already did; a plain writeFile+chmod overwrote the link target
  with auth metadata. tmp/ and Library/ go through a shared ensurePrivateDir
  that refuses anything but a real directory (mkdir -p + chmod followed links).
- bwrap: removing the access-only login copy after a public turn ran a bare rm
  on state/.claude/.credentials.json; a turn that swapped .claude for a link to
  the person's ~/.claude made the host delete their real login. A new
  removePrivateFile checks the directory first and removes nothing through a
  link; the two other .credentials.json removals use it too.
- A keyless Seatbelt "Not logged in" failure now carries bwrap's hint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MDs9DPFVAooJnqCSwrdxVw

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HEAD: 3479710 — GATE: HOLD

One Opus security review. The static case is closed: a link planted on one turn is no longer followed by the host on the next. One race and two smaller gaps are still open.

  • High (reasoned, not reproduced). The host checks a directory, then acts on its path. A concurrent turn of the same seat can swap that directory between the two steps, and on bwrap the swap can be atomic. That redirects the host's credential write or delete onto the person's own Claude login.
    • Fix: make the directory a mount point inside the jail, so it can't be renamed or replaced from inside.
  • Low.
    • On Seatbelt, a process left over from an earlier turn can make the permission change on the temp or Library folder apply to a link's target.
      • Fix: open the folder with no-follow flags and change the mode through that handle.
    • A failed write can leave a staged credential copy behind in the state folder.
      • Fix: clean up the copy on failure, and again in the after-turn cleanup.

Fixes are in progress.

GATE: HOLD

…folder modes change through a no-follow handle, and no staged login copy outlives a turn (F153)

- bwrap binds statePath/.claude and statePath/tmp onto themselves after the
  state bind, so a child cannot rename or replace them while any turn of the
  seat runs; the host's link check then holds for the life of the turn.
- ensurePrivateDir opens the folder O_NOFOLLOW|O_DIRECTORY and chmods the
  handle, so a link swapped in after the check is refused, never followed.
- replacePrivateFile removes its staged copy on any failure, and the after-turn
  cleanup removes the login copy and any staged copy beside it through the same
  link-checked removal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MDs9DPFVAooJnqCSwrdxVw

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HEAD: c210b92 — GATE: HOLD

Opus re-review of the race fixes. All three earlier findings are fixed correctly, and the bwrap side holds: self-bound mount points answer EBUSY, and bwrap drops all capabilities, so the child can't unmount. One level above the checks is still open on Seatbelt.

High: a Seatbelt child can replace statePath itself with a symlink. Verified with sandbox-exec on macOS for the sandbox step; the host side is from reading the code.

  • A (allow file-write* (subpath D)) rule, which is the shape at seatbelt.js:496, covers D itself. So the child can rmdir D and then ln -s <elsewhere> D.
  • The next turn's host then follows the link (claude.js:339-342, 351-363):
    • mkdir passes;
    • chmod(statePath, 0o700) changes the mode of whatever path the link names;
    • ensurePrivateDir(tmp|Library) creates or chmods inside the target;
    • the Keychains relink creates or removes a link named Keychains there.
  • replacePrivateFile then refuses, and every later turn fails until someone removes the link.
  • The impact is modes, directories and a link outside the state folder; there's no content or credential write. It's still better than main, where a key seat wrote .claude.json through the link.
  • Fix: mkdir(dirname(statePath), {recursive}), then ensurePrivateDir(statePath) in place of the plain chmod, and the same at :547-548.

Medium: chmod(staged, 0o600) follows a link (claude.js:387-389). It runs by path after the wx create, so a concurrent same-seat child could swap the staged file in the gap. Fix: drop the chmod (wx with mode: 0o600 is already 0600), or chmod through the handle.

Low:

  • A workspace that contains the state folder (cwd $HOME or ~/.commonly) covers the bwrap self-binds. This predates the PR; refuse that layout or bind state after the workspace.
  • A child can create a directory named like a staged copy, so rm throws EISDIR in removePrivateFile (:280). It breaks only its own seat's cleanup.
  • An old-code jail that's still running during an upgrade has no self-bind.

The 20 adapters.claude.state-symlinks tests pass.

GATE: HOLD

… for a link, before any step follows it

- ensureStateFolder(statePath): mkdir the parent, lstat, then
  ensurePrivateDir (O_NOFOLLOW|O_DIRECTORY + fchmod) in place of the plain
  mkdir + chmod, on both the Seatbelt and bwrap prepare paths. A Seatbelt
  child can rmdir its own state folder and plant a link there (a subpath
  rule covers the folder itself); the next turn now refuses before chmod,
  tmp/Library creation or the Keychains relink, and says to remove the link.
- Drop chmod(staged, 0o600) by path: the wx create already sets 0600.
- removePrivateFile leaves a directory named like a staged copy in place
  with a warning instead of throwing EISDIR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MDs9DPFVAooJnqCSwrdxVw

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HEAD: 439c862 — GATE: PASS

Opus re-review of the fixes for the HOLD at c210b92. All three are correct.

  • High (fixed): ensureStateFolder (claude.js:270-279) creates the parent folder, then refuses if statePath is a link, then runs ensurePrivateDir. Both prepare paths call it first (:367 Seatbelt, :575 bwrap), so nothing is chmodded, created or relinked through a link planted before the turn. The builder checked this with sandbox-exec: before the fix the planted link's target became 0700 and got tmp/, Library/ and a Keychains link; after it, the turn refuses and the target is untouched.
  • Medium (fixed): chmod(staged) is gone. A wx create with mode 0600 is already 0600.
  • Low (fixed): removePrivateFile skips EISDIR and still raises any other error.
  • Tests: state-symlinks plus environment give 135 passed, 1 skipped and 1 failed. The failure is environment's "runs in bwrap with a HOME outside the project", which fails the same way on origin/main 3c83cca, so it comes from this host or from main and not from this branch.

One race remains; it is a follow-up, not a blocker (F197).

  • Seatbelt has no PID namespace. The turn reaper (packages/seats/src/turn-processes.js) can be escaped with COMMONLY_KEEP_PROCESS=1, env -i, or a non-descendant platform binary. So a workspace-trust seat with Bash can leave a process running that swaps statePath after the check.
  • The serious window is the one between replacePrivateFile and buildSeatbeltProfile. The profile realpaths statePath (seatbelt.js:338, 432) and grants file-write* on the result (:496), so a link to $HOME would widen that turn's write access to the whole home folder.
  • Main has the same behaviour with no race needed: a key seat follows a planted link, writes .claude.json through it, and then grants write on the target. This PR narrows that to a timed race that also needs a process left running.
  • Follow-up:
    1. Pin the profile: refuse unless realpath(statePath) === realpath(dirname) + '/' + basename.
    2. Don't honour COMMONLY_KEEP_PROCESS for confined seats, or at least for those with Bash.
  • Public seats have no shell, so they can't stage this race.

GATE: PASS

@lilyshen0722
lilyshen0722 added this pull request to the merge queue Nov 5, 2026
Merged via the queue into main with commit 6b20543 Nov 5, 2026
35 checks passed
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.

1 participant