tombox-archive: reject fake EOCDs in comments and bound ZIP64/CD reads #44

Closed
github-bot wants to merge 1 commit from gh-pr/44/head into gh-pr/44/base
Owner

@⁠dea7hsmiless on GitHub · 2026-11-21 19:11 UTC · original
archive/eocd-bounds → main · +121 −14 · 2 files · 72 commits · closed without merging

Library / area: crates/tombox-archive (ZIP64 central-directory reader / extraction safety validator)

Exports changed (exports.txt rows): none

Source of the behaviour: PKWARE APPNOTE:

  • The EOCD record is the last record; its 16-bit comment length defines the
    file tail.
  • ZIP64 uses an EOCD locator 20 bytes before the EOCD and a ZIP64 EOCD
    record at the locator's offset.
  • The central-directory offset and size must resolve inside the file.

What changed:

  • find_eocd now scans backwards for an EOCD signature whose 22-byte
    record plus its declared comment ends exactly at EOF. A fake EOCD
    embedded in a real archive comment no longer hides the real EOCD and its
    entries.
  • Before seeking to a ZIP64 EOCD, its offset is checked with
    checked_add(56) against the file length; a past-EOF locator returns
    Malformed instead of an I/O error.
  • The central-directory end is computed with checked_add and compared to
    the file length. A ZIP64 cd_offset near u64::MAX returns Malformed
    instead of panicking on overflow.

Tests and results:

  • cargo test -p tombox-archive -> 13 passed, 0 failed.
  • cargo build -p tombox-archive -> exit 0.
  • Reproductions generated as temporary files (not committed):
    • fake_eocd_in_comment.zip: before -> 0 parsed entries, no findings,
      "safe to extract", exit 0; after -> 1 parsed entry, ../evil.txt
      blocked as path traversal, exit 3.
    • zip64_overflow.zip (cd_offset = 0xFFFF_FFFF_FFFF_FFF8,
      cd_size = 16): before -> panic attempt to add with overflow at
      lib.rs:162, exit 101; after -> malformed ZIP: central directory bounds overflow (...), exit 1.
  • New regression tests: fake EOCD inside a comment, normal EOCD with a
    comment, ZIP64 CD bounds overflow, ZIP64 EOCD offset past EOF.
  • Cross-checked against Python zipfile: for a real archive whose comment
    embeds a fake EOCD, Python reports 0 entries; this parser reports the
    real hello.txt. A normal comment and a 65,535-byte comment also parse
    correctly.

Negative control:

  • Removing the comment-end check made ignores_fake_eocd_inside_comment
    fail (left: 0, right: 1).
  • Reverting checked_add made rejects_zip64_cd_bounds_overflow fail
    with attempt to add with overflow.
  • Removing the ZIP64 record bounds check made rejects_zip64_eocd_past_eof
    fail with an I/O error instead of Malformed.
  • All three checks were restored; the full archive suite passes.

Environment: Windows 11 Pro 10.0.26300, rustc/cargo 1.99.1.

Proven / inferred / not tested:

  • Proven: both reproductions now fail safely, and the new tests pass.
  • Not tested: a real game archive; no archive fixtures are available. The
    rules are format-level and synthetic.
  • Limitation: a deliberately appended, structurally valid second EOCD at
    the exact end of the file remains ambiguous without scanning local file
    headers. This change rejects comment-embedded and malformed EOCDs and
    bounds-checks ZIP64/CD reads.
  • Behavior change: trailing bytes after the EOCD are now rejected because
    the record's comment must end at EOF.

Dependencies: none. PR #37 is independent (tombox-xshader only).

  • cargo test -p tombox-archive passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note

No game files, console DLLs, assets or binaries are included.

**@⁠dea7hsmiless** on GitHub · 2026-11-21 19:11 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/44) `archive/eocd-bounds` → `main` · +121 −14 · 2 files · 72 commits · closed without merging **Library / area:** `crates/tombox-archive` (ZIP64 central-directory reader / extraction safety validator) **Exports changed (exports.txt rows):** none **Source of the behaviour:** PKWARE APPNOTE: - The EOCD record is the last record; its 16-bit comment length defines the file tail. - ZIP64 uses an EOCD locator 20 bytes before the EOCD and a ZIP64 EOCD record at the locator's offset. - The central-directory offset and size must resolve inside the file. **What changed:** - `find_eocd` now scans backwards for an EOCD signature whose 22-byte record plus its declared comment ends exactly at EOF. A fake EOCD embedded in a real archive comment no longer hides the real EOCD and its entries. - Before seeking to a ZIP64 EOCD, its offset is checked with `checked_add(56)` against the file length; a past-EOF locator returns `Malformed` instead of an I/O error. - The central-directory end is computed with `checked_add` and compared to the file length. A ZIP64 `cd_offset` near `u64::MAX` returns `Malformed` instead of panicking on overflow. **Tests and results:** - `cargo test -p tombox-archive` -> 13 passed, 0 failed. - `cargo build -p tombox-archive` -> exit 0. - Reproductions generated as temporary files (not committed): - `fake_eocd_in_comment.zip`: before -> 0 parsed entries, no findings, "safe to extract", exit 0; after -> 1 parsed entry, `../evil.txt` blocked as path traversal, exit 3. - `zip64_overflow.zip` (`cd_offset = 0xFFFF_FFFF_FFFF_FFF8`, `cd_size = 16`): before -> panic `attempt to add with overflow` at `lib.rs:162`, exit 101; after -> `malformed ZIP: central directory bounds overflow (...)`, exit 1. - New regression tests: fake EOCD inside a comment, normal EOCD with a comment, ZIP64 CD bounds overflow, ZIP64 EOCD offset past EOF. - Cross-checked against Python `zipfile`: for a real archive whose comment embeds a fake EOCD, Python reports 0 entries; this parser reports the real `hello.txt`. A normal comment and a 65,535-byte comment also parse correctly. **Negative control:** - Removing the comment-end check made `ignores_fake_eocd_inside_comment` fail (`left: 0, right: 1`). - Reverting `checked_add` made `rejects_zip64_cd_bounds_overflow` fail with `attempt to add with overflow`. - Removing the ZIP64 record bounds check made `rejects_zip64_eocd_past_eof` fail with an I/O error instead of `Malformed`. - All three checks were restored; the full archive suite passes. **Environment:** Windows 11 Pro 10.0.26300, rustc/cargo 1.99.1. **Proven / inferred / not tested:** - Proven: both reproductions now fail safely, and the new tests pass. - Not tested: a real game archive; no archive fixtures are available. The rules are format-level and synthetic. - Limitation: a deliberately appended, structurally valid second EOCD at the exact end of the file remains ambiguous without scanning local file headers. This change rejects comment-embedded and malformed EOCDs and bounds-checks ZIP64/CD reads. - Behavior change: trailing bytes after the EOCD are now rejected because the record's comment must end at EOF. **Dependencies:** none. PR #37 is independent (`tombox-xshader` only). - [x] `cargo test -p tombox-archive` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note No game files, console DLLs, assets or binaries are included.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:08:17 +00:00
github-bot closed this pull request 2026-11-26 06:08:17 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:12 UTC · original

Main's history was rewritten, which auto-closed this PR. It is applied in 0a6490a (merged in 81a1252) with your authorship kept, thanks! The added comment lines were stripped on the applied copy (the project keeps code free of comment lines).

**@⁠omonkeyz** on GitHub · 2026-11-22 13:12 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/44#issuecomment-7079640694) Main's history was rewritten, which auto-closed this PR. It is applied in 0a6490a (merged in 81a1252) with your authorship kept, thanks! The added comment lines were stripped on the applied copy (the project keeps code free of comment lines).

Pull request closed

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