Cap unserialize recursion depth via unserialize_max_depth ini#414
Open
iliaal wants to merge 4 commits into
igbinary:masterigbinary/igbinary:masterfrom
iliaal:fix/unserialize-max-depthiliaal/igbinary:fix/unserialize-max-depthCopy head branch name to clipboard
Open
Cap unserialize recursion depth via unserialize_max_depth ini#414iliaal wants to merge 4 commits intoigbinary:masterigbinary/igbinary:masterfrom iliaal:fix/unserialize-max-depthiliaal/igbinary:fix/unserialize-max-depthCopy head branch name to clipboard
iliaal wants to merge 4 commits into
igbinary:masterigbinary/igbinary:masterfrom
iliaal:fix/unserialize-max-depthiliaal/igbinary:fix/unserialize-max-depthCopy head branch name to clipboard
Conversation
Cap recursion in igbinary_unserialize_zval at BG(unserialize_max_depth) (default 4096, 0 disables) on PHP 7.4+, hardcoded 4096 on 7.0-7.3. Without the cap, a deeply nested payload exhausts the C stack and segfaults the worker, reachable via session.serialize_handler=igbinary before userland code runs.
9a06f82 to
3d3f0c3
Compare
Mark igbinary_unserialize_zval zend_always_inline so the per-zval depth check folds into each callsite, restoring the pre-cap stack- frame profile on the unserialize hot path. Reorder the guard to short-circuit on the dynamic counter first and use ZEND_LONG_FMT for the warning so the value isn't silently truncated to 32 bits when zend_long is 64-bit and long is 32-bit (Windows x64).
tricky
reviewed
May 8, 2026
Member
tricky
left a comment
There was a problem hiding this comment.
Funnily enough the exact things I was going to comment on yesterday. :)
ZEND_LONG_FMT expands to "%" PRId64 / PRId32 from <inttypes.h>; older PHP Windows builds didn't pull that header transitively, so the warning in igbinary_unserialize_zval failed to compile with "missing ')' before PRId64". Include inttypes.h directly. The depth-bomb test built a 250 KB payload with a 50000-iteration .= loop (O(n^2) in PHP), which took longer than the 6h CI timeout under valgrind on PHP 8.0+. Build the same payload via str_repeat with 5000 levels instead.
BG(unserialize_max_depth) is backed by ts_allocate_id-malloc'd thread storage that basic_globals_ctor does not zero. On valgrind builds without --enable-valgrind, the field carries malloc-origin taint and reading it trips 'Conditional jump on uninitialised value' on every unserialize call. zend_ini_long() reads the canonical INI hashtable entry, which has a defined-storage path. Caught by the GitHub Actions valgrind job on PHP 8.x.
Author
|
After 8 tries finally a clean build 😮💨 |
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.
Cap recursion in
igbinary_unserialize_zvalatBG(unserialize_max_depth)(default 4096, 0 disables) on PHP 7.4+, hardcoded 4096 on 7.0-7.3. Without the cap, a deeply nested payload exhausts the C stack and segfaults the worker, reachable viasession.serialize_handler=igbinarybefore userland code runs