Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] gh-114572: Fix locking in cert_store_stats and get_ca_certs (GH-114573) #115549

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`ssl.SSLContext.cert_store_stats` and
:meth:`ssl.SSLContext.get_ca_certs` now correctly lock access to the
certificate store, when the :class:`ssl.SSLContext` is shared across
multiple threads.
65 changes: 60 additions & 5 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3621,7 +3621,7 @@
static PyObject *
get_options(PySSLContext *self, void *c)
{
return PyLong_FromLong(SSL_CTX_get_options(self->ctx));

Check warning on line 3624 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'function': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]

Check warning on line 3624 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'function': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]

Check warning on line 3624 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'function': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]

Check warning on line 3624 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'function': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]
}

static int
Expand All @@ -3635,7 +3635,7 @@

if (!PyArg_Parse(arg, "l", &new_opts))
return -1;
opts = SSL_CTX_get_options(self->ctx);

Check warning on line 3638 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'=': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]

Check warning on line 3638 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'=': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]

Check warning on line 3638 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'=': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]

Check warning on line 3638 in Modules/_ssl.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'=': conversion from 'uint64_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\_ssl.vcxproj]
clear = opts & ~new_opts;
set = ~opts & new_opts;

Expand Down Expand Up @@ -4529,6 +4529,50 @@
return 0;
}

#if OPENSSL_VERSION_NUMBER < 0x30300000L
static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj)
{
int ok;
X509_OBJECT *ret = X509_OBJECT_new();
if (ret == NULL) {
return NULL;
}
switch (X509_OBJECT_get_type(obj)) {
case X509_LU_X509:
ok = X509_OBJECT_set1_X509(ret, X509_OBJECT_get0_X509(obj));
break;
case X509_LU_CRL:
/* X509_OBJECT_get0_X509_CRL was not const-correct prior to 3.0.*/
ok = X509_OBJECT_set1_X509_CRL(
ret, X509_OBJECT_get0_X509_CRL((X509_OBJECT *)obj));
break;
default:
/* We cannot duplicate unrecognized types in a polyfill, but it is
* safe to leave an empty object. The caller will ignore it. */
ok = 1;
break;
}
if (!ok) {
X509_OBJECT_free(ret);
return NULL;
}
return ret;
}

static STACK_OF(X509_OBJECT) *
X509_STORE_get1_objects(X509_STORE *store)
{
STACK_OF(X509_OBJECT) *ret;
if (!X509_STORE_lock(store)) {
return NULL;
}
ret = sk_X509_OBJECT_deep_copy(X509_STORE_get0_objects(store),
x509_object_dup, X509_OBJECT_free);
X509_STORE_unlock(store);
return ret;
}
#endif

PyDoc_STRVAR(PySSLContext_sni_callback_doc,
"Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\
\n\
Expand Down Expand Up @@ -4558,7 +4602,12 @@
int x509 = 0, crl = 0, ca = 0, i;

store = SSL_CTX_get_cert_store(self->ctx);
objs = X509_STORE_get0_objects(store);
objs = X509_STORE_get1_objects(store);
if (objs == NULL) {
PyErr_SetString(PyExc_MemoryError, "failed to query cert store");
return NULL;
}

for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
obj = sk_X509_OBJECT_value(objs, i);
switch (X509_OBJECT_get_type(obj)) {
Expand All @@ -4572,12 +4621,11 @@
crl++;
break;
default:
/* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY.
* As far as I can tell they are internal states and never
* stored in a cert store */
/* Ignore unrecognized types. */
break;
}
}
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl,
"x509_ca", ca);
}
Expand Down Expand Up @@ -4609,7 +4657,12 @@
}

store = SSL_CTX_get_cert_store(self->ctx);
objs = X509_STORE_get0_objects(store);
objs = X509_STORE_get1_objects(store);
if (objs == NULL) {
PyErr_SetString(PyExc_MemoryError, "failed to query cert store");
goto error;
}

for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
X509_OBJECT *obj;
X509 *cert;
Expand Down Expand Up @@ -4637,9 +4690,11 @@
}
Py_CLEAR(ci);
}
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
return rlist;

error:
sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
Py_XDECREF(ci);
Py_XDECREF(rlist);
return NULL;
Expand Down