Skip to content

Commit 5fa74db

Browse files
committed
Fix use-after-free in Collator::sort() with a mutating comparator
collator_sort() and collator_asort() sort the array in place while the comparator may run a __toString() that appends to the same array through a reference, reallocating its storage under the running sort. Sort a copy and swap it back, matching usort(). Closes GH-22467
1 parent 64cf7b2 commit 5fa74db

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎ext/intl/collator/collator_sort.c‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,13 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
259259
UCollator* saved_collator;
260260
zval* array = NULL;
261261
HashTable* hash = NULL;
262+
zend_array* sorted = NULL;
262263
zend_long sort_flags = COLLATOR_SORT_REGULAR;
263264

264265
COLLATOR_METHOD_INIT_VARS
265266

266267
/* Parse parameters. */
267-
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa/|l",
268+
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa|l",
268269
&object, Collator_ce_ptr, &array, &sort_flags ) == FAILURE )
269270
{
270271
RETURN_THROWS();
@@ -283,24 +284,38 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
283284

284285
hash = Z_ARRVAL_P( array );
285286

287+
/* Copy array, so the in-place modifications will not be visible to the callback function */
288+
sorted = zend_array_dup( hash );
289+
286290
/* Convert strings in the specified array from UTF-8 to UTF-16. */
287-
collator_convert_hash_from_utf8_to_utf16( hash, COLLATOR_ERROR_CODE_P( co ) );
291+
collator_convert_hash_from_utf8_to_utf16( sorted, COLLATOR_ERROR_CODE_P( co ) );
292+
if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) ) {
293+
zend_array_destroy( sorted );
294+
}
288295
COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-8 to UTF-16" );
289296

290297
/* Save specified collator in the request-global (?) variable. */
291298
saved_collator = INTL_G( current_collator );
292299
INTL_G( current_collator ) = co->ucoll;
293300

294301
/* Sort specified array. */
295-
zend_hash_sort(hash, collator_compare_func, renumber);
302+
zend_hash_sort( sorted, collator_compare_func, renumber );
296303

297304
/* Restore saved collator. */
298305
INTL_G( current_collator ) = saved_collator;
299306

300307
/* Convert strings in the specified array back to UTF-8. */
301-
collator_convert_hash_from_utf16_to_utf8( hash, COLLATOR_ERROR_CODE_P( co ) );
308+
collator_convert_hash_from_utf16_to_utf8( sorted, COLLATOR_ERROR_CODE_P( co ) );
309+
if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) ) {
310+
zend_array_destroy( sorted );
311+
}
302312
COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-16 to UTF-8" );
303313

314+
zval garbage;
315+
ZVAL_COPY_VALUE( &garbage, array );
316+
ZVAL_ARR( array, sorted );
317+
zval_ptr_dtor( &garbage );
318+
304319
RETURN_TRUE;
305320
}
306321
/* }}} */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Collator::sort(): mutating the array from __toString() during comparison must not corrupt the sort
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
$c = new Collator('en_US');
8+
9+
class Grow {
10+
public static array $ref;
11+
public function __toString(): string {
12+
for ($i = 0; $i < 2000; $i++) {
13+
self::$ref[] = "x$i";
14+
}
15+
return "m";
16+
}
17+
}
18+
19+
$arr = ["z", new Grow(), "a", "b"];
20+
Grow::$ref = &$arr;
21+
var_dump($c->sort($arr));
22+
var_dump($arr);
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
array(4) {
27+
[0]=>
28+
string(1) "a"
29+
[1]=>
30+
string(1) "b"
31+
[2]=>
32+
object(Grow)#2 (0) {
33+
}
34+
[3]=>
35+
string(1) "z"
36+
}

0 commit comments

Comments
 (0)