Skip to content

fix(init): keep an existing file's mode when rtk init rewrites it - #4653

Merged
TaKO8Ki merged 4 commits into
rtk-ai:developfrom
KuSh:fix/atomic-write-keep-mode
Nov 30, 2026
Merged

TaKO8Ki merged 4 commits into
rtk-ai:developfrom
KuSh:fix/atomic-write-keep-mode

Conversation

@KuSh

@KuSh KuSh commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4631.

atomic_write renames a NamedTempFile over the target. That temp file is created 0600 and nothing copied the original file's mode onto it, so every file rtk init patched or cleaned (settings.json, CLAUDE.md / AGENTS.md patches, instruction and rules files) came back 0600, and every file it created was 0600 whatever the umask.

On Unix, atomic_write now copies an existing file's rwx bits onto the temp file after the write and before the rename, following a symlink to its target. The temp file stays 0600 until then, so a filesystem that refuses the chmod leaves the file owner-only rather than wider. A file RTK or its owner cannot read, or one that cannot be stat'd, is left owner-only (0600, without the ACL its directory hands down), and a missing one gets the mode fs::write would give it. The replacement also keeps who else can reach the file, through a new core::file_access. A new file starts with the process's group and the ACL its directory hands down, so with the mode restored a 0640 file could become readable by another group or by inherited ACL entries. The replaced file's group and access ACL (Unix; ACLs on Linux and macOS) or its DACL (Windows) are copied onto the temp file, what the directory handed down is removed when the original had none, and the owner is restored where the process may set it. The group and owner, the macOS ACL and the Windows DACL go on before any content is written (inherited macOS and Windows entries are not masked by the temp file's 0600); the Linux ACL and the mode go on after the write, since the ACL carries the mode. When they cannot be copied exactly the write fails and the original stays as it was, except a group whose bits equal the other bits, on a file with no Linux ACL: it carries no access, so a file root left 0644 by an earlier sudo rtk init is rewritten 0644 as the user's. A volume without NT security (FAT, network redirectors) is written as before, and on Windows a refused WRITE_DAC is no error when the new file already has the same DACL.

Gemini's settings.json patch had its own copy of the same temp-file write and now goes through atomic_write.

Three related fixes on the same write path, one commit each:

  • atomic_write flushes the temp file before the rename. The flush fails the write only when it reports lost data (I/O error, full disk, exceeded quota, stale network handle); a filesystem that cannot sync still gets the file.
  • A JSON backup is written the way atomic_write writes the file: a new temp file given the backed-up file's access (mode, group and ACL or DACL, and owner where the process may set it), renamed over the old backup, or no backup and init stops with the earlier one as it was. A read-only or another user's backup is replaced rather than written into. A symlink at the backup path is replaced rather than followed, except a link to /dev/null, which keeps no backups and is left as it is; any other backup path that is no regular file is refused. All five backup sites go through this.
  • The Copilot hook config self-heal had its own temp-file write and now uses atomic_write. It reads a config only when it is a regular file, and skips a project config reached through a symlinked .github, .github/hooks or config file, since that link came with the repository.

Behaviour changes:

  • rtk init -g --auto-patch on a 0644 ~/.claude/settings.json: stays 0644 (was 0600). Same for --codex on AGENTS.md, --gemini on settings.json, and every --uninstall.
  • A rewritten 0640 file keeps its group and ACL: AGENTS.md in group users stays users (was: the process's group), a file with no ACL in a directory with a default ACL gains none (was: the directory's entries, made effective by the restored mode), and a file's own ACL entries stay (was: replaced by the directory's). Same for the DACL on Windows, including a file moved in from a stricter directory: it keeps the entries it inherited there (was: the new directory's DACL).
  • A file rtk init creates follows the umask: ~/.gemini/settings.json created under umask 022 is 0644 (was 0600).
  • A rewritten file RTK or its owner cannot read (0200, or root's 0640 left by sudo rtk init) comes back 0600, owner-only, and gains no ACL from its directory (was 0600 with the directory's ACL).
  • rtk init --codex and its --uninstall in a project whose .codex/hooks.json.bak is a link out of the project replace the link with a backup inside .codex and complete (was: refused, exit 2). Nothing is written through the link either way.
  • On Windows, a file whose DACL lets the user only read it is rewritten and keeps that DACL (was: rewritten with its directory's DACL), as a 0444 file is on Unix: the directory decides. A file with the read-only attribute is still refused, as Windows refuses to replace it.
  • A file root left 0644 by sudo rtk init (readable, group root) is rewritten 0644, owned by the user and in the user's group (was 0600); its backup is the same.
  • A symlinked ~/.gemini/settings.json is edited through the link, like the other files rtk init writes (was: replaced by a regular file).
  • A new settings.json.bak of a 0640 file in group users is 0640 in group users, with the file's ACL and without its directory's (was: the process's group and the directory's ACL).
  • On Unix, a read-only ~/.claude/settings.json.bak left by an earlier run is replaced and rtk init -g completes (was: Failed to backup to ...settings.json.bak: Permission denied). Same for the Trae backups.
  • A settings.json.bak hard-linked to settings.json is replaced by a separate backup (was: the copy emptied both), and a .bak symlink is replaced by the backup (was: written through).
  • The Copilot self-heal keeps rtk-rewrite.json's mode and writes through a symlinked global config (was: new file's mode, link replaced). A symlinked project .github/hooks/rtk-rewrite.json is left as it is (was: link replaced by a regular file).

The tests that depend on the umask go through test_isolation::with_umask, which serialises them.

Test plan:

  • Unit tests on atomic_write (kept, new-file and special-bit modes, symlink target and loop, Pi read-error rewrite, Gemini patch, kept mode and symlinked settings.json; the owning group; a kept and a dropped inherited access ACL on Linux, set as raw xattrs; the same on macOS with chmod +a; the DACL on Windows, explicit, protected, read-only (granting its owner read and nothing else), and inherited in a stricter directory the file was moved from, compared as icacls /save SDDL, and a read-only-attribute file left alone), on the backup (its group, ACL and Windows DACL, the moved file's included; an existing wider or read-only backup replaced, at the Claude and Trae sites too; a refused copy keeping the earlier backup; a backup that is the file itself or has another name; a symlinked or dangling backup link replaced, a /dev/null one left alone, a FIFO refused; a world-readable file in a foreign group backed up) and on which sync errors fail the write; end-to-end Copilot heal tests for the mode, the symlink cases and FIFOs. The owner given back under sudo was run as root in a container. Each Unix one was checked by mutating the code it pins, and the suite passes with a default ACL on TMPDIR.
  • rtk init in an isolated HOME with the develop and the patched binary: develop leaves 600/600/600 and fails on a read-only .bak; the patch gives 644/640/644 and completes.
  • rtk init -g --codex on the cases above with the patched binary: group, missing ACL and own ACL all come out as before the rewrite.
  • cargo fmt --all, cargo clippy --all-targets, cargo test --all pass; Windows target type-checks. The macOS ACL code and the Windows DACL tests run first on CI.

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes rtk init so that atomic_write preserves an existing file's permission mode (and follows the umask for new files) instead of always leaving files at 0600, since the underlying NamedTempFile defaults to owner-only. It also makes atomic_write flush before renaming, unlocks read-only JSON backups so they can be overwritten, and migrates the Gemini settings patch and the Copilot hook self-heal to use the shared atomic_write path.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4631


Analyzed automatically by wshm · This is an automated analysis, not a human review.

Comment thread src/hooks/init/mod.rs Outdated
Comment on lines +715 to +720
#[cfg(unix)]
if let Some(mode) = kept_mode {
let _ = temp_file
.as_file()
.set_permissions(fs::Permissions::from_mode(mode));
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When replacing an existing file, restoring the mode bits alone can change who can access it. For example, on Linux:

Metadata Original file Replacement with the current implementation
Mode 0640 0640
Owning group developers May become the temporary file’s group, such as staff
Extended ACL None May retain an ACL inherited from the parent directory

Although the mode remains 0640, a different group may gain read access. Applying that mode can also enable inherited ACL entries that were absent from the original file.

Could we extend the permission restoration here to preserve the original own group and access ACL? If the original has no extended ACL, the temporary file’s inherited ACL should be removed. The restoration order should avoid temporarily granting unintended access. If the group or ACL cannot be restored, the original file should remain unchanged.

This would need platform-specific handling like:

  • Linux: use std::os::unix::fs::fchown() for the group and rustix’s xattr APIs for the POSIX access ACL.
  • macOS: use fchown() for the group and Darwin’s native ACL APIs.
  • Windows: preserve the relevant ownership and DACL information through Win32 security APIs, such as those exposed by windows-sys. POSIX mode bits do not enforce access there.

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.

Thanks, confirmed on Linux with the PR's binary before the change: a 0640 AGENTS.md in group users came back in my primary group, and in a directory with a default u:nobody:rw ACL a file with no ACL came back with user:nobody effective r-- (the restored group bits made the inherited entry effective). develop never showed it only because its 0600 masked both.

Now in the PR (folded into the first commit), as a new core::file_access used by atomic_write:

  • Unix: the temp file gets the original's group via fchown, then its owner where the process may set it (root), before any content is written; the mode goes on last.
  • Linux: the original's system.posix_acl_access is copied onto the temp file, or the inherited one removed when the original has none (libc xattr calls, no new dependency).
  • macOS: the original's extended ACL is set on the temp file (acl_get_file / acl_set_fd_np), or an empty one, before the content is written, since inherited ACEs are not masked by the 0600 there.
  • Windows: the original's DACL, keeping its protected or inherited state, is set on the temp file with SetNamedSecurityInfoW before the content is written, together with the owner where the process may set it. Where setting it is refused (an SMB share granting Change, or one that builds DACLs it cannot set) the write still goes ahead when the temp file already carries the same ACEs, as it does for a file that only inherits; a volume without NT security is written as before.

A file whose access RTK cannot read (it or its owner cannot read the file, or it cannot be stat'd) gets no copy: it is left owner-only, 0600 with any inherited ACL removed, never wider than the original or than develop's 0600.

Otherwise, if the group or ACL cannot be copied exactly the write fails and the original is left as it was. The owner is the one best-effort part: it is restored where the process may set it (as root on Unix; on Windows setting another owner needs SeRestore/SeTakeOwnership, and files created from an elevated shell are owned by Administrators), and a different owner only changes the owner's own rights. In a user namespace (a sandbox or rootless container), a group or ACL entry naming an id the namespace does not map is one the kernel refuses to set, so such a file is not rewritten there.

Known limits I left out of this PR:

  • On macOS and Windows, between creating the temp file and setting its ACL/DACL, the entries its directory hands down apply to the still-empty file. tempfile cannot create it with a security descriptor, so closing that window would mean creating the file by hand.
  • On Windows, a DACL that denies the user write makes tempfile's persist fail (it resets the temp file's attributes by path first), so such a file is no longer replaced.
  • On Linux only the POSIX access ACL is carried: NFSv4 and CIFS ACLs and SELinux labels are not. Nor are the ACLs of other Unixes such as FreeBSD, or the Windows integrity label.
  • The .json.bak backups are still taken with fs::copy, which gives a new backup the process's group and the directory's ACL, as on develop.

Tests: owner group, kept and dropped ACLs on Linux (raw xattrs, no setfacl needed) and macOS (chmod +a), and the DACL on Windows (explicit and protected, compared as icacls /save SDDL, plus the DACL comparison itself); each Unix one fails with the matching copy removed. The macOS and Windows paths run for the first time on CI.

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.

Two of the limits listed above are now fixed in the PR:

  • .json.bak backups: a backup is written the same way as the file, a temp file given the backed-up file's access and renamed over the old backup, so a new backup gets the file's group and ACL or DACL rather than the process's group and the directory's ACL. A symlink at the backup path is replaced rather than followed.
  • Windows read-only DACL: the temp file is renamed with fs::rename and, on Windows, created without tempfile's temporary attribute, whose removal in persist reopened the file by name. A file whose DACL lets the user only read it is now replaced and keeps that DACL, as a 0444 file is on Unix; a file with the read-only attribute is still refused, as Windows refuses to replace it.

One refinement to exact-or-refuse: a group whose bits equal the other bits, on a file with no Linux ACL, carries no access of its own, so it need not come back. A file root left 0644 by an earlier sudo rtk init is rewritten 0644 as the user's instead of stopping init.

The PR description lists the behaviour changes and the tests.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. I think the basic approach looks good. Could you add a Windows test for a file moved from a directory with a more restrictive, unprotected DACL? I'd like to verify that restoring it doesn't introduce additional permissions inherited from the current parent.

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.

Thanks, added test_atomic_write_keeps_the_dacl_of_a_file_moved_from_a_stricter_directory: a file created in a directory whose only inheritable entry gives the file's owner full control (OW) is moved into a directory that also hands Guests read, then rewritten with a backup. The file and its backup must keep the moved file's DACL exactly, compared as icacls /save SDDL.

It failed on the previous code: the rewritten file came back as D:AI(A;ID;FR;;;BG)(A;ID;FA;;;SY)…, with Guests read from the current parent in place of the OW entry it had inherited before the move. SetNamedSecurityInfoW with an unprotected DACL drops the inherited entries it is given and recomputes them from the new file's parent.

The original descriptor is now set with SetKernelObjectSecurity on a handle to the temp file, which keeps the entries as given. That plain set drops the DACL's auto-inherited flag, so a second pass on the same handle sets it with SE_DACL_AUTO_INHERIT_REQ: the kernel then keeps the inherited entries the file already has, which the first pass made the original's. Both passes use one handle opened with WRITE_DAC beforehand, so a DACL that only lets its owner read the file still gets its flag back; the read-only DACL test now seeds exactly D:PAI(A;;FR;;;OW) to cover that. The moved file, the explicit and protected cases and the read-only DACL case all come back with identical SDDL on both Windows CI jobs.

The branch is also rebased onto develop.

KuSh and others added 4 commits November 21, 2026 01:29
…ites it

atomic_write renames a NamedTempFile (created 0600) over the target, so
every file rtk init patched or cleaned lost its group and other bits,
and every file it created was 0600 whatever the umask. On Unix, an
existing file RTK and its owner can read now keeps its rwx bits (copied
onto the temp file after the write, best effort). Any other existing
file, one RTK cannot read or stat, is left owner-only, and a new file
gets the mode fs::write would give it.

With the mode restored, the new file's other access matters too: it
starts with the process's group and the ACL its directory hands down,
so a 0640 file could become readable by another group or by inherited
ACL entries. core::file_access copies the replaced file's group and
access ACL (Unix; ACLs on Linux and macOS) or DACL (Windows) onto the
temp file, removing what the directory handed down when the original had
none, and its owner where the process may set it. The write fails,
leaving the original as it was, when they cannot be copied exactly. A
DACL is copied as it stands, with the entries it inherited, so a file
moved in from a stricter directory gains none from its new one. A
group whose bits equal the other bits, on a file with no Linux ACL,
carries no access and need not come back: a file root left 0644 by an
earlier `sudo rtk init` is rewritten 0644 as the user's.

The temp file is renamed with fs::rename, as the OS renames: the
directory decides, so a file whose DACL lets the user only read it is
replaced like a 0444 file on Unix, and one Windows marks read-only is
refused. On Windows it is created without tempfile's temporary mark,
whose removal in persist reopens the file by name, which that DACL
refuses.

Gemini's settings.json patch had its own copy of the same write and now
goes through atomic_write, which also makes it follow a symlinked
settings.json like the other init writes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rtk init backs a JSON file up with fs::copy before rewriting it. A new
backup got the process's group and whatever ACL its directory hands
down, so a 0640 settings.json in a restricted group left a backup more
users could read, and backing up a read-only settings.json left a
read-only backup that the next run could not overwrite.

The backup is now written the way atomic_write writes a file: a new
temp file given the backed-up file's access (mode, group and ACL or
DACL, and owner where the process may set it), renamed over the old
backup. A refused copy leaves an earlier backup as it was, and a
read-only or another user's backup is replaced rather than written
into. A symlink at the backup path is replaced rather than followed,
so a planted link never sends the copy elsewhere; a link to /dev/null,
which keeps no backups, is left as it is, and any other backup path
that is no regular file is refused. All five backup sites go through
this. Codex's project install and uninstall therefore no longer vouch
for the backup sibling separately: a hooks.json.bak link out of the
project is replaced like any other, where it used to stop the command.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Nothing orders the data write before the rename on disk, so a crash
shortly after rtk init could leave the target empty or truncated,
which the temp file and rename are there to prevent. The sync fails
the write only when it reports lost data (an I/O error, a full disk,
an exceeded quota, a stale NFS handle, or a Windows network share
dropping the write), which some network filesystems report only at
that point; a filesystem that cannot sync still gets the file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The self-heal wrote its own temp file and renamed it over the config,
so a healed rtk-rewrite.json took a new file's mode and a symlinked
one was replaced by a regular file. It now uses the same write as
rtk init. The heal runs on every hook call without rtk init, so it
reads a config only when it is a regular file, and leaves a project
config reached through a symlink alone: that link came with the
repository, and following it would write wherever it points.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@TaKO8Ki
TaKO8Ki merged commit bec5b70 into rtk-ai:develop Nov 30, 2026
16 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Nov 30, 2026
@KuSh
KuSh deleted the fix/atomic-write-keep-mode branch November 30, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk init rewrites existing files with mode 0600, whatever mode they had

2 participants