Skip to content

Commit 18da64f

Browse files
committed
Zero-out the mutex and don't try to reacquire it in a callback.
1 parent ece939a commit 18da64f

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

@test_40106_tmpæ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TLS secrets log file, generated by OpenSSL / Python

Modules/_ssl.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,15 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
891891
self->server_hostname = NULL;
892892
self->err = err;
893893
self->exc = NULL;
894+
self->tstate_mutex = (PyMutex){0};
894895

895896
/* Make sure the SSL error state is initialized */
896897
ERR_clear_error();
897898

898-
PySSL_BEGIN_ALLOW_THREADS(self)
899+
Py_BEGIN_ALLOW_THREADS
899900
self->ssl = SSL_new(ctx);
900-
PySSL_END_ALLOW_THREADS(self)
901+
Py_END_ALLOW_THREADS
902+
_PySSL_FIX_ERRNO;
901903
if (self->ssl == NULL) {
902904
Py_DECREF(self);
903905
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
@@ -3203,6 +3205,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
32033205
self->psk_client_callback = NULL;
32043206
self->psk_server_callback = NULL;
32053207
#endif
3208+
self->tstate_mutex = (PyMutex){0};
32063209

32073210
/* Don't check host name by default */
32083211
if (proto_version == PY_SSL_VERSION_TLS_CLIENT) {

Modules/_ssl/debughelpers.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ _PySSL_keylog_callback(const SSL *ssl, const char *line)
140140
* critical debug helper.
141141
*/
142142

143-
PySSL_BEGIN_ALLOW_THREADS(ssl_obj)
143+
assert(PyMutex_IsLocked(&ssl_obj->tstate_mutex));
144+
Py_BEGIN_ALLOW_THREADS
144145
PyThread_acquire_lock(lock, 1);
145146
res = BIO_printf(ssl_obj->ctx->keylog_bio, "%s\n", line);
146147
e = errno;
147148
(void)BIO_flush(ssl_obj->ctx->keylog_bio);
148149
PyThread_release_lock(lock);
149-
PySSL_END_ALLOW_THREADS(ssl_obj)
150+
Py_END_ALLOW_THREADS
151+
_PySSL_FIX_ERRNO;
150152

151153
if (res == -1) {
152154
errno = e;

0 commit comments

Comments
 (0)