Skip to content

fix: Guard against size_t underflow when pausing progressive IDAT read - #995

Open
jortles wants to merge 1 commit into
pnggroup:libpng18from
jortles:fix-pause-save-underflow
Open

jortles wants to merge 1 commit into
pnggroup:libpng18from
jortles:fix-pause-save-underflow

Conversation

@jortles

@jortles jortles commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Calling png_process_data_pause from a row callback during progressive
IDAT processing causes size_t underflow in buffer_size and
current_buffer_size in png_push_read_IDAT.

In the current_buffer processing block, the local save_size captures
current_buffer_size before png_process_IDAT_data is called. If the
row callback calls png_process_data_pause(save=1), this internally
calls png_push_save_buffer, which zeroes both buffer_size and
current_buffer_size. After return, the stale save_size is subtracted
from the already-zeroed fields, wrapping them to SIZE_MAX.

With non-fatal CRC handling (PNG_CRC_QUIET_USE), the corrupted
buffer_size causes the outer loop in png_process_data to continue
past 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 the save=0 pause mode (which zeroes
only buffer_size).

Affected versions: libpng 1.5.0 through current libpng18, since
png_process_data_pause was introduced.

Fix

After png_process_IDAT_data returns, check whether buffer_size is
still nonzero before subtracting save_size. A zero buffer_size
reliably indicates that a pause occurred, because the block entry guard
(png_ptr->current_buffer_size != 0) ensures it was positive at entry
and 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 both save=0 and save=1
pause 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:

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7ffff48f6800
READ of size 1024 at 0x7ffff48f6800 thread T0
    #0 memcpy
    #1 png_push_fill_buffer pngpread.c:456
    #2 png_read_data pngrio.c:35
    #3 png_crc_read pngrutil.c:231
    #4 png_crc_finish_critical pngrutil.c:328
    #5 png_crc_finish pngrutil.c:362
    #6 png_handle_unknown pngrutil.c:3038
    #7 png_handle_chunk pngrutil.c:3286
    #8 png_push_read_chunk pngpread.c:414
    #9 png_process_some_data pngpread.c:123
    #10 png_process_data pngpread.c:60

After fix: clean pass, 0 ASAN findings, 40/40 tests pass.

Reported-by: Alejandro Hurtado amhurtado@pm.me

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>
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