-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-134698: Hold a lock when the thread state is detached in ssl
#134724
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
Open
ZeroIntensity
wants to merge
8
commits into
python:main
Choose a base branch
from
ZeroIntensity:fix-ssl-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ffb568
Lock when the thread state is detached.
ZeroIntensity d0e0668
Add a test.
ZeroIntensity 16cab83
Add blurb.
ZeroIntensity ece939a
Use global constant in the test.
ZeroIntensity 18da64f
Zero-out the mutex and don't try to reacquire it in a callback.
ZeroIntensity 4f6928d
Remove generated test file.
ZeroIntensity 13d2e7d
Lock the context when creating a new SSL socket.
ZeroIntensity d2f5cc1
Fix _PySSL_FIX_ERRNO silliness.
ZeroIntensity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2025-05-26-10-52-27.gh-issue-134698.aJ1mZ1.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix a crash when calling methods of :class:`ssl.SSLContext` or | ||
:class:`ssl.SSLSocket` across multiple threads. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,14 @@ | |
/* Redefined below for Windows debug builds after important #includes */ | ||
#define _PySSL_FIX_ERRNO | ||
|
||
#define PySSL_BEGIN_ALLOW_THREADS_S(save) \ | ||
do { (save) = PyEval_SaveThread(); } while(0) | ||
#define PySSL_END_ALLOW_THREADS_S(save) \ | ||
do { PyEval_RestoreThread(save); _PySSL_FIX_ERRNO; } while(0) | ||
#define PySSL_BEGIN_ALLOW_THREADS { \ | ||
#define PySSL_BEGIN_ALLOW_THREADS_S(save, mutex) \ | ||
do { (save) = PyEval_SaveThread(); PyMutex_Lock(mutex); } while(0) | ||
#define PySSL_END_ALLOW_THREADS_S(save, mutex) \ | ||
do { PyMutex_Unlock(mutex); PyEval_RestoreThread(save); _PySSL_FIX_ERRNO; } while(0) | ||
#define PySSL_BEGIN_ALLOW_THREADS(self) { \ | ||
PyThreadState *_save = NULL; \ | ||
PySSL_BEGIN_ALLOW_THREADS_S(_save); | ||
#define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } | ||
PySSL_BEGIN_ALLOW_THREADS_S(_save, &self->tstate_mutex); | ||
#define PySSL_END_ALLOW_THREADS(self) PySSL_END_ALLOW_THREADS_S(_save, &self->tstate_mutex); } | ||
|
||
#if defined(HAVE_POLL_H) | ||
#include <poll.h> | ||
|
@@ -309,6 +309,9 @@ typedef struct { | |
PyObject *psk_client_callback; | ||
PyObject *psk_server_callback; | ||
#endif | ||
/* Lock to synchronize calls when the thread state is detached. | ||
See also gh-134698. */ | ||
PyMutex tstate_mutex; | ||
} PySSLContext; | ||
|
||
#define PySSLContext_CAST(op) ((PySSLContext *)(op)) | ||
|
@@ -336,6 +339,9 @@ typedef struct { | |
* and shutdown methods check for chained exceptions. | ||
*/ | ||
PyObject *exc; | ||
/* Lock to synchronize calls when the thread state is detached. | ||
See also gh-134698. */ | ||
PyMutex tstate_mutex; | ||
} PySSLSocket; | ||
|
||
#define PySSLSocket_CAST(op) ((PySSLSocket *)(op)) | ||
|
@@ -885,13 +891,15 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, | |
self->server_hostname = NULL; | ||
self->err = err; | ||
self->exc = NULL; | ||
self->tstate_mutex = (PyMutex){0}; | ||
|
||
/* Make sure the SSL error state is initialized */ | ||
ERR_clear_error(); | ||
|
||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
self->ssl = SSL_new(ctx); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
emmatyping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_PySSL_FIX_ERRNO; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one shouldn't be needed, or am I wrong? |
||
if (self->ssl == NULL) { | ||
Py_DECREF(self); | ||
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); | ||
|
@@ -960,12 +968,12 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, | |
BIO_set_nbio(SSL_get_wbio(self->ssl), 1); | ||
} | ||
|
||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
if (socket_type == PY_SSL_CLIENT) | ||
SSL_set_connect_state(self->ssl); | ||
else | ||
SSL_set_accept_state(self->ssl); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
|
||
self->socket_type = socket_type; | ||
if (sock != NULL) { | ||
|
@@ -1034,10 +1042,10 @@ _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) | |
/* Actually negotiate SSL connection */ | ||
/* XXX If SSL_do_handshake() returns 0, it's also a failure. */ | ||
do { | ||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
ret = SSL_do_handshake(self->ssl); | ||
err = _PySSL_errno(ret < 1, self->ssl, ret); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
self->err = err; | ||
|
||
if (PyErr_CheckSignals()) | ||
|
@@ -2414,9 +2422,10 @@ PySSL_select(PySocketSockObject *s, int writing, PyTime_t timeout) | |
ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | ||
assert(ms <= INT_MAX); | ||
|
||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
rc = poll(&pollfd, 1, (int)ms); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
#else | ||
/* Guard against socket too large for select*/ | ||
if (!_PyIsSelectable_fd(s->sock_fd)) | ||
|
@@ -2428,13 +2437,14 @@ PySSL_select(PySocketSockObject *s, int writing, PyTime_t timeout) | |
FD_SET(s->sock_fd, &fds); | ||
|
||
/* Wait until the socket becomes ready */ | ||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); | ||
if (writing) | ||
rc = select(nfds, NULL, &fds, NULL, &tv); | ||
else | ||
rc = select(nfds, &fds, NULL, NULL, &tv); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
#endif | ||
|
||
/* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise | ||
|
@@ -2505,10 +2515,10 @@ _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) | |
} | ||
|
||
do { | ||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
retval = SSL_write_ex(self->ssl, b->buf, (size_t)b->len, &count); | ||
err = _PySSL_errno(retval == 0, self->ssl, retval); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
self->err = err; | ||
|
||
if (PyErr_CheckSignals()) | ||
|
@@ -2566,10 +2576,10 @@ _ssl__SSLSocket_pending_impl(PySSLSocket *self) | |
int count = 0; | ||
_PySSLError err; | ||
|
||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
count = SSL_pending(self->ssl); | ||
err = _PySSL_errno(count < 0, self->ssl, count); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
self->err = err; | ||
|
||
if (count < 0) | ||
|
@@ -2660,10 +2670,10 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len, | |
deadline = _PyDeadline_Init(timeout); | ||
|
||
do { | ||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
retval = SSL_read_ex(self->ssl, mem, (size_t)len, &count); | ||
err = _PySSL_errno(retval == 0, self->ssl, retval); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
self->err = err; | ||
|
||
if (PyErr_CheckSignals()) | ||
|
@@ -2762,7 +2772,7 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) | |
} | ||
|
||
while (1) { | ||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
/* Disable read-ahead so that unwrap can work correctly. | ||
* Otherwise OpenSSL might read in too much data, | ||
* eating clear text data that happens to be | ||
|
@@ -2775,7 +2785,7 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) | |
SSL_set_read_ahead(self->ssl, 0); | ||
ret = SSL_shutdown(self->ssl); | ||
err = _PySSL_errno(ret < 0, self->ssl, ret); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
self->err = err; | ||
|
||
/* If err == 1, a secure shutdown with SSL_shutdown() is complete */ | ||
|
@@ -3167,9 +3177,10 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) | |
// no other thread can be touching this object yet. | ||
// (Technically, we can't even lock if we wanted to, as the | ||
// lock hasn't been initialized yet.) | ||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
ctx = SSL_CTX_new(method); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
|
||
if (ctx == NULL) { | ||
_setSSLError(get_ssl_state(module), NULL, 0, __FILE__, __LINE__); | ||
|
@@ -3194,6 +3205,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) | |
self->psk_client_callback = NULL; | ||
self->psk_server_callback = NULL; | ||
#endif | ||
self->tstate_mutex = (PyMutex){0}; | ||
|
||
/* Don't check host name by default */ | ||
if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { | ||
|
@@ -3312,9 +3324,10 @@ context_clear(PyObject *op) | |
Py_CLEAR(self->psk_server_callback); | ||
#endif | ||
if (self->keylog_bio != NULL) { | ||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
BIO_free_all(self->keylog_bio); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
self->keylog_bio = NULL; | ||
} | ||
return 0; | ||
|
@@ -4037,7 +4050,8 @@ _password_callback(char *buf, int size, int rwflag, void *userdata) | |
_PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; | ||
PyObject *fn_ret = NULL; | ||
|
||
PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); | ||
pw_info->thread_state = PyThreadState_Swap(pw_info->thread_state); | ||
_PySSL_FIX_ERRNO; | ||
|
||
if (pw_info->error) { | ||
/* already failed previously. OpenSSL 3.0.0-alpha14 invokes the | ||
|
@@ -4067,13 +4081,13 @@ _password_callback(char *buf, int size, int rwflag, void *userdata) | |
goto error; | ||
} | ||
|
||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); | ||
pw_info->thread_state = PyThreadState_Swap(pw_info->thread_state); | ||
memcpy(buf, pw_info->password, pw_info->size); | ||
return pw_info->size; | ||
|
||
error: | ||
Py_XDECREF(fn_ret); | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); | ||
pw_info->thread_state = PyThreadState_Swap(pw_info->thread_state); | ||
pw_info->error = 1; | ||
return -1; | ||
} | ||
|
@@ -4126,10 +4140,10 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | |
SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); | ||
SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); | ||
} | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||
r = SSL_CTX_use_certificate_chain_file(self->ctx, | ||
PyBytes_AS_STRING(certfile_bytes)); | ||
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); | ||
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||
if (r != 1) { | ||
if (pw_info.error) { | ||
ERR_clear_error(); | ||
|
@@ -4144,11 +4158,11 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | |
} | ||
goto error; | ||
} | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||
r = SSL_CTX_use_PrivateKey_file(self->ctx, | ||
PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), | ||
SSL_FILETYPE_PEM); | ||
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); | ||
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||
Py_CLEAR(keyfile_bytes); | ||
Py_CLEAR(certfile_bytes); | ||
if (r != 1) { | ||
|
@@ -4165,9 +4179,9 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | |
} | ||
goto error; | ||
} | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); | ||
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||
r = SSL_CTX_check_private_key(self->ctx); | ||
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); | ||
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||
if (r != 1) { | ||
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); | ||
goto error; | ||
|
@@ -4384,9 +4398,9 @@ _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, | |
cafile_buf = PyBytes_AS_STRING(cafile_bytes); | ||
if (capath) | ||
capath_buf = PyBytes_AS_STRING(capath_bytes); | ||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
if (r != 1) { | ||
if (errno != 0) { | ||
PyErr_SetFromErrno(PyExc_OSError); | ||
|
@@ -4438,10 +4452,11 @@ _ssl__SSLContext_load_dh_params_impl(PySSLContext *self, PyObject *filepath) | |
return NULL; | ||
|
||
errno = 0; | ||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
dh = PEM_read_DHparams(f, NULL, NULL, NULL); | ||
fclose(f); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
if (dh == NULL) { | ||
if (errno != 0) { | ||
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); | ||
|
@@ -4593,6 +4608,7 @@ _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) | |
Py_BEGIN_ALLOW_THREADS | ||
rc = SSL_CTX_set_default_verify_paths(self->ctx); | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
if (!rc) { | ||
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); | ||
return NULL; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,13 +140,15 @@ _PySSL_keylog_callback(const SSL *ssl, const char *line) | |
* critical debug helper. | ||
*/ | ||
|
||
PySSL_BEGIN_ALLOW_THREADS | ||
assert(PyMutex_IsLocked(&ssl_obj->tstate_mutex)); | ||
Py_BEGIN_ALLOW_THREADS | ||
PyThread_acquire_lock(lock, 1); | ||
res = BIO_printf(ssl_obj->ctx->keylog_bio, "%s\n", line); | ||
e = errno; | ||
(void)BIO_flush(ssl_obj->ctx->keylog_bio); | ||
PyThread_release_lock(lock); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
_PySSL_FIX_ERRNO; | ||
|
||
if (res == -1) { | ||
errno = e; | ||
|
@@ -187,9 +189,9 @@ _PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg, | |
if (self->keylog_bio != NULL) { | ||
BIO *bio = self->keylog_bio; | ||
self->keylog_bio = NULL; | ||
PySSL_BEGIN_ALLOW_THREADS | ||
Py_BEGIN_ALLOW_THREADS | ||
BIO_free_all(bio); | ||
PySSL_END_ALLOW_THREADS | ||
Py_END_ALLOW_THREADS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're missing a call to |
||
} | ||
|
||
if (arg == Py_None) { | ||
|
@@ -211,13 +213,13 @@ _PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg, | |
self->keylog_filename = Py_NewRef(arg); | ||
|
||
/* Write a header for seekable, empty files (this excludes pipes). */ | ||
PySSL_BEGIN_ALLOW_THREADS | ||
PySSL_BEGIN_ALLOW_THREADS(self) | ||
if (BIO_tell(self->keylog_bio) == 0) { | ||
BIO_puts(self->keylog_bio, | ||
"# TLS secrets log file, generated by OpenSSL / Python\n"); | ||
(void)BIO_flush(self->keylog_bio); | ||
} | ||
PySSL_END_ALLOW_THREADS | ||
PySSL_END_ALLOW_THREADS(self) | ||
SSL_CTX_set_keylog_callback(self->ctx, _PySSL_keylog_callback); | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
PySSL_BEGIN_ALLOW_THREADS(ssl->ctx)
here. The PySSLContext's ctx is being accessed by SSL_new so we should lock it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would probably be good to add a test creating ssl contexts in multiple threads as well.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's nothing too special about this function, though. I'm not sure it's worth the effort to add a test for every single function fixed here.