com php-src: Fix use-after-free with delayed interned memoized const operand: Zend/tests/assign_coalesce_006.phpt Zend /zend_compile.c
| From: | Nikita Popov | Date: | Fri, 04 Oct 2019 08:38:11 +0000 |
| Subject: | com php-src: Fix use-after-free with delayed interned memoized const operand: Zend/tests/assign_coalesce_006.phpt Zend /zend_compile.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-108719@lists.php.net to get a copy of this message | ||
Commit: 9659562cb5422099cc1286263e0ef883dab4d647
Author: Nikita Popov <nikita.ppv@gmail.com> Fri, 4 Oct 2019 10:38:11 +0200
Parents: f26b60a583efb064a8c082b780b9b186c43cac64
Branches: PHP-7.4 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=9659562cb5422099cc1286263e0ef883dab4d647
Log:
Fix use-after-free with delayed interned memoized const operand
We should addref CONST operands during memoization, as they might
be destroyed by later compilation, e.g. through interning.
Changed paths:
A Zend/tests/assign_coalesce_006.phpt
M Zend/zend_compile.c
Diff:
diff --git a/Zend/tests/assign_coalesce_006.phpt b/Zend/tests/assign_coalesce_006.phpt
new file mode 100644
index 00000000000..dfe43b3c29a
--- /dev/null
+++ b/Zend/tests/assign_coalesce_006.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Null coalesce assign with memoized constant operand that is later interned (OSS-Fuzz #17903)
+--FILE--
+<?php
+$foo[__DIR__] ??= 42;
+var_dump($foo);
+?>
+--EXPECTF--
+array(1) {
+ ["%s"]=>
+ int(42)
+}
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 8f545ce51fa..9132656d41c 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -2054,6 +2054,9 @@ static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{
*/
} else if (result->op_type == IS_TMP_VAR) {
zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
} else {
+ if (result->op_type == IS_CONST) {
+ Z_TRY_ADDREF(result->u.constant);
+ }
memoized_result = *result;
}
@@ -7540,7 +7543,11 @@ void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
/* }}} */
static void znode_dtor(zval *zv) {
- efree(Z_PTR_P(zv));
+ znode *node = Z_PTR_P(zv);
+ if (node->op_type == IS_CONST) {
+ zval_ptr_dtor_nogc(&node->u.constant);
+ }
+ efree(node);
}
void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */