[php-src] Issue #25024: mb_ereg_replace(): the out-of-bounds read fixed in 8.4.26 is still present in PHP-8.3 and PHP-8.2

From: Date: Thu, 29 Oct 2026 01:00:19 +0000
Subject: [php-src] Issue #25024: mb_ereg_replace(): the out-of-bounds read fixed in 8.4.26 is still present in PHP-8.3 and PHP-8.2
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-253912@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/25024 Author: aygydiewr221 ### Description **This is not a new bug and not my finding.** It was fixed in 8.4.26 / 8.5.11 and credited to Ilia Alshanetsky: > Fixed mb_ereg_replace() emitting a NUL or out-of-bounds bytes > in the > replacement when a \k<name> backref has no closing > delimiter. > (Ilia Alshanetsky) I am asking about PHP-8.3 and PHP-8.2, which did not get the fix. Both are still in security support (8.3 to 2027-12-31, 8.2 to 2026-12-31) and the code there is still unclamped. ### Still present on both branches ext/mbstring/php_mbregex.c, mb_regex_substitute(). The fix added two MIN(..., eos) clamps. On PHP-8.4 head they are there: ```c p = MIN(p + clen, eos); /* php_mbregex.c:778 */ p = MIN(name_end + 1, eos); /* php_mbregex.c:800 */ ``` On PHP-8.3 and PHP-8.2 head the same two sites are unchanged: ```c p += clen; /* php_mbregex.c:774 */ p = name_end + 1; /* php_mbregex.c:794 */ ``` Both advance p past eos when a \k<name> backreference has no closing delimiter, and the smart_str_appendl(pbuf, sp, p - sp) that follows copies the overshoot into the result. (Line numbers are branch head as of 2026-10-29.) ### The following code ```php <?php echo "PHP ", PHP_VERSION, "\n"; // _ZSTR_STRUCT_SIZE(len) = 24 + len + 1 ; bins are multiples of 8. // len = bin-25 makes the NUL the final byte of the allocation, so a +4 // overshoot reads into the NEXT allocation. $target_lens = [7, 15, 23, 31, 39, 55, 71]; foreach ($target_lens as $L) { $tail = "\k<\xF0"; // 4 bytes, forces +4 overshoot $pad = $L - strlen($tail); if ($pad < 0) continue; $found = []; for ($i = 0; $i < 300; $i++) { // churn: allocate victims of the adjacent bin filled with markers $v = []; for ($j = 0; $j < 12; $j++) { $v[] = str_repeat('S', $L); } unset($v[$i % 12]); $rep = str_repeat('P', $pad) . $tail; // runtime-built, len = $L $r = @mb_ereg_replace('x', $rep, 'x'); if (!is_string($r)) continue; $leak = substr($r, $L); if ($leak !== '' && trim($leak, "\0") !== '') $found[bin2hex($leak)] = 1; } printf("len=%-3d alloc=%-3d distinct_nonzero=%-3d %s\n", $L, 24+$L+1, count($found), implode(' ', array_slice(array_keys($found), 0, 5))); } ``` The replacement is built at runtime so that it is a fresh allocation rather than an interned literal, and its length is chosen so the terminating NUL is the last byte of its Zend MM block — which puts the next block immediately after it. ### Resulted in this output On a 8.3.36 build, the line for length 7 (the hex differs between runs, because it is whatever is in the neighbouring allocation): ``` PHP 8.3.36 len=7 alloc=32 distinct_nonzero=7 003d5554 00797300 00726570 00617400 00696365 ``` Decoded, those are \0=UT, \0ys, \0rep, \0at, \0ice — fragments of unrelated live strings, returned to the caller inside the replacement result. ### But I expected this output instead Nothing past the replacement's own bytes, i.e. ``` len=7 alloc=32 distinct_nonzero=0 ``` ### What I measured With probes at both sites on a source build of 8.3.36, the overshoot is **1 to 4 bytes** and both sites fire: ``` [F2-a] p += clen OVERSHOOT by 2 [F2-b] p = name_end+1 OVERSHOOT by 4 ``` The diagnostic fired 2,100 times during the run. Two things worth stating against the severity, in fairness: - **The disclosure is allocator-dependent.** A zend_string is allocated as 24 + len + 1 rounded up to 8, so a 1–4 byte overshoot usually lands in intra-block padding. It crosses into a different allocation only when the content ends flush with the block end, which is what Zend MM's size bins produce — hence the chosen lengths above. Under ASAN's malloc the overshoot never left the block and ASAN reported nothing across lengths 4–88. - **No crash in any run.** This is a short read, not a write. The affected range is wider than the NEWS entry implies: 7.4.33 and 8.1.31 reproduce as well, and the file has no PHP_WIN32 conditional, so this is not platform-specific. ### The ask Apply the 8.4.26 fix (the two MIN(..., eos) clamps) to PHP-8.3, and to PHP-8.2 while that branch is still in security support. The fix is in php_mbregex.c in both branches and should apply directly. I am not asking for a CVE or for credit — the credit belongs to Ilia Alshanetsky. The only reason I am raising it is that an out-of-bounds read that reaches user-visible output is classified differently from the functional-bug treatment it received, and two supported branches still have it. If you judge 8.3 fine as it is, or 8.2 too close to end of life to warrant a release, that is a reasonable call and I will not argue it. ### PHP Version ```plain PHP 8.3.36 (cli) (built from source, gcc 13.3, NTS) ``` ### Operating System Ubuntu 24.04 (WSL2)

« previous php.bugs (#253912) next »