CVE-2026-58936 AI-generated analysis
platform/external/libpng, tRNS/PLTE storage (pngset.c), read/write struct teardown (pngread.c, pngwrite.c), read transforms (pngrtran.c)
Gives png_struct its own heap copies of the tRNS alpha array and the PLTE palette instead of aliasing the buffers owned by png_info, so that freeing them through info_ptr (png_free_data or a repeated png_set_tRNS/png_set_PLTE) no longer leaves png_ptr->trans_alpha / png_ptr->palette dangling for the row transform code.
Root cause
In pngset.c, png_set_tRNS ended with `png_ptr->trans_alpha = info_ptr->trans_alpha;` and png_set_PLTE assigned `info_ptr->palette = png_ptr->palette;`, so png_struct and png_info pointed at the same heap buffer. When the buffer was freed through info_ptr, either by png_free_data(PNG_FREE_TRNS/PNG_FREE_PLTE) or by the png_free_data call at the start of a second png_set_tRNS/png_set_PLTE, png_ptr kept the stale pointer. Before the fix, png_read_destroy freed png_ptr's copy only when png_ptr->free_me had the matching bit set. Per the commit messages, row transforms such as png_do_expand_palette then read freed memory, and the palette bit-shift path wrote to it (the code context shows in-place writes to png_ptr->palette[i] in the READ_SHIFT block). A TODO in png_handle_tRNS ("horrible side effect ... Fix this.") documented the aliasing. png_set_quantize also stored the caller's raw palette pointer into png_ptr->palette, which would have made an unconditional free in png_read_destroy unsafe.
How the fix works
- Commit 1 (52106375ca52, trans_alpha), pngset.c:png_set_tRNS: after png_free_data(PNG_FREE_TRNS) releases the info_ptr copy, info_ptr->trans_alpha is allocated (PNG_MAX_PALETTE_LENGTH) and filled as before. png_ptr->trans_alpha is then freed, and a second, independent PNG_MAX_PALETTE_LENGTH buffer is allocated with png_malloc and filled by memcpy with num_trans bytes. In the else branch (num_trans outside 1..PNG_MAX_PALETTE_LENGTH), png_ptr->trans_alpha is freed and set to NULL. The aliasing assignment `png_ptr->trans_alpha = info_ptr->trans_alpha` is removed.
- Commit 1, pngread.c:png_read_destroy: png_ptr->trans_alpha is now freed unconditionally instead of only when png_ptr->free_me has PNG_FREE_TRNS. The free_me mask manipulation is dropped.
- Commit 1, pngwrite.c:png_write_destroy: a new block under PNG_tRNS_SUPPORTED frees png_ptr->trans_alpha and sets it to NULL.
- Commit 1, pngrutil.c:png_handle_tRNS: only the TODO comment about the side effect is removed. The call to png_set_tRNS is unchanged.
- Commit 2 (ec4c649d74e8, tail bytes), pngset.c:png_set_tRNS: both newly allocated trans_alpha buffers are memset to 0xff (fully opaque) before the num_trans-byte memcpy, so entries beyond num_trans are defined.
- Commit 3 (a99601555dd3, palette), pngset.c:png_set_PLTE: frees any existing png_ptr->palette, then uses png_calloc to allocate two separate PNG_MAX_PALETTE_LENGTH*sizeof(png_color) buffers for png_ptr->palette and info_ptr->palette. It sets both num_palette fields and copies the caller data into each buffer. The aliasing assignment `info_ptr->palette = png_ptr->palette` is removed. png_calloc is used because the ARM NEON palette riffle reads all 256 entries.
- Commit 3, pngrtran.c:png_set_quantize: when png_ptr->palette is NULL, it now allocates an owned PNG_MAX_PALETTE_LENGTH*sizeof(png_color) buffer with png_calloc and copies num_palette entries from the caller, instead of storing the caller's pointer.
- Commit 3, pngread.c:png_read_destroy: png_ptr->palette is freed unconditionally with png_free. Previously png_zfree was called only when free_me had PNG_FREE_PLTE.
- Commit 3, pngwrite.c:png_write_destroy: a new unconditional free of png_ptr->palette is placed after the trans_alpha block.
- Commit 4 (cb4f8fc3b873, sync), pngrtran.c:png_read_transform_info: at entry, if png_ptr->transformations != 0, info_ptr->color_type is PNG_COLOR_TYPE_PALETTE and both palette pointers are non-NULL, it copies all PNG_MAX_PALETTE_LENGTH entries from png_ptr->palette into info_ptr->palette. Per the commit message, this keeps in-place edits made by png_init_read_transformations (for example gamma correction or background compositing) visible through png_get_PLTE. Without it, the decoupled info_ptr copy kept the original values and tests failed on indexed-colour background compositing.
Completeness
- Both aliased fields shown in the material, png_ptr->trans_alpha and png_ptr->palette, are decoupled. This covers both the read (png_read_destroy) and write (png_write_destroy) teardown paths. The quantize path that could put a non-owned pointer into png_ptr->palette is converted to an owned copy.
- Synchronisation back to info_ptr goes one way only, happens once at png_read_transform_info entry, and covers only the palette. Any in-place change to png_ptr->palette after that point, or to png_ptr->trans_alpha at any point, is not reflected in info_ptr. The material does not show whether such changes exist.
- The sync runs only when png_ptr->transformations != 0 and color_type is PALETTE. The material does not show whether this guard covers every case in which png_init_read_transformations edits the palette.
- png_set_tRNS's else branch sets png_ptr->trans_alpha to NULL, but how png_ptr->num_trans is handled in that branch is outside the shown diff.
- These commits do not examine any other pointers shared between png_struct and png_info. The material gives no evidence either way about other fields.
Behavior changes
- Each png_set_tRNS call now makes two allocations of PNG_MAX_PALETTE_LENGTH bytes. Each png_set_PLTE call makes two png_calloc allocations of PNG_MAX_PALETTE_LENGTH*sizeof(png_color). png_set_quantize allocates an owned palette copy.
- png_read_destroy and png_write_destroy free png_ptr->trans_alpha and png_ptr->palette unconditionally. png_read_destroy no longer checks or clears png_ptr->free_me for PNG_FREE_TRNS/PNG_FREE_PLTE.
- trans_alpha buffers are initialised to 0xff beyond num_trans. Palette buffers are zero-filled beyond num_palette.
- png_set_tRNS with num_trans outside 1..PNG_MAX_PALETTE_LENGTH now frees png_ptr->trans_alpha and sets it to NULL, instead of aliasing whatever info_ptr held.
- After png_read_transform_info, info_ptr->palette reflects png_ptr's in-place palette edits through an explicit copy rather than through aliasing. Between png_init_read_transformations and png_read_transform_info, the two palettes can differ.
Per major
- Android 14: Same fix: four commits in android-security-14.0.0_r31, applied in the order 52106375ca52 (trans_alpha), ec4c649d74e8 (tail bytes), a99601555dd3 (palette), cb4f8fc3b873 (palette sync). The resolver reports it resolved, with 7/8 patched-region lines present.
- Android 15: Fix present in android-security-15.0.0_r19. The shown tail-bytes commit 2e6ffdecb5c2 has the same added lines as Android 14, with different line offsets and the free_me/valid order swapped in the context. The other commits are not shown. Resolved, 7/8 lines present.
- Android 16: Fix present in android-security-16.0.0_r10. The shown palette commit ccc693f45ca2 makes the same changes as Android 14's a99601555dd3, with different context. The other commits are not shown. Resolved, 5/8 lines present; the material does not explain the lower count.
- Android 17: Adapted fix in android-security-17.0.0_r3. The shown trans_alpha commit 1e67678cb923 has the same pngset.c/pngread.c/pngwrite.c changes, but png_handle_tRNS in that branch returns handled_error/handled_ok, so the pngrutil.c hunk context differs. The other commits are not shown. Resolved, 6/8 lines present.
Unknowns
- Whether a crafted PNG file alone, without application calls to png_free_data or repeated png_set_* calls, can cause the info_ptr-side free before row processing. The material only names those two triggers.
- Which Android components reach the vulnerable paths (index search shows only aapt and androidfw users), and how the bulletin's Critical/RCE rating maps to a specific consumer.
- Whether other png_struct fields alias memory owned by png_info. The commits only address trans_alpha and palette.
- The exact content of the remaining commits on Android 15, 16 and 17 (only one commit per branch is shown).
- Why the patched-region match counts are below 8/8 on each branch.