Loader: verify OpenThread owner PID before reading thread stack #2989

Open
github-bot wants to merge 1 commit from github-bot/gh-2989 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 02:26 UTC · original
fuzeofficlal:fix/loader-stacks-verify-thread-pid → main · +38 −3 · 1 files · 1 commits · open

Problem

dump_stacks in crates/victory-loader/src/stacks.rs collects thread IDs owned by the current process from CreateToolhelp32Snapshot, then opens each TID with OpenThread and immediately passes the handle to frames_of (SuspendThread -> NtQueryInformationThread -> reading info.teb in our address space).

If a thread exits between the snapshot and OpenThread and Windows recycles its TID to another process, dump_stacks can open and suspend a thread belonging to that other process and dereference a foreign TEB pointer in our address space (potentially faulting before ResumeThread).

Fix

  • Add thread_in_process(thread, process) using GetProcessIdOfThread (already permitted by THREAD_QUERY_INFORMATION).
  • Verify thread_in_process(thread, process) immediately after OpenThread (which pins the kernel thread object so its owner PID cannot change) and close/skip the handle before frames_of on mismatch or failure.

Verification

  • Added stacks::tests::thread_in_process_verifies_handle_owner_pid verifying a real OpenThread(THREAD_QUERY_INFORMATION, ...) handle against the current PID, mismatched PID, PID 0, null handle, and INVALID_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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 02:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/2989) `fuzeofficlal:fix/loader-stacks-verify-thread-pid` → `main` · +38 −3 · 1 files · 1 commits · open ## Problem `dump_stacks` in `crates/victory-loader/src/stacks.rs` collects thread IDs owned by the current process from `CreateToolhelp32Snapshot`, then opens each TID with `OpenThread` and immediately passes the handle to `frames_of` (`SuspendThread` -> `NtQueryInformationThread` -> reading `info.teb` in our address space). If a thread exits between the snapshot and `OpenThread` and Windows recycles its TID to another process, `dump_stacks` can open and suspend a thread belonging to that other process and dereference a foreign TEB pointer in our address space (potentially faulting before `ResumeThread`). ## Fix - Add `thread_in_process(thread, process)` using `GetProcessIdOfThread` (already permitted by `THREAD_QUERY_INFORMATION`). - Verify `thread_in_process(thread, process)` immediately after `OpenThread` (which pins the kernel thread object so its owner PID cannot change) and close/skip the handle before `frames_of` on mismatch or failure. ## Verification - Added `stacks::tests::thread_in_process_verifies_handle_owner_pid` verifying a real `OpenThread(THREAD_QUERY_INFORMATION, ...)` handle against the current PID, mismatched PID, PID `0`, null handle, and `INVALID_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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:28 +00:00
Author
Owner

@⁠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 base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo 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.
  • The new test thread_in_process_verifies_handle_owner_pid is behind #[cfg(all(windows, feature = "diagnostics"))] (the stacks module), 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 existing stacks tests).
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#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_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-26 05:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/2989#issuecomment-7183149960) Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head `019aded` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo 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**. - The new test `thread_in_process_verifies_handle_owner_pid` is behind `#[cfg(all(windows, feature = "diagnostics"))]` (the `stacks` module), 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 existing `stacks` tests). - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#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_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:48 UTC · original

Automated triage for the maintainers.

Merge candidate (small). stacks::dump_stacks takes 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 new thread_in_process checks GetProcessIdOfThread on the opened handle (allowed by the THREAD_QUERY_INFORMATION access already requested), and skips and closes the handle on a mismatch before frames_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 inside thread_in_process repeats the caller's. Harmless.

The new test sits in the stacks module, which is only built with diagnostics. A tester ran it with that feature (3/3 stacks:: tests passed, RTX 5070 Ti).

CI: passing.

Recommending a look, not approving.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 05:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/2989#issuecomment-7183827861) <!-- victory-triage v1 head=019aded41dcc6ab62e10c5645a177b1f6abe1c28 verdict=merge-candidate at=2026-11-26T05:48:38Z --> Automated triage for the maintainers. **Merge candidate (small).** `stacks::dump_stacks` takes 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 new `thread_in_process` checks `GetProcessIdOfThread` on the opened handle (allowed by the `THREAD_QUERY_INFORMATION` access already requested), and skips and closes the handle on a mismatch before `frames_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 inside `thread_in_process` repeats the caller's. Harmless. The new test sits in the `stacks` module, which is only built with `diagnostics`. A tester ran it with that feature (3/3 `stacks::` tests passed, RTX 5070 Ti). CI: passing. Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠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.

**@⁠kotekompotek** on GitHub · 2026-11-26 02:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/2989#pullrequestreview-6067811056) · **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.
Author
Owner

@⁠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).

**@⁠Dukestar69** on GitHub · 2026-11-26 07:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2989#pullrequestreview-6070075428) · **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).
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 08:50 UTC · original

Independent local verification of head 019aded on 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 diagnostics feature, so --features diagnostics is required for it to run. Synthetic Win32 queries only; no game assets or DRM material used. AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-26 08:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2989#issuecomment-7187086377) Independent local verification of head `019aded` on 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 `diagnostics` feature, so `--features diagnostics` is required for it to run. Synthetic Win32 queries only; no game assets or DRM material used. AI-assisted (Hy4 flash).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2989/head:github-bot/gh-2989'
git switch 'github-bot/gh-2989'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2989'
git switch 'github-bot/gh-2989'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2989'
git switch 'github-bot/gh-2989'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2989'
git switch 'main'
git merge --squash 'github-bot/gh-2989'
git switch 'main'
git merge --ff-only 'github-bot/gh-2989'
git switch 'main'
git merge 'github-bot/gh-2989'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2989
No description provided.