find_hash_lookup: clamp raw section sizes and validate window and top parameters (OLD-9792) #1268

Merged
wetw0rks-c0ding merged 2 commits from upstream-pr/1268 into main 2026-11-28 19:36:59 +00:00
wetw0rks-c0ding commented 2026-11-24 21:05:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by wetw0rks-c0ding on 2026-11-24T21:05:48Z.


Claimed in the coordination issue as: OLD-9792

What and why

tools/find_hash_lookup.py trusted raw section sizes and unchecked window/top parameters, so malformed inputs could drive an out-of-range read. It now clamps the sizes and validates the parameters, with regression tests.

Tested

  • python3 -I tests/test_find_hash_lookup.py -> Ran 12 tests, OK (exit 0)
  • python -I scripts/check_no_game_data.py tools/find_hash_lookup.py tests/test_find_hash_lookup.py -> clean (exit 0)
  • Environment: Linux (CachyOS), CPU-only tests, no GPU required

AI-assisted: Yes. This work was done with OpenCode agents.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5480328535 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1268). Originally posted by [wetw0rks-c0ding](https://github.com/wetw0rks-c0ding) on 2026-11-24T21:05:48Z. --- Claimed in the coordination issue as: OLD-9792 ### What and why `tools/find_hash_lookup.py` trusted raw section sizes and unchecked window/top parameters, so malformed inputs could drive an out-of-range read. It now clamps the sizes and validates the parameters, with regression tests. ### Tested - `python3 -I tests/test_find_hash_lookup.py` -> Ran 12 tests, OK (exit 0) - `python -I scripts/check_no_game_data.py tools/find_hash_lookup.py tests/test_find_hash_lookup.py` -> clean (exit 0) - Environment: Linux (CachyOS), CPU-only tests, no GPU required AI-assisted: Yes. This work was done with OpenCode agents.
nicoco23 commented 2026-11-24 22:06:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by nicoco23 on 2026-11-24T22:06:14Z.


Reviewed the diff and ran the tests: python -I tests/test_find_hash_lookup.py on Windows 11, Python 3.12, 12 tests, OK.

The clamp in sections() reads right to me: len(data) - raw floored at 0, then min(vsize or rsize, rsize, file_avail), so a raw offset past EOF gives size 0 and the section is not scanned. Validating --window (>= 0) and --top (>= 1) before parsing is the right order.

Nits, none blocking:

  • test_truncated_pe_does_not_crash passes whether rank() returns, raises ValueError or raises struct.error, and asserts nothing about the result. A test that calls sections() on the truncated data and checks each size is <= len(data) - raw would pin the clamp itself. There is no case for raw past EOF or for vsize == 0.
  • The ValueError messages name --window and --top, but rank() takes window and top. Naming the parameters would read better for library callers.

NOT AI-assisted review

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7145807236 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1268#issuecomment-7145807236). Originally posted by [nicoco23](https://github.com/nicoco23) on 2026-11-24T22:06:14Z. --- Reviewed the diff and ran the tests: `python -I tests/test_find_hash_lookup.py` on Windows 11, Python 3.12, 12 tests, OK. The clamp in sections() reads right to me: len(data) - raw floored at 0, then min(vsize or rsize, rsize, file_avail), so a raw offset past EOF gives size 0 and the section is not scanned. Validating --window (>= 0) and --top (>= 1) before parsing is the right order. Nits, none blocking: - test_truncated_pe_does_not_crash passes whether rank() returns, raises ValueError or raises struct.error, and asserts nothing about the result. A test that calls sections() on the truncated data and checks each size is <= len(data) - raw would pin the clamp itself. There is no case for raw past EOF or for vsize == 0. - The ValueError messages name --window and --top, but rank() takes window and top. Naming the parameters would read better for library callers. NOT AI-assisted review
wavehs commented 2026-11-26 18:47:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by wavehs on 2026-11-26T18:47:55Z.


This is the same change as #696 (abnalstlll-byte, opened 2026-11-24 12:17, ~8h48m earlier): git diff of the two PR diffs is byte-identical (only the blob hashes differ), both touch tools/find_hash_lookup.py + tests/test_find_hash_lookup.py, and both claim OLD-9792. I applied #696 to main f107ae044: it applies cleanly and python3 -I tests/test_find_hash_lookup.py is OK.

No defect in this PR — just two copies of one reland. My vote is #696 because it is earlier and says it restores the original author explicitly; if this one is preferred for some reason, #696 should be the one closed. Either way only one should merge.

AI-assisted (review/verification only).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7200862732 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1268#issuecomment-7200862732). Originally posted by [wavehs](https://github.com/wavehs) on 2026-11-26T18:47:55Z. --- This is the same change as **#696** (abnalstlll-byte, opened 2026-11-24 12:17, ~8h48m earlier): `git diff` of the two PR diffs is byte-identical (only the blob hashes differ), both touch `tools/find_hash_lookup.py` + `tests/test_find_hash_lookup.py`, and both claim OLD-9792. I applied #696 to `main` `f107ae044`: it applies cleanly and `python3 -I tests/test_find_hash_lookup.py` is `OK`. No defect in this PR — just two copies of one reland. My vote is #696 because it is earlier and says it restores the original author explicitly; if this one is preferred for some reason, #696 should be the one closed. Either way only one should merge. AI-assisted (review/verification only).
MoonEyes commented 2026-11-28 19:36:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:36:55Z.


Red/green: tests/test_find_hash_lookup.py fails 7 assertions on main (raw section sizes not clamped to the file, bad --window/--top accepted) and passes with the PR (cloud run on main 7daa4a4, re-run on Windows against main 8b318bd).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main 7daa4a4a5 in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7244676774 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1268#issuecomment-7244676774). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T19:36:55Z. --- Red/green: tests/test_find_hash_lookup.py fails 7 assertions on main (raw section sizes not clamped to the file, bad --window/--top accepted) and passes with the PR (cloud run on main 7daa4a4, re-run on Windows against main 8b318bd). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main 7daa4a4a5 in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
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!1268
No description provided.