Skip to content

Commit 93b0da7

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 5051167 commit 93b0da7

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

Lib/test/test_ssl.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,6 +4730,28 @@ def msg_cb(conn, direction, version, content_type, msg_type, data):
47304730
msg
47314731
)
47324732

4733+
def test_msg_callback_deadlock_bpo43577(self):
4734+
client_context, server_context, hostname = testing_context()
4735+
server_context2 = testing_context()[1]
4736+
4737+
def msg_cb(conn, direction, version, content_type, msg_type, data):
4738+
pass
4739+
4740+
def sni_cb(sock, servername, ctx):
4741+
sock.context = server_context2
4742+
4743+
server_context._msg_callback = msg_cb
4744+
server_context.sni_callback = sni_cb
4745+
4746+
server = ThreadedEchoServer(context=server_context, chatty=False)
4747+
with server:
4748+
with client_context.wrap_socket(socket.socket(),
4749+
server_hostname=hostname) as s:
4750+
s.connect((HOST, server.port))
4751+
with client_context.wrap_socket(socket.socket(),
4752+
server_hostname=hostname) as s:
4753+
s.connect((HOST, server.port))
4754+
47334755

47344756
def test_main(verbose=False):
47354757
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
@@ -2205,6 +2205,11 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
22052205
Py_INCREF(value);
22062206
Py_SETREF(self->ctx, (PySSLContext *)value);
22072207
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
2208+
/* Set SSL* internal msg_callback to state of new context's state */
2209+
SSL_set_msg_callback(
2210+
self->ssl,
2211+
self->ctx->msg_cb ? _PySSL_msg_callback : NULL
2212+
);
22082213
#endif
22092214
} else {
22102215
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)