find_hash_lookup: refuse truncated PE buffers with ValueError (HASHLOOKUP-SHORT-PE) #887

Closed
leonida/mandy
morodirule wants to merge 0 commits from fix/hashlookup-short-pe into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

morodirule commented 2026-11-24 14:56:53 +00:00

Claimed in the coordination issue as: HASHLOOKUP-SHORT-PE

Carries abnalstlll-byte's finding: sections() ran struct.unpack_from(data, 0x3C) unchecked, so MZ-prefixed buffers under 64 bytes raised struct.error instead of the file's ValueError convention (the CLI maps ValueError to a clean exit-2 ERROR line).

Change: length guards before the e_lfanew, COFF-header and per-record section-table reads in tools/find_hash_lookup.py, plus test_a_truncated_image_is_refused_clearly in tests/test_find_hash_lookup.py (short MZ buffers, cut-off COFF, cut-off section record).

Complements open #696 (section-size clamping + --window/--top validation, different lines).

Tests: python -I tests/test_find_hash_lookup.py 6/6 OK; check_no_game_data.py PASS; compileall PASS. Linux x86-64, stdlib only. No game files, no GPU, nothing online.

AI-assisted.

Claimed in the coordination issue as: HASHLOOKUP-SHORT-PE Carries abnalstlll-byte's finding: sections() ran struct.unpack_from(data, 0x3C) unchecked, so MZ-prefixed buffers under 64 bytes raised struct.error instead of the file's ValueError convention (the CLI maps ValueError to a clean exit-2 ERROR line). Change: length guards before the e_lfanew, COFF-header and per-record section-table reads in tools/find_hash_lookup.py, plus test_a_truncated_image_is_refused_clearly in tests/test_find_hash_lookup.py (short MZ buffers, cut-off COFF, cut-off section record). Complements open #696 (section-size clamping + --window/--top validation, different lines). Tests: python -I tests/test_find_hash_lookup.py 6/6 OK; check_no_game_data.py PASS; compileall PASS. Linux x86-64, stdlib only. No game files, no GPU, nothing online. AI-assisted.
KernelSmoker reviewed 2026-11-24 15:00:31 +00:00
KernelSmoker left a comment
No description provided.
The focused tests pass, but one truncated COFF-header boundary is still accepted. The PE signature starts at `pe`; the fixed COFF header is 20 bytes after its 4-byte signature, so the minimum end is `pe + 24`. The current guard only requires `pe + 22`. With a synthetic buffer containing `MZ`, `e_lfanew=0x40`, `PE\0\0`, `NumberOfSections=0`, and `SizeOfOptionalHeader=0`, `sections()` accepts lengths `0x40+22` and `0x40+23` and returns `[]`, although both omit part of the COFF header. Please require the complete header and add a zero-section regression for these boundary lengths. Nonzero section counts already reach the later section-table length check.
LewisStephens99 commented 2026-11-24 15:06:54 +00:00

Independent verification on Windows 11, Python 3.14:n- python -I tests/test_find_hash_lookup.py: 6/6 passed.n`nNo game files or GPU used. AI-assisted.

Independent verification on Windows 11, Python 3.14:`n- `python -I tests/test_find_hash_lookup.py`: 6/6 passed.`n`nNo game files or GPU used. AI-assisted.
morodirule commented 2026-11-24 15:15:19 +00:00

Addressed: the COFF guard now requires pe + 24 (full 20-byte header after the 4-byte signature), with a zero-section boundary regression for pe + 22 / pe + 23 (refused) and the complete header (accepted, []). tests/test_find_hash_lookup.py 6/6 OK locally.

Addressed: the COFF guard now requires pe + 24 (full 20-byte header after the 4-byte signature), with a zero-section boundary regression for pe + 22 / pe + 23 (refused) and the complete header (accepted, []). tests/test_find_hash_lookup.py 6/6 OK locally.
KernelSmoker approved these changes 2026-11-24 15:19:11 +00:00
KernelSmoker left a comment

Retested the updated exact head ff9acc129692b59b2fa8dd1d250858a70aeafd2a. The fixed-header guard now requires pe + 24, and the new zero-section regressions reject both pe+22 and pe+23 while accepting the complete header. python3 -I tests/test_find_hash_lookup.py -v: 6/6 passed; Python compile and diff checks passed. The truncation finding is resolved on this head. No game data or GPU used.

Retested the updated exact head `ff9acc129692b59b2fa8dd1d250858a70aeafd2a`. The fixed-header guard now requires `pe + 24`, and the new zero-section regressions reject both `pe+22` and `pe+23` while accepting the complete header. `python3 -I tests/test_find_hash_lookup.py -v`: 6/6 passed; Python compile and diff checks passed. The truncation finding is resolved on this head. No game data or GPU used.
morodirule commented 2026-11-24 15:25:56 +00:00

Additional self-review hardening: zero-section images could also bypass validation of a truncated optional header because no section-record read ran. sections() now requires len(data) >= first (COFF end + SizeOfOptionalHeader) and the regression covers a one-byte-short optional header. Focused test remains 6/6 OK.

Additional self-review hardening: zero-section images could also bypass validation of a truncated optional header because no section-record read ran. sections() now requires len(data) >= first (COFF end + SizeOfOptionalHeader) and the regression covers a one-byte-short optional header. Focused test remains 6/6 OK.
Sign in to join this conversation.
No description provided.