Skip to content

Runtime: fix use-after-free when parsing --archetype-lookup - #5851

Open
Soled10 wants to merge 1 commit into
ItsNotRexed:mainfrom
Soled10:fix-archetype-rva-lifetime
Open

Soled10 wants to merge 1 commit into
ItsNotRexed:mainfrom
Soled10:fix-archetype-rva-lifetime

Conversation

@Soled10

@Soled10 Soled10 commented Nov 23, 2026

Copy link
Copy Markdown

What and why

In runtime/src/main.cpp, the --archetype-lookup parser passes a temporary
std::wstring substring to std::wcstoul. The temporary is destroyed at the
end of that statement, leaving the returned end pointer dangling. The next
statement dereferences that pointer to check for trailing characters.

Keep the RVA substring in a local std::wstring until the end-pointer check
completes. This preserves the existing RVA[,ARG] syntax and validation.

Tested

A standalone C++20 reproduction built with GCC and AddressSanitizer on Linux
reported heap-use-after-free with the original code. The revised code passed
all five cases:

  • Valid hexadecimal RVA without an argument suffix.
  • Valid hexadecimal RVA with the ,1 suffix.
  • Empty input rejected.
  • Zero RVA rejected.
  • Trailing non-hexadecimal characters rejected.

The full Windows runtime was outside the scope of this test. The reproduction
used synthetic inputs and required no GPU or game files.

Coordination

Tracking issue: #5781. The separate claim comment is pending.

AI-assisted: Yes, OpenAI Codex.

Checklist

  • No game data, keys or decryption.
  • Translator rule documentation is not applicable to this runtime-only change.

wcstoul returns a pointer into its input string. Retain the RVA substring until the end-pointer check is complete.
@DenRakEiw

Copy link
Copy Markdown
Contributor

Independent hardware/Windows build review of 6883ae8e6d3789867c84c0ccc68f97d344159e12 (trial-merged onto origin/main 561b65a83) — LGTM.

Environment

  • OS / Toolchain: Windows 11 x64, MSVC 2022 (19.44.35230.0), Ninja, Vulkan SDK 1.4.363.0, Python 3.12.10

Verification

  • git diff --cached --check: clean (0 issues)
  • cmake --build build --target gta6rt (MSVC 2022 Release, Ninja, with Vulkan SDK): built cleanly ([34/34] Linking CXX executable gta6rt.exe)
  • python -I tests/test_build_runtime_fallback.py -v: 6/6 passed
  • gta6rt.exe --dry-run --archetype-lookup <arg> nonexistent.exe (9 CLI cases, including long hex RVAs > MSVC's 8-wchar_t std::wstring SSO threshold): 9/9 passed
    • Valid (0x12345678, 0x12345678,1, 0x12345678,0, 123456789): parsed cleanly and proceeded past CLI validation (rc=1, error: cannot open nonexistent.exe)
    • Invalid ("", 0, 0,1, 0x12345678xyz, 0x12345678xyz,1): rejected with usage (rc=2)

Notes

  • Storing v.substr(0, comma) in const std::wstring address keeps the backing buffer alive across std::wcstoul and the *end check, eliminating the dangling-pointer UAF. Adding #include <cwchar> for std::wcstoul is also good hygiene. Checked the adjacent --view-mode-getter branch in runtime/src/main.cpp as well — it passes argv[++i] directly and has no equivalent lifetime issue.

AI-assisted (Gemini 4 Neon).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 6883ae8 merged with main 561b65a on Linux (WSL2), g++ with -fsanitize=address, standalone repro of the changed lines only (the runtime was not built; there is no test for this option): pass

The diff is 3 lines in runtime/src/main.cpp. I copied the --archetype-lookup parse into a 15-line program (input longer than the SSO buffer):

  • Main's form, std::wcstoul(v.substr(0, comma).c_str(), &end, 16) followed by *end: AddressSanitizer reports heap-use-after-free on the read of *end (the substr temporary is already destroyed).
  • The PR's form (const std::wstring address = v.substr(0, comma); then address.c_str()): no report, prints ok.
    Counts: 2 programs run, bad form fails, fixed form passes. <cwchar> is the right header for wcstoul. The change is behaviour-neutral otherwise; I did not run it on Windows or build the runtime.

@vibryxcx

Copy link
Copy Markdown

Tested 6883ae8 on AMD Radeon RX 6800 (RDNA2, discrete, gfx1030), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200), MSVC 14.44, RelWithDebInfo, full runtime build (gta6rt.exe included): pass, nothing changed by this PR.

  • The full Windows runtime builds with the change (the part the description left out of scope). There is no test for --archetype-lookup, so this checks the build and that nothing else moved, not the fix itself; @kapitche's and the description's ASan repros cover that.
  • ctest: 6 failures, all also failing on main 561b65a8 on this card in the same run (d3d12_indirect_dispatch_count, d3d12_m2_formats, d3d12_m2_textured, d3d12_m3_frame, d3d12_pipeline_library_host, d3d12_texture_tiles). They're main regressions here, unrelated to main.cpp; I'll look at them separately.
  • run_gpu_tests.py: 214/232 vs 215/232 on main; the difference is ms_wave_info_s3 (got -1.0 everywhere), the main failure @leozinhoszg bisected to test(shader): cover mesh allocation across a wave64 barrier (SHADER-761) #4298's merge; it passed in the main run here and failed in this one, so it is intermittent on this card, not this PR.

No game files. AI-assisted (Claude Code).

@vibryxcx

Copy link
Copy Markdown

Correction to my note above: I shouldn't have called the six ctest failures main regressions. They come from an incremental build reused across many main updates, and #3854 (stale M2 shader modules after a rebuild) matches at least the d3d12_m2_*/d3d12_m3_frame ones. They are the same with and without this PR, so the verdict for this PR stands; I'll check them on a clean build before reporting anything about main.

AI-assisted (Claude Code).

Kam471 added a commit to Kam471/Leonida that referenced this pull request Nov 23, 2026
The kit now ships gta6rt-final.exe (base ba4b0bd + ItsNotRexed#3777 + ItsNotRexed#5867/ItsNotRexed#5808/ItsNotRexed#5851 + ItsNotRexed#6230 @ 255efe0 + ItsNotRexed#6107/ItsNotRexed#6149 + runtime layer 7bce00b + ItsNotRexed#6351 XMemTransferMemory). Prefer it, fall back to gta6rt-final-pre-xmem.exe, then to the trace-only gta6rt.exe; verify the selected binary's hash and the five strings that decide capture and the Series startup path. Add the discover-presentx-slot.ps1 helper and its launch-exeday.ps1 bin caveat.
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.

4 participants