Skip to content

Runtime: GAME: paths through the Universal CRT's opens (RT-354) - #4996

Open
TimIdle wants to merge 2 commits into
ItsNotRexed:mainfrom
TimIdle:rt-354-crt-opens
Open

TimIdle wants to merge 2 commits into
ItsNotRexed:mainfrom
TimIdle:rt-354-crt-opens

Conversation

@TimIdle

@TimIdle TimIdle commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: RT-354 (#4933 (comment))

Related: #4621 (GetFullPathName keeps a GAME: path) touches the same file; no overlap in behaviour, but the shim table at the end of file_system.cpp may need a trivial rebase whichever lands second.

What and why

A package DLL linked against the dynamic CRT opens files with fopen or _open from api-ms-win-crt-stdio-l1-1-0 (libHttpClient.GDK.dll imports fopen, _wopen and _open, per the finding). ucrtbase's own CreateFileW is the host's, so a GAME: path reached it as a relative name and the open failed, and a write could land outside the overlay.

This shims the Universal CRT's opens under api-ms-win-crt-stdio-l1-1-0.dll and ucrtbase.dll: fopen, _wfopen, _fsopen, _wfsopen, fopen_s, _wfopen_s, freopen, _wfreopen, freopen_s, _wfreopen_s, _open, _wopen, _sopen, _wsopen, _sopen_s, _wsopen_s, _sopen_dispatch and _wsopen_dispatch. The last two matter for C++ callers: the _open/_sopen overloads in corecrt_io.h are inline and call _sopen_dispatch, so a shim of _open alone would miss them.

How it works:

  • A console path is mapped by the existing open_target, so the overlay, copy-up, deleted-name and .. rules are the CreateFile ones, not a second copy. The access and disposition come from the mode (r, w, wx, a, +) or the _O_CREAT/_O_EXCL/_O_TRUNC flags the way the CRT derives them: a and r+ copy a package file up first, w creates in the overlay.
  • The mapped host path goes to ucrtbase's own wide function (_wfopen, _wfsopen, _wfopen_s, _wfreopen(_s), _wsopen_dispatch). gta6rt links the static CRT, so a FILE* made inside the runtime would be unusable to a DLL that reads it with ucrtbase's fread; calling ucrtbase keeps the stream and descriptor in the caller's CRT, with its text mode, ccs= and sharing handled by the CRT itself.
  • A refused path sets EINVAL, a missing one ENOENT, in ucrtbase's errno (and _doserrno). Host paths, null names and modes the CRT rejects go to ucrtbase unchanged.

Not covered, and left for follow-ups (I'll post them as findings): the host msvcp140.dll that every std::fstream of a mapped title goes through (gta6rt loads the host copy, whose imports bind to the host CRT; it needs an import redirect in that module), and the CRT's other path calls (remove, rename, _stat, _access, _mkdir).

Tested

Windows 11 (10.0.26300), MSVC 14.44 (VS Build Tools 17.14), Ninja, RelWithDebInfo. NVIDIA GeForce RTX 2050, driver 32.0.16.2124 (no GPU involved in this change).

  • file_system_test: 171 checks, 0 failures. The new block resolves each function through rt::shims::find under the api set and ucrtbase.dll, and reads every result back with ucrtbase's own fread/_read, as a package DLL would: fopen reads a package file, _wfopen "w" creates in the overlay and not the package, fopen "a" copies up and leaves the package as installed, _open reads a package file, _wsopen_dispatch creates a T: file in the scratch folder, a missing file gives ENOENT, GAME:\.. gives EINVAL with nothing written, fopen_s returns ENOENT and clears the stream, and a host path passes through.
  • Negative control: with the mapping disabled in map_open (console paths passed to the CRT as on main), 10 of the new checks fail and the host-path check still passes.
  • Full ctest after a clean scripts/build_runtime.ps1: 66 of 66 pass, the same as main at 3faac557 on this machine (game-gated tests skip). /W4 gives no warning in the changed files.
  • tests/test_docs_links.py, test_text_encoding.py, test_text_write_line_endings.py, test_runtime_paths.py, test_ci_registration.py: OK.

Not tested: a mapped package DLL end to end under gta6rt --run (the test goes through the same registry lookup the loader uses, not through a mapped image), and Wine.

AI-assisted: yes (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

🤖 Generated with Claude Code

A package DLL linked against the dynamic CRT opens files with fopen or _open
from api-ms-win-crt-stdio-l1-1-0, and ucrtbase's own CreateFileW is the
host's, so a GAME: path reached it as a relative name and the open failed.

The fopen, _fsopen, fopen_s, freopen(_s) and _open, _sopen(_s),
_sopen_dispatch families, narrow and wide, are now shimmed under that api set
and ucrtbase.dll. A console path is mapped with open_target, the CreateFile
rules, using the access and disposition the CRT itself derives from the mode
or the _O_ flags, and ucrtbase's wide function opens the host path, so the
FILE* or descriptor belongs to the CRT the caller reads it with (gta6rt links
the static CRT). Host paths go to ucrtbase unchanged.

Not covered here: the host msvcp140.dll that std::fstream goes through, and
the CRT's other path calls (remove, rename, _stat, _access, _mkdir).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested a1935a7 on Windows 11 (NVIDIA GeForce RTX 4080 machine, driver 617.42; the test is CPU-side), cross-built with clang-cl 23.1.5 + xwin: pass.

file_system_test: exit 0, 171 check(s), 0 failure(s), including the new CRT cases:

  • PASS fopen reads a package file through GAME:
  • PASS _wfopen creates a console file
  • PASS append mode copies the package file up and leaves the package as installed
  • PASS _wsopen_dispatch creates a T: file
  • PASS a missing console file fails with ENOENT in the CRT's errno
  • PASS GAME:.. is refused with EINVAL and nothing is written
  • PASS fopen_s returns ENOENT and clears the stream
  • PASS a host path goes to the CRT unchanged

The negative control and the gta6rt end-to-end run were not done here.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent check of a1935a7d — pass on Windows, and directly relevant to booting the game.

Environment: Windows 11, MSVC 19.44 (RelWithDebInfo, Ninja), CPU only, synthetic dirs, no game files.

  • file_system_test.exe on the PR head → 171 checks, 0 failures — fopen/_wfopen/_fsopen/fopen_s through GAME: paths resolve, writes land in the scratch/overlay folder, missing console files give ENOENT/EINVAL through the CRT's errno, host paths pass through unchanged
  • Negative control: same binary against main's file_system.cpp/.h → segfaults at the new UCRT section (the opens don't exist on the parent), so the new checks exercise surface the parent lacks

This one matters for playability: the game will call fopen/_wfopen on GAME: paths through the UCRT, not just Win32 CreateFile — without it the first CRT read of a package file fails.

AI-assisted (Devin).

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reproduced on head a1935a7d with synthetic directories only:

  1. Put GAME:\x in the package directory and leave the overlay copy absent.
  2. Call _wopen(L"GAME:\\x", _O_CREAT | _O_EXCL | _O_WRONLY, _S_IREAD | _S_IWRITE).

It returns a descriptor and creates an overlay file. Exclusive create should fail with EEXIST because the file already exists in the package view. from_oflag maps this combination to CREATE_NEW, but open_target routes it through write_target(..., copy_up=false), which skips the package existence check; the new overlay file then shadows the installed file. Please check the package/overlay union before exclusive create and add a synthetic regression to file_system_test. No game files or GPU were used.

An exclusive create (_O_CREAT|_O_EXCL, fopen "wx", CreateFileW CREATE_NEW) went
to the overlay only, so a package-only file was shadowed by a new empty one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TimIdle

TimIdle commented Nov 23, 2026

Copy link
Copy Markdown
Author

@Flyander thanks, confirmed and fixed in ceb5efe.

open_target now checks the package/overlay view first for CREATE_NEW and fails with ERROR_FILE_EXISTS when the name is visible, so _open/_wopen with _O_CREAT | _O_EXCL and fopen "wx" get EEXIST and nothing is created in the overlay. This was older than this PR: CreateFileW(..., CREATE_NEW) on a package-only file had the same gap on main, so the guard sits in open_target, which covers both.

file_system_test has four new synthetic checks (_open with _O_CREAT | _O_EXCL, _wfopen "wx", CreateFileW CREATE_NEW, and that no overlay file shadows the package one). With the guard removed, the _open check and the no-shadow check fail. file_system_test 175/0; full ctest 66/66 (MSVC 14.44, no Vulkan SDK). No game files used.

@thehollowedone

Copy link
Copy Markdown

Independent Windows x64 check of ceb5efe5 on base 3faac557. Windows build 26300, MSVC 19.44.35230, Windows SDK 10.0.26100.0, RelWithDebInfo.

All four focused assertions passed with synthetic files. _open with _O_CREAT | _O_EXCL and _wfopen with wx both returned EEXIST for the existing package file. CreateFileW with CREATE_NEW returned ERROR_FILE_EXISTS, and no overlay file shadowed the original.

The scratch build used the PR's production files and a test entry point limited to those four published assertions. CPU only. AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested ceb5efe on NVIDIA GeForce RTX 4080, driver 617.42: file_system_test exits 0 with "175 check(s), 0 failure(s)". The output includes "PASS fopen reads a package file through GAME:", "PASS _wfopen creates a console file", "PASS _open reads a package file through GAME:", "PASS _wsopen_dispatch creates a T: file", "PASS fopen_s returns ENOENT and clears the stream", "PASS _wfopen "wx" fails with EEXIST on a package file", "PASS GAME:\.. is refused with EINVAL and nothing is written" and "PASS a host path goes to the CRT unchanged". This run does not cover a mapped package DLL under gta6rt or Wine.

AI-assisted.

@Davidutz19

Copy link
Copy Markdown

Tested ceb5efe5d with the title's own crash reporter on NixOS, a clang-cl 21.1.8 cross build, Wine 11.21 Staging, GTX 1060 / NVIDIA 580. Pass.

Base: a local integration branch at 5f2d8fc36 (main plus #7398 and other open runtime PRs), with and without this PR merged. The only conflicts were unions of the #include lists, the docs/runtime_imports.md bullet list and the end of file_system_test.cpp. --run with --overlay, no network, two runs each. Both builds reach the title's crash handler about 15 s in, and both write the minidump through CreateFileW to T:\ (overlay .gta6rt-temp).

  • Without this PR: no crash log. The title's OutputDebugString reports that it failed to save its T:\game-*.crash.log (WER message), in both runs.
  • With this PR: the crash log is written next to the dump in .gta6rt-temp, one per run (2.3 KB of text), and the failure message is gone. fopen on a T:\ path through api-ms-win-crt-stdio-l1-1-0.dll now maps.

Still failing with this PR: a later step of the crash reporter that re-reads the same log to attach it to its upload. It reports that it cannot add the file. The upload is offline under winhttp_offline anyway. I did not check which call that read uses. It may be one of the ones the PR lists as not covered, such as msvcp140.

AI-assisted (Claude). No game data.

@airp1ane

Copy link
Copy Markdown
Contributor

Security review (static): untrusted DLL search order when loading ucrtbase.dll (CWE-427)

  • Finding: In runtime/shims/file_system.cpp (ucrt()), ucrtbase.dll is loaded via plain ::LoadLibraryW(L"ucrtbase.dll") without LOAD_LIBRARY_SEARCH_SYSTEM32, and module is passed to ::GetProcAddress without a null check:
    template <typename Fn>
    Fn ucrt(const char* name) {
        static const HMODULE module = ::LoadLibraryW(L"ucrtbase.dll");
        return reinterpret_cast<Fn>(::GetProcAddress(module, name));
    }
    Because gta6rt.exe runs with its working directory in the game package folder (which may contain bundled CRT DLLs or untrusted files), plain LoadLibraryW searches the application directory and current working directory ahead of System32.
  • Suggested fix: Use ::LoadLibraryExW(L"ucrtbase.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) (matching pe_loader.cpp, winhttp_offline.cpp, d3d12_xs.cpp, and loader_test.cpp) and guard against module == nullptr before calling ::GetProcAddress.

(Note: stacked PRs #5010, #5027, and #5160 inherit this same ucrt() helper.)

(Reviewed at head ceb5efe5df.)


Static security review (AI-assisted, read-only diff audit; no code or game files were executed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants