Skip to content

Commit 020bbea

Browse files
committed
phar: Fix memory leak when openssl polyfill returns garbage
Closes phpGH-20210.
1 parent 939b972 commit 020bbea

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ PHP NEWS
5555
(nielsdos)
5656
. Fix potential buffer length truncation due to usage of type int instead
5757
of type size_t. (Girgias)
58+
. Fix memory leak when openssl polyfill returns garbage. (nielsdos)
5859

5960
- Random:
6061
. Fix Randomizer::__serialize() w.r.t. INDIRECTs. (nielsdos)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
openssl_sign() polyfill with wrong return value
3+
--EXTENSIONS--
4+
phar
5+
--SKIPIF--
6+
<?php
7+
if (getenv('SKIP_SLOW_TESTS')) die('skip');
8+
if (function_exists('openssl_sign')) die('skip requires openssl disabled for mocking purposes');
9+
?>
10+
--INI--
11+
phar.require_hash=0
12+
--FILE--
13+
<?php
14+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.tar';
15+
16+
function openssl_sign() {
17+
return str_repeat('foobar', random_int(1, 1));
18+
}
19+
20+
$phar = new PharData($fname);
21+
$phar->setSignatureAlgorithm(Phar::OPENSSL, "randomcrap");
22+
try {
23+
$phar->addEmptyDir('blah');
24+
} catch (PharException $e) {
25+
echo $e->getMessage();
26+
}
27+
28+
?>
29+
--CLEAN--
30+
<?php
31+
@unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.tar');
32+
?>
33+
--EXPECTF--
34+
phar error: unable to write signature to tar-based phar: unable to write phar "%s" with requested openssl signature

ext/phar/util.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,6 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
14711471
zval_ptr_dtor_str(&zp[2]);
14721472

14731473
switch (Z_TYPE(retval)) {
1474-
default:
14751474
case IS_LONG:
14761475
zval_ptr_dtor(&zp[1]);
14771476
if (1 == Z_LVAL(retval)) {
@@ -1483,6 +1482,9 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
14831482
*signature_len = Z_STRLEN(zp[1]);
14841483
zval_ptr_dtor(&zp[1]);
14851484
return SUCCESS;
1485+
default:
1486+
zval_ptr_dtor(&retval);
1487+
ZEND_FALLTHROUGH;
14861488
case IS_FALSE:
14871489
zval_ptr_dtor(&zp[1]);
14881490
return FAILURE;

0 commit comments

Comments
 (0)