Skip to content

Commit 542f327

Browse files
[3.12] gh-114572: Fix locking in cert_store_stats and get_ca_certs (GH-114573) (GH-115547)
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 <davidben@google.com>
1 parent d4a1c8e commit 542f327

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed
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

+60-5
Original file line numberDiff line numberDiff line change
@@ -4520,6 +4520,50 @@ set_sni_callback(PySSLContext *self, PyObject *arg, void *c)
45204520
return 0;
45214521
}
45224522

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

45514595
store = SSL_CTX_get_cert_store(self->ctx);
4552-
objs = X509_STORE_get0_objects(store);
4596+
objs = X509_STORE_get1_objects(store);
4597+
if (objs == NULL) {
4598+
PyErr_SetString(PyExc_MemoryError, "failed to query cert store");
4599+
return NULL;
4600+
}
4601+
45534602
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
45544603
obj = sk_X509_OBJECT_value(objs, i);
45554604
switch (X509_OBJECT_get_type(obj)) {
@@ -4563,12 +4612,11 @@ _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self)
45634612
crl++;
45644613
break;
45654614
default:
4566-
/* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY.
4567-
* As far as I can tell they are internal states and never
4568-
* stored in a cert store */
4615+
/* Ignore unrecognized types. */
45694616
break;
45704617
}
45714618
}
4619+
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
45724620
return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl,
45734621
"x509_ca", ca);
45744622
}
@@ -4600,7 +4648,12 @@ _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form)
46004648
}
46014649

46024650
store = SSL_CTX_get_cert_store(self->ctx);
4603-
objs = X509_STORE_get0_objects(store);
4651+
objs = X509_STORE_get1_objects(store);
4652+
if (objs == NULL) {
4653+
PyErr_SetString(PyExc_MemoryError, "failed to query cert store");
4654+
goto error;
4655+
}
4656+
46044657
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
46054658
X509_OBJECT *obj;
46064659
X509 *cert;
@@ -4628,9 +4681,11 @@ _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form)
46284681
}
46294682
Py_CLEAR(ci);
46304683
}
4684+
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
46314685
return rlist;
46324686

46334687
error:
4688+
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
46344689
Py_XDECREF(ci);
46354690
Py_XDECREF(rlist);
46364691
return NULL;

0 commit comments

Comments
 (0)