Skip to content

upb: deep-copy repeated closed-enum arrays in upb_Message_ConvertExtension - #31875

Open
hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-convert-extension-closed-enum-uaf
Open

hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-convert-extension-closed-enum-uaf

Conversation

@hyc-alt

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

Copy link
Copy Markdown

Summary

upb_Message_ConvertExtension() shallow-copies a repeated closed-enum upb_Array* directly into dst when converting to a repeated open-enum field or extension. When src also carries unknown wire values for that field (the normal result of upb_Decode() encountering unknown enum values on a closed-enum field), the subsequent unknown-field decoding pass in upb_Message_ConvertInternal() appends those values directly into src's shared upb_Array using dst's arena.

This:

  1. Mutates const upb_Message* src in place, appending invalid closed-enum values into src.
  2. Reallocates src's array buffer onto dst's arena (c->arena), causing a heap-use-after-free when src is read after dst's arena is freed.
  3. Fails the debug round-trip assertion (upb_Message_IsEqual(decoded_msg, dst, dst_mt, 0)) in upb_Message_Convert().

Root Cause

For regular base fields, upb_Converter_NeedsClosedEnumDeepConvert() (upb/message/convert.c:563-582) explicitly deep-copies repeated arrays for Closed -> Open conversions so decoding unknowns into dst cannot mutate src's array.

However, upb_Message_ConvertExtension() (upb/message/convert.c:784) only checked upb_MiniTableField_IsClosedEnum(dst_f), missing Closed -> Open conversions for both canonical repeated closed-enum extensions and demoted non-extension unknown upb_AuxField entries.

Fix

Use upb_Converter_NeedsClosedEnumDeepConvert(dst_f, src_f) in upb_Message_ConvertExtension() so repeated Closed -> Open enum arrays are deep-copied onto dst's arena instead of aliasing src's array.

Verification

  • Before fix (ASan + UBSan):
    • Canonical repeated closed-enum extension ([1, 1, 1, 1] + unknown values [99..95] on arena_a) converted to a repeated open-enum field on arena_b, followed by upb_Arena_Free(arena_b) and reading src, triggers AddressSanitizer: heap-use-after-free (READ of size 4 in upb_Array_Get).
    • 2-hop Proto2EnumMessage -> EmptyMessage -> TestAllTypesProto3 via AuxField fails UPB_ASSERT(upb_Message_IsEqual(decoded_msg, dst, dst_mt, 0)) in Debug and triggers the same cross-arena heap-use-after-free in -DNDEBUG ASan builds.
  • After fix:
    • upb/message/convert.c compiles cleanly with -Wall -Wextra -Werror.
    • Both 1-hop extension and 2-hop AuxField repros pass cleanly under Debug+ASan+UBSan, Release+ASan+UBSan (-DNDEBUG), Release (-O2 -DNDEBUG), and FastTable Release (-O2 -DNDEBUG -DUPB_ENABLE_FASTTABLE=1), leaving src completely unmodified after freeing dst's arena.
    • Added regression test ConvertTest.ClosedToOpenRepeatedEnumAuxFieldDoesNotMutateSource in upb/message/convert_test.cc.

…nsion

upb_Message_ConvertExtension() previously only checked
upb_MiniTableField_IsClosedEnum(dst_f), so converting a repeated
closed-enum extension or unknown aux field to a repeated open-enum field
or extension shallow-copied the source upb_Array*. When the source also
carries unknown wire values for that field, decoding unknowns into dst
mutates the const source array and reallocates its backing buffer onto
dst's arena, causing a heap-use-after-free once dst's arena is freed.

Use upb_Converter_NeedsClosedEnumDeepConvert(dst_f, src_f), matching
upb_Message_ConvertField().

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