PE: validate directory and table bounds in parser and loader (#106) #1367

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

@⁠slopgobblercobbler on GitHub · 2026-11-24 15:18 UTC · original
slopgobblercobbler:fix/pe-bounds-check → main · +181 −21 · 2 files · 1 commits · open

Addresses #106 Item 3 (PE parser & loader bounds checking):
Prevents out-of-bounds reads and panics on truncated or malformed PE binaries:

  1. pe.rs: in imports, delay_imports, and relocations, validates that directory descriptor and block sizes do not exceed remaining directory data (block <= d.size - pos), returning Err(PeError::BadRva) instead of attempting out-of-bounds slice indexing.
  2. pe.rs: in exports, validates AddressOfFunctions, AddressOfNames, and AddressOfNameOrdinals table spans against directory bounds and size_of_image.
  3. pe.rs: in thunks, validates slot and entry IAT RVAs against image bounds and checks checked_add on name RVA strings.
  4. loader.rs: validates section mapping bounds (s.va + s.mapped_size() <= size), relocation targets, import IAT slots, delay-import module handles, and clamps page protection spans.

What is in it

  • crates/xcl/src/pe.rs:
    • Directory and table bounds checks.
    • Unit tests: truncated_directories_are_rejected, import_directory_without_null_terminator_exceeding_size_is_error, delay_import_without_null_terminator_exceeding_size_is_error, export_tables_exceeding_directory_bounds_is_error, and thunks_with_out_of_bounds_iat_slot_is_error.
  • crates/xcl/src/loader.rs:
    • Section mapping and relocation address bounds guards.

Tested

  • cargo test -p xcl -- pe (17 passed, 0 failed).
  • cargo test -p xcl -- loader (2 passed, 0 failed).
  • Full workspace test suite: cargo test --workspace (707 passed, 0 failed).
  • Windows 11 x64, MSVC toolchain, rebased onto upstream/main (930e8b4).

Not tested

  • 32-bit PE binaries (Xbox Series binaries are strictly PE32+ x64).

AI use

  • Developed with Antigravity swarm coordinator & dedicated author/verifier agents (author-pebounds, verify-pebounds). Verified and reviewed independently.
**@⁠slopgobblercobbler** on GitHub · 2026-11-24 15:18 UTC · [original](https://github.com/xcl-dev/xcl/pull/1367) `slopgobblercobbler:fix/pe-bounds-check` → `main` · +181 −21 · 2 files · 1 commits · open Addresses #106 Item 3 (PE parser & loader bounds checking): Prevents out-of-bounds reads and panics on truncated or malformed PE binaries: 1. `pe.rs`: in `imports`, `delay_imports`, and `relocations`, validates that directory descriptor and block sizes do not exceed remaining directory data (`block <= d.size - pos`), returning `Err(PeError::BadRva)` instead of attempting out-of-bounds slice indexing. 2. `pe.rs`: in `exports`, validates `AddressOfFunctions`, `AddressOfNames`, and `AddressOfNameOrdinals` table spans against directory bounds and `size_of_image`. 3. `pe.rs`: in `thunks`, validates slot and entry IAT RVAs against image bounds and checks `checked_add` on name RVA strings. 4. `loader.rs`: validates section mapping bounds (`s.va + s.mapped_size() <= size`), relocation targets, import IAT slots, delay-import module handles, and clamps page protection spans. **What is in it** - `crates/xcl/src/pe.rs`: - Directory and table bounds checks. - Unit tests: `truncated_directories_are_rejected`, `import_directory_without_null_terminator_exceeding_size_is_error`, `delay_import_without_null_terminator_exceeding_size_is_error`, `export_tables_exceeding_directory_bounds_is_error`, and `thunks_with_out_of_bounds_iat_slot_is_error`. - `crates/xcl/src/loader.rs`: - Section mapping and relocation address bounds guards. **Tested** - `cargo test -p xcl -- pe` (17 passed, 0 failed). - `cargo test -p xcl -- loader` (2 passed, 0 failed). - Full workspace test suite: `cargo test --workspace` (707 passed, 0 failed). - Windows 11 x64, MSVC toolchain, rebased onto `upstream/main` (`930e8b4`). **Not tested** - 32-bit PE binaries (Xbox Series binaries are strictly PE32+ x64). **AI use** - Developed with Antigravity swarm coordinator & dedicated author/verifier agents (`author-pebounds`, `verify-pebounds`). Verified and reviewed independently.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:06 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 15:25 UTC · original · edited 2026-11-24 15:30 UTC

Full-suite run, since this PR has no test report yet. Tested exact head 8dbc465 (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 756 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: truncated_directories_are_rejected, import_directory_without_null_terminator_exceeding_size_is_error, delay_import_without_null_terminator_exceeding_size_is_error, export_tables_exceeding_directory_bounds_is_error, thunks_with_out_of_bounds_iat_slot_is_error.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 756 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 15:25 UTC · [original](https://github.com/xcl-dev/xcl/pull/1367#issuecomment-7136014428) · edited 2026-11-24 15:30 UTC Full-suite run, since this PR has no test report yet. Tested exact head `8dbc465` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **756 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `truncated_directories_are_rejected`, `import_directory_without_null_terminator_exceeding_size_is_error`, `delay_import_without_null_terminator_exceeding_size_is_error`, `export_tables_exceeding_directory_bounds_is_error`, `thunks_with_out_of_bounds_iat_slot_is_error`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **756 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 15:37 UTC · original

Review: this is the parse-step rejection my #836 proposal asked for — validating directory descriptors and block sizes once, in the parser (block <= d.size - pos, Err(BadRva) at parse time) instead of hardening write sites one at a time; five sibling projects converged on the same invariant today and this implements it. Two coverage questions before merge so #836 can be closed accurately rather than optimistically: ① does exports get the same descriptor-bounds loop, or does it still parse unbounded? ② the overflow fallback d.rva.checked_add(pos).unwrap_or(d.rva) reads as error-payload-only — confirming it never feeds a bounds decision (if it only picks which RVA to report, perfect as-is). Together with #1397's consumer-side rva_to_offset EOF bound, this closes both halves of the class from my side's evidence.

**@⁠pickles976** on GitHub · 2026-11-24 15:37 UTC · [original](https://github.com/xcl-dev/xcl/pull/1367#issuecomment-7136328393) Review: this is the parse-step rejection my #836 proposal asked for — validating directory descriptors and block sizes **once, in the parser** (`block <= d.size - pos`, `Err(BadRva)` at parse time) instead of hardening write sites one at a time; five sibling projects converged on the same invariant today and this implements it. Two coverage questions before merge so #836 can be closed accurately rather than optimistically: ① does `exports` get the same descriptor-bounds loop, or does it still parse unbounded? ② the overflow fallback `d.rva.checked_add(pos).unwrap_or(d.rva)` reads as error-payload-only — confirming it never feeds a bounds *decision* (if it only picks which RVA to report, perfect as-is). Together with #1397's consumer-side `rva_to_offset` EOF bound, this closes both halves of the class from my side's evidence.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 16:33 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 16:33 UTC · [original](https://github.com/xcl-dev/xcl/pull/1367#issuecomment-7137855676) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
Author
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 17:35 UTC · original

Thanks for the thorough review and verification, @⁠pickles976!

To answer your two coverage questions directly:

  1. Does \exports\ get the descriptor-bounds loop?:
    Unlike import and delay-import directories (which are variable-length arrays of 20-byte / 32-byte descriptor structs terminated by a zero-filled descriptor), the export directory consists of a single 40-byte \IMAGE_EXPORT_DIRECTORY\ header that indexes three tables: AddressOfFunctions (
    func * 4), AddressOfNames (
    names * 4), and AddressOfNameOrdinals (
    names * 2).
    In this PR, \exports():

    • Validates the base directory descriptor upfront: \d.size < 40 || d.rva.checked_add(d.size).is_none().
    • Bounds table counts (\min(65536)) and computes byte extents via \checked_mul.
    • Validates tables via \check_table: if a table resides inside the export directory span [d.rva, d.rva + d.size), it strictly checks
      va + size <= d.rva + d.size; additionally, it rejects tables where
      va.saturating_add(size) > size_of_image.
    • Individual function/name RVAs use checked arithmetic and boundary checks.
  2. Does \d.rva.checked_add(pos).unwrap_or(d.rva)\ feed any bounds decision?:
    Confirmed, it is strictly an error payload value (\PeError::BadRva(u32)).
    The bounds decision itself is evaluated strictly beforehand by \pos = i.checked_mul(...)\ and \if STEP > d.size - pos. The .unwrap_or(d.rva)\ only determines the RVA reported in the error enum for diagnostics, ensuring arithmetic safety while reporting the out-of-bounds offset.

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 17:35 UTC · [original](https://github.com/xcl-dev/xcl/pull/1367#issuecomment-7139429796) Thanks for the thorough review and verification, @⁠pickles976! To answer your two coverage questions directly: 1. **Does \exports\ get the descriptor-bounds loop?**: Unlike import and delay-import directories (which are variable-length arrays of 20-byte / 32-byte descriptor structs terminated by a zero-filled descriptor), the export directory consists of a single 40-byte \IMAGE_EXPORT_DIRECTORY\ header that indexes three tables: AddressOfFunctions (\ func * 4\), AddressOfNames (\ names * 4\), and AddressOfNameOrdinals (\ names * 2\). In this PR, \exports()\: - Validates the base directory descriptor upfront: \d.size < 40 || d.rva.checked_add(d.size).is_none()\. - Bounds table counts (\min(65536)\) and computes byte extents via \checked_mul\. - Validates tables via \check_table\: if a table resides inside the export directory span \[d.rva, d.rva + d.size)\, it strictly checks \ va + size <= d.rva + d.size\; additionally, it rejects tables where \ va.saturating_add(size) > size_of_image\. - Individual function/name RVAs use checked arithmetic and boundary checks. 2. **Does \d.rva.checked_add(pos).unwrap_or(d.rva)\ feed any bounds decision?**: Confirmed, it is **strictly an error payload value** (\PeError::BadRva(u32)\). The bounds decision itself is evaluated strictly beforehand by \pos = i.checked_mul(...)\ and \if STEP > d.size - pos\. The \.unwrap_or(d.rva)\ only determines the RVA reported in the error enum for diagnostics, ensuring arithmetic safety while reporting the out-of-bounds offset.
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:26 UTC · original

Regression run of 8dbc4652 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 8dbc4652fd051bba0f55856955229312a2ee7133
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1634 (PresentX 1634)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14814 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

**@⁠Toons29** on GitHub · 2026-11-25 03:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/1367#issuecomment-7151909720) Regression run of `8dbc4652` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 8dbc4652fd051bba0f55856955229312a2ee7133 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1634 (PresentX 1634) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14814 MB ``` Against the limits in CONTRIBUTING.md: 101 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
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/1367/head:github-bot/gh-1367'
git switch 'github-bot/gh-1367'

Merge

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