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/gh25817.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
GH-25817 (Use-after-free when a userspace stream_truncate() closes the stream)
--CREDITS--
snowjeon2
--FILE--
<?php
class W {
public $context;
function stream_open($path, $mode, $options, &$opened_path) { return true; }
function stream_truncate(int $new_size) { global $s; fclose($s); return 1; }
}
stream_wrapper_register("bomb", "W");
$s = fopen("bomb://", "w");
var_dump(ftruncate($s, 0));
?>
--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)
13 changes: 11 additions & 2 deletions main/streams/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading