leonida-lua: write_file writes through a hard link in the mod's folder to a file outside it #3212

Open
opened 2026-12-02 19:34:42 +00:00 by Kal0s-design · 1 comment
Kal0s-design commented 2026-12-02 19:34:42 +00:00 (Migrated from github.com)
Contributor

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T19:34:42Z.


What happens. If a mod's folder contains a hard link to a file outside the folder, write_file writes through it and changes the outside file. This happens on main (b41763227) and also with #3189 applied.

Why. manifest::inside() stops links and junctions by canonicalising the path and checking that the result starts with the mod's folder. A hard link has no target to resolve. It is just another name for the same file, so it canonicalises to its own path inside the folder and passes the check. After that, std::fs::write opens it and truncates and writes the shared file.

Repro (measured on Windows 11, NTFS, while verifying #3189).

  1. Make a file outside the mod folder, e.g. outside.txt with some content.
  2. In the mod's folder, run mklink /H h.txt <path to outside.txt>. No admin and no Developer Mode needed. Hard links on NTFS need no special right.
  3. From the mod, call write_file("h.txt", "MOD").
  4. outside.txt now contains MOD.

Same result with the inside() check from #3189. From reading the code, read_file goes through the same check, so it would also read the outside file's contents. That part I did not run.

How likely it is. Fairly low. A mod can't create a hard link from Lua. The only filesystem calls in api.rs are metadata, read, create_dir_all and write. Hard links also don't survive a zip, so a mod shipped as an archive can't carry one. Something else has to have placed the link in the folder already, like an installer, a script, or a mod manager that copies with links. Still, it's the same "write lands outside the folder" class that inside() is there to stop, and inside() can't see it.

Possible fixes (not tried).

  • Write to a temp file in the same folder and rename it over the target. The rename replaces the directory entry, so the outside file keeps its contents and the link is simply gone.
  • Or open the existing file first and refuse it when its link count is above 1 (GetFileInformationByHandle, nNumberOfLinks; on Unix st_nlink). Doing the same in read_file would cover reads too.

The first one also keeps a half-written file from being left behind if the process dies mid-write.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:6752926172 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3212). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T19:34:42Z. --- **What happens.** If a mod's folder contains a hard link to a file outside the folder, `write_file` writes through it and changes the outside file. This happens on `main` (`b41763227`) and also with #3189 applied. **Why.** `manifest::inside()` stops links and junctions by canonicalising the path and checking that the result starts with the mod's folder. A hard link has no target to resolve. It is just another name for the same file, so it canonicalises to its own path inside the folder and passes the check. After that, `std::fs::write` opens it and truncates and writes the shared file. **Repro (measured on Windows 11, NTFS, while verifying #3189).** 1. Make a file outside the mod folder, e.g. `outside.txt` with some content. 2. In the mod's folder, run `mklink /H h.txt <path to outside.txt>`. No admin and no Developer Mode needed. Hard links on NTFS need no special right. 3. From the mod, call `write_file("h.txt", "MOD")`. 4. `outside.txt` now contains `MOD`. Same result with the `inside()` check from #3189. From reading the code, `read_file` goes through the same check, so it would also read the outside file's contents. That part I did not run. **How likely it is.** Fairly low. A mod can't create a hard link from Lua. The only filesystem calls in `api.rs` are `metadata`, `read`, `create_dir_all` and `write`. Hard links also don't survive a zip, so a mod shipped as an archive can't carry one. Something else has to have placed the link in the folder already, like an installer, a script, or a mod manager that copies with links. Still, it's the same "write lands outside the folder" class that `inside()` is there to stop, and `inside()` can't see it. **Possible fixes (not tried).** - Write to a temp file in the same folder and rename it over the target. The rename replaces the directory entry, so the outside file keeps its contents and the link is simply gone. - Or open the existing file first and refuse it when its link count is above 1 (`GetFileInformationByHandle`, `nNumberOfLinks`; on Unix `st_nlink`). Doing the same in `read_file` would cover reads too. The first one also keeps a half-written file from being left behind if the process dies mid-write. AI-assisted: yes (Claude Code).
MoonEyes commented 2026-12-02 21:27:06 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T21:27:06Z.


PR #3226 is open and addresses this: it replaces the in-place write with a write that swaps in a new file, so a hard link in the mod folder is no longer written through. AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7359298678 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3212#issuecomment-7359298678). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T21:27:06Z. --- PR #3226 is open and addresses this: it replaces the in-place write with a write that swaps in a new file, so a hard link in the mod folder is no longer written through. AI-assisted (Claude Code).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy#3212
No description provided.