Fix heap OOB read in reflection-based Verify() from unchecked Type.index() - #9198
Open
prasanna8585 wants to merge 1 commit into
Open
Fix heap OOB read in reflection-based Verify() from unchecked Type.index()#9198prasanna8585 wants to merge 1 commit into
prasanna8585 wants to merge 1 commit into
Conversation
…dex() VerifyUnion, VerifyVector, and VerifyObject in reflection.cpp use field.type()->index() to index into schema.objects()/schema.enums() without validating the index is in range. Vector::Get()'s only bounds check is an assert that is compiled out in NDEBUG (release) builds, so a schema with an out-of-range Type.index() causes an out-of-bounds read when verified via the reflection API. This affects callers using flatbuffers::Verify() with a schema from an untrusted or externally supplied source. Adds bounds checks at all three affected call sites so verification fails closed instead, plus a regression test that builds a schema with an empty objects vector and an out-of-range field type.index(), and confirms Verify() now returns false instead of reading out of bounds. Note: CopyTable/CopyInline (~L680-757) have the same unchecked index() pattern but operate on data expected to already be verified; leaving those out of this fix to keep it minimal and reviewable, flagging for a maintainer look separately.
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
VerifyUnion,VerifyVector, andVerifyObjectinreflection.cppusefield.type()->index()to index intoschema.objects()/schema.enums()without validating the index is in range.Vector::Get()'s only bounds check is an assert that is compiled out inNDEBUG(release) builds, so a schema with an out-of-rangeType.index()causes an out-of-bounds read when verified via the reflection API.Who is affected
Applications that call
flatbuffers::Verify(schema, root, buf, len)with a schema from an untrusted or externally supplied source (e.g. tools that load.bfbsschemas at runtime, per the documented reflection use case).Fix
Adds a range check (
0 <= index() < size()) at each of the three call sites in the verification path before callingGet(), returningfalse(verification failure) on an out-of-range index rather than reading out of bounds.Testing
ReflectionInvalidObjectIndexTesttotests/reflection_test.cpp, which builds a schema with an emptyobjectsvector and a fieldtype.index()far out of range, and confirmsVerify()now fails closed.Vector::Getassert) against the unfixed source, and passes cleanly with the fix applied.flattests) passes with no regressions.Note for maintainers
CopyTable/CopyInline(roughly lines 680-757) have the same uncheckedindex()pattern, but they operate on data that callers are expected to have already run throughVerify(). Left out of this fix to keep the change minimal and focused on the verification path itself; flagging here in case it's worth a follow-up.