loader/stacks: suspend target thread before querying and reading its TEB stack bounds #586

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

@⁠AscilCH on GitHub · 2026-11-25 02:35 UTC · original
AscilCH:fix/loader-stacks-suspend-before-teb-read → main · +47 −22 · 1 files · 1 commits · open

Problem

In crates/victory-loader/src/stacks.rs, frames_of() queried NtQueryInformationThread(ThreadBasicInformation) and directly dereferenced info.teb.add(1) / info.teb.add(2) (StackBase / StackLimit) while the target thread was still running, before calling SuspendThread(thread).

  1. If a transient worker thread exited between OpenThread/NtQueryInformationThread and reading info.teb, Windows unmapped its TEB and frames_of() crashed with STATUS_ACCESS_VIOLATION.
  2. If the thread grew its stack between reading StackLimit and SuspendThread, unwind_into received a stale low bound and truncated the stack trace.

Fix

  • Call SuspendThread(thread) first in frames_of(), then query NtQueryInformationThread(ThreadBasicInformation) while suspended, verify status == 0 && info.exit_status == STATUS_PENDING && !info.teb.is_null(), check low <= high, unwind, and always call ResumeThread(thread).
  • Add synthetic unit test frames_of_handles_exited_thread_safely.

Verification

  • cargo test -p victory-loader --all-features --locked stacks::tests: 2 passed, 0 failed
  • cargo test -p victory-loader --locked: 24 unit + 3 integration passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 02:35 UTC · [original](https://github.com/fxceless/victory-fork/pull/586) `AscilCH:fix/loader-stacks-suspend-before-teb-read` → `main` · +47 −22 · 1 files · 1 commits · open ## Problem In `crates/victory-loader/src/stacks.rs`, `frames_of()` queried `NtQueryInformationThread(ThreadBasicInformation)` and directly dereferenced `info.teb.add(1)` / `info.teb.add(2)` (`StackBase` / `StackLimit`) **while the target thread was still running**, before calling `SuspendThread(thread)`. 1. If a transient worker thread exited between `OpenThread`/`NtQueryInformationThread` and reading `info.teb`, Windows unmapped its TEB and `frames_of()` crashed with `STATUS_ACCESS_VIOLATION`. 2. If the thread grew its stack between reading `StackLimit` and `SuspendThread`, `unwind_into` received a stale `low` bound and truncated the stack trace. ## Fix - Call `SuspendThread(thread)` first in `frames_of()`, then query `NtQueryInformationThread(ThreadBasicInformation)` while suspended, verify `status == 0 && info.exit_status == STATUS_PENDING && !info.teb.is_null()`, check `low <= high`, unwind, and always call `ResumeThread(thread)`. - Add synthetic unit test `frames_of_handles_exited_thread_safely`. ## Verification - `cargo test -p victory-loader --all-features --locked stacks::tests`: 2 passed, 0 failed - `cargo test -p victory-loader --locked`: 24 unit + 3 integration passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
- Move `SuspendThread(thread)` ahead of `NtQueryInformationThread` and the
  TEB `StackBase`/`StackLimit` reads in `frames_of()`, and require
  `info.exit_status == STATUS_PENDING && !info.teb.is_null() && low <= high`
  before dereferencing `info.teb` and unwinding.
- Eliminates both the TOCTOU access violation when a thread exits and unmaps
  its TEB between `NtQueryInformationThread` and `SuspendThread`, and stale
  `StackLimit` reads when a live thread grows its stack before suspension.
- Add synthetic unit test `frames_of_handles_exited_thread_safely`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:53 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/586/head:github-bot/gh-586'
git switch 'github-bot/gh-586'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-586'
git switch 'github-bot/gh-586'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-586'
git switch 'github-bot/gh-586'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-586'
git switch 'main'
git merge --squash 'github-bot/gh-586'
git switch 'main'
git merge --ff-only 'github-bot/gh-586'
git switch 'main'
git merge 'github-bot/gh-586'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.