Loader: fix 4 bounds/overflow bugs in tls.rs and stacks.rs with unit tests #938

Open
opened 2026-11-26 04:14:31 +00:00 by github-bot · 2 comments
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 14:21 UTC · original

Summary

Fixes 4 real bugs in crates/victory-loader/src/tls.rs and crates/victory-loader/src/stacks.rs, and expands unit tests (tls:: 1 -> 14, stacks:: 1 -> 2).

Because external PR creation is currently restricted (collaborators_only), the CI-green branch is ready on my fork for cherry-pick / review:

  • Branch: geiyiliyabumo/VIctory@test/loader-tls-and-stacks
  • Commit: geiyiliyabumo/VIctory@02ea74c (based on main ffe310e)

Bugs Fixed

  • crates/victory-loader/src/tls.rs:
    1. Bounds-checked the TLS directory and AddressOfIndex against size_of_image (previously followed unchecked, allowing reads past the image and writing 0 over an out-of-image u32).
    2. Guarded index * 8 against debug overflow for image-supplied indices near u32::MAX.
    3. Skipped zero-virtual_size executable sections instead of calling VirtualProtect(..., 0, ...) (OS error 87) and aborting redirect.
  • crates/victory-loader/src/stacks.rs:
    4. Extracted delays() to reject non-finite / out-of-range VICTORY_STACKS values (inf, NaN, 1e300) instead of panicking in Duration::from_secs_f64.

Verification (Windows 11 x64 MSVC)

  • rustfmt --edition 2024 --check: PASS
  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings (default + --features diagnostics): PASS (0 warnings)
  • cargo test -p victory-loader --locked -- tls:: (14/14 PASS) & --features diagnostics -- stacks:: (2/2 PASS)

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 14:21 UTC · [original](https://github.com/Marvisak/VIctory/issues/938) ### Summary Fixes 4 real bugs in `crates/victory-loader/src/tls.rs` and `crates/victory-loader/src/stacks.rs`, and expands unit tests (`tls::` 1 -> 14, `stacks::` 1 -> 2). Because external PR creation is currently restricted (`collaborators_only`), the CI-green branch is ready on my fork for cherry-pick / review: - **Branch**: `geiyiliyabumo/VIctory@test/loader-tls-and-stacks` - **Commit**: `geiyiliyabumo/VIctory@02ea74c` (based on `main` `ffe310e`) ### Bugs Fixed - **`crates/victory-loader/src/tls.rs`**: 1. Bounds-checked the TLS directory and `AddressOfIndex` against `size_of_image` (previously followed unchecked, allowing reads past the image and writing `0` over an out-of-image `u32`). 2. Guarded `index * 8` against debug overflow for image-supplied indices near `u32::MAX`. 3. Skipped zero-`virtual_size` executable sections instead of calling `VirtualProtect(..., 0, ...)` (OS error 87) and aborting redirect. - **`crates/victory-loader/src/stacks.rs`**: 4. Extracted `delays()` to reject non-finite / out-of-range `VICTORY_STACKS` values (`inf`, `NaN`, `1e300`) instead of panicking in `Duration::from_secs_f64`. ### Verification (Windows 11 x64 MSVC) - `rustfmt --edition 2024 --check`: **PASS** - `cargo xtask policy`: **PASS** (`154 files ok`) - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings` (default + `--features diagnostics`): **PASS** (0 warnings) - `cargo test -p victory-loader --locked -- tls::` (**14/14 PASS**) & `--features diagnostics -- stacks::` (**2/2 PASS**) *AI-assisted; synthetic tests only, no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:31 +00:00
Author
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 14:41 UTC · original

Independent Verification Review: geiyiliyabumo:test/loader-tls-and-stacks (02ea74c)

Audited the PE TLS directory validation, section protection bounds, and stack schedule parsing:


1. Correctness of Bug Fixes

A. TLS Directory & Index Bounds (crates/victory-loader/src/tls.rs)

  • Vulnerability: Untrusted PE headers could supply an AddressOfIndex pointing outside the image mapping (or near the end of address space). Previously, redirect performed an unchecked dereference and write (index_at.write(0)), potentially corrupting host memory or crashing with an access violation.
  • Bounds check: Validating directory + TLS64_ADDRESS_OF_INDEX + size_of::<usize>() <= size prevents running past image boundaries when reading the directory entry.
  • Empty Section Protection: VirtualProtect strictly rejects dwSize == 0 with ERROR_INVALID_PARAMETER (87). Skipping sections where virtual_size == 0 avoids aborting redirects on images containing empty placeholder sections (e.g. metadata or discardable debug headers).
  • Index Range: Bounding index <= MAX_INDEX prevents arithmetic overflow when calculating thunk offsets (index * 8).

B. Robust Float Duration Parsing (crates/victory-loader/src/stacks.rs)

  • Panic Vector: In dump_stacks_on_schedule, Duration::from_secs_f64 unconditionally panics if t is NaN, inf, or exceeds the maximum representable Duration (~1e19 seconds).
  • Delays Extraction: delays() properly filters with t.is_finite() and Duration::try_from_secs_f64(t.max(0.0)).ok().

2. Minor Hardening Recommendation (Integer Overflow Edge Case)

In crates/victory-loader/src/tls.rs:

let at = index_at as usize;
if at < base || at - base + size_of::<u32>() > size {
    bail!("_tls_index at {at:#x} is outside the image");
}

If index_at contains an arbitrary 64-bit value where at >= base and at - base > usize::MAX - 4, evaluating at - base + size_of::<u32>() will overflow and wrap around in release builds (or panic in debug builds).
To be completely watertight against arbitrary pointer values, consider bounds-checking without addition:

if at < base || at - base > size.saturating_sub(size_of::<u32>()) {
    bail!("_tls_index at {at:#x} is outside the image");
}

(or via (at - base).checked_add(size_of::<u32>()).map_or(true, |end| end > size)).


3. Test Fixture & Policy Compliance

  • The Synthetic image fixture in tls::tests cleanly allocates a mock PE image in private memory with VirtualAlloc, tests redirects, verifies slot calculations against tls_array(), and frees via Drop.
  • All tests use synthetic buffers and mock structures without external binaries (100% Rule #1 compliant).

Solid catch and ready to merge.

**@⁠DaniiL35** on GitHub · 2026-11-24 14:41 UTC · [original](https://github.com/Marvisak/VIctory/issues/938#issuecomment-7134780332) ### Independent Verification Review: geiyiliyabumo:test/loader-tls-and-stacks (`02ea74c`) Audited the PE TLS directory validation, section protection bounds, and stack schedule parsing: --- ### 1. Correctness of Bug Fixes #### A. TLS Directory & Index Bounds (`crates/victory-loader/src/tls.rs`) - **Vulnerability**: Untrusted PE headers could supply an `AddressOfIndex` pointing outside the image mapping (or near the end of address space). Previously, `redirect` performed an unchecked dereference and write (`index_at.write(0)`), potentially corrupting host memory or crashing with an access violation. - **Bounds check**: Validating `directory + TLS64_ADDRESS_OF_INDEX + size_of::<usize>() <= size` prevents running past image boundaries when reading the directory entry. - **Empty Section Protection**: `VirtualProtect` strictly rejects `dwSize == 0` with `ERROR_INVALID_PARAMETER` (`87`). Skipping sections where `virtual_size == 0` avoids aborting redirects on images containing empty placeholder sections (e.g. metadata or discardable debug headers). - **Index Range**: Bounding `index <= MAX_INDEX` prevents arithmetic overflow when calculating thunk offsets (`index * 8`). #### B. Robust Float Duration Parsing (`crates/victory-loader/src/stacks.rs`) - **Panic Vector**: In `dump_stacks_on_schedule`, `Duration::from_secs_f64` unconditionally panics if `t` is `NaN`, `inf`, or exceeds the maximum representable `Duration` (`~1e19` seconds). - **Delays Extraction**: `delays()` properly filters with `t.is_finite()` and `Duration::try_from_secs_f64(t.max(0.0)).ok()`. --- ### 2. Minor Hardening Recommendation (Integer Overflow Edge Case) In `crates/victory-loader/src/tls.rs`: ```rust let at = index_at as usize; if at < base || at - base + size_of::<u32>() > size { bail!("_tls_index at {at:#x} is outside the image"); } ``` If `index_at` contains an arbitrary 64-bit value where `at >= base` and `at - base > usize::MAX - 4`, evaluating `at - base + size_of::<u32>()` will overflow and wrap around in release builds (or panic in debug builds). To be completely watertight against arbitrary pointer values, consider bounds-checking without addition: ```rust if at < base || at - base > size.saturating_sub(size_of::<u32>()) { bail!("_tls_index at {at:#x} is outside the image"); } ``` (or via `(at - base).checked_add(size_of::<u32>()).map_or(true, |end| end > size)`). --- ### 3. Test Fixture & Policy Compliance - The `Synthetic` image fixture in `tls::tests` cleanly allocates a mock PE image in private memory with `VirtualAlloc`, tests redirects, verifies slot calculations against `tls_array()`, and frees via `Drop`. - All tests use synthetic buffers and mock structures without external binaries (100% Rule #1 compliant). Solid catch and ready to merge.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:59 UTC · original

Automated triage for the maintainers.

Part of this is already on main: crates/victory-loader/src/stacks.rs now parses the VICTORY_STACKS times with Duration::try_from_secs_f64 (PR #221), so inf/NaN no longer panic. tls::redirect in crates/victory-loader/src/tls.rs still follows the TLS directory and index without bounds checks and calls VirtualProtect on every executable section, including ones with a zero virtual_size; the image is the title's own, so the bounds checks guard malformed input rather than a case seen in the title. The branch is open as PR #1192, where review continues.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:59 UTC · [original](https://github.com/Marvisak/VIctory/issues/938#issuecomment-7155377783) <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-24T14:41:09Z at=2026-11-25T06:59:56Z --> Automated triage for the maintainers. Part of this is already on `main`: `crates/victory-loader/src/stacks.rs` now parses the `VICTORY_STACKS` times with `Duration::try_from_secs_f64` (PR #221), so `inf`/`NaN` no longer panic. `tls::redirect` in `crates/victory-loader/src/tls.rs` still follows the TLS directory and index without bounds checks and calls `VirtualProtect` on every executable section, including ones with a zero `virtual_size`; the image is the title's own, so the bounds checks guard malformed input rather than a case seen in the title. The branch is open as PR #1192, where review continues. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#938
No description provided.