fix: validate OBJ face indices against declared v/vt/vn counts#1199
Open
SongTonyLi wants to merge 1 commit into
Open
fix: validate OBJ face indices against declared v/vt/vn counts#1199SongTonyLi wants to merge 1 commit into
SongTonyLi wants to merge 1 commit into
Conversation
…e#1194) ParseVertexIndices accepted any non-zero face index without checking it against the number of declared v/vt/vn entries. Out-of-range indices propagated through MapPointToVertexIndices into the attribute dedup stage, causing a heap-buffer-overflow read in PointAttribute::DeduplicateFormattedValues. Add bounds validation in ParseVertexIndices for position, texcoord, and normal indices (both positive and negative OBJ forms) so the decoder rejects malformed faces at parse time rather than crashing downstream.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1194.
I found that
ParseVertexIndicesinobj_decoder.cconly checks that face indices are non-zero — it never actually checks whether they're within the range of declaredv/vt/vnentries. So an OBJ file likef 1/100/1 2/100/2 3/100/3(where only 3vtentries exist) gets accepted by the parser, and the bogus index 100 makes it all the way throughMapPointToVertexIndicesinto the dedup stage, whereDeduplicateFormattedValuesreads out of bounds atpoint_attribute.cc:219.What's going on
I traced through the code and here's the flow:
ParseVertexIndicesparses the face line and seesvt=100. It checksindex != 0(passes) but never checksindex <= num_tex_coords_(should fail). So 100 gets through.MapPointToVertexIndicesconverts it toAttributeValueIndex(100 - 1) = 99and stores it in the attribute map — no questions asked.DeduplicateFormattedValuesdoesvalue_map[indices_map_[i]]whereindices_map_[i]is 99, butvalue_maponly has 3 entries. Heap OOB read.I added some instrumentation probes to confirm and saw exactly this:
ParseVertexIndicesemitted{v:1, vt:100, vn:1}while counts were{v:3, vt:3, vn:3}MapPointToVertexIndicesflaggedoob={v:0, vt:1, vn:0}and stored mapped index 99Fix
I added a bounds check (
indices_in_boundslambda) inParseVertexIndicesthat validates each parsed v/vt/vn index against the declared pool size before returning. It handles both positive and negative OBJ index forms (OBJ supports negative indexing like-1for "last declared entry").I went with fixing it here instead of at the crash site because
ParseVertexIndicesis where untrusted OBJ text first becomes internal indices — it already returnsfalseon error and callers already handle that, so this felt like the natural place. A guard atDeduplicateFormattedValueswould only protect that one code path while leaving bad indices inindices_map_for anything else that reads them.Testing
Built with ASan (clang 14,
-fsanitize=address):ERROR: AddressSanitizer: heap-buffer-overflow— READ of size 4 inDeduplicateFormattedValues<float, 2>atpoint_attribute.cc:219Failed loading the input mesh: Failed to parse vertex indices.— clean exit, no sanitizer outputERROR SUMMARY: 0 errors from 0 contexts, all heap blocks freedtestdata/triangle.objstill encodes fine (exit 0) — valid files aren't affectedReproducer: