Skip to content

gh-163188: Fix use-after-free in zoneinfo clear_cache() re-entered from a key's __del__ - #163218

Open
abedalawieh wants to merge 1 commit into
python:mainfrom
abedalawieh:gh-163188-zoneinfo-clear-cache-reentrant
Open

abedalawieh wants to merge 1 commit into
python:mainfrom
abedalawieh:gh-163188-zoneinfo-clear-cache-reentrant

Conversation

@abedalawieh

Copy link
Copy Markdown

clear_strong_cache() freed the strong cache's linked list while state->ZONEINFO_STRONG_CACHE still pointed at it. Releasing a node's key or zone can run arbitrary code. If a str subclass key's __del__ calls ZoneInfo.clear_cache(), the nested call frees the same nodes again, and the outer loop then reads the freed nodes.

The fix detaches the list from the module state before freeing it, so a re-entrant call only sees an empty cache. The other paths that free nodes (eject_from_strong_cache() and the eviction in update_strong_cache()) already unlink the nodes before releasing them.

The new test in test_zoneinfo (it runs for both the C and Python implementations) checks that __del__ ran during clear_cache(). Without the fix, a --with-pydebug --with-address-sanitizer build aborts on a negative reference count. With the fix, test_zoneinfo passes on that build.

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.

1 participant