Repository navigation
server: close unix socket permission window and exec/destroy race - #854
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes two issues in the JSON-RPC server found during code review.
Unix socket permission window
listenUnixSocketused to bind the socket with umask-derived permissions and chmod it to0600only afterListenreturned. 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 created0700staging directory next to the target path, chmodded to0600, thenrenamed 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 created0700instead of0755. 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 thesun_pathlimit (104 bytes on macOS).Session exec vs destroy/reap race
session.execcalledlookupSessionand then marked the session busy outsidesessionsMu, so a concurrentsession.destroyor TTL reap could remove the session from the registry while exec was still running on it. The newacquireSessiondoes the lookup and marks the session busy in one step undersessionsMu. 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 already0600the first time it appears. Against the old code it fails ("first observed with mode 777").TestListenUnixSocketPrivateDirectoryAndNoStagingLeftovers: the parent dir is0700, no staging dir is left behind, and the final path accepts connections.TestAcquiredSessionCannotBeDestroyedOrReapedandTestConcurrentDestroyAndExec: 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.mdfor both guarantees. Also updated the TM-006 mitigation text inTHREAT_MODEL.md.make lint: 0 issues.GOOS=linuxandGOOS=windowsgo vet ./server/are clean. Tests were run on darwin only; Linux runs in CI.