Skip to content

Fix GH-25723: Avoid UAF and double-free in spl_autoload_unregister('spl_autoload_call') - #25783

Closed
prateekbhujel wants to merge 1 commit into
php:PHP-8.4from
prateekbhujel:fix/gh25723-spl-autoload-unregister-uaf
Closed

prateekbhujel wants to merge 1 commit into
php:PHP-8.4from
prateekbhujel:fix/gh25723-spl-autoload-unregister-uaf

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

spl_autoload_unregister('spl_autoload_call') previously called zend_hash_clean(spl_autoload_functions) directly. Because zend_hash_clean() invokes element destructors before clearing buckets or resetting nNumUsed, an autoloader object whose __destruct() re-enters spl_autoload_unregister() (on itself or on another autoloader still in the table) finds and deletes the bucket a second time, resulting in a use-after-free of the object and double-free of autoload_func_info. Re-entering spl_autoload_register() from such a destructor also silently dropped and leaked the newly registered loader.

Fix

Move the contents of spl_autoload_functions into a stack-local HashTable tmp, re-initialize spl_autoload_functions in place (zend_hash_init() + zend_hash_real_init_mixed()), and then call zend_hash_destroy(&tmp):

  • Keeps the spl_autoload_functions pointer intact for any active spl_perform_autoload() iteration (nNumUsed == 0, so iteration terminates cleanly).
  • Ensures destructors see an already-empty table: re-entrant spl_autoload_unregister() returns false, re-entrant spl_autoload_unregister('spl_autoload_call') is a no-op, re-entrant spl_autoload_register() persists without leaking, and exceptions thrown in __destruct() still finish destroying remaining detached entries cleanly.

Merge-up note for PHP-8.6 / master

In PHP-8.6, autoloader management moved to Zend/zend_autoload.c, where zend_autoload_clean_class_loaders() has the identical zend_hash_clean(zend_class_autoload_functions) call. Applying the same detach-and-destroy pattern there (re-initializing with zend_autoload_callback_zval_destroy) will make ext/spl/tests/gh25723.phpt pass on PHP-8.6 and master too.

Closes GH-25723.

…('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 phpGH-25723

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it should instead require a global solution as in: #20001

Second of all this behaviour is deprecated so I'm not sure it makes sense to fix at all and nobody knows about it.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Missed the existing cross-referenced PR on iliaal#477. Closing in favor of that one.

@prateekbhujel
prateekbhujel deleted the fix/gh25723-spl-autoload-unregister-uaf branch November 26, 2026 13:49
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