Skip to content

Fix heap-buffer-overflow reading MD2 skin names - #6832

Merged
kimkulling merged 3 commits into
assimp:masterfrom
afonsojanu:fix/md2-skin-name-heap-overflow
Sep 8, 2026
Merged

kimkulling merged 3 commits into
assimp:masterfrom
afonsojanu:fix/md2-skin-name-heap-overflow

Conversation

@afonsojanu

@afonsojanu afonsojanu commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

The skin name in an MD2 file lives in a fixed 64-byte field copied directly out of the file, and nothing guarantees it actually has a NUL terminator anywhere inside it. On a malformed file where that field is filled with non-zero bytes right up to the end of the file, strlen() on pcSkins->name just keeps going past the end of the read buffer looking for a byte that isn't there.

Fixed it by bounding the scan with strnlen(), capped at one less than the field's own size. MDCLoader already does the same thing for its own fixed-size name fields (ucName), so this brings MD2 in line with that.

Added a regression test with a small hand-built MD2 file: one skin, name field packed entirely with non-NUL bytes, ending exactly at the file's last byte. Confirmed with an ASan build that this crashes with a heap-buffer-overflow in strlen on unmodified master, and imports cleanly with the fix applied. Full unit suite (666 tests) passes clean under ASan+UBSan.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of malformed MD2 texture names to prevent reads beyond the available data.
    • MD2 files with full-length, unterminated skin-name fields now import successfully.
  • Tests

    • Added regression coverage for importing MD2 files with unterminated texture names.

The skin name field in an MD2 file is a fixed 64-byte buffer copied
straight from the file, with nothing guaranteeing it actually contains
a NUL terminator. If a malformed or crafted file fills those 64 bytes
with non-zero data right up against the end of the file, strlen() on
that field walks straight past the end of the read buffer looking for
a terminator that isn't there.

Bounded the scan with strnlen() capped at one less than the field's
size instead, the same approach MDCLoader already uses for its own
fixed-size name fields. Added a regression test with a hand-built MD2
file that reproduces the crash under ASan on the old code and imports
cleanly with the fix.
@coderabbitai

coderabbitai Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 3484a328-82fa-4f4b-9909-5ccfe6908a94

📥 Commits

Reviewing files that changed from the base of the PR and between 16c1d98 and ae57ab8.

📒 Files selected for processing (1)
  • code/AssetLib/MD2/MD2Loader.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • code/AssetLib/MD2/MD2Loader.cpp

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The MD2 loader now bounds skin-name scanning with strnlen. A regression test imports an MD2 file with a fully occupied, unterminated skin-name field.

Changes

MD2 skin-name safety

Layer / File(s) Summary
Bounded skin-name parsing and regression coverage
code/AssetLib/MD2/MD2Loader.cpp, test/unit/ImportExport/utMD2Importer.cpp
The loader caps skin-name scanning at the fixed field size minus one. Formatting-only declaration and brace changes remain in the loader. The regression test verifies successful import of an unterminated skin name.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to b442e

The MD2 importer now bounds malformed skin-name parsing and includes regression coverage for unterminated fixed-width names. No remaining merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: fixing a heap-buffer-overflow when reading MD2 skin names.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kimkulling kimkulling left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine for me.

@sonarqubecloud

sonarqubecloud Bot commented Sep 8, 2026

Copy link
Copy Markdown

@kimkulling
kimkulling merged commit 17ec36b into assimp:master Sep 8, 2026
15 checks passed
@kimkulling

Copy link
Copy Markdown
Member

Merged, thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants