Skip to content

Commit 9047524

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix GH-20678: resource created by GlobIterator crashes with fclose().
2 parents e82afcf + 97a90f4 commit 9047524

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ PHP NEWS
1616
- LDAP:
1717
. Fix memory leak in ldap_set_options(). (ndossche)
1818

19+
- SPL:
20+
. Fixed bug GH-20678 (resource created by GlobIterator crashes with fclose()).
21+
(David Carlier)
22+
1923
- Standard:
2024
. Fix error check for proc_open() command. (ndossche)
2125

ext/spl/spl_directory.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string *
297297
intern->type = SPL_FS_DIR;
298298
intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context));
299299

300+
if (intern->u.dir.dirp) {
301+
/* we prevent potential UAF with conflicting explicit fclose(), relying on the object destructor for this */
302+
intern->u.dir.dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
303+
}
304+
300305
if (ZSTR_LEN(path) > 1 && IS_SLASH_AT(ZSTR_VAL(path), ZSTR_LEN(path)-1)) {
301306
intern->path = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path)-1, 0);
302307
} else {

ext/spl/tests/gh20678.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-20678 (resource created by GlobalIterator crashes when it is called with fclose())
3+
--CREDITS--
4+
chongwick
5+
--FILE--
6+
<?php
7+
$iter = new GlobIterator(__DIR__ . '/*.abcdefghij');
8+
$resources = get_resources();
9+
$resource = end($resources);
10+
fclose($resource);
11+
?>
12+
--EXPECTF--
13+
14+
Warning: fclose(): %d is not a valid stream resource in %s on line %d

0 commit comments

Comments
 (0)