PE: validate directory and table bounds in parser and loader (#106) #1357
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/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?
Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-24T15:18:26Z.
Addresses #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.Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:25:53Z.
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
Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T15:37:04Z.
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.Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T16:33:36Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.Thanks!
Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-24T17:35:51Z.
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.
Imported from ivall/xcl. Originally posted by Toons29 on 2026-11-25T03:26:01Z.
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.Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-28T08:01:42Z.
Nothing has happened on this PR for 3 days, so it is marked
staleand will be closed in 4 days. A push or a comment takes the mark off: rebase it ontomainif it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:31:59Z.
Triage (#1607) – head
8dbc465Overlaps: #291 adds the same relocation-target bound in
loader.rsmap_image_ex, with a loader test that this PR lacks. #1216 makes the samehn.checked_add(2)change inPe::thunks. #1397 also boundsrva_to_offset.In
map_image_ex, the new IAT-slot check can never fire, becausethunksalready rejectsentry_iat_rva + 8 > size_of_image. It also sits afterresolve_importhas run for earlier entries, so a failure there frees an image that dependencies were already resolved against.imports/delay_importsnow 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_importserrors 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.
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.