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

Closed
github-bot wants to merge 3 commits from gh-pr/887/head into gh-pr/887/base
Owner

@⁠morodirule on GitHub · 2026-11-24 14:56 UTC · original
fix/hashlookup-short-pe → main · +28 −0 · 2 files · 3 commits · closed without merging

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.

**@⁠morodirule** on GitHub · 2026-11-24 14:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/887) `fix/hashlookup-short-pe` → `main` · +28 −0 · 2 files · 3 commits · closed without merging 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.
sections() unpacked e_lfanew at 0x3C without a length check, so an MZ-prefixed buffer under 64 bytes escaped as struct.error. Guard the e_lfanew, COFF header and section-table reads; rank()/CLI already map ValueError to a clean failure.
Review on #887: the PE signature ends at pe+4 and the fixed COFF header runs 20 bytes after it, so the minimum valid end is pe+24, not pe+22. Zero-section images at pe+22/pe+23 were accepted; they are refused now, with a boundary regression.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:26:25 +00:00
github-bot closed this pull request 2026-11-26 09:26:25 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 15:00 UTC · original · requested changes

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.

**@⁠KernelSmoker** on GitHub · 2026-11-24 15:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/887#pullrequestreview-6041683631) · **requested changes** 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.
Author
Owner

@⁠LewisStephens99 on GitHub · 2026-11-24 15:06 UTC · original

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.

**@⁠LewisStephens99** on GitHub · 2026-11-24 15:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/887#issuecomment-7135481695) 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.
Author
Owner

@⁠morodirule on GitHub · 2026-11-24 15:15 UTC · original

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.

**@⁠morodirule** on GitHub · 2026-11-24 15:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/887#issuecomment-7135715692) 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.
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 15:19 UTC · original · approved these changes

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.

**@⁠KernelSmoker** on GitHub · 2026-11-24 15:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/887#pullrequestreview-6042031920) · **approved these changes** 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.
Author
Owner

@⁠morodirule on GitHub · 2026-11-24 15:25 UTC · original

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.

**@⁠morodirule** on GitHub · 2026-11-24 15:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/887#issuecomment-7136015880) 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

This discussion has been locked. Commenting is limited to contributors.
No description provided.