loader/crash: bound VEH instruction reads, guard clzero writability, and fix MSVC thrown() null derefs #478

Open
AscilCH wants to merge 1 commit from upstream-pr/478 into main
AscilCH commented 2026-11-25 01:22:52 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T01:22:52Z.


Problem

In crates/victory-loader/src/crash.rs, three defects could cause nested Access Violations inside the vectored exception handler or crash reporter:

  1. Unwritable clzero fallthrough AV: When console_insn::emulate returns 0 because a clzero target is not writable (console_insn.rs:158-159), handler falls through to emulate_amd_only(info) (crash.rs:370), which unconditionally executed ((context.Rax & !63) as *mut u8).write_bytes(0, 64) with no writability check, crashing inside the VEH.
  2. Page-boundary overread AV in emulate_amd_only and sse4a: emulate_amd_only read [u8; 3] from context.Rip before computing room = 0x1000 - (Rip & 0xfff), and passed the raw code pointer instead of the page-bounded &bytes[..n] slice to sse4a(), which indexed up to code.add(6) unchecked. Any 1–6 byte illegal instruction at the end of a page adjacent to a PAGE_NOACCESS/unmapped page faulted inside the VEH.
  3. NULL/near-NULL dereferences in MSVC C++ thrown(): On a C++ rethrow (throw;, where p[1] == 0, p[2] == 0), thrown() read p[2] + 12 (0xC), called name(0) without checking count = rva(types) > 0, and dereferenced p[1] on the std::exception branch without checking p[1] != 0.

Fix

  • Copy page-bounded bytes (room = 0x1000 - (Rip & 0xfff)) at the top of emulate_amd_only before matching clzero/monitorx/mwaitx, clwb_loop, clwb_len, or sse4a.
  • Convert sse4a to take a bounded &[u8] slice using code.get(...).
  • Check that the 64-byte clzero line is committed and writable (VirtualQuery) before writing in emulate_amd_only, returning false otherwise.
  • Guard p[2] == 0 || p[3] == 0, zero RVA / empty catchable type array, and p[1] == 0 in thrown().
  • Add synthetic unit tests covering unwritable clzero (read-only page via emulate_amd_only and handler), page-boundary illegal instructions (ud2 at +0xFFE, truncated SSE4a prefix at +0xFFD) adjacent to PAGE_NOACCESS, and thrown() with MSVC rethrow (p[2] == 0) / null p[1].

Verification

  • cargo test -p victory-loader --locked: 25 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.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6594941184 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/478). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T01:22:52Z. --- ## Problem In `crates/victory-loader/src/crash.rs`, three defects could cause nested Access Violations inside the vectored exception handler or crash reporter: 1. **Unwritable `clzero` fallthrough AV**: When `console_insn::emulate` returns `0` because a `clzero` target is not writable (`console_insn.rs:158-159`), `handler` falls through to `emulate_amd_only(info)` (`crash.rs:370`), which unconditionally executed `((context.Rax & !63) as *mut u8).write_bytes(0, 64)` with no writability check, crashing inside the VEH. 2. **Page-boundary overread AV in `emulate_amd_only` and `sse4a`**: `emulate_amd_only` read `[u8; 3]` from `context.Rip` before computing `room = 0x1000 - (Rip & 0xfff)`, and passed the raw `code` pointer instead of the page-bounded `&bytes[..n]` slice to `sse4a()`, which indexed up to `code.add(6)` unchecked. Any 1–6 byte illegal instruction at the end of a page adjacent to a `PAGE_NOACCESS`/unmapped page faulted inside the VEH. 3. **NULL/near-NULL dereferences in MSVC C++ `thrown()`**: On a C++ rethrow (`throw;`, where `p[1] == 0, p[2] == 0`), `thrown()` read `p[2] + 12` (`0xC`), called `name(0)` without checking `count = rva(types) > 0`, and dereferenced `p[1]` on the `std::exception` branch without checking `p[1] != 0`. ## Fix - Copy page-bounded bytes (`room = 0x1000 - (Rip & 0xfff)`) at the top of `emulate_amd_only` before matching `clzero`/`monitorx`/`mwaitx`, `clwb_loop`, `clwb_len`, or `sse4a`. - Convert `sse4a` to take a bounded `&[u8]` slice using `code.get(...)`. - Check that the 64-byte `clzero` line is committed and writable (`VirtualQuery`) before writing in `emulate_amd_only`, returning `false` otherwise. - Guard `p[2] == 0 || p[3] == 0`, zero RVA / empty catchable type array, and `p[1] == 0` in `thrown()`. - Add synthetic unit tests covering unwritable `clzero` (read-only page via `emulate_amd_only` and `handler`), page-boundary illegal instructions (`ud2` at `+0xFFE`, truncated SSE4a prefix at `+0xFFD`) adjacent to `PAGE_NOACCESS`, and `thrown()` with MSVC rethrow (`p[2] == 0`) / null `p[1]`. ## Verification - `cargo test -p victory-loader --locked`: 25 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.)*
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/478:upstream-pr/478'
git switch 'upstream-pr/478'

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/478'
git switch 'upstream-pr/478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/478'
git switch 'upstream-pr/478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/478'
git switch 'main'
git merge --squash 'upstream-pr/478'
git switch 'main'
git merge --ff-only 'upstream-pr/478'
git switch 'main'
git merge 'upstream-pr/478'
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
fxceless/victory-fork!478
No description provided.