Skip to content

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

Open
jinroq wants to merge 1 commit into
ruby:masterfrom
jinroq:gc-guard-x509stctx_get_chain
Open

jinroq wants to merge 1 commit into
ruby:masterfrom
jinroq:gc-guard-x509stctx_get_chain

Conversation

@jinroq

@jinroq jinroq commented Dec 1, 2026

Copy link
Copy Markdown

ossl_x509stctx_get_chain takes the X509_STORE_CTX from self and converts the chain returned by X509_STORE_CTX_get0_chain() with ossl_x509_sk2ary(). The chain is owned by the X509_STORE_CTX, but self is not used after the X509_STORE_CTX is 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#verify creates a new StoreContext with StoreContext.new and passes it to ossl_x509stctx_get_chain as its last use; the StoreContext is referred to from nowhere else. If GC collects it 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(self) after ossl_x509_sk2ary(), so that the StoreContext stays alive until the conversion is done. This covers StoreContext#chain and 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.

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
@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?

This branch has not been deployed

No deployments
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