diff --git a/ext/standard/tests/streams/gh25817.phpt b/ext/standard/tests/streams/gh25817.phpt new file mode 100644 index 000000000000..2dd4eae7fd04 --- /dev/null +++ b/ext/standard/tests/streams/gh25817.phpt @@ -0,0 +1,20 @@ +--TEST-- +GH-25817 (Use-after-free when a userspace stream_truncate() 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 + +Warning: ftruncate(): W::stream_truncate value must be of type bool, int given in %s on line %d +bool(false) diff --git a/main/streams/userspace.c b/main/streams/userspace.c index 2bf858060a4c..3739f84377c0 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -1054,8 +1054,17 @@ static int php_userstreamop_set_option(php_stream *stream, int option, int value case PHP_STREAM_OPTION_LOCKING: return user_stream_set_locking(us, value); - case PHP_STREAM_OPTION_TRUNCATE_API: - return user_stream_set_truncation(us, value, ptrparam); + case PHP_STREAM_OPTION_TRUNCATE_API: { + uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE; + stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE; + + int ret = user_stream_set_truncation(us, value, ptrparam); + + stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE; + stream->flags |= orig_no_fclose; + + return ret; + } case PHP_STREAM_OPTION_READ_BUFFER: case PHP_STREAM_OPTION_WRITE_BUFFER: