Fix heap-buffer-overflow in ForAllFields for out-of-range field id - #9195
Open
prasanna8585 wants to merge 1 commit into
Open
Fix heap-buffer-overflow in ForAllFields for out-of-range field id#9195prasanna8585 wants to merge 1 commit into
prasanna8585 wants to merge 1 commit into
Conversation
reflection::ForAllFields() (src/reflection.cpp) resizes a std::vector<uint32_t> to the object's real field count, then writes to it using each field's raw, schema-supplied id (uint16_t, up to 65535) as the index, with no bounds check. This is the same bug class already fixed in StructDef::Deserialize (idl_parser.cpp, google#8988), but was never applied here. Reachable via flatc --nim, flatc --lua, and flatc --annotate, all of which call ForAllFields directly on a caller-supplied .bfbs reflection schema file. Confirmed via AddressSanitizer against the real, unmodified, compiled library: heap-buffer-overflow WRITE in field_to_id_map[field->id()] = i, at an attacker-controlled offset directly proportional to the crafted field id. Fix: skip any field whose id is out of range rather than writing OOB. ForAllFields is void with no error-return mechanism, so this is the minimal non-breaking fix, matching the validation StructDef::Deserialize already applies to this same untrusted data. Adds a regression test (ForAllFieldsOutOfRangeIdTest) alongside the existing ForAllFieldsReverseTest. Full official CMake test suite (flattests), ASan-instrumented: ALL TESTS PASSED, zero regressions.
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
reflection::ForAllFields()(src/reflection.cpp) resizes astd::vector<uint32_t>to the object's real field count, then writes to it using each field's raw, schema-suppliedid(uint16_t, up to 65535) as the index -- with no bounds check:This is the exact same bug class already fixed in
StructDef::Deserialize(idl_parser.cpp, #8988) -- resize a vector to field count, then index into it with a raw, schema-supplied field ID -- but that fix never touched this sibling function.Reachable via
flatc --nim,flatc --lua, andflatc --annotate, all of which callForAllFieldsdirectly on a caller-supplied.bfbsreflection schema file.Verification
Confirmed via AddressSanitizer against the real, unmodified, compiled library, using a
reflection::Objectbuilt through the library's own real builder API:(A 1-field object with
field->id() = 50overflows by exactly50*4 - 4 = 196bytes -- confirming the write offset is directly attacker-controlled via the field'sid.)Fix
Skip any field whose
idis out of range, rather than writing OOB.ForAllFieldsisvoidwith no error-return mechanism (unlikeStructDef::Deserialize, which can returnfalse), so this is the minimal, non-breaking fix -- matching the validationStructDef::Deserializealready applies to this same untrusted data.Testing
ForAllFieldsOutOfRangeIdTestalongside the existingForAllFieldsReverseTest, exercising the exact crafted-schema attack input.flattestssuite: ALL TESTS PASSED, zero regressions across the entire codebase.