Skip to content

Fix GH-17225: NULL deref in spl_directory.c #17231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,16 @@ static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
}
/* }}} */

static inline bool spl_intern_is_glob(const spl_filesystem_object *intern)
{
/* NULL check on `dirp` is necessary as destructors may interfere. */
return intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops);
return intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, I didn't reformat the code when I moved this

}

PHPAPI zend_string *spl_filesystem_object_get_path(spl_filesystem_object *intern) /* {{{ */
{
#ifdef HAVE_GLOB
if (intern->type == SPL_FS_DIR && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
if (intern->type == SPL_FS_DIR && spl_intern_is_glob(intern)) {
size_t len = 0;
char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
if (len == 0) {
Expand Down Expand Up @@ -658,7 +664,7 @@ static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *objec
if (intern->type == SPL_FS_DIR) {
#ifdef HAVE_GLOB
pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1);
if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
if (spl_intern_is_glob(intern)) {
ZVAL_STR_COPY(&tmp, intern->path);
} else {
ZVAL_FALSE(&tmp);
Expand Down Expand Up @@ -1614,11 +1620,11 @@ PHP_METHOD(GlobIterator, count)
RETURN_THROWS();
}

if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
if (spl_intern_is_glob(intern)) {
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
} else {
/* should not happen */
// TODO ZEND_ASSERT ?
/* This can happen by abusing destructors. */
/* TODO: relax this from E_ERROR to an exception */
php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
}
}
Expand Down
52 changes: 52 additions & 0 deletions ext/spl/tests/gh17225.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
GH-17225 (NULL deref in spl_directory.c)
--CREDITS--
YuanchengJiang
--EXTENSIONS--
phar
--INI--
phar.readonly=0
--FILE--
<?php
$fname = __DIR__ . '/gh17225.phar.zip';
$phar = new Phar($fname);
class HasDestructor {
public function __destruct() {
var_dump($GLOBALS['s']);
}
}
$s = new SplObjectStorage();
$s[$phar] = new HasDestructor();
register_shutdown_function(function() {
global $s;
});
var_dump($phar->isLink());
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/gh17225.phar.zip');
?>
--EXPECTF--
bool(false)
object(SplObjectStorage)#%d (1) {
["storage":"SplObjectStorage":private]=>
array(1) {
[0]=>
array(2) {
["obj"]=>
object(Phar)#%d (4) {
["pathName":"SplFileInfo":private]=>
string(0) ""
["fileName":"SplFileInfo":private]=>
string(0) ""
["glob":"DirectoryIterator":private]=>
bool(false)
["subPathName":"RecursiveDirectoryIterator":private]=>
string(0) ""
}
["inf"]=>
object(HasDestructor)#%d (0) {
}
}
}
}
Loading