Fix use-after-free when json_encode() re-enters an ArrayObject - #22921
Open
iliaal wants to merge 1 commit into
Open
Fix use-after-free when json_encode() re-enters an ArrayObject#22921iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
spl_array_get_hash_table_ptr() hands out a pointer into the ArrayObject storage without checking whether anything else holds a reference to it. json_encode() takes such a reference for the length of the encode and then runs userland through JsonSerializable::jsonSerialize() and property hooks, so an insert from there grows the table and frees the buckets the encoder is iterating. Separate the array-backed and self-backed cases the way the object-backed case in the same function already does. Closes phpGH-22921
iliaal
force-pushed
the
fix/json-arrayobject-reentrant-uaf
branch
from
July 29, 2026 12:57
fe2d17e to
cd79217
Compare
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() borrows an ArrayObject's storage table for the length of the encode and then runs userland through JsonSerializable::jsonSerialize() and property hooks. spl_array_get_hash_table_ptr() hands that same table back to the write paths, so an insert from the callback grows it and frees the buckets the encoder is iterating; valgrind reports invalid reads in php_json_encode_array() for the array-backed ArrayObject, ArrayIterator and self-backed cases in the test. The object-backed branch of that function already separates when the table is borrowed, so this gives the other two branches the same treatment. Encoding cost is unchanged: the separation only fires while a borrow is outstanding, and the refcount is 1 the rest of the time.
#20725 proposes the same SEPARATE_ARRAY call at the write sites instead. That placement leaves the by-reference auto-vivify path through spl_array_get_dimension_ptr() unprotected, which valgrind still reports as an invalid read, and it does not reach the self-backed case.