Skip to content

x509store: keep the StoreContext alive in StoreContext#chain - #1173

Merged
rhenium merged 1 commit into
ruby:masterfrom
jinroq:gc-guard-x509stctx_get_chain
Dec 1, 2026
Merged

rhenium merged 1 commit into
ruby:masterfrom
jinroq:gc-guard-x509stctx_get_chain

Conversation

@jinroq

@jinroq jinroq commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

OpenSSL::X509::Store#verify (ossl_x509store_verify) creates a new StoreContext with StoreContext.new and, after verification, calls ossl_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 by X509_STORE_CTX_get0_chain() with ossl_x509_sk2ary(). The chain is owned by the X509_STORE_CTX, and ossl_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_free runs at once (the type is RUBY_TYPED_THREAD_SAFE_FREE) and frees the X509_STORE_CTX together 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() during Store#verify, the only reference to the StoreContext was a callee-saved register that ossl_x509_sk2ary() had pushed in its prologue, holding a value that ossl_x509store_verify no 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) in ossl_x509store_verify after 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 of ossl_x509store_verify itself.

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.

@rhenium

rhenium commented Dec 1, 2026

Copy link
Copy Markdown
Member

Thanks for the catch! However, I think the bug is in X509::Store#verify, which should have ensured the object it created remained alive.

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
jinroq force-pushed the gc-guard-x509stctx_get_chain branch from 5747417 to ad3cf7a Compare December 1, 2026 08:50
@jinroq

jinroq commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@rhenium
Thanks for the review! I moved the guard to X509::Store#verify, which keeps the StoreContext it created alive until the chain is converted, and dropped the Claude-Session trailer from the commit message.

@rhenium rhenium left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@rhenium
rhenium merged commit 552b765 into ruby:master Dec 1, 2026
48 checks passed
@jinroq
jinroq deleted the gc-guard-x509stctx_get_chain branch December 1, 2026 10:24
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants