Skip to content

Commit 01c37f1

Browse files
[3.11] gh-114572: Fix locking in cert_store_stats and get_ca_certs (GH-114573) (#115549)
gh-114572: Fix locking in cert_store_stats and get_ca_certs (GH-114573) * gh-114572: Fix locking in cert_store_stats and get_ca_certs cert_store_stats and get_ca_certs query the SSLContext's X509_STORE with X509_STORE_get0_objects, but reading the result requires a lock. See openssl/openssl#23224 for details. Instead, use X509_STORE_get1_objects, newly added in that PR. X509_STORE_get1_objects does not exist in current OpenSSLs, but we can polyfill it with X509_STORE_lock and X509_STORE_unlock. * Work around const-correctness problem * Add missing X509_STORE_get1_objects failure check * Add blurb (cherry picked from commit bce6931) Co-authored-by: David Benjamin <[email protected]>
1 parent 5a0d51b commit 01c37f1

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:meth:`ssl.SSLContext.cert_store_stats` and
2+
:meth:`ssl.SSLContext.get_ca_certs` now correctly lock access to the
3+
certificate store, when the :class:`ssl.SSLContext` is shared across
4+
multiple threads.

Modules/_ssl.c

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4529,6 +4529,50 @@ set_sni_callback(PySSLContext *self, PyObject *arg, void *c)
45294529
return 0;
45304530
}
45314531

4532+
#if OPENSSL_VERSION_NUMBER < 0x30300000L
4533+
static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj)
4534+
{
4535+
int ok;
4536+
X509_OBJECT *ret = X509_OBJECT_new();
4537+
if (ret == NULL) {
4538+
return NULL;
4539+
}
4540+
switch (X509_OBJECT_get_type(obj)) {
4541+
case X509_LU_X509:
4542+
ok = X509_OBJECT_set1_X509(ret, X509_OBJECT_get0_X509(obj));
4543+
break;
4544+
case X509_LU_CRL:
4545+
/* X509_OBJECT_get0_X509_CRL was not const-correct prior to 3.0.*/
4546+
ok = X509_OBJECT_set1_X509_CRL(
4547+
ret, X509_OBJECT_get0_X509_CRL((X509_OBJECT *)obj));
4548+
break;
4549+
default:
4550+
/* We cannot duplicate unrecognized types in a polyfill, but it is
4551+
* safe to leave an empty object. The caller will ignore it. */
4552+
ok = 1;
4553+
break;
4554+
}
4555+
if (!ok) {
4556+
X509_OBJECT_free(ret);
4557+
return NULL;
4558+
}
4559+
return ret;
4560+
}
4561+
4562+
static STACK_OF(X509_OBJECT) *
4563+
X509_STORE_get1_objects(X509_STORE *store)
4564+
{
4565+
STACK_OF(X509_OBJECT) *ret;
4566+
if (!X509_STORE_lock(store)) {
4567+
return NULL;
4568+
}
4569+
ret = sk_X509_OBJECT_deep_copy(X509_STORE_get0_objects(store),
4570+
x509_object_dup, X509_OBJECT_free);
4571+
X509_STORE_unlock(store);
4572+
return ret;
4573+
}
4574+
#endif
4575+
45324576
PyDoc_STRVAR(PySSLContext_sni_callback_doc,
45334577
"Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\
45344578
\n\
@@ -4558,7 +4602,12 @@ _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self)
45584602
int x509 = 0, crl = 0, ca = 0, i;
45594603

45604604
store = SSL_CTX_get_cert_store(self->ctx);
4561-
objs = X509_STORE_get0_objects(store);
4605+
objs = X509_STORE_get1_objects(store);
4606+
if (objs == NULL) {
4607+
PyErr_SetString(PyExc_MemoryError, "failed to query cert store");
4608+
return NULL;
4609+
}
4610+
45624611
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
45634612
obj = sk_X509_OBJECT_value(objs, i);
45644613
switch (X509_OBJECT_get_type(obj)) {
@@ -4572,12 +4621,11 @@ _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self)
45724621
crl++;
45734622
break;
45744623
default:
4575-
/* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY.
4576-
* As far as I can tell they are internal states and never
4577-
* stored in a cert store */
4624+
/* Ignore unrecognized types. */
45784625
break;
45794626
}
45804627
}
4628+
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
45814629
return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl,
45824630
"x509_ca", ca);
45834631
}
@@ -4609,7 +4657,12 @@ _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form)
46094657
}
46104658

46114659
store = SSL_CTX_get_cert_store(self->ctx);
4612-
objs = X509_STORE_get0_objects(store);
4660+
objs = X509_STORE_get1_objects(store);
4661+
if (objs == NULL) {
4662+
PyErr_SetString(PyExc_MemoryError, "failed to query cert store");
4663+
goto error;
4664+
}
4665+
46134666
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
46144667
X509_OBJECT *obj;
46154668
X509 *cert;
@@ -4637,9 +4690,11 @@ _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form)
46374690
}
46384691
Py_CLEAR(ci);
46394692
}
4693+
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
46404694
return rlist;
46414695

46424696
error:
4697+
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
46434698
Py_XDECREF(ci);
46444699
Py_XDECREF(rlist);
46454700
return NULL;

0 commit comments

Comments
 (0)