-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143756: Fix potential data race in SSLContext.load_cert_chain #143818
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix potential thread safety issues in :mod:`ssl` module. |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,14 +43,17 @@ | |||||||
| /* Redefined below for Windows debug builds after important #includes */ | ||||||||
| #define _PySSL_FIX_ERRNO | ||||||||
|
|
||||||||
| #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_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(self) { \ | ||||||||
|
Member
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. While you are it, can you use a do-while construct? (here we would just have the do {) In addition can you make it multiline with line continuations aligned on a tab multiple? (like PEP-7) TiA.
Contributor
Author
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. ditto |
||||||||
| PyThreadState *_save = NULL; \ | ||||||||
| PySSL_BEGIN_ALLOW_THREADS_S(_save, &self->tstate_mutex); | ||||||||
| #define PySSL_END_ALLOW_THREADS(self) PySSL_END_ALLOW_THREADS_S(_save, &self->tstate_mutex); } | ||||||||
| PySSL_BEGIN_ALLOW_THREADS_S(_save); \ | ||||||||
| PyMutex_Lock(&(self)->tstate_mutex); | ||||||||
| #define PySSL_END_ALLOW_THREADS(self) \ | ||||||||
| PyMutex_Unlock(&(self)->tstate_mutex); \ | ||||||||
| PySSL_END_ALLOW_THREADS_S(_save); } | ||||||||
|
|
||||||||
| #if defined(HAVE_POLL_H) | ||||||||
| #include <poll.h> | ||||||||
|
|
@@ -4543,60 +4546,25 @@ _password_callback(char *buf, int size, int rwflag, void *userdata) | |||||||
| return -1; | ||||||||
| } | ||||||||
|
|
||||||||
| /*[clinic input] | ||||||||
| @critical_section | ||||||||
| _ssl._SSLContext.load_cert_chain | ||||||||
| certfile: object | ||||||||
| keyfile: object = None | ||||||||
| password: object = None | ||||||||
|
|
||||||||
| [clinic start generated code]*/ | ||||||||
|
|
||||||||
| static PyObject * | ||||||||
| _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | ||||||||
| PyObject *keyfile, PyObject *password) | ||||||||
| /*[clinic end generated code: output=9480bc1c380e2095 input=6c7c5e8b73e4264b]*/ | ||||||||
| load_cert_chain_lock_held(PySSLContext *self, _PySSLPasswordInfo *pw_info, | ||||||||
| PyObject *certfile_bytes, PyObject *keyfile_bytes) | ||||||||
| { | ||||||||
| PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; | ||||||||
| int r; | ||||||||
| PyObject *ret = NULL; | ||||||||
|
|
||||||||
| pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); | ||||||||
| void *orig_passwd_userdata = SSL_CTX_get_default_passwd_cb_userdata(self->ctx); | ||||||||
| _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; | ||||||||
| int r; | ||||||||
|
|
||||||||
| errno = 0; | ||||||||
| ERR_clear_error(); | ||||||||
| if (keyfile == Py_None) | ||||||||
| keyfile = NULL; | ||||||||
| if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { | ||||||||
| if (PyErr_ExceptionMatches(PyExc_TypeError)) { | ||||||||
| PyErr_SetString(PyExc_TypeError, | ||||||||
| "certfile should be a valid filesystem path"); | ||||||||
| } | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { | ||||||||
| if (PyErr_ExceptionMatches(PyExc_TypeError)) { | ||||||||
| PyErr_SetString(PyExc_TypeError, | ||||||||
| "keyfile should be a valid filesystem path"); | ||||||||
| } | ||||||||
| goto error; | ||||||||
| } | ||||||||
| if (password != Py_None) { | ||||||||
| if (PyCallable_Check(password)) { | ||||||||
| pw_info.callable = password; | ||||||||
| } else if (!_pwinfo_set(&pw_info, password, | ||||||||
| "password should be a string or callable")) { | ||||||||
| goto error; | ||||||||
| } | ||||||||
| 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, &self->tstate_mutex); | ||||||||
| 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); | ||||||||
| r = SSL_CTX_use_certificate_chain_file(self->ctx, | ||||||||
| PyBytes_AS_STRING(certfile_bytes)); | ||||||||
| PySSL_END_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||||||||
| PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); | ||||||||
| if (r != 1) { | ||||||||
| if (pw_info.error) { | ||||||||
| if (pw_info->error) { | ||||||||
| ERR_clear_error(); | ||||||||
| /* the password callback has already set the error information */ | ||||||||
| } | ||||||||
|
|
@@ -4609,15 +4577,14 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | |||||||
| } | ||||||||
| goto error; | ||||||||
| } | ||||||||
| PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||||||||
|
|
||||||||
| PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); | ||||||||
| r = SSL_CTX_use_PrivateKey_file(self->ctx, | ||||||||
| PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), | ||||||||
| PyBytes_AS_STRING(keyfile_bytes ? keyfile_bytes : certfile_bytes), | ||||||||
| SSL_FILETYPE_PEM); | ||||||||
| PySSL_END_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||||||||
| Py_CLEAR(keyfile_bytes); | ||||||||
| Py_CLEAR(certfile_bytes); | ||||||||
| PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); | ||||||||
| if (r != 1) { | ||||||||
| if (pw_info.error) { | ||||||||
| if (pw_info->error) { | ||||||||
| ERR_clear_error(); | ||||||||
| /* the password callback has already set the error information */ | ||||||||
| } | ||||||||
|
|
@@ -4630,25 +4597,74 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | |||||||
| } | ||||||||
| goto error; | ||||||||
| } | ||||||||
| PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||||||||
|
|
||||||||
| PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); | ||||||||
| r = SSL_CTX_check_private_key(self->ctx); | ||||||||
| PySSL_END_ALLOW_THREADS_S(pw_info.thread_state, &self->tstate_mutex); | ||||||||
| PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); | ||||||||
| if (r != 1) { | ||||||||
| _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); | ||||||||
| goto error; | ||||||||
| } | ||||||||
| SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); | ||||||||
| SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); | ||||||||
| PyMem_Free(pw_info.password); | ||||||||
| Py_RETURN_NONE; | ||||||||
|
|
||||||||
| ret = Py_None; | ||||||||
| error: | ||||||||
| SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); | ||||||||
| SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); | ||||||||
| return ret; | ||||||||
| } | ||||||||
|
|
||||||||
| /*[clinic input] | ||||||||
| _ssl._SSLContext.load_cert_chain | ||||||||
| certfile: object | ||||||||
| keyfile: object = None | ||||||||
| password: object = None | ||||||||
|
|
||||||||
| [clinic start generated code]*/ | ||||||||
|
|
||||||||
| static PyObject * | ||||||||
| _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, | ||||||||
| PyObject *keyfile, PyObject *password) | ||||||||
| /*[clinic end generated code: output=9480bc1c380e2095 input=30bc7e967ea01a58]*/ | ||||||||
| { | ||||||||
| PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; | ||||||||
| _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; | ||||||||
| PyObject *ret = NULL; | ||||||||
|
|
||||||||
| errno = 0; | ||||||||
| ERR_clear_error(); | ||||||||
| if (keyfile == Py_None) | ||||||||
| keyfile = NULL; | ||||||||
|
Comment on lines
+4634
to
+4635
Member
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. I think this can be added in the if (keyfile) check instead.
Contributor
Author
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. ditto; this is existing code |
||||||||
| if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { | ||||||||
| if (PyErr_ExceptionMatches(PyExc_TypeError)) { | ||||||||
| PyErr_SetString(PyExc_TypeError, | ||||||||
| "certfile should be a valid filesystem path"); | ||||||||
|
Member
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. For a follow-up: This is a bit misleading. For me such message usually implies a ValueError instead of TypeError. Do we use this formulation for other occurrences of FSConverter? Maybe: "expecting a path-like object for 'certfile', got ..."
Contributor
Author
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. ditto |
||||||||
| } | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { | ||||||||
| if (PyErr_ExceptionMatches(PyExc_TypeError)) { | ||||||||
| PyErr_SetString(PyExc_TypeError, | ||||||||
| "keyfile should be a valid filesystem path"); | ||||||||
| } | ||||||||
| goto done; | ||||||||
| } | ||||||||
| if (password != Py_None) { | ||||||||
| if (PyCallable_Check(password)) { | ||||||||
| pw_info.callable = password; | ||||||||
| } else if (!_pwinfo_set(&pw_info, password, | ||||||||
|
Member
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.
Suggested change
Contributor
Author
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. ditto |
||||||||
| "password should be a string or callable")) { | ||||||||
|
Member
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. And realign this adter putting the elseif on its own line
Contributor
Author
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. ditto |
||||||||
| goto done; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| PyMutex_Lock(&self->tstate_mutex); | ||||||||
| ret = load_cert_chain_lock_held(self, &pw_info, certfile_bytes, keyfile_bytes); | ||||||||
| PyMutex_Unlock(&self->tstate_mutex); | ||||||||
|
|
||||||||
| done: | ||||||||
| PyMem_Free(pw_info.password); | ||||||||
| Py_XDECREF(keyfile_bytes); | ||||||||
| Py_XDECREF(certfile_bytes); | ||||||||
| return NULL; | ||||||||
| return ret; | ||||||||
| } | ||||||||
|
|
||||||||
| /* internal helper function, returns -1 on error | ||||||||
|
|
||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Can you use uppercase letters for the macro parameters please?
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.
Let's not do a bunch of extra refactoring in a bugfix PR
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.
Oh right you want to backport this. I think there will be anyway conflicts so yeah let's not make it more complicate