[php-src] PR #25791: ext/mbstring: Fix integer overflow in mb_convert_variables() string count
| From: | jorgsowa | Date: | Thu, 26 Nov 2026 15:54:53 +0000 |
| Subject: | [php-src] PR #25791: ext/mbstring: Fix integer overflow in mb_convert_variables() string count | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41060@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25791
Author: jorgsowa
Counting the strings to detect among multiple encodings used an unsigned int. The same array counted
once per appearance (the recursion guard is cleared per array) could push the count to 2^32,
wrapping it to 0 and under-allocating the buffers the collection pass writes into, causing a heap
out-of-bounds write.
Count in
uint64_t so the total cannot wrap, and reject a count whose buffer size would
overflow size_t with a warning before allocating.
Regression test for the bug takes more than 60s, I'm not sure it is feasible to include it.
```php
// Reproducer: heap out-of-bounds write in mb_convert_variables()
// via string-count integer overflow (unsigned int wraps at 2^32).
//
// One array of 2^16 strings, referenced 2^16 times, is recounted on every
// appearance (the recursion guard is cleared per array), so the string count
// reaches 2^16 * 2^16 == 2^32 and wraps to 0. The collection buffers are then
// allocated at size 0 and written past -> heap OOB write.
ini_set('memory_limit', '-1');
$shared = array_fill(0, 65536, 'x'); // 2^16 strings
$outer = array_fill(0, 65536, $shared); // same array 2^16 times (copy-on-write)
// Needs >1 detection encoding to enter the auto-detect branch.
mb_convert_variables('UTF-8', ['ASCII', 'UTF-8', 'SJIS'],
$outer);
```
Triggering it requires the same array or object to be reachable from many places (copy-on-write
copies, & references, or repeated object handles) so that the visit count multiplies to 2^32.
Only PHP code or unserialize() can build such a structure. Parsers like json_decode()
or request parsing create a fresh value for every element.