Skip to content

ext/mbstring: Fix integer overflow in mb_convert_variables() string count - #25791

Open
jorgsowa wants to merge 1 commit into
php:PHP-8.4from
jorgsowa:fix-mb-convert-variables-count-overflow
Open

jorgsowa wants to merge 1 commit into
php:PHP-8.4from
jorgsowa:fix-mb-convert-variables-count-overflow

Conversation

@jorgsowa

Copy link
Copy Markdown
Contributor

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.

// 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.

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 that
would overflow the buffer allocation before allocating, matching the
array_merge() element-count fix.
@alexdowad

Copy link
Copy Markdown
Contributor

Thanks for bringing up this issue. The num < SIZE_MAX / sizeof(char*) guard seems unreachable in practice, and even without it, ecalloc already has an overflow guard. Is there a good reason for that guard?

@jorgsowa

Copy link
Copy Markdown
Contributor Author

It's for 32-bit: ecalloc() takes size_t, so a uint64_t count ≥ 2^32 would be truncated (2^32 → 0) before its overflow check sees it. On 64-bit it's unreachable.

PR is mostly about hardening. I can add comment about reachability and why there is no test.

@alexdowad

Copy link
Copy Markdown
Contributor

@jorgsowa OK. Well, if that's the case, then no objection. @youkidearitai, any thoughts?

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.

2 participants