Keep the object alive across jsonSerialize() in json_encode()#22469
Open
iliaal wants to merge 1 commit into
Open
Keep the object alive across jsonSerialize() in json_encode()#22469iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
php_json_encode_serializable_object() holds a raw pointer to the object across the jsonSerialize() call, then reads its recursion guard and compares the returned value's identity against it. A user error handler triggered from jsonSerialize() can drop the last reference to the object, for example by nulling a reference that aliases the encoded array slot, freeing it before those reads and causing a use-after-free. Hold a reference on the object across the call. The array path already guards against this with a ZVAL_COPY; the JsonSerializable object path did not. Same use-after-free class as phpGH-21024 in var_dump().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
json_encode() can use-after-free a JsonSerializable object when its jsonSerialize() triggers a user error handler that frees the object, for example by nulling a reference that aliases the encoded array slot. php_json_encode_serializable_object() holds a raw pointer to the object across the call and then reads its recursion guard and compares identity against the return value.
Fix: hold a reference on the object across the call. The array path already guards against this with a ZVAL_COPY ("Avoid modifications (and potential freeing) of the array... when a jsonSerialize() method is invoked"); the JsonSerializable object path did not.