Repository navigation
Fix use-after-free in Context.get_cert_store() - #1549
Open
qianeric-backup wants to merge 2 commits into
Open
qianeric-backup wants to merge 2 commits into
qianeric-backup wants to merge 2 commits into
Conversation
get_cert_store returns the X509_STORE* owned by the context, but the X509Store it hands back does not keep the context alive, so dropping the Context frees the store and leaves the Python object pointing at freed memory. add_cert on such a store writes into another context's store, or hangs on the freed lock. Keep the context alive from the store, like X509._get_name keeps its X509 alive for the X509Name it returns.
qianeric-backup
marked this pull request as ready for review
November 30, 2026 10:22
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.
Context.get_cert_store()returns theX509_STORE *that the context owns, andX509Store.__new__()wraps that borrowed pointer without holding on to the context:Dropping the last reference to the
Contextfrees theSSL_CTXand with it the store, so theX509Storeis left pointing at freed memory. The docstring recommends exactly this call ("can be used to add trusted certificates without usingload_verify_locations"), which makes it an easy one to hit:Depending on what the allocator hands out next,
add_certeither writes into another live context's store or spins forever on the freed store's lock - measured 20s with no return, against 3us once the context stays alive.Change
Keep the context alive from the store, the way
X509._get_name()already keeps itsX509alive for theX509Nameit returns (the only other borrowed handle in the package):X509Storegets an_ownerattribute defaulted toNonefor the type checker.Test
tests/test_ssl.py::TestContext::test_get_cert_store_keeps_context_alivedrops every reference to the context and asserts, through aweakref, that it is still alive while the store is. It fails on the unpatched tree.