Skip to content

Commit

Permalink
x509store: explicitly call rb_gc_mark() against Store/StoreContext
Browse files Browse the repository at this point in the history
We store the reverse reference to the Ruby object in the OpenSSL
struct for use from OpenSSL callback functions. To prevent the Ruby
object from being relocated by GC.compact, we must "pin" it by calling
rb_gc_mark().
  • Loading branch information
rhenium committed Oct 14, 2021
1 parent 1e6bcc5 commit 1db0fb3
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions ext/openssl/ossl_x509store.c
Expand Up @@ -105,6 +105,12 @@ VALUE cX509Store;
VALUE cX509StoreContext;
VALUE eX509StoreError;

static void
ossl_x509store_mark(void *ptr)
{
rb_gc_mark((VALUE)X509_STORE_get_ex_data(ptr, store_ex_verify_cb_idx));
}

static void
ossl_x509store_free(void *ptr)
{
Expand All @@ -114,7 +120,7 @@ ossl_x509store_free(void *ptr)
static const rb_data_type_t ossl_x509store_type = {
"OpenSSL/X509/STORE",
{
0, ossl_x509store_free,
ossl_x509store_mark, ossl_x509store_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};
Expand Down Expand Up @@ -456,23 +462,15 @@ ossl_x509store_verify(int argc, VALUE *argv, VALUE self)
return result;
}

/*
* Public Functions
*/
static void ossl_x509stctx_free(void*);


static const rb_data_type_t ossl_x509stctx_type = {
"OpenSSL/X509/STORE_CTX",
{
0, ossl_x509stctx_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};

/*
* Private functions
*/
static void
ossl_x509stctx_mark(void *ptr)
{
rb_gc_mark((VALUE)X509_STORE_CTX_get_ex_data(ptr, stctx_ex_verify_cb_idx));
}

static void
ossl_x509stctx_free(void *ptr)
{
Expand All @@ -484,6 +482,14 @@ ossl_x509stctx_free(void *ptr)
X509_STORE_CTX_free(ctx);
}

static const rb_data_type_t ossl_x509stctx_type = {
"OpenSSL/X509/STORE_CTX",
{
ossl_x509stctx_mark, ossl_x509stctx_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};

static VALUE
ossl_x509stctx_alloc(VALUE klass)
{
Expand Down

0 comments on commit 1db0fb3

Please sign in to comment.