Repository navigation
Conversation
ossl_x509stctx_get_chain does not use self after taking the X509_STORE_CTX, but the chain it converts with ossl_x509_sk2ary() is owned by the X509_STORE_CTX. ossl_x509_sk2ary() allocates Ruby objects, and GC there frees a StoreContext referred to from nowhere else together with its chain at once. X509::Store#verify creates a new StoreContext and passes it to ossl_x509stctx_get_chain as its last use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AEsEHM5UTTcBF5r2WrZmJF
Member
|
Thanks for the catch! However, I think the bug is in Also, can you drop the Claude-Session trailer from the commit message, since (I think) it's only viewable by you and not useful for the rest of us? |
This branch has not been deployed
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.
ossl_x509stctx_get_chaintakes theX509_STORE_CTXfromselfand converts the chain returned byX509_STORE_CTX_get0_chain()withossl_x509_sk2ary(). The chain is owned by theX509_STORE_CTX, butselfis not used after theX509_STORE_CTXis taken, so nothing keeps the StoreContext alive while the array is built.ossl_x509_sk2ary()allocates Ruby objects for each certificate, so GC can run in the middle of the loop.OpenSSL::X509::Store#verifycreates a new StoreContext withStoreContext.newand passes it toossl_x509stctx_get_chainas its last use; the StoreContext is referred to from nowhere else. If GC collects it there,ossl_x509stctx_freeruns at once (the type isRUBY_TYPED_THREAD_SAFE_FREE) and frees theX509_STORE_CTXtogether with the chain, and the loop keeps reading the freed stack.In a clang -O3 build, gdb showed that at the call to
ossl_x509_new()duringStore#verify, the only reference to the StoreContext was a callee-saved register thatossl_x509_sk2ary()had pushed in its prologue, holding a value thatossl_x509store_verifyno longer uses. It survives only by that accident of register allocation, which other compilers or optimization levels need not repeat.This adds
RB_GC_GUARD(self)afterossl_x509_sk2ary(), so that the StoreContext stays alive until the conversion is done. This coversStoreContext#chainand every C caller.I have not added a test, since GC does not collect the StoreContext before this change either while that register keeps it alive, so a test would not fail without the fix. The existing tests pass.