Skip to content

Fix GH-13833: Applying zero offset to null pointer in zend_hash.c #13847

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
3 changes: 3 additions & 0 deletions ext/phar/phar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,9 @@ static int phar_parse_pharfile(php_stream *fp, char *fname, size_t fname_len, ch

mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
mydata->is_persistent = PHAR_G(persist);
HT_INVALIDATE(&mydata->manifest);
HT_INVALIDATE(&mydata->mounted_dirs);
HT_INVALIDATE(&mydata->virtual_dirs);

/* check whether we have meta data, zero check works regardless of byte order */
SAFE_PHAR_GET_32(buffer, endbuffer, len);
Expand Down
52 changes: 32 additions & 20 deletions ext/phar/tests/gh13833.phpt
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
--TEST--
GH-13836 (Renaming a file in a Phar to an already existing filename causes a NULL pointer dereference)
--EXTENSIONS--
phar
GH-13833 (Applying zero offset to null pointer in zend_hash.c)
--INI--
phar.require_hash=0
phar.readonly=0
--FILE--
<?php
$fname = __DIR__ . '/gh13836.phar';
$fname = __DIR__ . '/gh13833.phar';
$pname = 'phar://' . $fname;
$file = "<?php
Phar::mapPhar('hio');
__HALT_COMPILER(); ?>";
$files = array();
$files['a'] = 'a';
include 'files/phar_test.inc';
include $fname;

$phar = new Phar($fname, 0, 'a.phar');
$phar['x'] = 'hi1';
$phar['y'] = 'hi2';
$file = "<?php __HALT_COMPILER(); ?>";
$files['a'] = array('cont' => 'a');
include 'files/phar_test.inc';

var_dump(rename("phar://a.phar/x", "phar://a.phar/y"));

var_dump(isset($phar['x']));
var_dump($phar['y']);
$phar = new Phar($fname);
$phar->setMetadata((object) ['my' => 'friend']);
// NOTE: Phar will use the cached value of metadata if setMetaData was called on that Phar path before.
// Save the writes to the phar and use a different file path.
$fname_new = "$fname.copy.phar";
copy($fname, $fname_new);
try {
new Phar($fname_new);
} catch (UnexpectedValueException $e) {
echo $e->getMessage(), "\n";
}
?>
--EXTENSIONS--
phar
--CLEAN--
<?php
unlink(__DIR__ . '/gh13836.phar');
unlink(__DIR__ . '/gh13833.phar');
unlink(__DIR__ . '/gh13833.phar.copy.phar');
?>
--CREDITS--
Yuancheng Jiang <yuancheng@comp.nus.edu.sg>
Felix De Vliegher <felix.devliegher@gmail.com>
--EXPECTF--
bool(true)
bool(false)
object(PharFileInfo)#2 (2) {
["pathName":"SplFileInfo":private]=>
string(%d) "phar://%sgh13836.phar/y"
["fileName":"SplFileInfo":private]=>
string(1) "y"
}
internal corruption of phar "%sgh13833.phar.copy.phar" (trying to read past buffer end)
33 changes: 33 additions & 0 deletions ext/phar/tests/gh13836.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
GH-13836 (Renaming a file in a Phar to an already existing filename causes a NULL pointer dereference)
--EXTENSIONS--
phar
--INI--
phar.require_hash=0
phar.readonly=0
--FILE--
<?php
$fname = __DIR__ . '/gh13836.phar';

$phar = new Phar($fname, 0, 'a.phar');
$phar['x'] = 'hi1';
$phar['y'] = 'hi2';

var_dump(rename("phar://a.phar/x", "phar://a.phar/y"));

var_dump(isset($phar['x']));
var_dump($phar['y']);
?>
--CLEAN--
<?php
unlink(__DIR__ . '/gh13836.phar');
?>
--EXPECTF--
bool(true)
bool(false)
object(PharFileInfo)#2 (2) {
["pathName":"SplFileInfo":private]=>
string(%d) "phar://%sgh13836.phar/y"
["fileName":"SplFileInfo":private]=>
string(1) "y"
}