Bound the free list walk in zend_mm_gc() - #23566
Open
jvoisin wants to merge 1 commit into
Open
Conversation
zend_mm_gc() counts free slots by walking each bin's free list to the end.
The walk is unbounded, and a cycle in the list makes it spin forever.
Such a cycle is easy to produce from a plain double free, as long as one
other slot of the same size class is freed in between:
efree(a); efree(b); efree(a); /* a -> b -> a */
Both links are written through zend_mm_set_next_free_slot(), so every
shadow is valid and the corruption detection added in 25360ef does not
apply. Neither would a check comparing the freed pointer against the head
of the list, since b is the head at the second efree(a).
gc_mem_caches() is exposed to userland, so this turns any double free into
a reliable interpreter hang. It also corrupts metadata while spinning:
free_counter has no upper bound, but ZEND_MM_SRUN_FREE_COUNTER is only 10
bits, so after 16384 iterations free_counter << 16 reaches
ZEND_MM_IS_LRUN and the page map entry silently becomes an NRUN. A bailout
interrupting the loop, e.g. the execution timeout, leaves that behind.
A slot can be on a free list at most once, so the number of free slots
counted for a page cannot exceed the number of slots the page holds.
Check that. This is a real invariant across repeated collections, because
the last loop of zend_mm_gc() resets the counter of every page it keeps
with ZEND_MM_SRUN(bin_num) and releases the pages that are entirely free,
so counting always starts from zero.
The check costs a compare and a branch per free slot, on a path that only
runs on memory pressure or on an explicit gc_mem_caches() call.
Verified with the sequence above: zend_mm_gc() previously never returned,
and now aborts with "zend_mm_heap corrupted". No false positives over 80
gc_mem_caches() calls spanning all 29 size classes and collecting 130MB,
plus 200 object churn collections. 11327 tests pass.
The two other unbounded free list walks do not need the same treatment.
zend_mm_refresh_key_child() re-encodes with the new key as it goes, so the
third visit to a cycled slot fails the old key check and panics, which was
confirmed the same way. The removal loop of zend_mm_gc() is only reached
once counting has completed, which now implies no duplicated slot.
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.
zend_mm_gc() counts free slots by walking each bin's free list to the end. The walk is unbounded, and a cycle in the list makes it spin forever.
Such a cycle is easy to produce from a plain double free, as long as one other slot of the same size class is freed in between:
Both links are written through zend_mm_set_next_free_slot(), so every shadow is valid and the corruption detection added in 25360ef does not apply. Neither would a check comparing the freed pointer against the head of the list, since b is the head at the second efree(a).
gc_mem_caches() is exposed to userland, so this turns any double free into a reliable interpreter hang. It also corrupts metadata while spinning: free_counter has no upper bound, but ZEND_MM_SRUN_FREE_COUNTER is only 10 bits, so after 16384 iterations free_counter << 16 reaches ZEND_MM_IS_LRUN and the page map entry silently becomes an NRUN. A bailout interrupting the loop, e.g. the execution timeout, leaves that behind.
A slot can be on a free list at most once, so the number of free slots counted for a page cannot exceed the number of slots the page holds. Check that. This is a real invariant across repeated collections, because the last loop of zend_mm_gc() resets the counter of every page it keeps with ZEND_MM_SRUN(bin_num) and releases the pages that are entirely free, so counting always starts from zero.
The check costs a compare and a branch per free slot, on a path that only runs on memory pressure or on an explicit gc_mem_caches() call.
Verified with the sequence above: zend_mm_gc() previously never returned, and now aborts with "zend_mm_heap corrupted". No false positives over 80 gc_mem_caches() calls spanning all 29 size classes and collecting 130MB, plus 200 object churn collections. 11327 tests pass.
The two other unbounded free list walks do not need the same treatment. zend_mm_refresh_key_child() re-encodes with the new key as it goes, so the third visit to a cycled slot fails the old key check and panics, which was confirmed the same way. The removal loop of zend_mm_gc() is only reached once counting has completed, which now implies no duplicated slot.