Skip to content

Close the UDS socket bind-to-chmod race in the daemon - #1548

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1506-uds-bind-chmod-race
Jul 6, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1506-uds-bind-chmod-race

Conversation

@malibio

@malibio malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Closes #1506

UnixListener::bind creates the socket file honoring the process's
ambient umask; the subsequent set_permissions(0o600) call left a
window where the socket could be group/other-reachable on a
permissive-umask system. Narrow the umask to 0o177 for the duration
of the bind so the socket is owner-only from the instant it appears,
per ADR-052 §3. Applied to both the headless and tray-driven server
loops via a shared helper.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This is a clean, well-scoped fix for the UDS bind→chmod race described in ADR-052 §3 and issue #1506. The bind_uds_owner_only() helper correctly narrows the process umask to 0o177 for the duration of UnixListener::bind, so the socket is created at 0o600 from the instant it appears on disk, restores the prior umask immediately afterward, and still applies set_permissions(0o600) defensively. It replaces the duplicated bind+chmod blocks in both serve_headless() and serve_grpc(). Verified locally: cargo clippy -p nodespace-daemon --bin nodespaced --all-features -- -D warnings is clean, cargo fmt --check is clean, and the new uds_permission_tests::bind_uds_owner_only_is_0o600_and_restores_umask test passes.

This is a net positive and addresses the acceptance criteria in #1506. I have one blocking-adjacent correctness note (asterisked below as an Improvement, not a Blocker, since it doesn't reopen the security window) and a couple of nits.

Findings

Critical Issues

None. The core security property — no window where the socket is exposed at the ambient umask — is delivered correctly:

  • main() calls exactly one of serve_headless() or serve_grpc() per process, never both, so there's no intra-process race on the process-global umask between concurrent binds.
  • The narrowed-umask window wraps only the bind() call itself, not the subsequent set_permissions, which is the correct minimal scope per ADR-052 §3.
  • Confirmed no peer-cred check (SO_PEERCRED/LOCAL_PEERCRED) and no Windows DACL work is present in this diff — correctly out of scope per the issue; those remain tracked under ADR-052 §2/§4 and issue Windows Named Pipe has no ACL and is squattable #1498.

Suggested Improvements

  • packages/daemon/src/main.rs:67-72 (bind_uds_owner_only): if bind() succeeds but the defensive set_permissions call fails, the function returns Err and the UnixListener returned by bind is dropped without being used. UnixListener::drop does not unlink the socket file, so a stale (but still 0o600, so not a security regression) socket file is left on disk, and the caller's next start-up attempt will need remove_file to succeed again (which it already does at the top of serve_headless/serve_grpc, so this is self-healing on next launch, but worth a one-line comment noting the leftover file is not a bug). Not a blocker — no security property is violated since permissions are already correct at that point — but worth a short comment so a future reader doesn't have to re-derive that the leftover file is benign.
  • Test coverage is good for the happy path (permissive umask → 0o600 result, umask restored). Consider also asserting the umask is restored even when bind() fails (e.g., bind to a path inside a nonexistent directory) — this exercises the branch where result.with_context(...)? propagates Err and confirms umask restoration isn't accidentally coupled to the success path. Given the current code structure (libc::umask(prev_umask) runs unconditionally right after bind, before the ?), this is already correct — the additional test would just make that invariant explicit and regression-proof.

Nitpicks

  • Nit: packages/daemon/src/main.rs:59-62: the // SAFETY: comment says "this must run before any concurrent thread binds a socket of its own — true here since this runs at startup before the tokio runtime spawns other tasks." This is accurate for the current two call sites, but it's an invariant on the caller, not something bind_uds_owner_only itself enforces. If a future call site invokes this from inside an already-running multi-threaded runtime (e.g. a hot-reload/re-bind path), the safety argument silently breaks. Consider a debug_assert or doc-level # Safety note on the function itself (not just inline) making this a documented precondition of the function, so it travels with the signature rather than living only in the current call sites' comment.
  • Nit: the module doc comment on bind_uds_owner_only and the test module both independently explain the "umask is process-global" rationale — mild duplication, but justified here since the test comment is explaining test isolation (why one #[tokio::test] not two) while the function comment explains the production safety window. Fine as-is.
  • Nit: libc = "0.2" is unpinned within the 0.2 line like the other three crates that already depend on it (packages/cli, packages/agent, packages/nlp-engine) — consistent with existing convention, no action needed.

Verification Performed

  • git diff origin/main...HEAD reviewed in full.
  • cargo clippy -p nodespace-daemon --bin nodespaced --all-features -- -D warnings — clean.
  • cargo fmt -p nodespace-daemon -- --check — clean.
  • cargo test -p nodespace-daemon --bin nodespaced uds_permission_tests and full daemon binary test suite — all pass, no umask cross-test interference (only one test touches umask in this binary).
  • Confirmed serve_headless/serve_grpc are mutually exclusive per-process (no concurrent-bind hazard for the process-global umask mutation).
  • Confirmed no peer-cred or Windows DACL code was introduced or silently dropped — matches the stated scope boundary (issue Close the UDS socket bind-to-chmod race in the daemon #1506 vs. Windows Named Pipe has no ACL and is squattable #1498 and ADR-052 §2/§4).

Recommendation

Approve. This is a focused, correct fix for the stated problem, matches existing dependency patterns, has a passing regression test, and does not bundle unrelated changes. The one improvement noted (stale socket file on set_permissions failure) is not security-relevant and does not block merge.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review complete — net positive, recommend merge. See detailed findings in the PR comment above. No blocking issues; one non-blocking improvement noted (stale socket file if set_permissions fails post-bind) and two nits. Clippy/fmt clean, test passes, scope correctly excludes peer-cred/Windows work (tracked separately under #1498).

Reviewer noted the SAFETY comment's concurrency argument was really a
caller precondition, not something the function enforces. Moved it
into the doc comment and named which two call sites satisfy it.

Skipped two other suggestions as non-blocking: the stale-socket-file
edge case on set_permissions failure (already self-healing via the
next startup's remove_file, not a security issue), and the reviewer's
own note that current test coverage is "fine as-is."

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed Review Feedback

✅ Addressed (1):

  • Promoted the # SAFETY comment's concurrency argument on bind_uds_owner_only() into a documented precondition, naming the two call sites (serve_headless, serve_grpc) that satisfy it. (4dbb708)

⏭️ Skipped (2, both explicitly non-blocking suggestions):

  • Stale socket file if set_permissions fails after a successful bind: already self-healing (next startup's remove_file call clears it), and the reviewer confirmed it's not a security regression — the file would still be 0o600. Not worth added error-path complexity for a benign edge case.
  • Additional test coverage for the umask-restoration-on-bind-failure path: the reviewer's own note called current coverage "fine as-is."

No 🔴 Critical or 🟡 Important findings were raised — the review's recommendation was to merge as-is.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: The only change made is a documentation/comment edit (no logic change) in response to a non-blocking nit. Verified cargo clippy -p nodespace-daemon --all-targets -- -D warnings, cargo fmt --check, cargo test -p nodespace-daemon, and the full test:all suite (frontend + Rust + e2e daemon round-trip) all pass. Ready for merge pending final approval.

@malibio
malibio merged commit 93a55a8 into main Jul 6, 2026
@malibio
malibio deleted the worktree-issue-1506-uds-bind-chmod-race branch July 6, 2026 17:36
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.

Close the UDS socket bind-to-chmod race in the daemon

1 participant