Repository navigation
Conversation
…wed by AuxField upb_Message_DeleteUnknown() wraps upb_Message_DeleteUnknown2(), which advances the iterator via upb_Message_NextUnknown2() and may stop on a non-canonical upb_AuxField entry rather than a raw upb_StringView unknown field. Because upb_MessageUnknown.value is a union of upb_StringView (16 bytes) and const upb_AuxField* (8 bytes), landing on an AuxField overwrites unknown.value.bytes.data with the upb_AuxField* pointer while leaving unknown.value.bytes.size equal to the previously deleted StringView's length. In debug builds this trips UPB_ASSERT(unknown.type == kUpb_MessageUnknownType_StringView); in release (-DNDEBUG) builds upb_Message_DeleteUnknown() copies the type-confused StringView back to *data and returns kUpb_DeleteUnknown_IterUpdated. Callers iterating with the documented upb_Message_NextUnknown()/upb_Message_DeleteUnknown() loop then read out of bounds past the upb_AuxField allocation, or on a subsequent upb_Message_DeleteUnknown() call reinterpret the upb_AuxField as a upb_StringView, corrupting its value slot and aux_data tag. When upb_Message_DeleteUnknown2() updates the iterator to a non-StringView entry, advance to the next StringView entry via upb_Message_NextUnknown() (preserving any non-canonical aux fields untouched) and return kUpb_DeleteUnknown_DeletedLast if no StringView unknowns remain.
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
Fixes a union type confusion bug in
upb_Message_DeleteUnknown()(upb/message/unknown_fields.c) when a rawupb_StringViewunknown field is followed by a non-canonicalupb_AuxFieldentry (such as a non-canonical extension or freestanding demoted field).Root Cause
upb_Message_NextUnknown()skipskUpb_MessageUnknownType_AuxFieldentries and only yieldskUpb_MessageUnknownType_StringViewchunks.upb_Message_DeleteUnknown()delegates toupb_Message_DeleteUnknown2(), which advances*iterviaupb_Message_NextUnknown2()(upb/message/unknown_fields.c:180) and may stop on a followingAuxField.upb_MessageUnknown.valueis a union{ upb_StringView bytes; const upb_AuxField* aux_field; }. Settingvalue.aux_fieldoverwritesvalue.bytes.datawith theupb_AuxField*pointer while leavingvalue.bytes.sizeset to the deletedStringView's length.UPB_ASSERT(unknown.type == kUpb_MessageUnknownType_StringView).-DNDEBUG) builds, line 199 copies*data = unknown.value.bytesand returnskUpb_DeleteUnknown_IterUpdated. Callers following the documented loop inupb/message/unknown_fields.hthen:upb_AuxFieldallocation when inspectingdata.data[0..data.size - 1](reproduced under ASan asuse-after-poisonREAD).upb_Message_DeleteUnknown()again on the returneddata, reinterpretupb_AuxField*asupb_StringView*viaupb_TaggedPtrAux_StringViewRepr(), enter the middle-split branch, overwrite theupb_AuxField's value slot with a pointer difference, re-tag it as an aliasedUnknownStringView, and produce a wrapped~SIZE_MAXlength that crashes subsequentupb_Encode().Fix
When
upb_Message_DeleteUnknown2()returnskUpb_DeleteUnknown_IterUpdatedwithunknown.type != kUpb_MessageUnknownType_StringView, advance to the nextStringViewunknown viaupb_Message_NextUnknown(msg, data, iter)(leaving any intervening non-canonicalAuxFieldentries untouched) and returnkUpb_DeleteUnknown_DeletedLastif noStringViewunknowns remain.Testing
GeneratedCode.DeleteUnknownSkipsNonCanonicalAuxFieldandGeneratedCode.DeleteUnknownFollowedOnlyByAuxFieldReturnsDeletedLastinupb/message/unknown_fields_test.cc.-DNDEBUG) and Debug builds as well as-std=c99 -Wall -Wextra -Werror.