From d27bb022001e0300ce7ead6fb5b5491fd65a9af0 Mon Sep 17 00:00:00 2001 From: Pratik Bhujel Date: Thu, 26 Nov 2026 18:19:44 +0545 Subject: [PATCH] Fix GH-25723: Avoid UAF and double-free in spl_autoload_unregister('spl_autoload_call') When spl_autoload_unregister('spl_autoload_call') called zend_hash_clean() directly on spl_autoload_functions, element destructors ran while the bucket was still present in the table. An autoloader object whose __destruct() re-entered spl_autoload_unregister() could match and destroy the bucket being cleaned (or another still-pending bucket) a second time, causing a use-after-free and double-free, while spl_autoload_register() from such a destructor leaked the newly registered entry. Detach the entries into a local HashTable and re-initialize spl_autoload_functions in place before calling zend_hash_destroy(&tmp). This keeps the HashTable pointer valid for any active spl_perform_autoload() iteration while ensuring destructors observe a clean, empty table. Closes GH-25723 --- ext/spl/php_spl.c | 11 ++- ext/spl/tests/gh25723.phpt | 144 +++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 ext/spl/tests/gh25723.phpt diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index af3b115238bb..a086f0dcb0ba 100644 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -578,9 +578,14 @@ PHP_FUNCTION(spl_autoload_unregister) if (fcc.function_handler && zend_string_equals_literal( fcc.function_handler->common.function_name, "spl_autoload_call")) { - if (spl_autoload_functions) { - /* Don't destroy the hash table, as we might be iterating over it right now. */ - zend_hash_clean(spl_autoload_functions); + if (spl_autoload_functions && spl_autoload_functions->nNumUsed) { + /* Detach the entries before destroying them so destructors cannot + * re-enter on buckets currently being cleaned, while keeping the + * HashTable pointer valid for active autoload iteration. */ + HashTable tmp = *spl_autoload_functions; + zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0); + zend_hash_real_init_mixed(spl_autoload_functions); + zend_hash_destroy(&tmp); } RETURN_TRUE; } diff --git a/ext/spl/tests/gh25723.phpt b/ext/spl/tests/gh25723.phpt new file mode 100644 index 000000000000..80185922bc6d --- /dev/null +++ b/ext/spl/tests/gh25723.phpt @@ -0,0 +1,144 @@ +--TEST-- +GH-25723: spl_autoload_unregister('spl_autoload_call') UAF/double-free on destructor re-entry +--FILE-- +other, 'autoload'])); + } +} +$b = new OtherLoader(); +$a = new UnregisterOther(); +$a->other = $b; +spl_autoload_register([$a, 'autoload']); +spl_autoload_register([$b, 'autoload']); +unset($a, $b); +var_dump(spl_autoload_unregister('spl_autoload_call')); +$x = str_repeat('x', 7); +$y = str_repeat('y', 7); +echo "case2 ok\n"; + +// 3. Destructor re-enters spl_autoload_unregister('spl_autoload_call') and registers a new loader +class ReplacementLoader { + public function autoload($c) {} +} +class ReenterAndRegister { + public function autoload($c) {} + public function __destruct() { + var_dump(spl_autoload_unregister('spl_autoload_call')); + spl_autoload_register([new ReplacementLoader(), 'autoload']); + } +} +$a = new ReenterAndRegister(); +spl_autoload_register([$a, 'autoload']); +unset($a); +var_dump(spl_autoload_unregister('spl_autoload_call')); +var_dump(count(spl_autoload_functions())); +var_dump(spl_autoload_unregister('spl_autoload_call')); +var_dump(count(spl_autoload_functions())); +echo "case3 ok\n"; + +// 4. Closure capturing $this and __call trampoline autoloader +class ClosureLoader { + public function makeClosure() { + return function ($c) {}; + } + public function __destruct() { + var_dump(count(spl_autoload_functions())); + foreach (spl_autoload_functions() as $f) { + spl_autoload_unregister($f); + } + } +} +$c1 = new ClosureLoader(); +spl_autoload_register($c1->makeClosure()); +unset($c1); +var_dump(spl_autoload_unregister('spl_autoload_call')); + +class TrampolineLoader { + public function __call($name, $args) {} + public function __destruct() { + var_dump(spl_autoload_unregister([$this, 'dynamicAutoload'])); + } +} +$c2 = new TrampolineLoader(); +spl_autoload_register([$c2, 'dynamicAutoload']); +unset($c2); +var_dump(spl_autoload_unregister('spl_autoload_call')); +echo "case4 ok\n"; + +// 5. Destructor throwing an exception still cleans remaining entries without leaking +class ThrowingLoader { + public function autoload($c) {} + public function __destruct() { + throw new RuntimeException('boom'); + } +} +spl_autoload_register([new ThrowingLoader(), 'autoload']); +spl_autoload_register([new OtherLoader(), 'autoload']); +try { + spl_autoload_unregister('spl_autoload_call'); +} catch (RuntimeException $e) { + echo "caught: ", $e->getMessage(), "\n"; +} +var_dump(count(spl_autoload_functions())); +echo "case5 ok\n"; + +// 6. Calling spl_autoload_unregister('spl_autoload_call') inside an active autoloader +class ClearingDuringAutoload { + public function autoload($c) { + spl_autoload_unregister('spl_autoload_call'); + } +} +spl_autoload_register([new ClearingDuringAutoload(), 'autoload']); +spl_autoload_register([new OtherLoader(), 'autoload']); +spl_autoload_call('GH25723NonExistentClass'); +var_dump(count(spl_autoload_functions())); +echo "case6 ok\n"; +?> +--EXPECT-- +bool(false) +bool(true) +case1 ok +bool(false) +bool(true) +case2 ok +bool(true) +bool(true) +int(1) +bool(true) +int(0) +case3 ok +int(0) +bool(true) +bool(false) +bool(true) +case4 ok +caught: boom +int(0) +case5 ok +int(0) +case6 ok