Repository navigation
Fix unmapping of an unrelated range when the aligned chunk mmap fails - #24542
Merged
arnaud-lb merged 3 commits intoOct 26, 2026
Merged
Conversation
When the first mmap() in zend_mm_chunk_alloc_int() is not suitably aligned, it is released and a larger mapping is requested. If that second mmap() failed, the POSIX code carried on with ptr == NULL and called munmap() on [size, size + alignment - page size), an absolute low address range. In a non-PIE binary this range can contain the executable itself, so the process crashed instead of reporting "Out of memory". Return NULL when the retry fails.
ndossche
approved these changes
Oct 24, 2026
ndossche
left a comment
Member
There was a problem hiding this comment.
I think this is right but would like a second look by either @iluuu1994 or @arnaud-lb
arnaud-lb
added a commit
that referenced
this pull request
Oct 26, 2026
* PHP-8.6: Fix unmapping of an unrelated range when the aligned chunk mmap fails (#24542)
Member
|
Thank you! |
shivammathur
pushed a commit
to shivammathur/php-src
that referenced
this pull request
Oct 26, 2026
* PHP-8.4: Fix unmapping of an unrelated range when the aligned chunk mmap fails (php#24542)
shivammathur
pushed a commit
to shivammathur/php-src
that referenced
this pull request
Oct 26, 2026
* PHP-8.5: Fix unmapping of an unrelated range when the aligned chunk mmap fails (php#24542)
arnaud-lb
added a commit
to prateekbhujel/php-src
that referenced
this pull request
Oct 27, 2026
* PHP-8.4: (26 commits) Fix phpGH-24695: FPM tester sends signal to process group when PID is missing (php#24698) Fix phpGH-24605: Guard var_export() against native stack overflow (php#24671) Fix use-after-free of CURLOPT_STDERR stream on cloned curl handle IR: Backport code buffer rollback fix Fix SQL test on Windows (php#24755) Fix phpGH-24697: cli: passing PHP_HTTP_PARSER_STRICT build option doesn't work Fix phpGH-24438: Preserve indirect types in JIT side traces Fix phpGH-17733: System entropy does not take into account function order, resulting in wrong functions possibly called in different configurations Fix unmapping of an unrelated range when the aligned chunk mmap fails (php#24542) Backport: Adapt intl timezone test expectations for ICU 79.1 (php#24724) Tracing JIT: keep the SEND_VAL type guard after assigning to a reference Backport: Adapt intl timezone test expectations for ICU 79.1 (php#24684) ext/zip: Reject broken libzip versions on Windows (php#24371) ext/zip: Exclude broken libzip 1.12 (php#24358) Fix phpGH-24264: Unwind/GracefulExit discarded in spl_iterators.c phpdbg: Backport watchpoint fixes to PHP-8.4 Fix JIT reading packed-turned-hash array as packed after string key write ext/standard: Skip proc_open descriptor 0 tests without /proc/self/fd symlinks JIT: Fix operand type check in zend_redo_pass_two_ex() (php#24658) Fix error paths in dom_xml_serializing_a_document_node() ...
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.
If the first
mmap()inzend_mm_chunk_alloc_int()is not suitably aligned, the mapping is released and a larger one is requested so that it can be trimmed to alignment. Nothing checks whether that secondmmap()succeeded. On POSIX, when it fails, the code carries on withptr == NULL:That is a
munmap()of the absolute range[size, size + 2 MiB - 4 KiB), followed by an ordinary "Out of memory". In a PIE binary nothing is usually mapped there, so the straymunmap()does no harm. In a non-PIE binary (for example a plain./configurebuild with clang 12 on Ubuntu 20.04, which does not default to PIE) that range can be PHP's own text, data or heap, and the process crashes with SIGSEGV instead of reporting "Out of memory". The secondmmap()can fail under an address-space limit (RLIMIT_AS,ulimit -v), with strict overcommit, or when the address space is exhausted.The Windows path already has a NULL check for the later retry (added for GH-9650). The first retry has none on either platform.
Reproducer
Non-PIE release build (
./configure --disable-all, clang), text segment at 0x600000:In the crashing window the first
mmap()succeeds and the aligned retry fails. gdb shows the crash inzend_mm_munmap()called fromzend_mm_chunk_alloc_int(), and the r-x mapping at 0x600000 is gone frominfo proc mappings.Fix
Return NULL when the retry
mmap()fails. The check sits before the#ifdef _WIN32split, so it also covers the first retry on Windows, which until now would have passed NULL on tozend_mm_munmap()andzend_mm_mmap_fixed().Test
Zend/tests/zend_mm_chunk_alloc_mmap_failure.phptsets up this situation from inside the script. It reads the start of the executable mapping from/proc/self/mapsand uses it as the allocation size. It then setsRLIMIT_AS(viaposix_setrlimit()) to the current VmSize plus that size plus 1 MiB, which leaves room for the firstmmap()but not for the aligned retry. Finally it allocates the block. Either a clean "Out of memory" or a successful allocation (if the first mapping happens to be aligned) is accepted. The test needs posix, Linux and 64-bit. It is skipped for PIE builds (where the bug cannot be observed) and withUSE_ZEND_ALLOC=0.Termsig=11, deterministically in my runs.Zend/tests: 4918 passed, 143 skipped, 1 expected fail, 0 failed.The Windows part of the change is not tested; I have no Windows build environment.
This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.
🤖 Generated with Claude Code