Skip to content

fs::copy onto a .bak path follows a symlink there, so a cloned repo can redirect rtk init writes outside the project #4157

Open

Description

@KuSh

Pre-existing on develop (verified at 0924356b, rtk 0.48.0). Surfaced while reviewing #4119, which is unrelated to it.

Summary

backup_and_atomic_write copies the file it is about to replace to <name>.json.bak with fs::copy. fs::copy follows a symlink at the destination, so a repository that ships .codex/hooks.json.bak as a symlink pointing outside the working tree makes rtk init --codex overwrite that outside file with the project's hooks.json. Exit 0, no warning.

Like #4156 this needs nothing from the victim but a clone and rtk init --codex, and no race.

Reproduction

W=$(mktemp -d); cd "$W"
mkdir outside origin
printf 'victim contents\n' > outside/victim.txt

cd origin
git init -q -b main .; git config user.email [email protected]; git config user.name t
mkdir .codex
printf '{"hooks":{}}\n' > .codex/hooks.json
ln -s ../../outside/victim.txt .codex/hooks.json.bak
git add -A; git commit -qm "ship .codex/hooks.json.bak as a symlink"

cd "$W"; git clone -q origin clone; cd clone
rtk init --codex < /dev/null

cat "$W/outside/victim.txt"

Observed

exit=0

$ cat $W/outside/victim.txt
{"hooks":{}}

outside/victim.txt was overwritten with the project's .codex/hooks.json. The link itself is untouched, so nothing in the clone shows that anything left it.

Expected

The backup lands next to the file being replaced, inside the project. A .bak path that resolves outside the working tree should be refused or reported, not written through.

Mechanism

backup_and_atomic_write (src/hooks/init.rs):

let backup_path = path.with_extension("json.bak");
fs::copy(path, &backup_path).with_context(...)?;

fs::copy opens the destination with O_CREAT|O_WRONLY|O_TRUNC and no O_NOFOLLOW, so an existing symlink at that path is followed. Closing it needs O_NOFOLLOW or an fd-based copy; a path check ahead of the copy narrows the window but does not remove it, since the check and the open are separate syscalls and the link can be flipped between them.

Callers reached by rtk init: patch_codex_hooks_json and the other backup_and_atomic_write sites in src/hooks/init.rs.

Relation to #4119

#4119 adds ensure_inside_project, which checks the destination in run_codex_mode. The copy happens later, in patch_codex_hooks_json -> backup_and_atomic_write, so the deterministic clone case above is closed by that check but a concurrent flip of .codex/hooks.json.bak between the check and the copy is not. That residue needs a different mechanism (O_NOFOLLOW/fd-based copy) and a much stronger prerequisite — code already running on the machine — so it is worth recording but is not the case the reproduction above shows.

The same defect exists at a second site, and a content probe does not close it

backup_and_atomic_write is one instance of a pattern: choose a .bak path, then fs::copy onto it. The copy follows a symlink at the destination wherever that pattern appears.

The second site arrives with #3875: back_up_modified_extension -> plan_copy_backup -> free_backup_slot, then fs::copy(path, &backup_path). free_backup_slot probes each candidate with fs::read and treats an occupied-but-unreadable slot as taken, which closes the existing-symlink case — a link pointing at a readable file elsewhere reads back as content that differs, so the slot is skipped.

A dangling symlink defeats that probe, and only a dangling one. fs::read on a link whose target does not exist returns NotFound, which is exactly the signal the probe uses to mean "this slot is free". Measured across all four cases the probe can meet:

case                   symlink_metadata.is_symlink   fs::read
absent file            false                         Err(NotFound)
dangling link          true                          Err(NotFound)     <-- reported free, escapes
link to existing       true                          Ok(25 bytes)      <-- skipped, no escape
ordinary file          false                         Ok(6 bytes)       <-- skipped, no escape
dangling .bak symlink -> fs::read gives NotFound -> slot reported Free
fs::copy wrote 15 bytes
escaped file created outside the project? YES

The control matters as much as the failure: a link to an existing readable file reads back content that differs, so the slot is skipped and nothing escapes. The probe is not blind — it closes three of four cases, and closes the existing-target case by accident, because content is what it compares and a dangling link has none. A fix should be written against the one case that slips, not against a probe presumed useless.

Why it slips is that NotFound carries two meanings: "nothing is here" and "a link to nothing is here". fs::symlink_metadata separates them — it returns Ok with file_type().is_symlink() true on a dangling link, which is how dangling_symlink_target already detects them elsewhere in this file. Consulting it before fs::read lets a symlinked slot be treated as taken, or refused, while leaving the content comparison for ordinary files untouched. That fix is independent of whether the caller copies or renames, which the two below are not.

Reachability for that site: PI_LOCAL_DIR is .pi and OMP_LOCAL_DIR is .omp, both project-relative, so a clone controls them exactly as it controls .codex. A repository shipping .pi/extensions/rtk.ts together with a dangling .pi/extensions/rtk.ts.bak -> <outside> gets the extension's contents written to a path it chose. Verified: the primitive above, the call chain, and the constants. Not verified: an end-to-end rtk init --pi --local run.

Why #4119 is not affected

Its backup moves rather than copies. fs::rename replaces a symlink at the destination instead of following it, so free_backup_slot -> for_rename -> fs::rename cannot write outside the project even onto a planted link.

That makes the choice of rename over copy load-bearing for containment, which nothing in the code says. Whatever fixes this class should also state it at BackupSlot::for_rename, or a later caller will switch to fs::copy for a reason that looks purely local and reopen the hole.

Scope

Fixing backup_and_atomic_write alone leaves the pattern intact. Worth either an O_NOFOLLOW/fd-based copy helper that every .bak writer goes through, or removing the second site's need to copy at all.

Activity

  1. added
    bug
    on Sep 20, 2026
  2. changed the title [-]backup_and_atomic_write copies through a symlinked .bak, so a cloned repo can redirect rtk init --codex outside the project[/-] [+]fs::copy onto a .bak path follows a symlink there, so a cloned repo can redirect rtk init writes outside the project[/+] on Sep 21, 2026
  3. added a commit that references this issue on Sep 28, 2026
    19fefaa
  4. added 2 commits that reference this issue on Oct 5, 2026
    0577984
    50b3b47
  5. added 2 commits that reference this issue on Oct 16, 2026
    f4dd01f
    4114dd2
  6. added 2 commits that reference this issue on Oct 29, 2026
    0db0db9
    33319a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:securitybughooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions