Repository navigation
Description
Description
spl_autoload_unregister('spl_autoload_call') removes all autoloaders with zend_hash_clean() (ext/spl/php_spl.c:590). The element destructor autoload_func_info_destroy() (ext/spl/php_spl.c:357-370) releases the autoloader object (:359). When the table is the only owner of that object, its __destruct() runs right there, inside the clean loop. The bucket has not been cleared at this point, so when the destructor calls spl_autoload_unregister([$this, 'autoload']), spl_find_registered_function() (:481-493) still finds the entry that is being destroyed and zend_hash_del_bucket() (:606) destroys it a second time. The result is a use-after-free of the autoloader object and a double free of the autoload_func_info struct.
This is reachable from plain PHP in default builds, but only through the spl_autoload_unregister('spl_autoload_call') shortcut, which is deprecated since PHP 8.5.
The following code:
<?php
// idx 277: spl_autoload_unregister('spl_autoload_call') sentinel branch runs
// zend_hash_clean(); the element dtor releases the sole-ref autoloader object
// whose __destruct reenters spl_autoload_unregister() on the bucket currently
// being cleaned -> the entry is destroyed twice: use-after-free of the object
// (zend_objects_store_del) and of the autoloader info struct, which is freed twice.
class A {
public function autoload($c) {}
public function __destruct() {
spl_autoload_unregister([$this, 'autoload']);
}
}
$a = new A();
spl_autoload_register([$a, 'autoload']);
unset($a); // spl_autoload_functions hash is now the sole owner
spl_autoload_unregister('spl_autoload_call'); // E_DEPRECATED + zend_hash_clean
echo "survived\n";Resulted in this output:
$ USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 sapi/cli/php poc.php
Deprecated: spl_autoload_unregister(): Using spl_autoload_call() as a callback for spl_autoload_unregister() is deprecated, to remove all registered autoloaders, call spl_autoload_unregister() for all values returned from spl_autoload_functions() in poc.php on line 16
ERROR: AddressSanitizer: heap-use-after-free on address 0x504000039150 at pc 0x599779faca1b bp 0x7fff7a0adfa0 sp 0x7fff7a0adf90
READ of size 4 at 0x504000039150 thread T0
#0 0x599779faca1a in zend_gc_delref Zend/zend_types.h:1395
#1 0x599779faca1a in zend_objects_store_del Zend/zend_objects_API.c:182
#2 0x599779810891 in zend_object_release Zend/zend_objects_API.h:77
#3 0x599779810891 in autoload_func_info_destroy ext/spl/php_spl.c:359
#4 0x599779810a56 in autoload_func_info_zval_dtor ext/spl/php_spl.c:374
#5 0x599779f13574 in zend_hash_clean Zend/zend_hash.c:2124
#6 0x59977981198c in zif_spl_autoload_unregister ext/spl/php_spl.c:590
0x504000039150 is located 0 bytes inside of 40-byte region [0x504000039150,0x504000039178)
freed by thread T0 here:
#1 0x599779c23c16 in __zend_free Zend/zend_alloc.c:3976
#2 0x599779c325db in _efree Zend/zend_alloc.c:2928
#3 0x599779fac8e3 in zend_objects_store_del Zend/zend_objects_API.c:200
#4 0x599779fadad9 in zend_object_release Zend/zend_objects_API.h:77
#5 0x599779fadad9 in zend_objects_destroy_object Zend/zend_objects.c:185
#6 0x599779fac9da in zend_objects_store_del Zend/zend_objects_API.c:181
#7 0x599779810891 in zend_object_release Zend/zend_objects_API.h:77
#8 0x599779810891 in autoload_func_info_destroy ext/spl/php_spl.c:359
#9 0x599779810a56 in autoload_func_info_zval_dtor ext/spl/php_spl.c:374
#10 0x599779f13574 in zend_hash_clean Zend/zend_hash.c:2124
#11 0x59977981198c in zif_spl_autoload_unregister ext/spl/php_spl.c:590
previously allocated by thread T0 here:
#1 0x599779c24dff in __zend_malloc Zend/zend_alloc.c:3947
#2 0x599779c32343 in _emalloc Zend/zend_alloc.c:2918
#3 0x599779fadbc5 in zend_objects_new Zend/zend_objects.c:191
#4 0x599779c41460 in _object_and_properties_init Zend/zend_API.c:1854
#5 0x599779c41460 in object_init_ex Zend/zend_API.c:1877
#6 0x599779db8cfc in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER Zend/zend_vm_execute.h:11316
SUMMARY: AddressSanitizer: heap-use-after-free Zend/zend_types.h:1395 in zend_gc_delref
(The AddressSanitizer/libc frames, the frames below zif_spl_autoload_unregister() and the shadow bytes are omitted; paths are relative to php-src.)
Without AddressSanitizer the script prints the notice and survived and exits with code 0, but the allocator state is already damaged (see "Non-ASan builds" below).
But I expected this output instead:
Deprecated: spl_autoload_unregister(): Using spl_autoload_call() as a callback for spl_autoload_unregister() is deprecated, to remove all registered autoloaders, call spl_autoload_unregister() for all values returned from spl_autoload_functions() in poc.php on line 16
survived
without any use-after-free, and with a heap that is still intact afterwards.
Root cause
zend_hash_clean()(Zend/zend_hash.c:2091) calls the destructor of every bucket (:2124) without clearing the bucket first._zend_hash_del_el_ex()does it the other way round: it sets the slot toIS_UNDEF(:1708) before it calls the destructor (:1709).autoload_func_info_destroy()(ext/spl/php_spl.c:357) releases the object (:359). Its refcount drops to 0 andzend_objects_store_del()(Zend/zend_objects_API.c:162) sets the refcount to 1 (:180) and calls the destructor (:181).- The destructor calls
spl_autoload_unregister([$this, 'autoload']).spl_find_registered_function()(:481-493) iteratesspl_autoload_functionsand returns the bucket that is being cleaned.zend_hash_del_bucket()(:606) callsautoload_func_info_zval_dtor()(:372) for it, soautoload_func_info_destroy()runs a second time for the same struct: it decrements the object's refcount and frees the struct (:369). - The destructor returns.
zend_objects_destroy_object()drops its own reference (Zend/zend_objects.c:185) and the object is freed (Zend/zend_objects_API.c:200). - The first
zend_objects_store_del()continues withGC_DELREF(object)(:182) on the freed object. This is the access reported above.GC_DELREFis a read-modify-write, so in a plain build it writes into the freed block. - The first
autoload_func_info_destroy()continues with the freed struct: it readsalfi->func_ptr(:361-362) and frees the struct a second time (:369).
gdb on a plain build confirms this order: struct freed from zend_hash_del_bucket() ← ext/spl/php_spl.c:606, object freed from zend_objects_destroy_object() ← :185, GC_DELREF on the freed object from Zend/zend_objects_API.c:182, struct freed again from zend_hash_clean() ← ext/spl/php_spl.c:590.
Non-ASan builds
Default allocator. The PoC prints survived, but two free lists are damaged: the freed object block is the head of the 40-byte list and its link was modified in step 5, and the freed struct is in the 32-byte list twice. The ZendMM of PHP 8.5 keeps an obfuscated copy of every link and compares it when a block is taken (Zend/zend_alloc.c:1450-1460), so the damage is detected at the next allocation. One more line after the PoC is enough:
$t = str_repeat('x', 15); // 39 bytes: the size class of the freed object
echo "survived\n";$ sapi/cli/php poc.php
zend_mm_heap corrupted
Aborted (exit status 134)
30/30 runs. The PHP-8.3 branch does not have this protection (the 8.4 and 8.5 branches do). In an emulation without it (8.5.12 built with -DZEND_MM_HEAP_PROTECTION=0, plus a small change to make that configuration link) the PoC also prints survived, and a script that then allocates two 31-byte strings crashes with SIGSEGV (3/3 runs). I did not run the PoC on 8.3 itself.
libc allocator (USE_ZEND_ALLOC=0). The PoC itself crashes with SIGSEGV (exit status 139, 30/30 runs) in autoload_func_info_destroy() at ext/spl/php_spl.c:361: after the first free, alfi->func_ptr is whatever the allocator stored in the block.
Variants
The destructor does not have to unregister itself. If it removes another autoloader that the table owns, the clean loop destroys that already-removed entry a second time. The same happens with the common "remove all autoloaders" pattern foreach (spl_autoload_functions() as $f) spl_autoload_unregister($f); in a destructor.
<?php
// The destructor of the first autoloader removes the second one while the table is being cleaned.
class B {
public function autoload($c) {}
}
class A {
public $b;
public function autoload($c) {}
public function __destruct() {
spl_autoload_unregister([$this->b, 'autoload']);
}
}
$b = new B();
$a = new A();
$a->b = $b;
spl_autoload_register([$a, 'autoload']);
spl_autoload_register([$b, 'autoload']);
unset($a, $b);
error_reporting(E_ALL & ~E_DEPRECATED);
spl_autoload_unregister('spl_autoload_call');
$x = str_repeat('x', 7); // 31 bytes: the size class of the freed autoloader info struct
$y = str_repeat('y', 7);
echo "survived\n";ERROR: AddressSanitizer: heap-use-after-free on address 0x5030000186a8 at pc 0x58fc8f610888 bp 0x7ffd2d93c140 sp 0x7ffd2d93c130
READ of size 8 at 0x5030000186a8 thread T0
#0 0x58fc8f610887 in autoload_func_info_destroy ext/spl/php_spl.c:358
#1 0x58fc8f610a56 in autoload_func_info_zval_dtor ext/spl/php_spl.c:374
#2 0x58fc8fd13574 in zend_hash_clean Zend/zend_hash.c:2124
#3 0x58fc8f61198c in zif_spl_autoload_unregister ext/spl/php_spl.c:590
0x5030000186a8 is located 8 bytes inside of 32-byte region [0x5030000186a0,0x5030000186c0)
freed by thread T0 here:
#1 0x58fc8fa23c16 in __zend_free Zend/zend_alloc.c:3976
#2 0x58fc8fa325db in _efree Zend/zend_alloc.c:2928
#3 0x58fc8f61087c in autoload_func_info_destroy ext/spl/php_spl.c:369
#4 0x58fc8f610a56 in autoload_func_info_zval_dtor ext/spl/php_spl.c:374
#5 0x58fc8fd0e74e in _zend_hash_del_el_ex Zend/zend_hash.c:1709
#6 0x58fc8fd0e74e in _zend_hash_del_el Zend/zend_hash.c:1736
#7 0x58fc8fd0e74e in zend_hash_del_bucket Zend/zend_hash.c:1753
#8 0x58fc8f6119f1 in zif_spl_autoload_unregister ext/spl/php_spl.c:606
previously allocated by thread T0 here:
#1 0x58fc8fa24dff in __zend_malloc Zend/zend_alloc.c:3947
#2 0x58fc8fa2cfb4 in _emalloc_32 Zend/zend_alloc.c:2840
#3 0x58fc8f60e579 in autoload_func_info_from_fci ext/spl/php_spl.c:379
#4 0x58fc8f610e8d in zif_spl_autoload_register ext/spl/php_spl.c:538
SUMMARY: AddressSanitizer: heap-use-after-free ext/spl/php_spl.c:358 in autoload_func_info_destroy
On a plain build with the default allocator this script aborts with zend_mm_heap corrupted at the str_repeat() calls (30/30 runs).
Other shapes that give the same use-after-free (20/20 each under ASan):
- A closure created inside a method as autoloader: releasing the closure (
ext/spl/php_spl.c:366-368) drops$thisand runs the destructor. - An autoloader method that does not exist and is served by
__call(): the trampoline is released twice as well.
Not affected:
- Request shutdown. The engine calls the destructors before SPL's RSHUTDOWN empties the table (
ext/spl/php_spl.c:753), so the table is already clean by then (20/20). - An autoloader object that is still referenced elsewhere, a destructor that does not touch the table, plain function names (20/20 each).
Side effect. spl_autoload_register() from such a destructor adds the new autoloader to the table that is being cleaned; it is then dropped without being destroyed. LeakSanitizer reports the 32-byte struct as leaked and spl_autoload_functions() returns an empty array.
PHP Version
PHP 8.5.12 (cli) (built: Nov 15 2026 15:05:06) (NTS)
Operating System
Ubuntu 24.04.5 LTS, Linux 7.0.0-38-generic x86_64
Activity
Girgias commentedon Nov 26, 2026
Won't fix, this is a deprecated (and frankly unused) feature for a reason and messing around with destructors requires a global solution see: #20001