Skip to content

Fix uninitialized row buffer for non-interlaced reads - #994

Open
ChronNoc wants to merge 1 commit into
pnggroup:masterfrom
ChronNoc:fix-uninitialized-row-buf
Open

ChronNoc wants to merge 1 commit into
pnggroup:masterfrom
ChronNoc:fix-uninitialized-row-buf

Conversation

@ChronNoc

Copy link
Copy Markdown

Summary

png_read_start_row allocates big_row_buf with png_calloc only for
interlaced images, and with a plain png_malloc otherwise:

if (png_ptr->interlaced != 0)
   png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, row_bytes + 48);
else
   png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);

For a non-interlaced read, the row isn't always fully overwritten before
filter/transform code reads it, so the uninitialized tail can leak into
the decoded output path. This fixes it by always zero-initializing the
buffer (matching what the interlaced branch already did), rather than
special-casing it.

Found via

OSS-Fuzz issue 553267077
(libpng_readapi_fuzzer, MemorySanitizer use-of-uninitialized-value in
png_read_filter_row_paeth_1byte_pixel), now public past its 90-day
disclosure deadline.

While reproducing it locally I found the same uninitialized buffer is
also reachable from png_do_expand and png_do_expand_palette
depending on color type/bit depth — this one change fixes all three
call sites.

This looks like a long-standing issue rather than a recent regression
in the allocation itself. It was likely only newly reachable through
this specific fuzz target after 905e1f8 corrected how
libpng_readapi_fuzzer.cc registers PNG_TRANSFORM_* flags (it had
previously been calling png_set_scale_16/png_set_packing/
png_set_expand directly in combination with
png_read_png(..., PNG_TRANSFORM_IDENTITY, ...), which the libpng
manual says not to do).

Testing

Built libpng_readapi_fuzzer with MemorySanitizer via this project's
own contrib/oss-fuzz/build.sh and ran the standard PngSuite images
against it before and after this change:

Image Before After
basn0g01.png crash in png_do_expand clean
basn0g08.png crash in png_read_filter_row_paeth_1byte_pixel clean
basn3p01.png crash in png_do_expand_palette clean
basn3p02.png, basn3p04.png, basn3p08.png crash in png_do_expand_palette clean
basn0g02.png, basn0g04.png clean clean
ibasn0g08.png, ibasn3p08.png (interlaced) clean clean

All 10 inputs run with zero MemorySanitizer warnings after this change.

🤖 Generated with Claude Code

png_read_start_row allocated big_row_buf with png_calloc only for
interlaced images, and with plain png_malloc otherwise. For a
non-interlaced read, png_read_IDAT_data does not always account for
every byte of the padded row_bytes allocation, so the unfilled tail
could reach filter and transform code uninitialized.

Fixes an MSan use-of-uninitialized-value in
png_read_filter_row_paeth_1byte_pixel reported by OSS-Fuzz
(https://issues.oss-fuzz.com/issues/553267077, now public past its
90-day disclosure deadline). The same uninitialized buffer is also
reachable from png_do_expand and png_do_expand_palette depending on
color type; this change fixes all three call sites, confirmed with
the standard PngSuite images (basn0g01/08, basn3p01/02/04/08) built
against libpng_readapi_fuzzer under MemorySanitizer -- all previously
crashing inputs now run clean, while their interlaced counterparts
(which already used png_calloc) were unaffected either way.

This appears to be a long-standing issue rather than a recent
regression in big_row_buf's own allocation; it was likely only newly
reachable through this fuzz target after 905e1f8 corrected how
libpng_readapi_fuzzer.cc registers PNG_TRANSFORM_* flags.
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