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

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

@⁠AscilCH on GitHub · 2026-11-25 01:22 UTC · original
AscilCH:fix/loader-crash-veh-clzero-bounds-and-thrown → main · +144 −34 · 1 files · 1 commits · open

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

**@⁠AscilCH** on GitHub · 2026-11-25 01:22 UTC · [original](https://github.com/fxceless/victory-fork/pull/478) `AscilCH:fix/loader-crash-veh-clzero-bounds-and-thrown` → `main` · +144 −34 · 1 files · 1 commits · open ## 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.)*
Fix three VEH / crash-reporter fault hazards in `crates/victory-loader/src/crash.rs`:
1. In `emulate_amd_only`, verify the 64-byte `clzero` target line is committed and writable via `VirtualQuery` before calling `write_bytes(0, 64)`, so an unwritable `clzero` that `console_insn::emulate` rejected does not fall through and trigger a nested Access Violation inside the VEH.
2. Copy page-bounded instruction bytes (`room = 0x1000 - (Rip & 0xfff)`) before inspecting `[0x0f, 0x01, ...]` or `clwb_len`, and pass the bounded slice `&bytes[..n]` into a safe slice-based `sse4a(&[u8], ...)` so 1–6 byte illegal instructions at the end of a page adjacent to an unmapped/no-access page cannot fault while decoding.
3. In `thrown()`, handle MSVC `throw;` rethrows (`p[2] == 0 || p[3] == 0`), guard `rva(types) == 0` / zero catchable type count before calling `name(0)`, and require `p[1] != 0` before reading `std::exception`'s message pointer.

Add synthetic unit tests for all three failure modes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:44:16 +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/478/head:github-bot/gh-478'
git switch 'github-bot/gh-478'

Merge

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