Repository navigation
fix(cli): link-safe writes in a seat's Claude state directory (F153) - #3479
Conversation
…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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MDs9DPFVAooJnqCSwrdxVw
lilyshen0722
left a comment
There was a problem hiding this comment.
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.
- 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.
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
left a comment
There was a problem hiding this comment.
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 atseatbelt.js:496, covers D itself. So the child canrmdir Dand thenln -s <elsewhere> D. - The next turn's host then follows the link (
claude.js:339-342, 351-363):mkdirpasses;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
Keychainsthere.
replacePrivateFilethen 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.jsonthrough the link. - Fix:
mkdir(dirname(statePath), {recursive}), thenensurePrivateDir(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
$HOMEor~/.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
rmthrowsEISDIRinremovePrivateFile(: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
left a comment
There was a problem hiding this comment.
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 ifstatePathis a link, then runsensurePrivateDir. Both prepare paths call it first (:367Seatbelt,:575bwrap), so nothing is chmodded, created or relinked through a link planted before the turn. The builder checked this withsandbox-exec: before the fix the planted link's target became 0700 and gottmp/,Library/and aKeychainslink; after it, the turn refuses and the target is untouched. - Medium (fixed):
chmod(staged)is gone. Awxcreate with mode 0600 is already 0600. - Low (fixed):
removePrivateFileskipsEISDIRand still raises any other error. - Tests:
state-symlinksplusenvironmentgive 135 passed, 1 skipped and 1 failed. The failure isenvironment's "runs in bwrap with a HOME outside the project", which fails the same way onorigin/main3c83cca, 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 withCOMMONLY_KEEP_PROCESS=1,env -i, or a non-descendant platform binary. So a workspace-trust seat with Bash can leave a process running that swapsstatePathafter the check. - The serious window is the one between
replacePrivateFileandbuildSeatbeltProfile. The profile realpathsstatePath(seatbelt.js:338, 432) and grantsfile-write*on the result (:496), so a link to$HOMEwould 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.jsonthrough 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:
- Pin the profile: refuse unless
realpath(statePath) === realpath(dirname) + '/' + basename. - Don't honour
COMMONLY_KEEP_PROCESSfor confined seats, or at least for those with Bash.
- Pin the profile: refuse unless
- Public seats have no shell, so they can't stage this race.
GATE: PASS
@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.
.claude.jsonis written withreplacePrivateFile(temp file, then rename), as bwrap already did.tmp/andLibrary/go through a sharedensurePrivateDir, which refuses anything that isn't a real directory.removePrivateFile. It checks the directory first and removes nothing through a link. The other two.credentials.jsonremovals use it too.Tests
adapters.claude.state-symlinksis new, with 13 tests. 8 of them failed before the change, and all 13 pass after it.Reverse checks:
writeFileback for.claude.jsonfails both Seatbelt link tests.rmback fails the link-removal test.The existing adapter, Seatbelt and bwrap suites pass, including the real
sandbox-execsmoke.adapters.claude.environmenthas one failure, and it fails the same way on main (the #3457 note).The
cli/package.jsonversion line is unchanged.🤖 Generated with Claude Code
https://claude.ai/code/session_01MDs9DPFVAooJnqCSwrdxVw