Skip to content

Fix OSS-Fuzz #385993744 #18972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: PHP-8.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions ext/standard/tests/filters/oss_fuzz_385993744.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
OSS-Fuzz #385993744
--FILE--
<?php

class SampleFilter extends php_user_filter
{
private $data = '';

public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in))
{
$this->data .= $bucket->data;
}

$bucket = stream_bucket_new($this->stream, $this->data);
stream_bucket_append($out, $bucket);

return PSFS_FEED_ME;
}
}
stream_filter_register('sample.filter', SampleFilter::class);
var_dump(file_get_contents('php://filter/read=sample.filter/resource='. __FILE__));

?>
--EXPECT--
string(0) ""
7 changes: 7 additions & 0 deletions main/streams/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ PHPAPI int php_stream_filter_append_ex(php_stream_filter_chain *chain, php_strea
Reset stream's internal read buffer since the filter is "holding" it. */
stream->readpos = 0;
stream->writepos = 0;

/* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */
while (brig_out.head) {
bucket = brig_out.head;
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
}
break;
case PSFS_PASS_ON:
/* If any data is consumed, we cannot rely upon the existing read buffer,
Expand Down
22 changes: 18 additions & 4 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,12 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size)
/* when a filter needs feeding, there is no brig_out to deal with.
* we simply continue the loop; if the caller needs more data,
* we will read again, otherwise out job is done here */

/* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */
while ((bucket = brig_outp->head)) {
Copy link
Member

Choose a reason for hiding this comment

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

just little nit: is the style difference voluntary (i.e. initialising the var in while) ?

php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
}
break;

case PSFS_ERR_FATAL:
Expand Down Expand Up @@ -1263,14 +1269,22 @@ static ssize_t _php_stream_write_filtered(php_stream *stream, const char *buf, s
php_stream_bucket_delref(bucket);
}
break;
case PSFS_FEED_ME:
/* need more data before we can push data through to the stream */
break;

case PSFS_ERR_FATAL:
/* some fatal error. Theoretically, the stream is borked, so all
* further writes should fail. */
return (ssize_t) -1;
consumed = (ssize_t) -1;
ZEND_FALLTHROUGH;

case PSFS_FEED_ME:
/* need more data before we can push data through to the stream */
/* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */
while (brig_inp->head) {
bucket = brig_inp->head;
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
}
break;
}

return consumed;
Expand Down
Loading