Repository navigation
Conversation
A crafted, deliberately truncated unserialize() payload can leave Exception::$trace holding a non-array value, since the typed-property check is skipped on the parse failure path. getTraceAsString() then reinterpreted the object as a HashTable, causing an out-of-bounds read. Guard against a non-array trace and return an empty string instead. Fix php#22257
|
Shouldn't this throw instead? I don't think it makes sense trying to support/continue operating on maliciously-crafted code. |
|
yes, you re not wrong. |
|
Hello @ndossche @devnexen Thank you in advance! |
ndossche
left a comment
There was a problem hiding this comment.
Actually, $trace is defined with type array, so it should not be possible to break that constraint. This should be blocked instead of adding a check in getTraceAsString
|
To clarify, if I reset to the |
Yeah it should not be allowed to violate the property type so that seems better. |
| * declared type so we restore the default | ||
| */ | ||
| zval *tmp = &obj->ce->default_properties_table[OBJ_PROP_TO_NUM(info->offset)]; | ||
| zval_ptr_dtor(data); |
There was a problem hiding this comment.
I assume evil destructors can cause issues here, so a temporary copy may be necessary (i.e. the zval garbage; trick)
I wonder though if "restoring after violation" is the right approach, but that may be a much bigger change
There was a problem hiding this comment.
Went with var_push_dtor_value rather than the inline zval garbage, it moves the old value into the deferred-dtor list. So nothing is destroyed during the unwind or the delayed calls wdyt ?
ndossche
left a comment
There was a problem hiding this comment.
To me this seems fine, but best to get the opinion of a few more people
cc @arnaud-lb @iluuu1994
| var_push_dtor_value(var_hash, data); | ||
| } | ||
| ZVAL_COPY_OR_DUP(data, tmp); | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
| if (Z_REFCOUNTED_P(data)) { | ||
| var_push_dtor_value(var_hash, data); | ||
| } | ||
| ZVAL_COPY_OR_DUP(data, tmp); |
There was a problem hiding this comment.
Nit: could use ZVAL_COPY_VALUE_PROP() like in _object_properties_init()
There was a problem hiding this comment.
I think ZVAL_COPY_OR_DUP_PROP is then more appropriate due to ref count (esp for other cases than spl).
A crafted, deliberately truncated unserialize() payload can leave Exception::$trace holding a non-array value, since the typed-property check is skipped on the parse failure path. getTraceAsString() then reinterpreted the object as a HashTable, causing an out-of-bounds read. Guard against a non-array trace and return an empty string instead.
Fix #22257