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
20 changes: 20 additions & 0 deletions ext/standard/tests/streams/gh25815.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
GH-25815 (heap-use-after-free when a userspace stream_stat() closes the stream)
--CREDITS--
snowjeon2
--FILE--
<?php
class W {
public $context;
function stream_open($path, $mode, $options, &$opened_path) { return true; }
function stream_stat() { global $s; fclose($s); return []; }
function stream_read($count) { return ''; }
function stream_eof() { return true; }
}
stream_wrapper_register("bomb", "W");
$s = fopen("bomb://", "r");
var_dump(stream_get_contents($s));
?>
--EXPECTF--
Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
string(0) ""
6 changes: 6 additions & 0 deletions main/streams/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,16 @@ static int php_userstreamop_stat(php_stream *stream, php_stream_statbuf *ssb)
php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
int ret = -1;

uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;

zend_string *func_name = ZSTR_INIT_LITERAL(USERSTREAM_STAT, false);
zend_result call_result = zend_call_method_if_exists(Z_OBJ(us->object), func_name, &retval, 0, NULL);
zend_string_release_ex(func_name, false);

stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= orig_no_fclose;

if (UNEXPECTED(call_result == FAILURE)) {
php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_STAT " is not implemented!",
ZSTR_VAL(us->wrapper->ce->name));
Expand Down
Loading