diff --git a/NEWS b/NEWS index 64c61f8f121b..2a39f4a8e84a 100644 --- a/NEWS +++ b/NEWS @@ -108,6 +108,11 @@ PHP NEWS . Fixed session upload progress to respect session.use_only_cookies. (jorgsowa) +- SPL: + . Fixed use-after-free when an error handler replaces the backing store of + an ArrayObject during an offset access or ArrayIterator::seek(). + (Nicolas Grekas) + - Standard: . Fixed dns_get_record() returning partially built records for malformed DNS responses. (Marti2203) diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index b60b3b9c0dac..4271c2a581bd 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -325,9 +325,9 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object { zval *retval; spl_hash_key key; - HashTable *ht = spl_array_get_hash_table(intern); + HashTable *ht; - if (!offset || Z_ISUNDEF_P(offset) || !ht) { + if (!offset || Z_ISUNDEF_P(offset)) { return &EG(uninitialized_zval); } @@ -342,7 +342,11 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object &EG(error_zval) : &EG(uninitialized_zval); } + /* Fetched after get_hash_key(), whose errors may call a handler that replaces the storage */ + ht = spl_array_get_hash_table(intern); + if (key.key) { +find_key: retval = zend_hash_find(ht, key.key); if (retval) { if (Z_TYPE_P(retval) == IS_INDIRECT) { @@ -358,7 +362,11 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object break; case BP_VAR_RW: zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key)); - ZEND_FALLTHROUGH; + /* The error handler may have replaced the storage, + * which can free the table that retval points to */ + ht = spl_array_get_hash_table(intern); + type = BP_VAR_W; + goto find_key; case BP_VAR_W: { ZVAL_NULL(retval); } @@ -376,6 +384,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object break; case BP_VAR_RW: zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key)); + /* The error handler may have replaced the storage */ + ht = spl_array_get_hash_table(intern); ZEND_FALLTHROUGH; case BP_VAR_W: { zval value; @@ -397,6 +407,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object break; case BP_VAR_RW: zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h); + /* The error handler may have replaced the storage */ + ht = spl_array_get_hash_table(intern); ZEND_FALLTHROUGH; case BP_VAR_W: { zval value; @@ -596,7 +608,7 @@ static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object } if (!value) { - HashTable *ht = spl_array_get_hash_table(intern); + HashTable *ht; spl_hash_key key; if (get_hash_key(&key, intern, offset) == FAILURE) { @@ -604,6 +616,9 @@ static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object return 0; } + /* Fetched after get_hash_key(), whose errors may call a handler that replaces the storage */ + ht = spl_array_get_hash_table(intern); + if (key.key) { tmp = zend_hash_find(ht, key.key); spl_hash_key_release(&key); @@ -1687,7 +1702,7 @@ PHP_METHOD(ArrayIterator, seek) zend_long opos, position; zval *object = ZEND_THIS; spl_array_object *intern = Z_SPLARRAY_P(object); - HashTable *aht = spl_array_get_hash_table(intern); + HashTable *aht; int result; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) { @@ -1702,6 +1717,8 @@ PHP_METHOD(ArrayIterator, seek) while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS); + /* Fetched after parsing the argument, as its errors may call a handler that replaces the storage */ + aht = spl_array_get_hash_table(intern); if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) { return; /* ok */ } diff --git a/ext/spl/tests/ArrayObject/ArrayObject_storage_replaced_in_error_handler.phpt b/ext/spl/tests/ArrayObject/ArrayObject_storage_replaced_in_error_handler.phpt new file mode 100644 index 000000000000..3add7c1d0933 --- /dev/null +++ b/ext/spl/tests/ArrayObject/ArrayObject_storage_replaced_in_error_handler.phpt @@ -0,0 +1,81 @@ +--TEST-- +ArrayObject: an error handler can replace the backing store during an offset access +--FILE-- +exchangeArray(['replaced' => str_repeat('x', 3)]); + }); +} + +echo "-- read\n"; +$ao = new ArrayObject(['a' => 1, 'b' => 2]); +replace_in_error_handler($ao); +var_dump($ao[1.5]); +restore_error_handler(); + +echo "-- isset\n"; +$ao = new ArrayObject(['a' => 1, 'b' => 2]); +replace_in_error_handler($ao); +var_dump(isset($ao[1.5])); +restore_error_handler(); + +echo "-- read-write of a missing key\n"; +$ao = new ArrayObject(['a' => 1], ArrayObject::ARRAY_AS_PROPS); +replace_in_error_handler($ao); +$ao->b .= 'x'; +restore_error_handler(); +var_dump($ao->getArrayCopy()); + +echo "-- read-write of an unset property of a backing object\n"; +class C { + public $p; +} +$o = new C(); +unset($o->p); +$ao = @new ArrayObject($o, ArrayObject::ARRAY_AS_PROPS); +unset($o); +replace_in_error_handler($ao); +$ao->p .= 'x'; +restore_error_handler(); +var_dump($ao->getArrayCopy()); + +echo "-- ArrayIterator::seek()\n"; +$it = new ArrayIterator(['a', 'b']); +set_error_handler(function (int $errno, string $errstr) use ($it) { + echo $errstr, "\n"; + $it->__construct(['x', 'y', 'z']); +}); +$it->seek(1.5); +restore_error_handler(); +var_dump($it->current()); +?> +--EXPECT-- +-- read +Implicit conversion from float 1.5 to int loses precision +Undefined array key 1 +NULL +-- isset +Implicit conversion from float 1.5 to int loses precision +bool(false) +-- read-write of a missing key +Undefined array key "b" +array(2) { + ["replaced"]=> + string(3) "xxx" + ["b"]=> + string(1) "x" +} +-- read-write of an unset property of a backing object +Undefined array key "p" +array(2) { + ["replaced"]=> + string(3) "xxx" + ["p"]=> + string(1) "x" +} +-- ArrayIterator::seek() +Implicit conversion from float 1.5 to int loses precision +string(1) "y"