← Back to DAGDA | Support | Source: https://github.com/python/cpython/commit/c145c03281af850130dd7f75abc19c6ea49626e6
Skip to content

Commit c145c03

Browse files
bpo-43577: Fix deadlock with SSLContext._msg_callback and sni_callback (GH-24957)
OpenSSL copies the internal message callback from SSL_CTX->msg_callback to SSL->msg_callback. SSL_set_SSL_CTX() does not update SSL->msg_callback to use the callback value of the new context. PySSL_set_context() now resets the callback and _PySSL_msg_callback() resets thread state in error path. Signed-off-by: Christian Heimes <christian@python.org> (cherry picked from commit 77cde50) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 107cf07 commit c145c03

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4739,6 +4739,28 @@ def msg_cb(conn, direction, version, content_type, msg_type, data):
47394739
msg
47404740
)
47414741

4742+
def test_msg_callback_deadlock_bpo43577(self):
4743+
client_context, server_context, hostname = testing_context()
4744+
server_context2 = testing_context()[1]
4745+
4746+
def msg_cb(conn, direction, version, content_type, msg_type, data):
4747+
pass
4748+
4749+
def sni_cb(sock, servername, ctx):
4750+
sock.context = server_context2
4751+
4752+
server_context._msg_callback = msg_cb
4753+
server_context.sni_callback = sni_cb
4754+
4755+
server = ThreadedEchoServer(context=server_context, chatty=False)
4756+
with server:
4757+
with client_context.wrap_socket(socket.socket(),
4758+
server_hostname=hostname) as s:
4759+
s.connect((HOST, server.port))
4760+
with client_context.wrap_socket(socket.socket(),
4761+
server_hostname=hostname) as s:
4762+
s.connect((HOST, server.port))
4763+
47424764

47434765
def test_main(verbose=False):
47444766
if support.verbose:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix deadlock when using :class:`ssl.SSLContext` debug callback with :meth:`ssl.SSLContext.sni_callback`.

‎Modules/_ssl.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,11 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
22032203
Py_INCREF(value);
22042204
Py_SETREF(self->ctx, (PySSLContext *)value);
22052205
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
2206+
/* Set SSL* internal msg_callback to state of new context's state */
2207+
SSL_set_msg_callback(
2208+
self->ssl,
2209+
self->ctx->msg_cb ? _PySSL_msg_callback : NULL
2210+
);
22062211
#endif
22072212
} else {
22082213
PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext");

‎Modules/_ssl/debughelpers.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _PySSL_msg_callback(int write_p, int version, int content_type,
2323
ssl_obj = (PySSLSocket *)SSL_get_app_data(ssl);
2424
assert(PySSLSocket_Check(ssl_obj));
2525
if (ssl_obj->ctx->msg_cb == NULL) {
26+
PyGILState_Release(threadstate);
2627
return;
2728
}
2829

0 commit comments

Comments
 (0)