Repository navigation
fix(cli): create connection state root privately before locking - #5148
MilindChatrabhuji wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
|
Thanks for working on the umask 002 failure. The review of f7d8f47 found that some routes can still leave the state root unusable, unsafe, or changed. In cli.ts:200, a second independent pass found that the update notifier can still create a fresh root at 0o775. In a non-JSON interactive connect with a fresh state directory and umask 002, the detached update worker can finish before command dispatch, and its cache writer uses a recursive mkdir with no mode. The new helper then sees an existing directory and skips it, so connect still fails with At command-connection.ts:51, a second independent pass found that connect and disconnect change a caller-owned 0o775 or 0o777 root under a trusted parent before cloud admission sees it. The root check repairs the mode to 0o700, so admission passes. The base does the same, but it breaks the promise to preserve and refuse such a root. Every affected connection command must admit the root without changing it, keep the ancestry, type and owner checks, and refuse group- or other-writable roots before any repair or lock. At cloud-connection-record.ts:163, a second independent pass found that recursive creation can fail before the chmod runs. For a non-root POSIX user with umask 0200 and a missing parent, the parent is created at 0o500 and creating the root inside it fails with At cloud-connection-record.ts:167, a second independent pass found that the chmod can hit a directory this call did not create. If this call creates a missing parent and a concurrent process creates the root, mkdir returns the parent path, and the other process's 0o775 root becomes 0o700 and passes admission. If the root can be replaced by a symlink between mkdir and chmod, the target's mode changes and later writes can follow the link. The subdirectory chmod also depends on a stable parent. A mode change must apply only to a directory this call created exclusively ( At cloud-connection-record.ts:74, a second independent pass found that the lock can see an unfinished root or recreate a vanished one. Under umask 0200, a second process can see the 0o500 root between mkdir and chmod, return early, and fail to create its lock. If the root vanishes before locking, the lock helper (also here) recreates it with default permissions, which gives 0o775 under umask 002. No caller may use the root before it is ready. Both connection lock paths must require the prepared root, and refuse or repeat private initialization if it is gone. At cloud-connection-record.ts:135, a second independent pass found that a credential write can rebuild the root after the lock was taken. If the state directory is reset while connect waits for its provider, the pending record rewrite recreates the root and publishes into it, and a second lock for the same session becomes possible. The base refuses this rewrite. Writes made under a lock must require the root admitted for that lock and refuse if it disappears or changes. Only the initial or standalone write path may create it. At remote-connection-state.ts:412, a second independent pass found that under umask 0200 a missing Could connect call On the open threads, the Windows ownership one does not apply: Windows admission checks only the path shape by design (ADR 0016), the base had no ACL privacy either, and this change claims none. The remote-state mode one does not apply to the umask 002 case in #5137: the base fails the same way under an owner-bit mask, and current production routes create the root first. The stricter owner-bit case is still requested above for CI is pending. The main Linux and macOS jobs are waiting for fork approval, so none of the new POSIX umask and ownership tests has run. The reported validation ran on Windows, where these tests are skipped. Not verified here: no test was run locally, and the claim that the CLI regression test fails on the base comes only from reading the code. Not verified here: whether an older concurrent Before merge, the problems above need fixes, and the umask tests must run and pass in fork-approved CI. |
|
Thanks for the update. At 114e208, parent directory creation under umask 0200 and the Windows handling raised earlier are fixed, but the problems below still need changes in the code. The first open problem from the earlier review remains on a non-JSON interactive connect with a fresh state directory and umask 002. The upgrade notifier runs before dispatch, and its update worker or prompt writer creates the state root with a recursive mkdir and no mode. The root is then 0775, private setup accepts the existing root, and cloud admission refuses it, so connect likely still fails with The second earlier problem also remains, and it is a regression against the base. Also, a second independent pass found that the helper applies chmod by pathname under a parent that may permit replacement. If the new entry is replaced before chmod, chmod likely follows a symbolic link and changes the substituted target. This race remains for the cloud root at both lock wrappers (also line 91) and the credential write, and this change adds it to the remote state writer. Cloud admission uses In addition, a second independent pass found that a concurrent caller can likely use a directory before it has its final mode. On POSIX with umask 0200, one caller creates the root as 0500 and pauses before chmod, while another sees it as existing or gets Related to this, a second independent pass found that both connection lock wrappers can recreate a vanished root with default permissions. If the prepared root disappears before the lock starts, generic lock parent creation ( Finally, a second independent pass found the same rewrite risk at the credential write when the root is reset during the provider wait. The pending-record rewrite and the attachment writes in The two earlier inline threads no longer apply and can be resolved: Windows ACL privacy is not claimed now, because chmod is skipped on Windows, the doc says so, and Windows admission checks only the path shape by design; remote state modes are fixed because each created level gets mode 0700 before the next one is created. CI is still pending: one reported check passes, and the main Linux and macOS jobs wait for fork approval. The new POSIX umask tests in Before merge, please fix the problems above, starting with the two earlier ones (establishing the root once with |
|
@thymikee Thank you for the detailed review on
Local Verification (
|
|
Thanks for the update. At 2f6490b, command-connection.ts:53, line 175 and connect-preflight.ts:54 pass A second independent pass found that an owned 0755 or 0750 root under a safe parent is still changed to 0700 by A second independent pass found that the option at command-connection.ts:53 also stops A second independent pass found that connect-preflight.ts:54 makes A second independent pass found that a root moved aside and replaced after admission can receive lock files before cloud-connection-record.ts:92 leads to an identity check: both the awaited and the try path lock by pathname, so the guard and claim can be written into the replacement. Can lock and guard creation and removal stay bound to the admitted root identity, since a check after acquisition is too late? A second independent pass found that verification closes the directory handle, so a root replaced after the check can receive record or attachment bytes through cloud-connection-record.ts:189, the record publisher and the attachment writer, which resolve the original path again. Can the admitted identity stay live through publication, with subdirectory creation and both credential writes resolved inside that directory? A second independent pass found that file.ts:271, and the daemon establish path at script-destination.ts:560, still A second independent pass found that on macOS with umask 0700, a missing root directly under A second independent pass found that with umask 0200 a second writer can see an unfinished 0500 directory and return it at cloud-connection-record.ts:214, then fail with A second independent pass found that generated-config.ts:32 now rejects a A second independent pass found that read-only commands such as These earlier threads do not apply at this commit and can be resolved: Windows privacy claim is settled because the doc names a POSIX-only privacy model, and per-level private creation is addressed because each level is set to 0700 before the next one is created. CI is pending. One reported check passes. The Linux and macOS jobs wait for fork approval and have not run at this commit. Not verified here: no tests were run locally. The refusal of 0775 directories comes from reading the code. It was not traced which writer creates Before merge, first make an owned group-writable root get repaired to 0700 instead of refused. The umask 002, 0200 and 0700 tests in |
…p handle identity across cloud writes
|
@thymikee Thank you for the second-pass review on
Validation (
|
|
Thanks for the follow-up. At e8c3a00, several problems from the earlier review are fixed: owned 0775 and 0777 roots and managed directories are repaired to 0700 again, The new readiness wait at script-destination.ts:810 catches any owned directory without group or other write bits and without full owner access, such as a state root or A second independent pass found that the wait at script-destination.ts:810 also refuses an owned state root, A second independent pass found that the walk at script-destination.ts:373 now waits for owner write permission on an owned 0555 ancestor that it only traverses. Creating a state root, generated config, remote state or a script under an existing 0700 child worked on the base and now reports an initialization failure. Traversal must follow the existing ancestry rule, with a wait only on a directory that the operation needs to change and has reason to treat as still being created. A second independent pass found that cloud record reads at cloud-connection-record.ts:497 now wait on, then reject, a private 0500 A second independent pass found that on macOS, lock work at cloud-connection-record.ts:183 still resolves the state root by pathname. A root moved and replaced after the last check can receive the guard or claim, release has the same gap, and the identity error comes after the change. Every guard, claim and cleanup operation must be bound to the already open root with descriptor-relative operations, as the Linux route does through its descriptor path. A second independent pass found the same gap for writes at cloud-connection-record.ts:508: on macOS, a root replaced after the directory checks can receive record files or attachment credential bytes, and the later check cannot undo them. Every subdirectory creation and file publish must be descriptor-relative, with the root, cloud and session handles open until the final publish. A second independent pass found that when umask removes owner read, script-destination.ts:627 changes the mode by pathname after checking the entry, so a replacement made in between can be changed. New cloud and session directories do the same at cloud-connection-record.ts:453-471, and the physical parent check does not compare the parent with the walked inode. Every creation must keep the checked parent identity, and set the mode with an inode-bound or no-follow operation, only on the directory it created. A second independent pass found that the waits at script-destination.ts:571 can accept a different directory that takes the name during the wait, and the walk or root admission then continues on it. The cloud subdirectory wait at cloud-connection-record.ts:365-375 does the same and reopens the path at cloud-connection-record.ts:498. Every wait must compare each observation with the first device and inode, carry that identity through the open, refuse a changed one, and return the retriable error if the original stays unready. A second independent pass found that on Linux without A second independent pass found that with an absent state root, the update worker can write through a symlink in an existing shared XDG fallback cache and overwrite its target (update-check.ts:168). Setup errors are ignored, and the directory kind check and mode 0600 do not prevent this. The fallback cache directory must be admitted before the worker gets it, and cache data must be published through an exclusive temporary file and an atomic rename that does not follow the final entry. These earlier threads do not apply at this commit and can be resolved: Windows privacy claim is settled because the POSIX-only privacy model is documented and Windows behavior is unchanged since the previous review, and per-level private creation is fixed because each created level is set to exactly 0700. CI is pending. One reported check passes. The Linux and macOS jobs, which run the tests on the changed paths, wait for fork approval and have not run at this commit. Not verified here: no tests were run locally, and the 0555 failure comes from comparing the base and new admission code, not from a reproduction. The Before merge, first restore the 0700 repair for owned directories that lack full owner access, such as 0555 and 0550, and limit the walk's wait to levels it created. Then the umask 002, 0200 and 0700, 0750 and 0755 preservation, 0775 repair and owned-symlink tests in Could the later hardening that drives most of the +828 lines be moved out of this change? That is a maintainer decision. |
Summary
Fixes #5137.
Invariant: Connection admission repairs owned group/other-writable state roots (
0o775/0o777->0o700), preserves owned non-writable roots (0o750/0o755) onconnect/disconnect, creates missing roots/subdirectories privately via physical-parent walks andO_DIRECTORY | O_NOFOLLOWdescriptorfchmodSync, waits on0o500init underumask 0200, keeps admitted(dev, ino)handles open across locks/writes, and keeps update-check active without creatingstateDir.Before / after
Validation
Validated on Windows 11 (
Node v22.14.0):oxfmt --check(19 files),pnpm typecheck,scripts/layering/check.ts(2080files),oxlint . --deny-warnings(5614files),tsdown --config-loader native(553files): passed.--project unit-core): non-POSIX cases passed infile.test.ts,daemon-resolution-state-root.test.ts,remote-connection-state.test.ts,cloud-connection-record.test.ts,command-connection.test.ts(at--testTimeout=60000), and-tfilteredcli-state-root-admission.test.ts/update-check.test.ts. POSIXumask002/0200/0700, symlink, andcli-cloud-session.test.tssuites require Linux/macOS CI approval.