Skip to content

nacp, romfs: reject a control.nacp or RomFS image cut short within their contracts (#454) - #605

Closed
mrkayhyun wants to merge 1 commit into
seedds:mainfrom
mrkayhyun:fix/nacp-romfs-short-files
Closed

mrkayhyun wants to merge 1 commit into
seedds:mainfrom
mrkayhyun:fix/nacp-romfs-short-files

Conversation

@mrkayhyun

Copy link
Copy Markdown
Contributor

What and why

Fixes #454. The fuzz harnesses (#455) found two parsers that raise struct.error on files cut short, instead of keeping their contracts:

  • nacp.name unpacked the u16 size of a compressed title area before checking the file holds two bytes, so a 0- or 1-byte control.nacp raised struct.error where name() otherwise returns ''. It now tries the compressed area only when the file holds that size; a shorter file falls through to the plain title scan and returns ''.
  • romfs.Image unpacked the 32-byte header from whatever read returned, so an image cut short after its magic raised struct.error. It now raises the same ValueError('invalid RomFS image header: …') as the other header checks when fewer than 32 bytes come back.

dm/tools/fuzz/known.tsv loses the two #454 rows, so corpus/nacp/empty.bin and corpus/romfs/magic-only.img replay as ordinary regression tests in test_fuzz_corpus.py instead of expected failures. No reference entry or game note: the change keeps the parsers' documented contracts and adds no mechanism, as with the uepak (#453) and shader_split (#452) fixes.

Checked

  • Tests (Linux, Python 3.14, the tool-test loop of tests.yml with DM_GAME=010015100B514000): every dm/tools/tests, dm/ios/tests and dm/gfx/tests file has the same result as on main except test_fuzz_corpus.py, which goes from OK (expected failures=2) to OK (32 tests). The files that fail on Linux fail the same way on main (they need macOS: renamex_np, Xcode, Metal, the .NET translator, spirv-val).
  • With the code changes reverted and the rows removed, test_nacp_empty_bin and test_romfs_magic_only_img fail with struct.error@nacp.py:name and struct.error@romfs.py:__init__.
  • Crafted inputs: control.nacp of 0, 1 and 2 bytes and a u16 size of 0xffff over a cut deflate stream return '' / []; DMROMFS1 images of 0 to 31 bytes raise ValueError.
  • Fuzz (fuzz.yml's command, atheris 3.1.0, 60 s each): nacp 571,690 runs, romfs 1,353,160 runs, no finding.
  • Lint ratchet with the pinned tools (ruff 0.17.2, pyright 1.1.414): 392 findings, 392 in the baseline, 0 new, 0 fixed but still listed.
  • Games run: none; the change is in two file readers and needs no game files. Devices: none (Linux only). scripts/regress.py check --games not run: it needs local games, and the reach is two readers' handling of files under 32 bytes, which no game's files are.

Docs

  • The finding is in its game note or its docs/reference entry — not applicable: no new mechanism (see above)
  • A chain tool changed — not applicable
  • No game files, keys or data extracted from a game in the change or the attachments

This change was prepared with the help of AI coding tools; I reviewed the diff and ran the checks above myself.

…eir contracts (GitHub issue seedds#454)

The fuzz harnesses (seedds#455) found two readers that raise struct.error on
files cut short. nacp.name unpacked the u16 size of a compressed title
area before checking that the file holds two bytes, so a 0- or 1-byte
control.nacp raised where name() otherwise returns ''. romfs.Image
unpacked its 32-byte header from a short read, so an image cut short
after its magic raised struct.error instead of its ValueError.

- name() tries the compressed title area only when the file holds its
  size; a shorter file returns '' through the plain title scan.
- Image() raises "invalid RomFS image header" when fewer than 32 header
  bytes come back, as its other header checks do.

known.tsv loses the 2 seedds#454 rows; corpus/nacp/empty.bin and
corpus/romfs/magic-only.img now replay as regular tests.

Checked: test_fuzz_corpus passes (32 tests) and fails with both code
changes reverted. The other tool tests have main's results on Linux.
The lint ratchet reports 0 new. atheris, 60 s per harness: nacp 571,690
runs, romfs 1,353,160 runs, no finding.
seedds added a commit that referenced this pull request Oct 29, 2026
…eir contracts (GitHub issue #454) (#605)

The fuzz harnesses (#455) found two readers that raise struct.error on
files cut short. nacp.name unpacked the u16 size of a compressed title
area before checking that the file holds two bytes, so a 0- or 1-byte
control.nacp raised where name() otherwise returns ''. romfs.Image
unpacked its 32-byte header from a short read, so an image cut short
after its magic raised struct.error instead of its ValueError.

- name() tries the compressed title area only when the file holds its
  size; a shorter file returns '' through the plain title scan.
- Image() raises "invalid RomFS image header" when fewer than 32 header
  bytes come back, as its other header checks do.

known.tsv loses the 2 #454 rows; corpus/nacp/empty.bin and
corpus/romfs/magic-only.img now replay as regular tests.

Checked: test_fuzz_corpus passes (32 tests) and fails with both code
changes reverted. The other tool tests have main's results on Linux.
The lint ratchet reports 0 new. atheris, 60 s per harness: nacp 571,690
runs, romfs 1,353,160 runs, no finding.

Co-authored-by: DongHyun Kim <271588+mrkayhyun@users.noreply.github.com>
@seedds

seedds commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Landed on main as 713ecaa, unchanged, with you as co-author. Thanks! #610 landed on top of it as e4292dd.

@seedds seedds closed this Oct 29, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…eir contracts (GitHub issue seedds#454) (seedds#605)

The fuzz harnesses (seedds#455) found two readers that raise struct.error on
files cut short. nacp.name unpacked the u16 size of a compressed title
area before checking that the file holds two bytes, so a 0- or 1-byte
control.nacp raised where name() otherwise returns ''. romfs.Image
unpacked its 32-byte header from a short read, so an image cut short
after its magic raised struct.error instead of its ValueError.

- name() tries the compressed title area only when the file holds its
  size; a shorter file returns '' through the plain title scan.
- Image() raises "invalid RomFS image header" when fewer than 32 header
  bytes come back, as its other header checks do.

known.tsv loses the 2 seedds#454 rows; corpus/nacp/empty.bin and
corpus/romfs/magic-only.img now replay as regular tests.

Checked: test_fuzz_corpus passes (32 tests) and fails with both code
changes reverted. The other tool tests have main's results on Linux.
The lint ratchet reports 0 new. atheris, 60 s per harness: nacp 571,690
runs, romfs 1,353,160 runs, no finding.

Co-authored-by: DongHyun Kim <271588+mrkayhyun@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nacp and romfs: struct.error on files cut short (fuzzing)

2 participants