verifier: bounds check start offset in VerifyBufferFromStart and prevent overflow in VerifySizePrefixedBuffer - #9275
Open
filtede98 wants to merge 1 commit into
Conversation
…ent overflow in VerifySizePrefixedBuffer 1. In VerifyBufferFromStart(), validate that the remaining buffer starting from start has at least FLATBUFFERS_MIN_BUFFER_SIZE (start <= size_ && size_ - start >= FLATBUFFERS_MIN_BUFFER_SIZE) and at least 2 * sizeof(uoffset_t) when an identifier is present. Previously, only size_ >= ... was checked without accounting for start, which could allow out-of-bounds reads in BufferHasIdentifier() when verifying size-prefixed buffers or custom start offsets with identifiers. 2. In VerifySizePrefixedBuffer(), check ReadScalar<SizeT>(buf_) <= size_ - sizeof(SizeT) to prevent unsigned integer overflow when adding sizeof(SizeT) to a large prefix value. 3. In base.h, wrap the FLATBUFFERS_MIN_BUFFER_SIZE macro expression in parentheses to eliminate operator precedence hazards. 4. In monster_test.cpp, add unit test assertions in SizePrefixedTest() verifying truncated size-prefixed buffers and boundary offsets are safely rejected.
This branch has not been deployed
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
This PR addresses boundary checking and overflow safety in the core
flatbuffers::Verifier:Bounds Check
startOffset inVerifyBufferFromStart():When verifying buffers that start at an offset (such as size-prefixed buffers where
start = sizeof(SizeT)or custom offsets),VerifyBufferFromStart()previously checked:Checking
size_ >= ...without accounting forstartdid not guarantee that the remaining buffer starting atbuf_ + starthas sufficient length. When verifying a buffer with an identifier wherestart > 0,BufferHasIdentifier(buf_ + start, identifier)reads bytes up tobuf_ + start + 2 * sizeof(uoffset_t). If the remaining buffer afterstartis smaller than 8 bytes, this could read past the end of the buffer.We now validate that
start <= size_ && size_ - start >= FLATBUFFERS_MIN_BUFFER_SIZEand thatstart <= size_ && size_ - start >= 2 * sizeof(flatbuffers::uoffset_t)before callingBufferHasIdentifier().Prevent Integer Overflow in
VerifySizePrefixedBuffer():In
VerifySizePrefixedBuffer(), the check:Check(ReadScalar<SizeT>(buf_) + sizeof(SizeT) <= size_)could wrap around if
ReadScalar<SizeT>(buf_)is close to the integer maximum (e.g.SIZE_MAX). We change this to:Check(ReadScalar<SizeT>(buf_) <= size_ - sizeof(SizeT))which is immune to integer overflow since
Verify<SizeT>(0U)already guaranteessizeof(SizeT) <= size_.Macro Precedence in
base.h:Wrapped the
FLATBUFFERS_MIN_BUFFER_SIZEmacro expression in parentheses to avoid operator precedence hazards when used in composite expressions.Unit Tests:
Added assertions in
SizePrefixedTest()intests/monster_test.cppverifying that truncated size-prefixed buffers and boundary offsets are safely rejected.