Repository navigation
Hardening: add null guards, bounds checks, and buffer underflow safety to flexbuffers - #9374
Open
anagnorisis2peripeteia wants to merge 1 commit into
Conversation
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 hardens
include/flatbuffers/flexbuffers.hagainst null pointer dereferences, buffer underflows, and out-of-bounds access across the reader, builder, and verifier APIs:GetRootUnderflow & Truncation Protection:GetRoot(buffer, size)requires at least 3 bytes (size >= 3) and validatessize >= static_cast<size_t>(byte_width) + 2before decrementing pointers. Previously, passing a null or zero-length buffer caused pointer arithmetic before the buffer (*--end), triggering heap/stack buffer underflow.Null Guards in Scalar & Indirect Decoders:
ReadSizedScalar,ReadInt64,ReadUInt64,ReadDoubleguard againstdata == nullptrand return safe defaults (0 / 0.0).Indirectchecks!offset || byte_width == 0and returnsnullptr.Null-safe Reference, Vector, and Map Access:
Sized::read_size()guards!data_ || byte_width_ == 0.Referenceaccessor methods (AsInt64,AsDouble,AsBool,AsKey,AsString,AsBlob,AsVector, etc.) guard against!data_and validate indirect offsets before dereferencing.Reference::Mutate*checksdata_ != nullptr,dest != nullptr, and input pointers.Vector::operator[],TypedVector::operator[],FixedTypedVector::operator[]guard!data_.Map::operator[](const char* key)guards against nullkeyand nullkeys.data_, preventing crashes instd::bsearchandKeyCompare.Builder and Verifier Input Validation:
Builder::KeyandBuilder::Stringsafely handle null string pointers.Builder::WriteBytesandBuilder::CreateAlignedBlobhandle null data.Builder::EndVectorandBuilder::EndMapguard againststart > stack_.size().Verifier::VerifyOffsetandVerifier::VerifyBuffervalidatebuf_ != nullptrbefore pointer comparisons and buffer size checks.Unit Tests:
FlexBuffersSafetyTest()intests/flexbuffers_test.cppverifying behavior with null buffers, truncated inputs, null map keys, and empty builder calls.-DFLATBUFFERS_STRICT_MODE=ON,flattests, andflatc_cpp_tests.py.