Skip to content
Closed
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
38 changes: 38 additions & 0 deletions Zend/tests/gh22257.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
GH-22257 (Type confusion / OOB read unserializing an Exception with a non-array trace)
--CREDITS--
Igor Sak-Sakovskiy (Positive Technologies)
--FILE--
<?php
/* A crafted, deliberately truncated payload makes the nested value of the typed
* "array $trace" property fail to unserialize. On that failure path the slot used
* to keep the half-built (non-array) value, and the partially-built Exception was
* then exposed to getTraceAsString() through SplHeap's delayed __unserialize(),
* type-confusing the object as a HashTable. The slot is now reset to the property
* default, so the run completes without an out-of-bounds read. */
$n = "\x00";
try {
unserialize(
'O:9:"Exception":1:{s:16:"' . $n . 'Exception' . $n . 'trace";' .
'O:8:"stdClass":2:{s:1:"0";O:10:"SplMaxHeap":2:{i:0;a:0:{}i:1;a:2:{' .
's:5:"flags";i:0;s:13:"heap_elements";a:2:{i:0;s:0:"";i:1;R:1;}}}z}}'
);
} catch (\Throwable $e) {
for (; $e; $e = $e->getPrevious()) {
printf("%s: %s\n", $e::class, $e->getMessage());
}
}

/* By-ref type violation: the slot is reset to the property default. */
class Test { public int $i; public array $a; }
try {
var_dump(unserialize('O:4:"Test":2:{s:1:"i";N;s:1:"a";R:2;}'));
} catch (\Throwable $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}
echo "OK\n";
?>
--EXPECTF--
Warning: unserialize(): Error at offset %d of %d bytes in %s on line %d
TypeError: Cannot assign null to property Test::$i of type int
OK
24 changes: 18 additions & 6 deletions ext/standard/var_unserializer.re
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ static zend_never_inline void var_push_dtor_value(php_unserialize_data_t *var_ha
}
}

static zend_always_inline void var_restore_prop_default(php_unserialize_data_t *var_hash, zend_object *obj, zend_property_info *info, zval *data)
{
/* A partially/incorrectly unserialized value may violate the property's
* declared type, so restore the default and keep the slot consistent. */
zval *tmp = &obj->ce->default_properties_table[OBJ_PROP_TO_NUM(info->offset)];
if (Z_REFCOUNTED_P(data)) {
var_push_dtor_value(var_hash, data);
}
ZVAL_COPY_OR_DUP_PROP(data, tmp);
}

static zend_always_inline zval *tmp_var(php_unserialize_data_t *var_hashx, zend_long num)
{
var_dtor_entries *var_hash;
Expand Down Expand Up @@ -677,18 +688,19 @@ second_try:
}

if (!php_var_unserialize_internal(data, p, max, var_hash)) {
if (info && Z_ISREF_P(data)) {
/* Add type source even if we failed to unserialize.
* The data is still stored in the property. */
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), info);
if (info) {
if (Z_ISREF_P(data)) {
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), info);
} else {
var_restore_prop_default(var_hash, obj, info, data);
}

@arnaud-lb arnaud-lb Jun 25, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory the same problem happens after zend_verify_prop_assignable_by_ref() bellow, but right now the call to SplMaxHeap->__unserialize() (and thus Exception->getTraceAsString()) is prevented due to EG(exception) being set. The prop is undef, but IS_PROP_UNINIT is not set. Maybe update that code path too?

Edit: the if (Z_ISREF_P(data)) { case above could also leave a prop assigned to a wrong type

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the if (Z_ISREF_P(data)) case, left it for now since it's gated by EG(exception) too, feels like follow up material, wdyt ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's fine

}
goto failure;
}

if (UNEXPECTED(info)) {
if (!zend_verify_prop_assignable_by_ref(info, data, /* strict */ 1)) {
zval_ptr_dtor(data);
ZVAL_UNDEF(data);
var_restore_prop_default(var_hash, obj, info, data);
goto failure;
}

Expand Down
Loading