Skip to content

Commit 36c4195

Browse files
iliaalGirgias
authored andcommitted
phar: fix NULL dereference in Phar::webPhar() when SCRIPT_NAME is absent
In the CGI/FastCGI branch of webPhar(), sapi_getenv("SCRIPT_NAME") can return NULL when the upstream server doesn't forward SCRIPT_NAME in the FastCGI params block. The return value was passed directly to strstr() without a NULL check, causing a segfault. Add a NULL guard that jumps to the finish: label, which is already used for the "SCRIPT_NAME doesn't match the phar basename" case. The fix matches the intent of the existing strstr check and requires no new cleanup. Closes GH-21797 Closes GH-21802
1 parent b77b505 commit 36c4195

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

ext/phar/phar_object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ PHP_METHOD(Phar, webPhar)
649649
char *testit;
650650

651651
testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1);
652+
if (!testit) {
653+
goto finish;
654+
}
652655
if (!(pt = strstr(testit, basename))) {
653656
efree(testit);
654657
goto finish;

ext/phar/tests/gh21797.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-21797: Phar::webPhar() NULL dereference when SCRIPT_NAME absent from SAPI environment
3+
--CGI--
4+
--EXTENSIONS--
5+
phar
6+
--INI--
7+
phar.readonly=0
8+
phar.require_hash=0
9+
variables_order=EGPC
10+
register_argc_argv=0
11+
cgi.fix_pathinfo=0
12+
--ENV--
13+
REQUEST_METHOD=GET
14+
PATH_INFO=/gh21797.phar
15+
--FILE--
16+
<?php
17+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
18+
$phar = new Phar($fname);
19+
$phar->addFromString('index.php', '<?php echo "ok\n"; ?>');
20+
$phar->setStub('<?php
21+
Phar::webPhar();
22+
echo "no crash\n";
23+
__HALT_COMPILER(); ?>');
24+
unset($phar);
25+
include $fname;
26+
?>
27+
--CLEAN--
28+
<?php @unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar'); ?>
29+
--EXPECT--
30+
no crash

0 commit comments

Comments
 (0)