Skip to content

Commit 6297a11

Browse files
committed
Fixed bug #71323 - Output of stream_get_meta_data can be falsified by its input
1 parent 1c1b8b6 commit 6297a11

15 files changed

+260
-227
lines changed

ext/standard/streamsfuncs.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,12 @@ PHP_FUNCTION(stream_get_meta_data)
496496

497497
array_init(return_value);
498498

499+
if (!php_stream_populate_meta_data(stream, return_value)) {
500+
add_assoc_bool(return_value, "timed_out", 0);
501+
add_assoc_bool(return_value, "blocked", 1);
502+
add_assoc_bool(return_value, "eof", php_stream_eof(stream));
503+
}
504+
499505
if (stream->wrapperdata) {
500506
MAKE_STD_ZVAL(newval);
501507
MAKE_COPY_ZVAL(&stream->wrapperdata, newval);
@@ -531,12 +537,6 @@ PHP_FUNCTION(stream_get_meta_data)
531537
add_assoc_string(return_value, "uri", stream->orig_path, 1);
532538
}
533539

534-
if (!php_stream_populate_meta_data(stream, return_value)) {
535-
add_assoc_bool(return_value, "timed_out", 0);
536-
add_assoc_bool(return_value, "blocked", 1);
537-
add_assoc_bool(return_value, "eof", php_stream_eof(stream));
538-
}
539-
540540
}
541541
/* }}} */
542542

@@ -696,7 +696,7 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
696696
} else { /* HASH_KEY_IS_STRING */
697697
zend_hash_update(new_hash, key, key_len, (void *)elem, sizeof(zval *), (void **)&dest_elem);
698698
}
699-
699+
700700
if (dest_elem) {
701701
zval_add_ref(dest_elem);
702702
}
@@ -1453,19 +1453,19 @@ PHP_FUNCTION(stream_set_chunk_size)
14531453
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The chunk size must be a positive integer, given %ld", csize);
14541454
RETURN_FALSE;
14551455
}
1456-
/* stream.chunk_size is actually a size_t, but php_stream_set_option
1456+
/* stream.chunk_size is actually a size_t, but php_stream_set_option
14571457
* can only use an int to accept the new value and return the old one.
14581458
* In any case, values larger than INT_MAX for a chunk size make no sense.
14591459
*/
14601460
if (csize > INT_MAX) {
14611461
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The chunk size cannot be larger than %d", INT_MAX);
14621462
RETURN_FALSE;
14631463
}
1464-
1464+
14651465
php_stream_from_zval(stream, &zstream);
14661466

14671467
ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL);
1468-
1468+
14691469
RETURN_LONG(ret > 0 ? (long)ret : (long)EOF);
14701470
}
14711471
/* }}} */
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Bug #71323: Output of stream_get_meta_data can be falsified by its input
3+
--FILE--
4+
<?php
5+
$file = 'data:text/plain;z=y;uri=eviluri;mediatype=wut?;mediatype2=hello,somedata';
6+
$meta = stream_get_meta_data(fopen($file, "r"));
7+
var_dump($meta);
8+
?>
9+
--EXPECTF--
10+
array(10) {
11+
["mediatype"]=>
12+
string(10) "text/plain"
13+
["z"]=>
14+
string(1) "y"
15+
["uri"]=>
16+
string(72) "data:text/plain;z=y;uri=eviluri;mediatype=wut?;mediatype2=hello,somedata"
17+
["mediatype2"]=>
18+
string(5) "hello"
19+
["base64"]=>
20+
bool(false)
21+
["wrapper_type"]=>
22+
string(7) "RFC2397"
23+
["stream_type"]=>
24+
string(7) "RFC2397"
25+
["mode"]=>
26+
string(1) "r"
27+
["unread_bytes"]=>
28+
int(0)
29+
["seekable"]=>
30+
bool(true)
31+
}

ext/standard/tests/streams/stream_get_meta_data_dir_basic.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ var_dump(stream_get_meta_data($dirObject->handle));
1313
?>
1414
--EXPECT--
1515
array(8) {
16+
["timed_out"]=>
17+
bool(false)
18+
["blocked"]=>
19+
bool(true)
20+
["eof"]=>
21+
bool(false)
1622
["wrapper_type"]=>
1723
string(9) "plainfile"
1824
["stream_type"]=>
@@ -23,14 +29,14 @@ array(8) {
2329
int(0)
2430
["seekable"]=>
2531
bool(true)
32+
}
33+
array(8) {
2634
["timed_out"]=>
2735
bool(false)
2836
["blocked"]=>
2937
bool(true)
3038
["eof"]=>
3139
bool(false)
32-
}
33-
array(8) {
3440
["wrapper_type"]=>
3541
string(9) "plainfile"
3642
["stream_type"]=>
@@ -41,10 +47,4 @@ array(8) {
4147
int(0)
4248
["seekable"]=>
4349
bool(true)
44-
["timed_out"]=>
45-
bool(false)
46-
["blocked"]=>
47-
bool(true)
48-
["eof"]=>
49-
bool(false)
5050
}

ext/standard/tests/streams/stream_get_meta_data_file_basic.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ fclose($fp);
1212
?>
1313
--EXPECTF--
1414
array(9) {
15+
["timed_out"]=>
16+
bool(false)
17+
["blocked"]=>
18+
bool(true)
19+
["eof"]=>
20+
bool(false)
1521
["wrapper_type"]=>
1622
string(9) "plainfile"
1723
["stream_type"]=>
@@ -24,10 +30,4 @@ array(9) {
2430
bool(true)
2531
["uri"]=>
2632
string(%i) "%sstream_get_meta_data_file_basic.php"
27-
["timed_out"]=>
28-
bool(false)
29-
["blocked"]=>
30-
bool(true)
31-
["eof"]=>
32-
bool(false)
3333
}

0 commit comments

Comments
 (0)