Lua: refuse hard links in read_file, require and the manifest script #3364

Open
MoonEyes wants to merge 6 commits from upstream-pr/3364 into main
MoonEyes commented 2026-12-03 16:27:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-03T16:27:59Z.


Claimed in the coordination issue as: follow-up of #3212 (claim comment in #3312)

Depends on: #3226 (its head is on a fork, so this branch is cut from it: until #3226 merges, the diff also shows its commits; the two commits to review are 2e2cd25b and bd7ea1cb)

Refs: #3212

What and why

#3226 makes write_file replace a hard-linked file instead of writing through it, and leaves reading out of scope. This closes the read side for mods: read_file refuses a file with more than one link, and so do require and the manifest script, because a hard-linked x.lua pointing outside the mod folder otherwise leaked a fragment of that file through Lua's own syntax error. All three go through one helper, manifest::open_inside: inside check, an is_file pre-check (a FIFO with no writer would block open), one File::open, then regular-file, size and link-count checks on the same handle; read_capped reads at most max bytes (the size limit was only checked before the read). link_count uses nlink on Unix and GetFileInformationByHandle on Windows. docs/lua-modding.md is updated.

Trade-off for a maintainer to confirm: a mod manager that deploys by hard link can no longer have its files read by the mod through read_file/require; it has to copy instead. #3226 had left read_file out for that reason.

Tested

  • cargo test -p leonida-lua --locked on Linux: 50 passed. Two new tests are red on #3226's head and green here (read_file_refuses_hard_link_to_a_file_outside_the_folder, require_and_the_manifest_script_refuse_a_hard_link); reverting only runtime.rs keeps the second one red, so run_script is covered on its own.
  • cargo check and clippy of leonida-lua for x86_64-pc-windows-msvc from Linux: clean. The Windows branch of link_count is read and type-checked, not run.
  • python -I scripts/check_no_game_data.py, test_docs_*: clean.
  • Two independent read-only reviews. Known cosmetic changes: the manifest-script errors lose the "cannot read" prefix, and require calls inside twice per candidate.

GPUs tested: n/a

AI-assisted: yes. Written with Claude Sonnet 5.5; independently reviewed read-only twice by Claude Opus 5.5.

Checklist

  • Based on current main plus #3226 (see Depends on).
  • One topic.
  • No game data, keys or decryption.
  • Not a translator rule.

Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5630563362 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3364). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-03T16:27:59Z. --- Claimed in the coordination issue as: follow-up of #3212 (claim comment in #3312) Depends on: #3226 (its head is on a fork, so this branch is cut from it: until #3226 merges, the diff also shows its commits; the two commits to review are 2e2cd25b and bd7ea1cb) Refs: #3212 ### What and why #3226 makes `write_file` replace a hard-linked file instead of writing through it, and leaves reading out of scope. This closes the read side for mods: `read_file` refuses a file with more than one link, and so do `require` and the manifest script, because a hard-linked `x.lua` pointing outside the mod folder otherwise leaked a fragment of that file through Lua's own syntax error. All three go through one helper, `manifest::open_inside`: `inside` check, an `is_file` pre-check (a FIFO with no writer would block `open`), one `File::open`, then regular-file, size and link-count checks on the same handle; `read_capped` reads at most `max` bytes (the size limit was only checked before the read). `link_count` uses `nlink` on Unix and `GetFileInformationByHandle` on Windows. `docs/lua-modding.md` is updated. **Trade-off for a maintainer to confirm:** a mod manager that deploys by hard link can no longer have its files read by the mod through `read_file`/`require`; it has to copy instead. #3226 had left `read_file` out for that reason. ### Tested - `cargo test -p leonida-lua --locked` on Linux: 50 passed. Two new tests are red on #3226's head and green here (`read_file_refuses_hard_link_to_a_file_outside_the_folder`, `require_and_the_manifest_script_refuse_a_hard_link`); reverting only `runtime.rs` keeps the second one red, so `run_script` is covered on its own. - `cargo check` and `clippy` of `leonida-lua` for `x86_64-pc-windows-msvc` from Linux: clean. The Windows branch of `link_count` is read and type-checked, not run. - `python -I scripts/check_no_game_data.py`, `test_docs_*`: clean. - Two independent read-only reviews. Known cosmetic changes: the manifest-script errors lose the "cannot read" prefix, and `require` calls `inside` twice per candidate. GPUs tested: n/a AI-assisted: yes. Written with Claude Sonnet 5.5; independently reviewed read-only twice by Claude Opus 5.5. ### Checklist - [x] Based on current `main` plus #3226 (see `Depends on`). - [x] One topic. - [x] No game data, keys or decryption. - [x] Not a translator rule. --- _Generated by [Claude Code](https://claude.ai/code)_
Kal0s-design commented 2026-12-03 16:44:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-03T16:44:44Z.


Ran natively on Windows 11 (NTFS), head bd7ea1cb3 merged into current main (717b90233, clean merge).

I opened #3212, so I ran the same probes as on #3226, this time on the read side. The Windows branch of link_count works: every hard link I tried is refused by read_file, require and the manifest script, and nothing outside the folder comes back. Findings first.

  • Trade-off, in practice. A hard-linked main.lua doesn't just fail a read_file, it disables the whole mod (Failed(main.lua is a hard link and cannot be read by a mod)). That also happens when both names are inside the mod's folder. So a mod manager that deploys by hard link gets mods that don't load at all, not mods that load and lose some reads. Worth saying in the docs line, or the maintainer may want to allow nlink > 1 when every name is inside the folder (not tried).
  • One handle is true for the file, not for the path. Type, size and link count are checked on the handle that gets read, and swapping a plain file and a hard link under one name gets nothing through (0 of 8584 reads, main 1984). Two races are still open. Both need a second process changing the folder while the mod reads. From reading the code the mod can't do that itself: Lua can't make hard links or junctions, and one mod's read_file and write_file calls don't overlap. Anything that can swap names in the folder mid-read could also just copy the outside file in, so I'd see these as hardening, not a blocker:
    • link removed after the open. Open r.txt (a hard link to an outside file, 2 links), then another process deletes r.txt. On Windows 11 the delete goes through while the handle is open (File::open on Windows shares DELETE by default), and the handle now reports 1 link, so the check passes and the outside content is read. Done step by step on one handle: links 2 before the delete, 1 after, name gone, read returns SECRET. In a 4 s race loop: 57 of 28364 reads. Untried idea: open with share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE) so nobody can delete or rename the name while the handle is open.
    • folder swapped for a junction between inside and the open. Renaming a real folder and a junction to an outside folder in and out of d/: 640 of 14134 reads of d/f.txt returned the outside file, main 626. This one is from before this PR (it's path-based and the PR doesn't change it), but the doc comment says "it cannot change between the check and the read", which only holds for the file's type, size and links. Untried idea: compare GetFinalPathNameByHandleW of the open handle with the canonical root.
  • read_capped does cap. A writer thread keeps rewriting a 3 MiB file and appending up to 5.5 MiB while read_file loops: 0 reads over 4 MiB on the PR (133 refused as longer than 4194304 bytes), 12 over 4 MiB on main (largest 5,767,168 bytes).
  • mod.json is the one read left outside open_inside. manifest::read still uses read_to_string with no link or size check. A mod.json hard-linked to an outside JSON file is loaded (its name shows up as the mod's name). A non-JSON one is not echoed: the error says only unexpected text at byte 0. read_file("mod.json") on that mod is refused now (on main it returned the outside JSON).

Tests (cargo test -p leonida-lua --lib --locked, --test-threads=4)

result
main 47 passed, 0 failed
merge 50 passed, 0 failed
merge, api.rs + manifest.rs + runtime.rs reverted to #3226's head, tests kept 48 passed, 2 failed: read_file_refuses_hard_link_to_a_file_outside_the_folder (["hard SECRET", "nested SECRET", "plain OK"]), require_and_the_manifest_script_refuse_a_hard_link (h.lua:1: unexpected symbol near '=')
merge, only runtime.rs reverted require_and_the_manifest_script_refuse_a_hard_link still fails, at the manifest-script assert

clippy -p leonida-lua --all-targets -D warnings: clean. --locked passes, Cargo.lock doesn't change: the Cargo.toml line only adds the Win32_Storage_FileSystem feature to the existing windows-sys dependency. cargo fmt --check: main has two diffs in api.rs (require and teleport), the merge has one (teleport); the rewritten require is formatted.

Probes (throwaway tests calling the real read_file, and require/the manifest script through Runtime::load_dir, in a temp folder, same probes on main and on the merge). All measured.

case main this PR
read_file hard link to an outside file (also nested, also under a junction to an inside folder) returns outside content refused, "is a hard link"
require / manifest script, hard link to outside file with a syntax error fragment leaks in the syntax error refused, no content in the message
same, outside file is valid Lua runs it (SECRET ran) refused, mod b Failed
hard link, both names inside the folder ok (all three) refused (all three); a hard-linked main.lua disables the mod
hard link whose outside name was deleted (1 link left) ok ok
plain file, read-only, hidden ok ok
open elsewhere: rw handle share R/W/D, R/W (no DELETE), R; r handle share R ok ok
open elsewhere, share none os error 32 os error 32 (message now names the file for require)
directory named x.lua read_file refused; script: Access is denied (os error 5) refused, is not a file of at most ...
h.lua is a directory and h/init.lua exists require loads init.lua same
junction to a folder inside ok ok
junction to a folder outside refused refused
dangling junction, the junction itself and a file under it refused refused
200-char name, single and nested / 201-char ok / refused ok / refused
exactly 4 MiB (read_file) / exactly 1 MiB (require, script) ok ok
one byte over refused refused
empty file ok, 0 bytes ok, 0 bytes
s.txt:ads, s.txt::$DATA (the stream exists) refused by the name check refused by the name check
CON, con, NUL, NUL.lua, nul.txt, COM1.lua, LPT1.txt, AUX., PRN , sub/NUL.lua refused by the name check same
CONIN$, CONOUT$, COM¹.lua, LPT², CON .lua, NUL .txt (pass the name check), read_file and manifest script os error 2, no device opened, no hang same
require of CONIN$, COM¹, LONGMO~1, h:ads "bad module name" same
\\.\NUL, \\?\C:\x, //./NUL, //?/C:/x, /x, ./x, sub/../x, trailing dot or space refused by the name check same
8.3 alias (LONGFI~1.TXT, short names are on, seen with dir /x), read_file and manifest script reads the file reads the file; alias of a 2-link file is refused too
read_file loop while another thread calls the real write_file (64 KiB, 4 s) not rerun 787 reads, 0 torn, 0 errors; 2921 writes, 0 errors, no temp file left

Rows of the #3226 table that change with this PR: only the last one, "read_file through a hard link to an outside file", which goes from "still returns outside content" to refused. The write rows are #3226's and stay as they were.

#3212. With #3226 and this PR, a hard link is no longer a way in or out for the files a mod names (write_file, read_file, require, the manifest script). I'm fine closing #3212 with both. The two races above are a different shape (another process changing the folder during the call) and would fit better in a follow-up, along with mod.json.

Something I hit while probing, not from this PR: write_file("MOD~1.JSO", ...) gets past the mod.json guard, since the check is on the name the mod passes. On main it overwrites mod.json. With #3226 it replaces it with a file named MOD~1.JSO, so mod.json is gone (measured). From reading manifest::read, the mod then won't load next time. Only where 8.3 names are on. I didn't find an issue for it and can open one.

Conflicts: open PRs touching these files are #3226 (this PR is stacked on it), #3125 (merges clean with main and with this merge), #3115 (sandbox.rs and tests.rs, same conflict region against main and against this merge) and #3176 (.github/workflows/d3d12.yml only, same on both). This PR adds none.

Not checked: Windows 10 (device-name rules differ there), file symlinks (they need Developer Mode), other filesystems (FAT32/exFAT, network shares, volumes with 8.3 names off), the 2-writer write_file race from the #3226 table, a real mod manager in the game, and the two untried fixes above.

GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7385372688 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3364#issuecomment-7385372688). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-03T16:44:44Z. --- Ran natively on Windows 11 (NTFS), head `bd7ea1cb3` merged into current `main` (`717b90233`, clean merge). I opened #3212, so I ran the same probes as on #3226, this time on the read side. The Windows branch of `link_count` works: every hard link I tried is refused by `read_file`, `require` and the manifest script, and nothing outside the folder comes back. Findings first. - **Trade-off, in practice.** A hard-linked `main.lua` doesn't just fail a `read_file`, it disables the whole mod (`Failed(main.lua is a hard link and cannot be read by a mod)`). That also happens when both names are inside the mod's folder. So a mod manager that deploys by hard link gets mods that don't load at all, not mods that load and lose some reads. Worth saying in the docs line, or the maintainer may want to allow `nlink > 1` when every name is inside the folder (not tried). - **One handle is true for the file, not for the path.** Type, size and link count are checked on the handle that gets read, and swapping a plain file and a hard link under one name gets nothing through (0 of 8584 reads, `main` 1984). Two races are still open. Both need a second process changing the folder while the mod reads. From reading the code the mod can't do that itself: Lua can't make hard links or junctions, and one mod's `read_file` and `write_file` calls don't overlap. Anything that can swap names in the folder mid-read could also just copy the outside file in, so I'd see these as hardening, not a blocker: - **link removed after the open.** Open `r.txt` (a hard link to an outside file, 2 links), then another process deletes `r.txt`. On Windows 11 the delete goes through while the handle is open (`File::open` on Windows shares DELETE by default), and the handle now reports 1 link, so the check passes and the outside content is read. Done step by step on one handle: links 2 before the delete, 1 after, name gone, read returns `SECRET`. In a 4 s race loop: 57 of 28364 reads. Untried idea: open with `share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)` so nobody can delete or rename the name while the handle is open. - **folder swapped for a junction between `inside` and the open.** Renaming a real folder and a junction to an outside folder in and out of `d/`: 640 of 14134 reads of `d/f.txt` returned the outside file, `main` 626. This one is from before this PR (it's path-based and the PR doesn't change it), but the doc comment says "it cannot change between the check and the read", which only holds for the file's type, size and links. Untried idea: compare `GetFinalPathNameByHandleW` of the open handle with the canonical root. - **`read_capped` does cap.** A writer thread keeps rewriting a 3 MiB file and appending up to 5.5 MiB while `read_file` loops: 0 reads over 4 MiB on the PR (133 refused as `longer than 4194304 bytes`), 12 over 4 MiB on `main` (largest 5,767,168 bytes). - **`mod.json` is the one read left outside `open_inside`.** `manifest::read` still uses `read_to_string` with no link or size check. A `mod.json` hard-linked to an outside JSON file is loaded (its `name` shows up as the mod's name). A non-JSON one is not echoed: the error says only `unexpected text at byte 0`. `read_file("mod.json")` on that mod is refused now (on `main` it returned the outside JSON). **Tests** (`cargo test -p leonida-lua --lib --locked`, `--test-threads=4`) | | result | |---|---| | `main` | 47 passed, 0 failed | | merge | 50 passed, 0 failed | | merge, `api.rs` + `manifest.rs` + `runtime.rs` reverted to #3226's head, tests kept | 48 passed, 2 failed: `read_file_refuses_hard_link_to_a_file_outside_the_folder` (`["hard SECRET", "nested SECRET", "plain OK"]`), `require_and_the_manifest_script_refuse_a_hard_link` (`h.lua:1: unexpected symbol near '='`) | | merge, only `runtime.rs` reverted | `require_and_the_manifest_script_refuse_a_hard_link` still fails, at the manifest-script assert | clippy `-p leonida-lua --all-targets -D warnings`: clean. `--locked` passes, `Cargo.lock` doesn't change: the `Cargo.toml` line only adds the `Win32_Storage_FileSystem` feature to the existing `windows-sys` dependency. `cargo fmt --check`: `main` has two diffs in `api.rs` (`require` and `teleport`), the merge has one (`teleport`); the rewritten `require` is formatted. **Probes** (throwaway tests calling the real `read_file`, and `require`/the manifest script through `Runtime::load_dir`, in a temp folder, same probes on `main` and on the merge). All measured. | case | `main` | this PR | |---|---|---| | `read_file` hard link to an outside file (also nested, also under a junction to an inside folder) | returns outside content | refused, "is a hard link" | | `require` / manifest script, hard link to outside file with a syntax error | fragment leaks in the syntax error | refused, no content in the message | | same, outside file is valid Lua | runs it (`SECRET ran`) | refused, mod `b` Failed | | hard link, both names inside the folder | ok (all three) | **refused (all three); a hard-linked `main.lua` disables the mod** | | hard link whose outside name was deleted (1 link left) | ok | ok | | plain file, read-only, hidden | ok | ok | | open elsewhere: rw handle share R/W/D, R/W (no DELETE), R; r handle share R | ok | ok | | open elsewhere, share none | os error 32 | os error 32 (message now names the file for `require`) | | directory named `x.lua` | `read_file` refused; script: `Access is denied (os error 5)` | refused, `is not a file of at most ...` | | `h.lua` is a directory and `h/init.lua` exists | `require` loads `init.lua` | same | | junction to a folder inside | ok | ok | | junction to a folder outside | refused | refused | | dangling junction, the junction itself and a file under it | refused | refused | | 200-char name, single and nested / 201-char | ok / refused | ok / refused | | exactly 4 MiB (`read_file`) / exactly 1 MiB (`require`, script) | ok | ok | | one byte over | refused | refused | | empty file | ok, 0 bytes | ok, 0 bytes | | `s.txt:ads`, `s.txt::$DATA` (the stream exists) | refused by the name check | refused by the name check | | `CON`, `con`, `NUL`, `NUL.lua`, `nul.txt`, `COM1.lua`, `LPT1.txt`, `AUX.`, `PRN `, `sub/NUL.lua` | refused by the name check | same | | `CONIN$`, `CONOUT$`, `COM¹.lua`, `LPT²`, `CON .lua`, `NUL .txt` (pass the name check), `read_file` and manifest script | os error 2, no device opened, no hang | same | | `require` of `CONIN$`, `COM¹`, `LONGMO~1`, `h:ads` | "bad module name" | same | | `\\.\NUL`, `\\?\C:\x`, `//./NUL`, `//?/C:/x`, `/x`, `./x`, `sub/../x`, trailing dot or space | refused by the name check | same | | 8.3 alias (`LONGFI~1.TXT`, short names are on, seen with `dir /x`), `read_file` and manifest script | reads the file | reads the file; alias of a 2-link file is refused too | | `read_file` loop while another thread calls the real `write_file` (64 KiB, 4 s) | not rerun | 787 reads, 0 torn, 0 errors; 2921 writes, 0 errors, no temp file left | Rows of the #3226 table that change with this PR: only the last one, "`read_file` through a hard link to an outside file", which goes from "still returns outside content" to refused. The write rows are #3226's and stay as they were. **#3212.** With #3226 and this PR, a hard link is no longer a way in or out for the files a mod names (`write_file`, `read_file`, `require`, the manifest script). I'm fine closing #3212 with both. The two races above are a different shape (another process changing the folder during the call) and would fit better in a follow-up, along with `mod.json`. Something I hit while probing, not from this PR: `write_file("MOD~1.JSO", ...)` gets past the `mod.json` guard, since the check is on the name the mod passes. On `main` it overwrites `mod.json`. With #3226 it replaces it with a file named `MOD~1.JSO`, so `mod.json` is gone (measured). From reading `manifest::read`, the mod then won't load next time. Only where 8.3 names are on. I didn't find an issue for it and can open one. Conflicts: open PRs touching these files are #3226 (this PR is stacked on it), #3125 (merges clean with `main` and with this merge), #3115 (`sandbox.rs` and `tests.rs`, same conflict region against `main` and against this merge) and #3176 (`.github/workflows/d3d12.yml` only, same on both). This PR adds none. Not checked: Windows 10 (device-name rules differ there), file symlinks (they need Developer Mode), other filesystems (FAT32/exFAT, network shares, volumes with 8.3 names off), the 2-writer `write_file` race from the #3226 table, a real mod manager in the game, and the two untried fixes above. GPUs tested: n/a (CPU-only tests). AI-assisted: yes (Claude Code).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3364:upstream-pr/3364'
git switch 'upstream-pr/3364'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3364'
git switch 'upstream-pr/3364'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3364'
git switch 'upstream-pr/3364'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3364'
git switch 'main'
git merge --squash 'upstream-pr/3364'
git switch 'main'
git merge --ff-only 'upstream-pr/3364'
git switch 'main'
git merge 'upstream-pr/3364'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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!3364
No description provided.