diff --git a/ext/standard/tests/streams/gh25815.phpt b/ext/standard/tests/streams/gh25815.phpt new file mode 100644 index 000000000000..4cacd0e4b6f9 --- /dev/null +++ b/ext/standard/tests/streams/gh25815.phpt @@ -0,0 +1,20 @@ +--TEST-- +GH-25815 (heap-use-after-free when a userspace stream_stat() closes the stream) +--CREDITS-- +snowjeon2 +--FILE-- + +--EXPECTF-- +Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d +string(0) "" diff --git a/main/streams/userspace.c b/main/streams/userspace.c index 2bf858060a4c..63c984ba9901 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -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));