Skip to content

Commit feaa8a1

Browse files
committed
Merge branch 'PHP-8.5' into PHP-8.6
* PHP-8.5: streams: UAF in php_stream_cast() when stream_flush() closes the stream # Conflicts: # ext/standard/tests/streams/gh14506.phpt # main/streams/userspace.c
2 parents a46fadc + 91452ab commit feaa8a1

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎ext/standard/tests/streams/gh14506.phpt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@ Warning: stream_select(): Cannot represent a stream of type user-space as a sele
9292

9393
Warning: stream_select(): Cannot represent a stream of type user-space as a select()able descriptor in %s on line %d
9494
ValueError: No stream arrays were passed
95-
TypeError: fclose(): Argument #1 ($stream) must be an open stream resource
95+
96+
Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-25586 (heap-use-after-free in php_stream_cast() when a userspace stream_flush() closes the stream)
3+
--CREDITS--
4+
YuanchengJiang
5+
--FILE--
6+
<?php
7+
class W {
8+
public $context;
9+
function stream_open($path, $mode, $options, &$opened_path) { return true; }
10+
function stream_flush() { global $s; fclose($s); return true; }
11+
}
12+
stream_wrapper_register("bomb", "W");
13+
$s = fopen("bomb://", "w");
14+
var_dump(proc_open("true", [1 => $s], $pipes));
15+
?>
16+
--EXPECTF--
17+
Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
18+
19+
Warning: proc_open(): W::stream_cast is not implemented! in %s on line %d
20+
21+
Warning: proc_open(): Cannot represent a stream of type user-space as a File Descriptor in %s on line %d
22+
bool(false)

‎main/streams/userspace.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,10 +737,16 @@ static int php_userstreamop_flush(php_stream *stream)
737737

738738
assert(us != NULL);
739739

740+
uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
741+
stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
742+
740743
zend_string *func_name = ZSTR_INIT_LITERAL(USERSTREAM_FLUSH, false);
741744
zend_result call_result = zend_call_method_if_exists(Z_OBJ(us->object), func_name, &retval, 0, NULL);
742745
zend_string_release_ex(func_name, false);
743746

747+
stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE;
748+
stream->flags |= orig_no_fclose;
749+
744750
int ret = call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF && zend_is_true(&retval) ? 0 : -1;
745751

746752
zval_ptr_dtor(&retval);

0 commit comments

Comments
 (0)