Skip to content

Commit a026480

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix phpGH-17225: NULL deref in spl_directory.c
2 parents 62dc89d + 4bfe69b commit a026480

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ PHP NEWS
8888
- SPL:
8989
. Fixed bug GH-17198 (SplFixedArray assertion failure with get_object_vars).
9090
(nielsdos)
91+
. Fixed bug GH-17225 (NULL deref in spl_directory.c). (nielsdos)
9192

9293
- Streams:
9394
. Fixed bug GH-17037 (UAF in user filter when adding existing filter name due

ext/spl/spl_directory.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,16 @@ static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
203203
}
204204
/* }}} */
205205

206+
static inline bool spl_intern_is_glob(const spl_filesystem_object *intern)
207+
{
208+
/* NULL check on `dirp` is necessary as destructors may interfere. */
209+
return intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops);
210+
}
211+
206212
PHPAPI zend_string *spl_filesystem_object_get_path(const spl_filesystem_object *intern) /* {{{ */
207213
{
208214
#ifdef HAVE_GLOB
209-
if (intern->type == SPL_FS_DIR && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
215+
if (intern->type == SPL_FS_DIR && spl_intern_is_glob(intern)) {
210216
size_t len = 0;
211217
char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
212218
if (len == 0) {
@@ -636,7 +642,7 @@ static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *objec
636642
}
637643
if (intern->type == SPL_FS_DIR) {
638644
#ifdef HAVE_GLOB
639-
if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
645+
if (spl_intern_is_glob(intern)) {
640646
ZVAL_STR_COPY(&tmp, intern->path);
641647
} else {
642648
ZVAL_FALSE(&tmp);
@@ -1590,11 +1596,11 @@ PHP_METHOD(GlobIterator, count)
15901596
RETURN_THROWS();
15911597
}
15921598

1593-
if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
1599+
if (spl_intern_is_glob(intern)) {
15941600
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
15951601
} else {
1596-
/* should not happen */
1597-
// TODO ZEND_ASSERT ?
1602+
/* This can happen by abusing destructors. */
1603+
/* TODO: relax this from E_ERROR to an exception */
15981604
php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
15991605
}
16001606
}

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)