Skip to content

Commit 4bfe69b

Browse files
committed
Fix GH-17225: NULL deref in spl_directory.c
NULL checks for the glob stream are inconsistently applied. To solve this generally, factor it out to a helper function so it's less likely to be forgotten in the future. Closes GH-17231.
1 parent 61615d5 commit 4bfe69b

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ PHP NEWS
7272
on SO_SNDTIMEO/SO_RCVTIMEO for socket_set_option().
7373
(David Carlier)
7474

75+
- SPL:
76+
. Fixed bug GH-17225 (NULL deref in spl_directory.c). (nielsdos)
77+
7578
- Streams:
7679
. Fixed bug GH-17037 (UAF in user filter when adding existing filter name due
7780
to incorrect error handling). (nielsdos)

ext/spl/spl_directory.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,16 @@ static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
212212
}
213213
/* }}} */
214214

215+
static inline bool spl_intern_is_glob(const spl_filesystem_object *intern)
216+
{
217+
/* NULL check on `dirp` is necessary as destructors may interfere. */
218+
return intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops);
219+
}
220+
215221
PHPAPI zend_string *spl_filesystem_object_get_path(spl_filesystem_object *intern) /* {{{ */
216222
{
217223
#ifdef HAVE_GLOB
218-
if (intern->type == SPL_FS_DIR && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
224+
if (intern->type == SPL_FS_DIR && spl_intern_is_glob(intern)) {
219225
size_t len = 0;
220226
char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
221227
if (len == 0) {
@@ -658,7 +664,7 @@ static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *objec
658664
if (intern->type == SPL_FS_DIR) {
659665
#ifdef HAVE_GLOB
660666
pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1);
661-
if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
667+
if (spl_intern_is_glob(intern)) {
662668
ZVAL_STR_COPY(&tmp, intern->path);
663669
} else {
664670
ZVAL_FALSE(&tmp);
@@ -1614,11 +1620,11 @@ PHP_METHOD(GlobIterator, count)
16141620
RETURN_THROWS();
16151621
}
16161622

1617-
if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
1623+
if (spl_intern_is_glob(intern)) {
16181624
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
16191625
} else {
1620-
/* should not happen */
1621-
// TODO ZEND_ASSERT ?
1626+
/* This can happen by abusing destructors. */
1627+
/* TODO: relax this from E_ERROR to an exception */
16221628
php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
16231629
}
16241630
}

ext/spl/tests/gh17225.phpt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
GH-17225 (NULL deref in spl_directory.c)
3+
--CREDITS--
4+
YuanchengJiang
5+
--EXTENSIONS--
6+
phar
7+
--INI--
8+
phar.readonly=0
9+
--FILE--
10+
<?php
11+
$fname = __DIR__ . '/gh17225.phar.zip';
12+
$phar = new Phar($fname);
13+
class HasDestructor {
14+
public function __destruct() {
15+
var_dump($GLOBALS['s']);
16+
}
17+
}
18+
$s = new SplObjectStorage();
19+
$s[$phar] = new HasDestructor();
20+
register_shutdown_function(function() {
21+
global $s;
22+
});
23+
var_dump($phar->isLink());
24+
?>
25+
--CLEAN--
26+
<?php
27+
@unlink(__DIR__ . '/gh17225.phar.zip');
28+
?>
29+
--EXPECTF--
30+
bool(false)
31+
object(SplObjectStorage)#%d (1) {
32+
["storage":"SplObjectStorage":private]=>
33+
array(1) {
34+
[0]=>
35+
array(2) {
36+
["obj"]=>
37+
object(Phar)#%d (4) {
38+
["pathName":"SplFileInfo":private]=>
39+
string(0) ""
40+
["fileName":"SplFileInfo":private]=>
41+
string(0) ""
42+
["glob":"DirectoryIterator":private]=>
43+
bool(false)
44+
["subPathName":"RecursiveDirectoryIterator":private]=>
45+
string(0) ""
46+
}
47+
["inf"]=>
48+
object(HasDestructor)#%d (0) {
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)