Loader: validate TLS pointers and bounds in OwnTls #3367

Open
Arkenoz wants to merge 1 commit from upstream-pr/3367 into main
Arkenoz commented 2026-11-26 20:38:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Arkenoz on 2026-11-26T20:38:46Z.


Tracked in #1633.

Problem

In crates/victory-loader/src/threads.rs, OwnTls::save() dereferenced TEB.ThreadLocalStoragePointer (gs:[0x58]), (*dir).index, and *array.add(slot) without null checks, and computed the TLS block length with unchecked arithmetic ((*dir).end - (*dir).start + (*dir).zero_fill as usize).

Changes

  • Extracted a private save_from(array, dir) -> Option<OwnTls> helper that validates non-null array, dir, (*dir).index, and resolved block, computes the block length with checked_sub/checked_add, and returns None for zero-length or invalid bounds.
  • Updated OwnTls::save() to delegate to save_from and on_thread to only restore when Some(saved) is returned.
  • Guarded OwnTls::restore() against null block or empty saved.
  • Added unit tests in threads::tests covering null pointers, invalid bounds/overflow/zero length, and synthetic TLS save/restore round-trip.

Verification

  • cargo test -p victory-loader (38 unit + 4 integration tests passed)
  • cargo clippy -p victory-loader --all-targets (0 warnings)

AI-assisted with Gemini.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5516844316 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3367). Originally posted by [Arkenoz](https://github.com/Arkenoz) on 2026-11-26T20:38:46Z. --- Tracked in #1633. ### Problem In `crates/victory-loader/src/threads.rs`, `OwnTls::save()` dereferenced `TEB.ThreadLocalStoragePointer` (`gs:[0x58]`), `(*dir).index`, and `*array.add(slot)` without null checks, and computed the TLS block length with unchecked arithmetic (`(*dir).end - (*dir).start + (*dir).zero_fill as usize`). ### Changes - Extracted a private `save_from(array, dir) -> Option<OwnTls>` helper that validates non-null `array`, `dir`, `(*dir).index`, and resolved `block`, computes the block length with `checked_sub`/`checked_add`, and returns `None` for zero-length or invalid bounds. - Updated `OwnTls::save()` to delegate to `save_from` and `on_thread` to only restore when `Some(saved)` is returned. - Guarded `OwnTls::restore()` against null `block` or empty `saved`. - Added unit tests in `threads::tests` covering null pointers, invalid bounds/overflow/zero length, and synthetic TLS save/restore round-trip. ### Verification - `cargo test -p victory-loader` (38 unit + 4 integration tests passed) - `cargo clippy -p victory-loader --all-targets` (0 warnings) AI-assisted with Gemini.
Dukestar69 commented 2026-11-26 20:49:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T20:49:49Z.

Review: APPROVED


LGTM — verified on Windows against origin/main (63bcca4).

  • Validating array, dir, dir.index, and block for null and using checked_sub / checked_add for the TLS block length cleanly guards OwnTls::save and OwnTls::restore against invalid TLS pointers or overflowing bounds.
  • cargo test -p victory-loader --lib threads:: -- --test-threads=1: 5 passed, 0 failed (including own_tls_save_rejects_null_pointers, own_tls_save_rejects_invalid_bounds_and_lengths, and own_tls_save_and_restore_preserves_state).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6080349875 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3367#pullrequestreview-6080349875). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T20:49:49Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Validating `array`, `dir`, `dir.index`, and `block` for null and using `checked_sub` / `checked_add` for the TLS block length cleanly guards `OwnTls::save` and `OwnTls::restore` against invalid TLS pointers or overflowing bounds. - `cargo test -p victory-loader --lib threads:: -- --test-threads=1`: 5 passed, 0 failed (including `own_tls_save_rejects_null_pointers`, `own_tls_save_rejects_invalid_bounds_and_lengths`, and `own_tls_save_and_restore_preserves_state`). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 21:21:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:21:39Z.

Review: APPROVED


Clean defensive validation of OwnTls::save / OwnTls::restore in crates/victory-loader/src/threads.rs:301-348.

  • save_from validates array, dir, (*dir).index, and resolved block for null, uses checked_sub/checked_add for (*dir).end - (*dir).start + (*dir).zero_fill, and returns None on zero-length or invalid bounds; on_thread cleanly flattens the Option and OwnTls::restore guards against null/empty state.
  • Verification (merged onto origin/main 63bcca43 on Windows 11 x64):
    • cargo xtask policy (191 files ok), cargo clippy --locked -p victory-loader --all-targets -- -D warnings (clean), and all victory-loader unit + integration tests (38 + 4 passed, 0 failed) pass.
    • Verified live on a Windows Rust thread that OwnTls::save() still succeeds on the real _tls_used directory (is_some = true, len = 217) and that a valid BSS-only TLS layout (start == end, zero_fill > 0) is accepted and restored properly.

Minor non-blocking note: for completeness in save_from, checking len > isize::MAX as usize (the slice::from_raw_parts length precondition) and slot == u32::MAX as usize (TLS_OUT_OF_INDEXES) would harden synthetic callers further, though neither can happen for a valid linker-emitted _tls_used.

LGTM!

<!-- greyfinch-sync:Marvisak/VIctory:review:6080585278 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3367#pullrequestreview-6080585278). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:21:39Z. **Review:** APPROVED --- Clean defensive validation of `OwnTls::save` / `OwnTls::restore` in `crates/victory-loader/src/threads.rs:301-348`. - `save_from` validates `array`, `dir`, `(*dir).index`, and resolved `block` for null, uses `checked_sub`/`checked_add` for `(*dir).end - (*dir).start + (*dir).zero_fill`, and returns `None` on zero-length or invalid bounds; `on_thread` cleanly flattens the `Option` and `OwnTls::restore` guards against null/empty state. - **Verification** (merged onto `origin/main` `63bcca43` on Windows 11 x64): - `cargo xtask policy` (`191 files ok`), `cargo clippy --locked -p victory-loader --all-targets -- -D warnings` (clean), and all `victory-loader` unit + integration tests (38 + 4 passed, 0 failed) pass. - Verified live on a Windows Rust thread that `OwnTls::save()` still succeeds on the real `_tls_used` directory (`is_some = true, len = 217`) and that a valid BSS-only TLS layout (`start == end, zero_fill > 0`) is accepted and restored properly. Minor non-blocking note: for completeness in `save_from`, checking `len > isize::MAX as usize` (the `slice::from_raw_parts` length precondition) and `slot == u32::MAX as usize` (`TLS_OUT_OF_INDEXES`) would harden synthetic callers further, though neither can happen for a valid linker-emitted `_tls_used`. LGTM!
Jackster commented 2026-11-26 22:44:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:44:14Z.


Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-loader: 38 passed / 0 failed (lib; unpatched 63bcca4 has 35, and +1 of those three is #3256's clzero test if you stack them), plus the integration suites (1 and 2 passed).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.

The TLS-pointer and bounds validation tests are in that run; no crash or panic in the loader suite here. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204940939 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3367#issuecomment-7204940939). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:44:14Z. --- Windows 11, `63bcca4` + this PR: - `cargo test -p victory-loader`: 38 passed / 0 failed (lib; unpatched `63bcca4` has 35, and +1 of those three is #3256's clzero test if you stack them), plus the integration suites (1 and 2 passed). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. The TLS-pointer and bounds validation tests are in that run; no crash or panic in the loader suite here. Synthetic only. AI-assisted.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3367:upstream-pr/3367'
git switch 'upstream-pr/3367'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3367'
git switch 'upstream-pr/3367'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3367'
git switch 'upstream-pr/3367'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3367'
git switch 'main'
git merge --squash 'upstream-pr/3367'
git switch 'main'
git merge --ff-only 'upstream-pr/3367'
git switch 'main'
git merge 'upstream-pr/3367'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3367
No description provided.