Repository navigation
x509store: keep reference to Store in StoreContext - #1170
Merged
Merged
Conversation
X509_STORE_CTX_init() stores a raw pointer to the underlying X509_STORE without incrementing its reference count. If the Ruby OpenSSL::X509::Store object passed to OpenSSL::X509::StoreContext.new is garbage collected while the StoreContext is still live, subsequent operations such as StoreContext#verify dereference the freed X509_STORE. Store a hidden internal instance variable reference to the Store object on the StoreContext after X509_STORE_CTX_init() succeeds so that the Store (and its underlying X509_STORE) remains alive for the lifetime of the StoreContext.
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.
Summary
OpenSSL::X509::StoreContext#initializepasses theX509_STORE *from the givenOpenSSL::X509::StoretoX509_STORE_CTX_init(), which stores a raw pointer without incrementing theX509_STOREreference count. BecauseStoreContextdid not retain a Ruby reference to theStoreobject, garbage collection ofStorewhileStoreContextis still alive freed the underlyingX509_STORE, causing a use-after-free on subsequent calls such asStoreContext#verify.Fix
id_store = rb_intern_const("store")) referencingstoreonStoreContextafterX509_STORE_CTX_init()succeeds inext/openssl/ossl_x509store.c, matching the existing hidden-ivar retention pattern used elsewhere inruby/openssl(e.g.,id_original_contextinossl_ssl.c).OpenSSL::X509::Storeobject alive ensures both the underlyingX509_STOREand itsdmark/dcompacthooks remain valid for the lifetime of theStoreContext, without exposing a new visible instance variable onStoreContext.Verification
test_store_context_gcintest/openssl/test_x509store.rb:master1ea17b55):test_store_context_gcfails (OpenSSL::X509::Storeis collected afterGC.start), and runningctx.verifyunderMallocScribble=1terminates withSIGSEGV.RUBY_OPENSSL_EXTCFLAGS=-Werror;test/openssl/test_x509store.rb(13 tests, 110 assertions, including underMallocScribble=1) andtest/openssl/test_x509*.rb(91 tests, 683 assertions) pass.