PE: validate directory and table bounds in parser and loader (#106) #1367
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!1367
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1367"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@slopgobblercobbler on GitHub · 2026-11-24 15:18 UTC · original
slopgobblercobbler:fix/pe-bounds-check→main· +181 −21 · 2 files · 1 commits · openAddresses #106 Item 3 (PE parser & loader bounds checking):
Prevents out-of-bounds reads and panics on truncated or malformed PE binaries:
pe.rs: inimports,delay_imports, andrelocations, validates that directory descriptor and block sizes do not exceed remaining directory data (block <= d.size - pos), returningErr(PeError::BadRva)instead of attempting out-of-bounds slice indexing.pe.rs: inexports, validatesAddressOfFunctions,AddressOfNames, andAddressOfNameOrdinalstable spans against directory bounds andsize_of_image.pe.rs: inthunks, validates slot and entry IAT RVAs against image bounds and checkschecked_addon name RVA strings.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: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, andthunks_with_out_of_bounds_iat_slot_is_error.crates/xcl/src/loader.rs:Tested
cargo test -p xcl -- pe(17 passed, 0 failed).cargo test -p xcl -- loader(2 passed, 0 failed).cargo test --workspace(707 passed, 0 failed).upstream/main(930e8b4).Not tested
AI use
author-pebounds,verify-pebounds). Verified and reviewed independently.@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 base930e8b4).Tested
cargo build --workspace, thenXCL_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.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.XCL_ADAPTER=radeon),cargo test --workspace -- --test-threads=4: 756 passed, 0 failed, 41 ignored, no skips.Not tested
AI use
@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: ① doesexportsget the same descriptor-bounds loop, or does it still parse unbounded? ② the overflow fallbackd.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-siderva_to_offsetEOF bound, this closes both halves of the class from my side's evidence.@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!
@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:
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():
va + size <= d.rva + d.size; additionally, it rejects tables where
va.saturating_add(size) > size_of_image.
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 on GitHub · 2026-11-25 03:26 UTC · original
Regression run of
8dbc4652on the retail title, for this PR's Regression run section. Both blocks are fromscripts/smoke.ps1as printed, not edited:mainon4178e888, this PR on its head commit with no uncommitted changes, same title and time.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.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.