Skip to content

Fix unmapping of an unrelated range when the aligned chunk mmap fails - #24542

Merged
arnaud-lb merged 3 commits into
php:PHP-8.4from
Marti2203:fix-zend-mm-chunk-alloc-mmap-failure
Oct 26, 2026
Merged

arnaud-lb merged 3 commits into
php:PHP-8.4from
Marti2203:fix-zend-mm-chunk-alloc-mmap-failure

Conversation

@Marti2203

Copy link
Copy Markdown
Contributor

If the first mmap() in zend_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 second mmap() succeeded. On POSIX, when it fails, the code carries on with ptr == NULL:

zend_mm_munmap(ptr, size);
ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE);   /* NULL on failure */
...
offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);          /* 0 */
...
if (alignment > REAL_PAGE_SIZE) {
	zend_mm_munmap((char*)ptr + size, alignment - REAL_PAGE_SIZE);
}
return ptr;

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 stray munmap() does no harm. In a non-PIE binary (for example a plain ./configure build 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 second mmap() 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:

<?php
$s = str_repeat('A', 0x600000 - 4096);   // huge block of exactly 0x600000 bytes
echo "allocation succeeded\n";
for v in 25000 25600 26000 27096 27352 28000; do
  (ulimit -v $v; php -n -d memory_limit=-1 chunk-unmap.php)
done
ulimit -v (KiB) before after
25000 Out of memory Out of memory
25600 - 27352 Segmentation fault Out of memory
28000 allocation succeeded allocation succeeded

In the crashing window the first mmap() succeeds and the aligned retry fails. gdb shows the crash in zend_mm_munmap() called from zend_mm_chunk_alloc_int(), and the r-x mapping at 0x600000 is gone from info proc mappings.

Fix

Return NULL when the retry mmap() fails. The check sits before the #ifdef _WIN32 split, so it also covers the first retry on Windows, which until now would have passed NULL on to zend_mm_munmap() and zend_mm_mmap_fixed().

Test

Zend/tests/zend_mm_chunk_alloc_mmap_failure.phpt sets up this situation from inside the script. It reads the start of the executable mapping from /proc/self/maps and uses it as the allocation size. It then sets RLIMIT_AS (via posix_setrlimit()) to the current VmSize plus that size plus 1 MiB, which leaves room for the first mmap() 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 with USE_ZEND_ALLOC=0.

  • Without the fix (non-PIE build): fails with Termsig=11, deterministically in my runs.
  • With the fix: passes (5/5 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

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 ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is right but would like a second look by either @iluuu1994 or @arnaud-lb

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@arnaud-lb
arnaud-lb merged commit cbda101 into php:PHP-8.4 Oct 26, 2026
12 of 18 checks passed
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)
@arnaud-lb

Copy link
Copy Markdown
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()
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants