Skip to content

Loader: validate TLS pointers and bounds in OwnTls - #3367

Open
Arkenoz wants to merge 1 commit into
Marvisak:mainfrom
Arkenoz:fix/own-tls-validation
Open

Arkenoz wants to merge 1 commit into
Marvisak:mainfrom
Arkenoz:fix/own-tls-validation

Conversation

@Arkenoz

@Arkenoz Arkenoz commented Nov 26, 2026

Copy link
Copy Markdown

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 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants