Skip to content
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
5 changes: 5 additions & 0 deletions ext/standard/streamsfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,12 @@ static void apply_filter_to_stream(bool append, INTERNAL_FUNCTION_PARAMETERS)
}

if (read_write & PHP_STREAM_FILTER_WRITE) {
bool originally_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;

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.

The read chain has the same problem, stream_filter_append($s, 'convert.base64-encode', STREAM_FILTER_READ, ['write_seek_mode' => $obj]) still frees the stream, so the guard needs to wrap both branches.

stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
if (!originally_no_fclose) {
stream->flags ^= PHP_STREAM_FLAG_NO_FCLOSE;
}
if (filter == NULL) {
RETURN_FALSE;
}
Expand Down
33 changes: 33 additions & 0 deletions ext/standard/tests/streams/write_seek_mode-close-uaf.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
`write_seek_mode` with a stringable object closing the stream triggers UAF
--EXTENSIONS--
zlib
--CREDITS--
Calif.io
--FILE--
<?php

$stream = fopen('php://temp', 'w+');

final class CloseStreamForWriteSeekMode
{
public function __toString(): string
{
fclose($GLOBALS['stream']);
return 'reset';
}
}

$filter = stream_filter_append(
$stream,
'zlib.deflate',
STREAM_FILTER_WRITE,
['write_seek_mode' => new CloseStreamForWriteSeekMode()],
);

var_dump(is_resource($filter), is_resource($stream));
?>
--EXPECTF--
Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
bool(true)
bool(true)
43 changes: 43 additions & 0 deletions ext/standard/tests/streams/write_seek_mode-reuse.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
`write_seek_mode` with a stringable object closing and reopening the stream triggers reuse
--EXTENSIONS--
zlib
--CREDITS--
Calif.io
--FILE--
<?php

$stream = fopen('php://temp', 'w+');
$replacementStream = null;

final class ReplaceStreamForWriteSeekMode
{
public function __toString(): string
{
fclose($GLOBALS['stream']);
$GLOBALS['replacementStream'] = fopen('php://temp', 'w+');
return 'reset';
}
}

$filter = stream_filter_append(
$stream,
'zlib.deflate',
STREAM_FILTER_WRITE,
['write_seek_mode' => new ReplaceStreamForWriteSeekMode()],
);

var_dump(is_resource($filter), is_resource($replacementStream));
fwrite($replacementStream, str_repeat('A', 64));
var_dump(stream_filter_remove($filter));
rewind($replacementStream);
$raw = stream_get_contents($replacementStream);
var_dump(strlen($raw), bin2hex($raw));
?>
--EXPECTF--
Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
bool(true)
bool(true)
bool(true)
int(64)
string(128) "41414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141"
Loading