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

Closed
morodirule wants to merge 3 commits from upstream-pr/887 into main
morodirule commented 2026-11-24 14:56:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T14:56:53Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5474590598 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/887). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T14:56:53Z. --- 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 commented 2026-11-24 15:00:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KernelSmoker on 2026-11-24T15:00:31Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6041683631 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/887#pullrequestreview-6041683631). Originally posted by [KernelSmoker](https://github.com/KernelSmoker) on 2026-11-24T15:00:31Z. **Review:** CHANGES_REQUESTED --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by LewisStephens99 on 2026-11-24T15:06:54Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7135481695 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/887#issuecomment-7135481695). Originally posted by [LewisStephens99](https://github.com/LewisStephens99) on 2026-11-24T15:06:54Z. --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T15:15:19Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7135715692 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/887#issuecomment-7135715692). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T15:15:19Z. --- 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 commented 2026-11-24 15:19:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KernelSmoker on 2026-11-24T15:19:11Z.

Review: APPROVED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6042031920 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/887#pullrequestreview-6042031920). Originally posted by [KernelSmoker](https://github.com/KernelSmoker) on 2026-11-24T15:19:11Z. **Review:** APPROVED --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T15:25:56Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7136015880 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/887#issuecomment-7136015880). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T15:25:56Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!887
No description provided.