Repository navigation
x509store: keep the StoreContext alive in StoreContext#chain - #1173
Merged
Merged
Conversation
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? |
X509::Store#verify creates a new StoreContext and calls ossl_x509stctx_get_chain() on it as its last use. The chain that function converts with ossl_x509_sk2ary() is owned by the X509_STORE_CTX, and ossl_x509_sk2ary() allocates Ruby objects. GC there may free the StoreContext, referred to from nowhere else, together with its chain at once. Keep the StoreContext alive until the chain has been converted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jinroq
force-pushed
the
gc-guard-x509stctx_get_chain
branch
from
December 1, 2026 08:50
5747417 to
ad3cf7a
Compare
Contributor
Author
|
@rhenium |
jinroq
added a commit
to jinroq/gc-guard-checker
that referenced
this pull request
Dec 3, 2026
ruby/openssl does not set the memory functions of OpenSSL and registers ex_data indices without callbacks, so OpenSSL runs Ruby code only through the callbacks it sets. Accessors, *_up_ref, *_dup, *_cmp, BN (but the generation and tests of primes), ASN1_STRING, EC_POINT and the setters of contexts are taken as not triggering GC, when they have no body. The pointer an OpenSSL *_get0_* function returns is taken as into what its argument points to, so that a pointer into the context, given to a function allocating, is still reported (ruby/openssl#1173). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AEsEHM5UTTcBF5r2WrZmJF
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.
OpenSSL::X509::Store#verify(ossl_x509store_verify) creates a new StoreContext withStoreContext.newand, after verification, callsossl_x509stctx_get_chain()on it to set@chain. That call is the last use of the StoreContext, and the object is referred to from nowhere else.ossl_x509stctx_get_chain()converts the chain returned byX509_STORE_CTX_get0_chain()withossl_x509_sk2ary(). The chain is owned by theX509_STORE_CTX, andossl_x509_sk2ary()allocates Ruby objects for each certificate, so GC can run in the middle of the loop. If GC collects the StoreContext 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(ctx)inossl_x509store_verifyafter the chain is converted, so that the StoreContext it created stays alive until then. With the change, gdb shows the StoreContext held in the frame ofossl_x509store_verifyitself.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.