Skip to content

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

Open
slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/pe-bounds-check
Open

slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/pe-bounds-check

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

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.

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

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.

@pickles976

Copy link
Copy Markdown

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.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

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

Thanks!

@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

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.

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

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.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…stream xcl-dev#1367)

Ports live head 8dbc465 (still OPEN vs upstream/main). pe.rs: directory
descriptor/block sizes bounded by remaining dir data; export AddressOf
tables bounded by dir+image; thunk slot/entry RVAs + name strings
checked_add-bounded with size_of_image clamps; Mapped rva_to_offset/tail
image clamps; all 5 parser tests as-is. loader.rs: section va+mapped
outside-image fail; import IAT-slot and delay-HMODULE outside-image
fails. Loader reloc guards + page-span clamp already on linux (xcl-dev#291 /
section_protect_range helper) — not re-ported.

Failing-first: truncated_directories_are_rejected FAILED pre-fix;
Mapped-layout probe proved past-dir-end acceptance pre-fix (removed).
Gates: both checks 0 errors, lib 737/0/1 (+5), title e2e 12 GPU-only
FAILs, retail HOLDS at +0x2237320.
@github-actions

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 8dbc465

Overlaps: #291 adds the same relocation-target bound in loader.rs map_image_ex, with a loader test that this PR lacks. #1216 makes the same hn.checked_add(2) change in Pe::thunks. #1397 also bounds rva_to_offset.
In map_image_ex, the new IAT-slot check can never fire, because thunks already rejects entry_iat_rva + 8 > size_of_image. It also sits after resolve_import has run for earlier entries, so a failure there frees an image that dependencies were already resolved against.
imports/delay_imports now refuse a descriptor table whose null terminator lies past the directory's Size. Windows' loader only walks to the terminator, so check this against real titles. delay_imports errors are only logged, so the title's delay-loads would silently stay unresolved.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

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

Labels

stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants