Skip to content

B3D: validate chunk size against buffer and parent chunk - #6705

Merged
kimkulling merged 2 commits into
assimp:masterfrom
SaudSatopay:b3d-validate-chunk-size
Jun 22, 2026
Merged

kimkulling merged 2 commits into
assimp:masterfrom
SaudSatopay:b3d-validate-chunk-size

Conversation

@SaudSatopay

@SaudSatopay SaudSatopay commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

B3DImporter::ReadChunk() pushed _pos + sz onto the chunk stack with sz taken directly from the file and never validated. ChunkSize() returns _stack.back() - _pos, so a crafted .b3d can make it either report a value far larger than the remaining buffer (oversized chunk) or underflow to a near-SIZE_MAX value when a nested chunk's end exceeds its parent's (after ExitChunk() advances _pos past the outer chunk end).

The element counts derived from ChunkSize() then drive unbounded allocations:

  • ReadVRTS(): n_verts = ChunkSize() / sz; _vertices.resize(v0 + n_verts);
  • ReadTRIS(): n_tris = ChunkSize() / 12; new aiFace[n_tris];

so a small malformed file triggers a wild allocation / OOM. (The per-field reads are already bounds-checked, so this is an allocation/DoS issue rather than an out-of-bounds read.)

Fix

Harden the two chunk-model chokepoints rather than each consumer:

  • ReadChunk() now validates that each chunk lies within the file and within its enclosing chunk before pushing its end offset.
  • ChunkSize() is made underflow-safe.

This caps every ChunkSize()-derived count at the remaining file size, fixing ReadVRTS / ReadTRIS / ReadTEXS / ReadBRUS at the root. No behavioral change for well-formed files — a conformant chunk never exceeds its parent or the file.

Summary by CodeRabbit

Bug Fixes

  • Enhanced validation of B3D file imports to better detect and reject malformed or corrupted files, improving stability and robustness when loading 3D model files.

ReadChunk() pushed _pos + sz onto the chunk stack using an unvalidated, attacker-controlled size. As a result ChunkSize() (computed as _stack.back() - _pos) could either report a value far larger than the remaining file, or underflow to a huge size_t when a nested chunk overran its parent. The element counts derived from ChunkSize() in ReadVRTS() and ReadTRIS() then drove unbounded _vertices.resize() / new aiFace[] allocations on crafted .b3d input.

Validate each chunk's extent at the ReadChunk() chokepoint (it must fit within the file and within its enclosing chunk) and make ChunkSize() underflow-safe, hardening every downstream chunk consumer at once.
@strix-security

Copy link
Copy Markdown

Strix is installed on this repository, but we could not run this PR security review because this workspace does not have an active plan. If you'd like to continue receiving code reviews, you can add a payment method or manage billing here.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1ea1541d-3b7c-437d-ae1f-a03ffdbd5c15

📥 Commits

Reviewing files that changed from the base of the PR and between e04b60f and 4d4815e.

📒 Files selected for processing (1)
  • code/AssetLib/B3D/B3DImporter.cpp

📝 Walkthrough

Walkthrough

B3DImporter::ReadChunk adds pre-parse validation that checks the declared chunk size fits within the remaining file buffer and does not exceed the enclosing chunk's validated end stored in _stack, calling Fail("Bad chunk size") on violation. B3DImporter::ChunkSize adds an underflow guard that returns 0 when _stack.back() <= _pos.

Changes

B3D Chunk Parsing Hardening

Layer / File(s) Summary
ReadChunk bounds check and ChunkSize underflow guard
code/AssetLib/B3D/B3DImporter.cpp
ReadChunk now validates the declared sz against the remaining buffer size and the enclosing chunk's stack-recorded end before pushing the new validated end, rejecting malformed sizes via Fail(). ChunkSize returns 0 when _stack.back() <= _pos to prevent unsigned underflow on subtraction.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • assimp/assimp#6438: Also adds validation to prevent malformed chunk/data sizes from being used in parsing, in a different importer (MDCLoader vertex buffer boundary checks).

Suggested labels

Bug, Security Risk

Suggested reviewers

  • kimkulling

Poem

🐇 Hop hop, no chunk shall slip past my guard,
A crafted bad size? Rejected, not hard.
The stack holds the end, the buffer won't lie,
Unsigned underflow? Returns zero, goodbye!
Safe parsing for B3D, hip hip hooray! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main security fix: validating B3D chunk sizes against both the file buffer and parent chunk boundaries.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@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.

@kimkulling

Copy link
Copy Markdown
Member

Merged, thank you for your contribution.

@kimkulling
kimkulling merged commit 381b82c into assimp:master Jun 22, 2026
14 checks passed
@kimkulling kimkulling self-assigned this Jun 22, 2026
@sonarqubecloud

Copy link
Copy Markdown

kimkulling added a commit that referenced this pull request Jul 1, 2026
ReadChunk() pushed _pos + sz onto the chunk stack using an unvalidated, attacker-controlled size. As a result ChunkSize() (computed as _stack.back() - _pos) could either report a value far larger than the remaining file, or underflow to a huge size_t when a nested chunk overran its parent. The element counts derived from ChunkSize() in ReadVRTS() and ReadTRIS() then drove unbounded _vertices.resize() / new aiFace[] allocations on crafted .b3d input.

Validate each chunk's extent at the ReadChunk() chokepoint (it must fit within the file and within its enclosing chunk) and make ChunkSize() underflow-safe, hardening every downstream chunk consumer at once.

Co-authored-by: Kim Kulling <[email protected]>
DerDoktorX pushed a commit to DerDoktorX/assimp that referenced this pull request Aug 22, 2026
ReadChunk() pushed _pos + sz onto the chunk stack using an unvalidated, attacker-controlled size. As a result ChunkSize() (computed as _stack.back() - _pos) could either report a value far larger than the remaining file, or underflow to a huge size_t when a nested chunk overran its parent. The element counts derived from ChunkSize() in ReadVRTS() and ReadTRIS() then drove unbounded _vertices.resize() / new aiFace[] allocations on crafted .b3d input.

Validate each chunk's extent at the ReadChunk() chokepoint (it must fit within the file and within its enclosing chunk) and make ChunkSize() underflow-safe, hardening every downstream chunk consumer at once.

Co-authored-by: Kim Kulling <[email protected]>
@kimkulling kimkulling assigned SaudSatopay and unassigned kimkulling Aug 27, 2026
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