loader/obj: guard against out-of-range face indices to prevent panic - #322
Open
ChrisJr404 wants to merge 1 commit into
Open
loader/obj: guard against out-of-range face indices to prevent panic#322ChrisJr404 wants to merge 1 commit into
ChrisJr404 wants to merge 1 commit into
Conversation
A face line whose vertex/uv/normal index points past the decoded arrays (e.g. "f 1 2 999999", or a large negative index) caused NewGeometry to panic with an index-out-of-range while copying vertices, since the indices read straight from the OBJ were passed to ArrayF32.GetVector3/GetVector2 without any bounds check. A malformed or malicious .obj is enough to crash a program that loads it. Validate each index against the corresponding array length before use and return an error instead of panicking. The bounds are checked with the element count (len/stride) rather than stride*index so the check itself cannot overflow. Adds tests covering the out-of-range cases and a valid file.
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.
Problem
Loading an OBJ file whose face lines reference vertex/UV/normal indices outside the range of the data actually present in the file causes a panic (
index out of range) rather than an error.In
NewGeometry, the indices parsed fromflines are handed straight tomath32.ArrayF32.GetVector3/GetVector2, which do unchecked slice access:Nothing verifies that the index is within the decoded
Vertices/Normals/Uvsarrays. A file such as:panics on the standard load path (
obj.Decode/DecodeReaderfollowed byNewGroup/NewGeometry). Large negative indices (e.g.f -100 -100 -100) hit the same problem via the relative-index math. Since OBJ files are frequently loaded from untrusted or user-supplied sources, a single malformed file can crash the host program.Fix
Bounds-check each vertex/UV/normal index against the corresponding array before dereferencing it, and return an error from
NewGeometryinstead of panicking. The check uses the element count (len/stride) rather thanstride*index, so it can't overflow on 32-bit platforms. Valid files are completely unaffected.Tests
Added
loader/obj/obj_test.gocovering out-of-range vertex/UV/normal indices (including a large negative index) and a well-formed file that must still build geometry without error. The out-of-range tests panic without this change and pass with it.go test ./loader/obj/,go vet, andgofmtare clean.