Repository navigation
Fix GH-25723: Avoid UAF and double-free in spl_autoload_unregister('spl_autoload_call') - #25783
Closed
prateekbhujel wants to merge 1 commit into
Closed
prateekbhujel wants to merge 1 commit into
prateekbhujel wants to merge 1 commit into
Conversation
…('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
Contributor
Author
|
Missed the existing cross-referenced PR on iliaal#477. Closing in favor of that one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
spl_autoload_unregister('spl_autoload_call')previously calledzend_hash_clean(spl_autoload_functions)directly. Becausezend_hash_clean()invokes element destructors before clearing buckets or resettingnNumUsed, an autoloader object whose__destruct()re-entersspl_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 ofautoload_func_info. Re-enteringspl_autoload_register()from such a destructor also silently dropped and leaked the newly registered loader.Fix
Move the contents of
spl_autoload_functionsinto a stack-localHashTable tmp, re-initializespl_autoload_functionsin place (zend_hash_init()+zend_hash_real_init_mixed()), and then callzend_hash_destroy(&tmp):spl_autoload_functionspointer intact for any activespl_perform_autoload()iteration (nNumUsed == 0, so iteration terminates cleanly).spl_autoload_unregister()returnsfalse, re-entrantspl_autoload_unregister('spl_autoload_call')is a no-op, re-entrantspl_autoload_register()persists without leaking, and exceptions thrown in__destruct()still finish destroying remaining detached entries cleanly.Merge-up note for
PHP-8.6/masterIn
PHP-8.6, autoloader management moved toZend/zend_autoload.c, wherezend_autoload_clean_class_loaders()has the identicalzend_hash_clean(zend_class_autoload_functions)call. Applying the same detach-and-destroy pattern there (re-initializing withzend_autoload_callback_zval_destroy) will makeext/spl/tests/gh25723.phptpass onPHP-8.6andmastertoo.Closes GH-25723.