Skip to content

Commit 1edcfcc

Browse files
committed
Fix #77432: Segmentation fault on including phar file
phar_get_pharfp() can return NULL. In this case this is because the stream gets closed by the include code in the engine. However, the phar entry is still cached, so when the next include happens the engine tries to read from a closed (and nullified) stream. Use the same fix as in phar_open_entry_fp(): take into account that the phar_get_pharfp() can return NULL and in that case reopen the phar archive. Closes GH-13056.
1 parent 77ac1e8 commit 1edcfcc

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ PHP NEWS
5151
. Fixed bug GH-12974 (Apache crashes on shutdown when using pg_pconnect()).
5252
(nielsdos)
5353

54+
- Phar:
55+
. Fixed bug #77432 (Segmentation fault on including phar file). (nielsdos)
56+
5457
- PHPDBG:
5558
. Fixed bug GH-12962 (Double free of init_file in phpdbg_prompt.c). (nielsdos)
5659

ext/phar/stream.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
254254
php_url_free(resource);
255255
goto phar_stub;
256256
} else {
257+
php_stream *stream = phar_get_pharfp(phar);
258+
if (stream == NULL) {
259+
if (UNEXPECTED(FAILURE == phar_open_archive_fp(phar))) {
260+
php_stream_wrapper_log_error(wrapper, options, "phar error: could not reopen phar \"%s\"", ZSTR_VAL(resource->host));
261+
efree(internal_file);
262+
php_url_free(resource);
263+
return NULL;
264+
}
265+
stream = phar_get_pharfp(phar);
266+
}
267+
257268
phar_entry_info *entry;
258269

259270
entry = (phar_entry_info *) ecalloc(1, sizeof(phar_entry_info));
@@ -266,7 +277,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
266277
entry->is_crc_checked = 1;
267278

268279
idata = (phar_entry_data *) ecalloc(1, sizeof(phar_entry_data));
269-
idata->fp = phar_get_pharfp(phar);
280+
idata->fp = stream;
270281
idata->phar = phar;
271282
idata->internal_file = entry;
272283
if (!phar->is_persistent) {

ext/phar/tests/bug77432.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Bug #77432 (Segmentation fault on including phar file)
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
--FILE--
8+
<?php
9+
10+
$filename = __DIR__ . '/bug77432.phar';
11+
12+
$phar = new Phar($filename);
13+
$phar->startBuffering();
14+
$phar->addFromString('test.txt', 'text');
15+
$phar->setStub('<?php echo "hello world\n"; __HALT_COMPILER(); ?>');
16+
$phar->stopBuffering();
17+
unset($phar);
18+
19+
echo "--- Include 1 ---\n";
20+
include("phar://" . $filename);
21+
echo "--- Include 2 ---\n";
22+
// Note: will warn because the halting offset is redefined, but won't display the name because "zend_mangle_property_name" starts the name with \0
23+
// However, this is just the easiest way to reproduce it, so go with this test.
24+
include("phar://" . $filename);
25+
echo "--- After unlink ---\n";
26+
unlink($filename);
27+
// This will just fail, as it should, but it is here to test the reopen error-handling path.
28+
include("phar://" . $filename);
29+
30+
?>
31+
--CLEAN--
32+
<?php
33+
@unlink(__DIR__ . '/bug77432.phar');
34+
?>
35+
--EXPECTF--
36+
--- Include 1 ---
37+
hello world
38+
--- Include 2 ---
39+
40+
Warning: Constant already defined in %s on line %d
41+
hello world
42+
--- After unlink ---
43+
44+
Warning: include(%sbug77432.phar): Failed to open stream: phar error: could not reopen phar "%sbug77432.phar" in %s on line %d
45+
46+
Warning: include(): Failed opening '%sbug77432.phar' for inclusion (include_path='.:') in %s on line %d

0 commit comments

Comments
 (0)