Skip to content

upb: fix union type confusion in upb_Message_DeleteUnknown when followed by AuxField - #31877

Open
hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-delete-unknown-auxfield-type-confusion
Open

hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-delete-unknown-auxfield-type-confusion

Conversation

@hyc-alt

@hyc-alt hyc-alt commented Nov 27, 2026

Copy link
Copy Markdown

Summary

Fixes a union type confusion bug in upb_Message_DeleteUnknown() (upb/message/unknown_fields.c) when a raw upb_StringView unknown field is followed by a non-canonical upb_AuxField entry (such as a non-canonical extension or freestanding demoted field).

Root Cause

  1. upb_Message_NextUnknown() skips kUpb_MessageUnknownType_AuxField entries and only yields kUpb_MessageUnknownType_StringView chunks.
  2. upb_Message_DeleteUnknown() delegates to upb_Message_DeleteUnknown2(), which advances *iter via upb_Message_NextUnknown2() (upb/message/unknown_fields.c:180) and may stop on a following AuxField.
  3. upb_MessageUnknown.value is a union { upb_StringView bytes; const upb_AuxField* aux_field; }. Setting value.aux_field overwrites value.bytes.data with the upb_AuxField* pointer while leaving value.bytes.size set to the deleted StringView's length.
  4. In debug builds, line 194 aborts on UPB_ASSERT(unknown.type == kUpb_MessageUnknownType_StringView).
  5. In release (-DNDEBUG) builds, line 199 copies *data = unknown.value.bytes and returns kUpb_DeleteUnknown_IterUpdated. Callers following the documented loop in upb/message/unknown_fields.h then:
    • Read out of bounds past the upb_AuxField allocation when inspecting data.data[0..data.size - 1] (reproduced under ASan as use-after-poison READ).
    • If calling upb_Message_DeleteUnknown() again on the returned data, reinterpret upb_AuxField* as upb_StringView* via upb_TaggedPtrAux_StringViewRepr(), enter the middle-split branch, overwrite the upb_AuxField's value slot with a pointer difference, re-tag it as an aliased UnknownStringView, and produce a wrapped ~SIZE_MAX length that crashes subsequent upb_Encode().

Fix

When upb_Message_DeleteUnknown2() returns kUpb_DeleteUnknown_IterUpdated with unknown.type != kUpb_MessageUnknownType_StringView, advance to the next StringView unknown via upb_Message_NextUnknown(msg, data, iter) (leaving any intervening non-canonical AuxField entries untouched) and return kUpb_DeleteUnknown_DeletedLast if no StringView unknowns remain.

Testing

  • Added regression tests GeneratedCode.DeleteUnknownSkipsNonCanonicalAuxField and GeneratedCode.DeleteUnknownFollowedOnlyByAuxFieldReturnsDeletedLast in upb/message/unknown_fields_test.cc.
  • Verified before/after under ASan+UBSan in both Release (-DNDEBUG) and Debug builds as well as -std=c99 -Wall -Wextra -Werror.

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant