Skip to content
Open
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
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ PHP NEWS
nested isset()/empty(). (Jorg Sowa)
. Fixed AST printing dropping readonly on promoted constructor parameters.
(Jorg Sowa)
. Fixed use-after-free when the error handler releases or modifies the array
or object while an undefined variable is assigned to one of its elements or
properties. (Jorg Sowa)
. Fixed bug GH-25311 (Assertion ret == SUCCESS in array_map()/array_filter()
after delayed out-of-memory). (ndossche)
. Fixed AST export of by-reference property hooks. (Jorg Sowa)
Expand Down Expand Up @@ -129,6 +132,8 @@ PHP NEWS
. Fixed function JIT ASSIGN_DIM reporting an undefined value or key out of
order, or when the VM does not, on non-array containers. (Jorg Sowa)
. Fix JIT register allocation for ASSIGN to traced reference. (ndossche)
. Fixed use-after-free in JIT when the error handler releases the object or
typed reference while an undefined variable is assigned to it. (Jorg Sowa)

- PDO_Firebird:
. Fixed stack buffer overflow with long connection user names, passwords,
Expand Down
56 changes: 56 additions & 0 deletions Zend/tests/assign_dim_op_undef_op_data_free.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
ASSIGN_DIM_OP: error handler releases the array while reporting undefined OP_DATA
--FILE--
<?php
class C { public int $t = 1; }

set_error_handler(function ($_, $msg) {
echo $msg, "\n";
if ($msg === 'Undefined variable $undef') {
$GLOBALS['x'] = 1;
}
});

$x = [1, 'a'];
$x[1] .= $undef;
var_dump($x);

$x = [1, 2];
$k = 0;
$x[$k] += $undef;
var_dump($x);

$x = [1, 2];
$x[] .= $undef;
var_dump($x);

$x = [[1], 2];
$x[0][0] .= $undef;
var_dump($x);

$x = false;
$x[0] .= $undef;
var_dump($x);

$o = new C;
$x = [1, 2];
$x[0] = &$o->t;
$x[0] += $undef;
var_dump($x, $o->t);
?>
--EXPECT--
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Automatic conversion of false to array is deprecated
Undefined array key 0
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
int(1)
57 changes: 57 additions & 0 deletions Zend/tests/assign_dim_undef_op_data_free.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
ASSIGN_DIM: error handler releases the array while reporting undefined OP_DATA
--FILE--
<?php
set_error_handler(function ($_, $msg) {
echo $msg, "\n";
if ($msg === 'Undefined variable $undef') {
$GLOBALS['x'] = 1;
}
});

$x = [1, 2];
$x[0] = $undef;
var_dump($x);

$x = [1, 2];
$x['k'] = $undef;
var_dump($x);

$x = [1, 2];
$k = 5;
$x[$k] = $undef;
var_dump($x);

$x = [1, 2];
$x[] = $undef;
var_dump($x);

$x = [[1], 2];
$x[0][1] = $undef;
var_dump($x);

$x = false;
$x[0] = $undef;
var_dump($x);

$x = [1, 2];
$r = ($x[0] = $undef);
var_dump($x, $r);
?>
--EXPECT--
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
Automatic conversion of false to array is deprecated
Undefined variable $undef
int(1)
Undefined variable $undef
int(1)
NULL
83 changes: 83 additions & 0 deletions Zend/tests/assign_dim_undef_op_data_modify.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
--TEST--
ASSIGN_DIM: error handler modifies the containers while reporting undefined OP_DATA
--FILE--
<?php
class C { public int $t = 1; }

function grow_outer() {
global $x;
set_error_handler(function ($_, $msg) {
echo $msg, "\n";
for ($i = 0; $i < 100; $i++) {
$GLOBALS['x'][] = $i;
}
});
$x = [[1]];
$x[0][] = $undef;
var_dump(count($x), $x[0]);
}

function copy_and_replace() {
global $x;
set_error_handler(function ($_, $msg) {
echo $msg, "\n";
$GLOBALS['keep'] = $GLOBALS['x'];
$GLOBALS['x'] = 'str';
});
$x = [1];
$x[] = $undef;
var_dump($x);
}

function copy_only() {
global $x;
set_error_handler(function ($_, $msg) {
echo $msg, "\n";
$GLOBALS['keep'] = $GLOBALS['x'];
});
$x = [1];
$x[0] = $undef;
$x = [1];
$x[] = $undef;
$x = [1];
$x[0] .= $undef;
echo "ok\n";
}

function typed_ref() {
global $x, $o;
set_error_handler(function ($_, $msg) {
echo $msg, "\n";
$GLOBALS['x'] = 1;
$GLOBALS['o'] = null;
});
$o = new C;
$x = [0];
$x[0] = &$o->t;
$x[0] = $undef;
var_dump($x, $o);
}

grow_outer();
copy_and_replace();
copy_only();
typed_ref();
?>
--EXPECT--
Undefined variable $undef
int(101)
array(2) {
[0]=>
int(1)
[1]=>
NULL
}
Undefined variable $undef
string(3) "str"
Undefined variable $undef
Undefined variable $undef
Undefined variable $undef
ok
Undefined variable $undef
int(1)
NULL
45 changes: 45 additions & 0 deletions Zend/tests/assign_obj_undef_op_data_free.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
ASSIGN_OBJ: error handler releases the fetched container while reporting undefined OP_DATA
--FILE--
<?php
set_error_handler(function ($_, $msg) {
echo $msg, "\n";
$GLOBALS['a'] = 0;
});

function dim() {
global $a;
$a = [new stdClass];
$a[0]->p = $undef;
var_dump($a);
}

function dim_op() {
global $a;
$o = new stdClass;
$o->p = 'x';
$a = [$o];
unset($o);
$a[0]->p .= $undef;
var_dump($a);
}

function prop() {
global $a;
$a = new stdClass;
$a->o = new stdClass;
$a->o->p = $undef;
var_dump($a);
}

dim();
dim_op();
prop();
?>
--EXPECT--
Undefined variable $undef
int(0)
Undefined variable $undef
int(0)
Undefined variable $undef
int(0)
29 changes: 29 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,35 @@ static zend_never_inline ZEND_COLD zval* zval_undefined_cv(uint32_t var EXECUTE_
return &EG(uninitialized_zval);
}

/* Returns false if the error handler destroyed or shared the array. */
static zend_never_inline ZEND_COLD bool zend_undefined_op_data_write(HashTable *ht, uint32_t var EXECUTE_DATA_DC)
{
GC_ADDREF(ht);
zval_undefined_cv(var EXECUTE_DATA_CC);
if (UNEXPECTED(GC_DELREF(ht) != 1)) {
if (!GC_REFCOUNT(ht)) {
zend_array_destroy(ht);
}
return false;
}
return true;
}

/* An INDIRECT op1 may point into storage the error handler releases, so keep a copy in its slot. */
static zend_never_inline ZEND_COLD zval *zend_undefined_op_data_obj(zval *object, const zend_op *opline EXECUTE_DATA_DC)
{
if (opline->op1_type == IS_VAR) {
zval *var = EX_VAR(opline->op1.var);

if (Z_TYPE_P(var) == IS_INDIRECT) {
ZVAL_COPY(var, object);
object = var;
}
}
zval_undefined_cv((opline+1)->op1.var EXECUTE_DATA_CC);
return object;
}

static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op1(EXECUTE_DATA_D)
{
return zval_undefined_cv(EX(opline)->op1.var EXECUTE_DATA_CC);
Expand Down
43 changes: 31 additions & 12 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,12 @@ ZEND_VM_HANDLER(28, ZEND_ASSIGN_OBJ_OP, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, OP)
property = GET_OP2_ZVAL_PTR(BP_VAR_R);

do {
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
if ((opline+1)->op1_type == IS_CV && UNEXPECTED(Z_TYPE_P(EX_VAR((opline+1)->op1.var)) == IS_UNDEF)) {
object = zend_undefined_op_data_obj(object, opline EXECUTE_DATA_CC);
value = &EG(uninitialized_zval);
} else {
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
}

if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
if (Z_ISREF_P(object) && Z_TYPE_P(Z_REFVAL_P(object)) == IS_OBJECT) {
Expand Down Expand Up @@ -1177,7 +1182,14 @@ ZEND_VM_C_LABEL(assign_dim_op_new_array):
}
}

value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
if ((opline+1)->op1_type == IS_CV && UNEXPECTED(Z_TYPE_P(EX_VAR((opline+1)->op1.var)) == IS_UNDEF)) {
if (UNEXPECTED(!zend_undefined_op_data_write(ht, (opline+1)->op1.var EXECUTE_DATA_CC))) {
ZEND_VM_C_GOTO(assign_dim_op_ret_null);
}
value = &EG(uninitialized_zval);
} else {
value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
}

do {
if (OP2_TYPE != IS_UNUSED && UNEXPECTED(Z_ISREF_P(var_ptr))) {
Expand Down Expand Up @@ -2433,7 +2445,11 @@ ZEND_VM_HANDLER(24, ZEND_ASSIGN_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, CACHE_

SAVE_OPLINE();
object = GET_OP1_OBJ_ZVAL_PTR_PTR_UNDEF(BP_VAR_W);
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
value = GET_OP_DATA_ZVAL_PTR_UNDEF(BP_VAR_R);
if (OP_DATA_TYPE == IS_CV && UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
object = zend_undefined_op_data_obj(object, opline EXECUTE_DATA_CC);
value = &EG(uninitialized_zval);
}

if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
if (Z_ISREF_P(object) && Z_TYPE_P(Z_REFVAL_P(object)) == IS_OBJECT) {
Expand Down Expand Up @@ -2629,22 +2645,19 @@ ZEND_VM_HANDLER(23, ZEND_ASSIGN_DIM, VAR|CV, CONST|TMPVAR|UNUSED|NEXT|CV, SPEC(O
ZEND_VM_C_LABEL(try_assign_dim_array):
SEPARATE_ARRAY(object_ptr);
if (OP2_TYPE == IS_UNUSED) {
HashTable *ht = Z_ARRVAL_P(object_ptr);

value = GET_OP_DATA_ZVAL_PTR_UNDEF(BP_VAR_R);
if (OP_DATA_TYPE == IS_CV && UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
HashTable *ht = Z_ARRVAL_P(object_ptr);
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
GC_ADDREF(ht);
}
value = zval_undefined_cv((opline+1)->op1.var EXECUTE_DATA_CC);
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
zend_array_destroy(ht);
if (UNEXPECTED(!zend_undefined_op_data_write(ht, (opline+1)->op1.var EXECUTE_DATA_CC))) {
ZEND_VM_C_GOTO(assign_dim_error);
}
value = &EG(uninitialized_zval);
}
if (OP_DATA_TYPE == IS_CV || OP_DATA_TYPE == IS_VAR) {
ZVAL_DEREF(value);
}
value = zend_hash_next_index_insert(Z_ARRVAL_P(object_ptr), value);
value = zend_hash_next_index_insert(ht, value);
if (UNEXPECTED(value == NULL)) {
zend_cannot_add_element();
ZEND_VM_C_GOTO(assign_dim_error);
Expand Down Expand Up @@ -2675,7 +2688,13 @@ ZEND_VM_C_LABEL(try_assign_dim_array):
if (UNEXPECTED(variable_ptr == NULL)) {
ZEND_VM_C_GOTO(assign_dim_error);
}
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
value = GET_OP_DATA_ZVAL_PTR_UNDEF(BP_VAR_R);
if (OP_DATA_TYPE == IS_CV && UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
if (UNEXPECTED(!zend_undefined_op_data_write(Z_ARRVAL_P(object_ptr), (opline+1)->op1.var EXECUTE_DATA_CC))) {
ZEND_VM_C_GOTO(assign_dim_error);
}
value = &EG(uninitialized_zval);
}
value = zend_assign_to_variable_ex(variable_ptr, value, OP_DATA_TYPE, EX_USES_STRICT_TYPES(), &garbage);
}
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
Expand Down
Loading
Loading