Loader: verify OpenThread owner PID before reading thread stack #2989
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2989
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2989"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@fuzeofficlal on GitHub · 2026-11-26 02:26 UTC · original
fuzeofficlal:fix/loader-stacks-verify-thread-pid→main· +38 −3 · 1 files · 1 commits · openProblem
dump_stacksincrates/victory-loader/src/stacks.rscollects thread IDs owned by the current process fromCreateToolhelp32Snapshot, then opens each TID withOpenThreadand immediately passes the handle toframes_of(SuspendThread->NtQueryInformationThread-> readinginfo.tebin our address space).If a thread exits between the snapshot and
OpenThreadand Windows recycles its TID to another process,dump_stackscan open and suspend a thread belonging to that other process and dereference a foreign TEB pointer in our address space (potentially faulting beforeResumeThread).Fix
thread_in_process(thread, process)usingGetProcessIdOfThread(already permitted byTHREAD_QUERY_INFORMATION).thread_in_process(thread, process)immediately afterOpenThread(which pins the kernel thread object so its owner PID cannot change) and close/skip the handle beforeframes_ofon mismatch or failure.Verification
stacks::tests::thread_in_process_verifies_handle_owner_pidverifying a realOpenThread(THREAD_QUERY_INFORMATION, ...)handle against the current PID, mismatched PID, PID0, null handle, andINVALID_HANDLE_VALUE.cargo test --locked -p victory-loader --features diagnostics: 42/42 passed.cargo test --locked -p victory-loader: 39/39 passed.cargo clippy --locked -p victory-loader --tests -- -D warnings(default &--features diagnostics): 0 warnings.cargo xtask policy: 191 files ok.@futurepro62 on GitHub · 2026-11-26 05:04 UTC · original
Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head
019aded(on its base63bcca4).Tested
dxil.dllfrom Windows SDK 10.0.26100 (the loader's default), debug.cargo build --workspace --locked, thencargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 866 passed, 0 failed, 10 ignored.thread_in_process_verifies_handle_owner_pidis behind#[cfg(all(windows, feature = "diagnostics"))](thestacksmodule), so the workspace run above does not build it. On the same head:cargo test -p victory-loader --lib --locked --features diagnostics -- stacks::: 3 passed, 0 failed (the new test plus the two existingstackstests).--skipleaves out tests that fail on main63bcca4on this card too, not because of this PR:heap_direct_texture_reads_mark_their_heap_indexends thevictory-shadertest process with0xc0000005(#2571), so without the skip the rest of that crate never reports;dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_viewsand the twostride_testshit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317);gathers_at_a_level_match_the_hardwarefails on lane 0 (#1744).Not tested
AI use
@Marvisak on GitHub · 2026-11-26 05:48 UTC · original
Automated triage for the maintainers.
Merge candidate (small).
stacks::dump_stackstakes thread ids from a Toolhelp snapshot and opens them later. If a thread exits in between and its id is reused by another process, the old code would suspend that thread and read its TEB pointer as if it were ours. The newthread_in_processchecksGetProcessIdOfThreadon the opened handle (allowed by theTHREAD_QUERY_INFORMATIONaccess already requested), and skips and closes the handle on a mismatch beforeframes_of. The open handle keeps the thread object alive, so the check can't race. The race is rare, but the fix is cheap and correct.Nit: the
is_null()check insidethread_in_processrepeats the caller's. Harmless.The new test sits in the
stacksmodule, which is only built withdiagnostics. A tester ran it with that feature (3/3stacks::tests passed, RTX 5070 Ti).CI: passing.
Recommending a look, not approving.
Generated by Claude Code
@kotekompotek on GitHub · 2026-11-26 02:36 UTC · original · reviewed
Verified: real TOCTOU — thread IDs are process-table slots, and between the Toolhelp snapshot and OpenThread an ID can be reused by another process, after which we'd suspend and read a foreign thread's stack. GetProcessIdOfThread is the right guard (returns 0 on failure, which also covers INVALID_HANDLE_VALUE in the test). Scoped to threads only — the process-level entries in the same dump need no check.
@Dukestar69 on GitHub · 2026-11-26 07:31 UTC · original · approved these changes
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64:cargo test -p victory-loader --features diagnostics -- --test-threads=1: all tests passed.cargo clippy -p victory-loader --features diagnostics --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@cloph-dsp on GitHub · 2026-11-26 08:50 UTC · original
Independent local verification of head
019adedon Windows 11, Rust 1.100.0/MSVC:cargo test --locked -p victory-loader --lib --features diagnostics stacks::tests::thread_in_process_verifies_handle_owner_pid— 1 passed, 0 failed.git diff --check origin/main HEAD— clean.Note: the test lives behind the
diagnosticsfeature, so--features diagnosticsis required for it to run. Synthetic Win32 queries only; no game assets or DRM material used. AI-assisted (Hy4 flash).View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.