Skip to content

Commit 97a90f4

Browse files
committed
Fix GH-20678: resource created by GlobIterator crashes with fclose().
close GH-20697
1 parent b37a6e7 commit 97a90f4

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
@@ -306,6 +306,11 @@ static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string *
306306
intern->type = SPL_FS_DIR;
307307
intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context));
308308

309+
if (intern->u.dir.dirp) {
310+
/* we prevent potential UAF with conflicting explicit fclose(), relying on the object destructor for this */
311+
intern->u.dir.dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
312+
}
313+
309314
if (ZSTR_LEN(path) > 1 && IS_SLASH_AT(ZSTR_VAL(path), ZSTR_LEN(path)-1)) {
310315
intern->path = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path)-1, 0);
311316
} 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)