Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77cde50

Browse files
authored
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 <[email protected]>
1 parent 20a5b7e commit 77cde50

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
@@ -4764,6 +4764,28 @@ def msg_cb(conn, direction, version, content_type, msg_type, data):
47644764
msg
47654765
)
47664766

4767+
def test_msg_callback_deadlock_bpo43577(self):
4768+
client_context, server_context, hostname = testing_context()
4769+
server_context2 = testing_context()[1]
4770+
4771+
def msg_cb(conn, direction, version, content_type, msg_type, data):
4772+
pass
4773+
4774+
def sni_cb(sock, servername, ctx):
4775+
sock.context = server_context2
4776+
4777+
server_context._msg_callback = msg_cb
4778+
server_context.sni_callback = sni_cb
4779+
4780+
server = ThreadedEchoServer(context=server_context, chatty=False)
4781+
with server:
4782+
with client_context.wrap_socket(socket.socket(),
4783+
server_hostname=hostname) as s:
4784+
s.connect((HOST, server.port))
4785+
with client_context.wrap_socket(socket.socket(),
4786+
server_hostname=hostname) as s:
4787+
s.connect((HOST, server.port))
4788+
47674789

47684790
def test_main(verbose=False):
47694791
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
@@ -2202,6 +2202,11 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
22022202
Py_INCREF(value);
22032203
Py_SETREF(self->ctx, (PySSLContext *)value);
22042204
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
2205+
/* Set SSL* internal msg_callback to state of new context's state */
2206+
SSL_set_msg_callback(
2207+
self->ssl,
2208+
self->ctx->msg_cb ? _PySSL_msg_callback : NULL
2209+
);
22052210
#endif
22062211
} else {
22072212
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)