Skip to content

server: close unix socket permission window and exec/destroy race - #854

Merged
ewhauser merged 2 commits into
mainfrom
claude/server-socket-session-race
Oct 19, 2026
Merged

ewhauser merged 2 commits into
mainfrom
claude/server-socket-session-race

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes two issues in the JSON-RPC server found during code review.

Unix socket permission window

listenUnixSocket used to bind the socket with umask-derived permissions and chmod it to 0600 only after Listen returned. During that window another local user could connect and keep the connection open. With --readwrite-root, that client could then write to host paths.

Now, on Unix platforms (server/unix_listen_unix.go), the socket is bound inside a freshly created 0700 staging directory next to the target path, chmodded to 0600, then renamed into place. The listener stays bound to the same inode, so it is never reachable at a permissive mode. The umask is not touched, so other goroutines are unaffected. Missing parent directories are now created 0700 instead of 0755. Non-Unix builds (server/unix_listen_other.go) keep the previous listen+chmod behavior, so the Windows build still works.

Caveat: the staging path (<dir>/.gbsNNNN/s) can be a few bytes longer than the final path, which matters only for paths near the sun_path limit (104 bytes on macOS).

Session exec vs destroy/reap race

session.exec called lookupSession and then marked the session busy outside sessionsMu, so a concurrent session.destroy or TTL reap could remove the session from the registry while exec was still running on it. The new acquireSession does the lookup and marks the session busy in one step under sessionsMu. Destroy and reap already skip busy sessions under that lock.

Tests (all pass with go test -race ./server/...)

  • TestListenUnixSocketNeverExposesPermissiveSocket: with umask 0, it watches the path while listening and asserts the socket is already 0600 the first time it appears. Against the old code it fails ("first observed with mode 777").
  • TestListenUnixSocketPrivateDirectoryAndNoStagingLeftovers: the parent dir is 0700, no staging dir is left behind, and the final path accepts connections.
  • TestAcquiredSessionCannotBeDestroyedOrReaped and TestConcurrentDestroyAndExec: the second races acquire+exec against destroy 200 times. It fails if the lookup and the busy-marking are split again.

SPEC

Updated the server protocol section of SPEC.md for both guarantees. Also updated the TM-006 mitigation text in THREAT_MODEL.md.

make lint: 0 issues. GOOS=linux and GOOS=windows go vet ./server/ are clean. Tests were run on darwin only; Linux runs in CI.

Bind the Unix socket inside a fresh 0700 staging directory, chmod it to
0600, then rename it into place so other local users can never connect
during the window before the chmod. Missing parent directories are now
created 0700.

Look up a session and mark it busy atomically under sessionsMu for
session.exec, so a concurrent session.destroy or TTL reap cannot remove
a session that is about to run.
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 06:22
@ewhauser
ewhauser merged commit 6e47240 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/server-socket-session-race branch October 19, 2026 06:28
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