fix(daemon): serialize the umask swap around the socket bind - #16
Merged
Merged
Conversation
CI fails now and then with "socket ... has insecure permissions 0o755". It hit run_embedded_exits_on_cancel on #14 and synchronous_startup_explicit_path_uses_parent_as_sandbox_root on #13. The socket gets its mode from the umask at bind time, and the umask is process-wide. synchronous_startup and four socket tests each did umask(077) / bind / umask(old) with no lock, and cargo test runs them on parallel threads. If one thread restores 022 between another thread's swap and its bind, that socket comes out 0755. The post-bind check then rightly refuses it. The swap now lives in bind_owner_only, behind a static mutex, and synchronous_startup and the tests both go through it. The lock is in production code, not a test-only mutex, so the integration test binaries that call synchronous_startup in parallel are covered too. The real daemon binds before any other thread exists, so it never waits on the lock. Stress run of `daemon::` lib tests, 200 iterations at 16 threads: 5 failures before, 0 after.
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.
Why
CI sometimes fails with:
It failed
run_embedded_exits_on_cancelon #14 (a docs-only PR) andsynchronous_startup_explicit_path_uses_parent_as_sandbox_rooton #13.A socket's mode comes from the umask at
bindtime, and the umask is process-wide.synchronous_startupand four socket tests insrc/daemon.rseach didumask(077)→bind→umask(old)without a lock, andcargo testruns them on parallel threads:umask(077), which returns the default022.umask(077), which returns077.022.0755.Change
bind_owner_only(path)insrc/daemon.rs. It does the umask swap and the bind under a staticUMASK_LOCK.synchronous_startupand the four socket-permission tests now call it.The lock is in production code rather than a test-only mutex in
test_support. The integration test binaries (daemon_integration,e2e_helpersusers) also callsynchronous_startupfrom parallel tests, and a test-only mutex wouldn't reach them. The real daemon binds before any other thread exists, so it never waits on the lock.Only the socket depends on the umask. The proxy CA certificate already fixes its mode with
fchmod.Verification
cargo fmt,cargo clippy --all-targets -D warnings: clean.cargo test --lib: 480 passed.cargo test --test daemon_integration: 13 passed, 1 ignored.daemon::lib tests, 200 iterations with--test-threads=16.verify_socket_permissions_accepts_owner_onlyandrun_embedded_exits_on_cancel.🤖 Generated with Claude Code