Fix wild pointer in map_key()/map_value() for malformed map_entry descriptors#27163
Open
jortles wants to merge 1 commit intoprotocolbuffers:mainfrom
Open
Fix wild pointer in map_key()/map_value() for malformed map_entry descriptors#27163jortles wants to merge 1 commit intoprotocolbuffers:mainfrom
jortles wants to merge 1 commit intoprotocolbuffers:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
…criptors When BuildFile() with AllowUnknownDependencies() processes a FileDescriptorProto containing a message marked as map_entry with 0 fields (instead of the required 2), map_value() returns field(1) on a 0-element array. The ABSL_DCHECK_EQ(field_count(), 2) guard is a no-op in Release builds (NDEBUG defined), producing a wild pointer. Subsequent calls to cpp_type() on this wild pointer read garbage from adjacent memory and index kTypeToCppTypeMap out of bounds. Fix: 1. Change ABSL_DCHECK_EQ to ABSL_CHECK_EQ in map_key() and map_value() so the invariant is enforced in all build configurations. 2. Add validation in BuildMessage() to reject map_entry messages that don't have exactly 2 fields, catching the problem at descriptor build time before map_key()/map_value() can be called.
ae83cef to
264c869
Compare
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
Descriptor::map_key()andmap_value()useABSL_DCHECK_EQ(field_count(), 2)to guard against invalid map_entry descriptors. SinceABSL_DCHECKis a no-op in Release builds (NDEBUGdefined), a malformed map_entry descriptor with 0 fields causesfield(1)to return a wild pointer. Subsequent calls tocpp_type()on this wild pointer read garbage and indexkTypeToCppTypeMapout of bounds, producing a global-buffer-overflow.Trigger path
DescriptorPool::BuildFile()withAllowUnknownDependencies()accepts aFileDescriptorProtocontaining a message withoptions.map_entry = trueand 0 fieldsDynamicMessageFactory::GetPrototype()+Message::ParseFromArray()on that messageIsInitialized(),ReflectionOpscallsdescriptor->map_value()which returns&fields_[1]on a 0-element array (wild pointer)field->cpp_type()reads garbagetype_from the wild pointer and indexeskTypeToCppTypeMap[garbage]out of boundsBehavior by build type
Fix
ABSL_DCHECK_EQ→ABSL_CHECK_EQinmap_key()andmap_value()so the invariant is enforced in all build configurationsBuildMessage()to reject map_entry messages withfield_count() != 2at descriptor build time, beforemap_key()/map_value()can be calledTesting
Verified with a 95-byte crafted
FileDescriptorProtoinput:AddressSanitizer: global-buffer-overflowinFieldDescriptor::cpp_type()atdescriptor.h:3164BuildFile()rejects the input with error "Map entry message must have exactly 2 fields (key and value)." — clean exit, no crash