Skip to content

Fix GH-25903: bzip2.decompress write filter writes past its input buffer after a data error - #25904

Closed
ArtUkrainskiy wants to merge 1 commit into
php:PHP-8.4from
ArtUkrainskiy:bz2-decompress-error-reset
Closed

ArtUkrainskiy wants to merge 1 commit into
php:PHP-8.4from
ArtUkrainskiy:bz2-decompress-error-reset

Conversation

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

Fixes GH-25903

When BZ2_bzDecompress() fails, php_bz2_decompress_filter() returns before it resets next_in and avail_in. bzlib has already moved next_in forward. The filter stays on the stream, so the next write copies its input at the advanced next_in, and the copy can go past the end of the 2048-byte inbuf.

This resets both before returning, like zlib.inflate does. The notice now goes last, because an error handler can close the stream and free the filter. zlib.inflate got the same order in #24176.

The test writes one byte at a time, so the position moves past the buffer after about 2048 writes. It needs USE_ZEND_ALLOC=0. With the Zend allocator the overflow doesn't crash. Without the fix it aborts in glibc on a debug build and fails with ASan. ext/bz2 tests pass with ASan and ZTS. The code change applies cleanly to 8.5, 8.6 and master.

…buffer after a data error

On a data error the filter returned before it reset next_in and avail_in.
The filter stays on the stream, so the next write copied its input at the
advanced next_in, and the copy could go past the end of the 2048-byte
input buffer.

Reset both before returning, as zlib.inflate does, and raise the notice
last, because an error handler can close the stream and free the filter.
@ArtUkrainskiy
ArtUkrainskiy marked this pull request as ready for review November 29, 2026 20:34

@jorgsowa jorgsowa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks.

@iliaal iliaal closed this in 6ed1187 Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants