Repository navigation
Conversation
Calling png_process_data_pause from a row callback during IDAT processing caused size_t underflow in buffer_size and current_buffer_size. In the current_buffer processing block of png_push_read_IDAT, the local save_size captured the pre-pause current_buffer_size; png_push_save_buffer (save=1) or the pause itself (save=0) then zeroed both counters. Subtracting the stale save_size from the already-zeroed fields wrapped size_t to its maximum value, corrupting the buffer state. With non-fatal CRC handling (PNG_CRC_QUIET_USE), the corrupted buffer_size caused the outer loop in png_process_data to continue processing past the caller's buffer boundary, resulting in an out-of-bounds heap read. The fix checks whether buffer_size is still nonzero after png_process_IDAT_data returns. A zero buffer_size reliably indicates that a pause occurred (buffer_size cannot reach zero through natural exhaustion because the block entry guard ensures it is positive at entry). Both the save_buffer and current_buffer processing blocks are guarded. The same fix pattern is applied in both blocks for consistency. Reported-by: Alejandro Hurtado <amhurtado@pm.me>
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
Calling
png_process_data_pausefrom a row callback during progressiveIDAT processing causes
size_tunderflow inbuffer_sizeandcurrent_buffer_sizeinpng_push_read_IDAT.In the current_buffer processing block, the local
save_sizecapturescurrent_buffer_sizebeforepng_process_IDAT_datais called. If therow callback calls
png_process_data_pause(save=1), this internallycalls
png_push_save_buffer, which zeroes bothbuffer_sizeandcurrent_buffer_size. After return, the stalesave_sizeis subtractedfrom the already-zeroed fields, wrapping them to
SIZE_MAX.With non-fatal CRC handling (
PNG_CRC_QUIET_USE), the corruptedbuffer_sizecauses the outer loop inpng_process_datato continuepast the caller's buffer, leading to out-of-bounds heap reads. With
default CRC handling, the corrupted state triggers a CRC mismatch that
calls
png_error, which limits the impact to an unexpected fatal error.The same underflow affects the save_buffer processing block
(
buffer_size -= save_size) and thesave=0pause mode (which zeroesonly
buffer_size).Affected versions: libpng 1.5.0 through current libpng18, since
png_process_data_pausewas introduced.Fix
After
png_process_IDAT_datareturns, check whetherbuffer_sizeisstill nonzero before subtracting
save_size. A zerobuffer_sizereliably indicates that a pause occurred, because the block entry guard
(
png_ptr->current_buffer_size != 0) ensures it was positive at entryand cannot reach zero through natural IDAT exhaustion within the block.
Both the save_buffer and current_buffer blocks are guarded for
consistency.
Test
A new regression test (
pngpause) exercises bothsave=0andsave=1pause modes during progressive IDAT processing, feeding a synthetic PNG
in two chunks to trigger the vulnerable code path. The test passes on
clean completion of all image rows without errors; it fails on a crash
or a report from a memory sanitizer.
AddressSanitizer result before fix:
After fix: clean pass, 0 ASAN findings, 40/40 tests pass.
Reported-by: Alejandro Hurtado amhurtado@pm.me