tools/import_call_sites: bounds-check ordinary IAT slots and parse PE once in main #1855

Closed
leonida/mandy
kirigayakazima wants to merge 0 commits from fix/import-call-sites-safety into main

Pull request closed

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

kirigayakazima commented 2026-11-25 13:13:32 +00:00

Claimed in the coordination issue as: IMPORT-CALL-SITES-ORDINARY-IAT-BOUNDS-AND-SINGLE-PARSE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163426538)

What and why

  • Guard ordinary PE32+ import descriptors in tools/import_call_sites.py's import_entries() against zero or unmapped iat_rva / lookup RVAs (img.offset(..., 8)) and stop slot enumeration if img.offset(slot, 8) is None, matching the existing delay-import slot validation.
  • Extract _call_sites_from_image() and _unmatched_from_entries() so main() parses pe_inspect.PeImage and import_entries(img) once per CLI run instead of twice (while keeping public call_sites() and unmatched_selectors() signatures unchanged).
  • Add regression tests in tests/test_import_call_sites.py.

Tested

  • python -I tests/test_import_call_sites.py -v -> Ran 24 tests in 0.106s, OK.
  • Detached upstream/main (98c2f777) baseline worktree with updated tests/test_import_call_sites.py -> Ran 24 tests in 0.090s, FAILED (failures=4).
  • python -I tests/test_ci_registration.py -> Ran 16 tests in 6.127s, OK.
  • python -I tests/test_text_write_line_endings.py -> Ran 3 tests in 14.536s, OK.
  • python -I scripts/check_no_game_data.py tools/import_call_sites.py tests/test_import_call_sites.py, python -I -m compileall -q tools/import_call_sites.py tests/test_import_call_sites.py, and git diff --check -> exit 0.

GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
Claimed in the coordination issue as: IMPORT-CALL-SITES-ORDINARY-IAT-BOUNDS-AND-SINGLE-PARSE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163426538) ### What and why - Guard ordinary PE32+ import descriptors in `tools/import_call_sites.py`'s `import_entries()` against zero or unmapped `iat_rva` / `lookup` RVAs (`img.offset(..., 8)`) and stop slot enumeration if `img.offset(slot, 8) is None`, matching the existing delay-import slot validation. - Extract `_call_sites_from_image()` and `_unmatched_from_entries()` so `main()` parses `pe_inspect.PeImage` and `import_entries(img)` once per CLI run instead of twice (while keeping public `call_sites()` and `unmatched_selectors()` signatures unchanged). - Add regression tests in `tests/test_import_call_sites.py`. ### Tested - `python -I tests/test_import_call_sites.py -v` -> `Ran 24 tests in 0.106s`, `OK`. - Detached `upstream/main` (`98c2f777`) baseline worktree with updated `tests/test_import_call_sites.py` -> `Ran 24 tests in 0.090s`, `FAILED (failures=4)`. - `python -I tests/test_ci_registration.py` -> `Ran 16 tests in 6.127s`, `OK`. - `python -I tests/test_text_write_line_endings.py` -> `Ran 3 tests in 14.536s`, `OK`. - `python -I scripts/check_no_game_data.py tools/import_call_sites.py tests/test_import_call_sites.py`, `python -I -m compileall -q tools/import_call_sites.py tests/test_import_call_sites.py`, and `git diff --check` -> exit `0`. GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64) AI-assisted: yes ### Checklist - [x] Based on current main and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
madvillain91 reviewed 2026-11-25 13:30:11 +00:00
madvillain91 left a comment
No description provided.
Independent Windows 11 26H2 (26300.9711) check at 9a936d9e8 against main 384abe5b0 (PR base 98c2f7777), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. Passed test_import_call_sites.py (24/24 in 0.215s) and test_text_write_line_endings.py (3/3 in 19.667s), with check_no_game_data.py and compileall clean. Verified ordinary IAT descriptors with zero/unmapped RVAs are skipped, truncated IAT tables keep only whole mapped 8-byte slots, and main() parses the PE image and import entries once via _call_sites_from_image/_unmatched_from_entries while keeping public call_sites() and unmatched_selectors() intact. LGTM.
DeadlyChirp commented 2026-11-25 15:37:38 +00:00

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_import_call_sites.py:

  • PR: 24 passed. main: 21 passed. The new tests pass on Windows.

(AI-assisted test run; output checked by hand.)

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_import_call_sites.py`: - PR: 24 passed. main: 21 passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
aslan2142 commented 2026-11-25 17:27:32 +00:00

Thanks. The new bounds check copies the existing delay-import check, but it only fires on a malformed executable: an image that loads has mapped IATs. The single-parse change saves one header parse.

Can you name a real input that reaches the new guard? Otherwise we'd rather not take the extra code.

AI-assisted (Claude Code, Claude Opus 5.5).

Thanks. The new bounds check copies the existing delay-import check, but it only fires on a malformed executable: an image that loads has mapped IATs. The single-parse change saves one header parse. Can you name a real input that reaches the new guard? Otherwise we'd rather not take the extra code. AI-assisted (Claude Code, Claude Opus 5.5).
MSedgeMC approved these changes 2026-11-25 17:47:26 +00:00
MSedgeMC left a comment

Verified on the PR branch: python3 -m unittest tests.test_import_call_sites -v -> 24 tests OK, including the new cases for zero/unmapped ordinary IAT, truncated IAT keeping only whole mapped slots, and parsing the PE image once in the CLI. These guard against reading out-of-range IAT slots from malformed PE data. Python-only, no game data.

Verified on the PR branch: python3 -m unittest tests.test_import_call_sites -v -> 24 tests OK, including the new cases for zero/unmapped ordinary IAT, truncated IAT keeping only whole mapped slots, and parsing the PE image once in the CLI. These guard against reading out-of-range IAT slots from malformed PE data. Python-only, no game data.
kirigayakazima commented 2026-12-01 11:12:53 +00:00

Thank you @aslan2142, @madvillain91, @MSedgeMC, and @DeadlyChirp for the reviews and verification! Since there is no real loadable executable reproducing malformed ordinary IATs, closing this PR cleanly per @aslan2142's preference not to take the extra guard code. (AI-assisted.)

Thank you @aslan2142, @madvillain91, @MSedgeMC, and @DeadlyChirp for the reviews and verification! Since there is no real loadable executable reproducing malformed ordinary IATs, closing this PR cleanly per @aslan2142's preference not to take the extra guard code. (AI-assisted.)
Sign in to join this conversation.
No description provided.