Skip to content

Commit 5839060

Browse files
raminfpmiss-islington
authored andcommitted
pythongh-144833: Fix use-after-free in SSL module when SSL_new() fails (pythonGH-144843)
In newPySSLSocket(), when SSL_new() returns NULL, Py_DECREF(self) was called before _setSSLError(get_state_ctx(self), ...), causing a use-after-free. Additionally, get_state_ctx() was called with self (PySSLSocket*) instead of sslctx (PySSLContext*), which is a type confusion bug. Fix by calling _setSSLError() before Py_DECREF() and using sslctx instead of self for get_state_ctx(). (cherry picked from commit c91638c) Co-authored-by: Ramin Farajpour Cami <ramin.blackhat@gmail.com>
1 parent ee902ce commit 5839060

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a use-after-free in :mod:`ssl` when ``SSL_new()`` returns NULL in
2+
``newPySSLSocket()``. The error was reported via a dangling pointer after the
3+
object had already been freed.

Modules/_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,8 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
844844
self->ssl = SSL_new(ctx);
845845
PySSL_END_ALLOW_THREADS
846846
if (self->ssl == NULL) {
847+
_setSSLError(get_state_ctx(sslctx), NULL, 0, __FILE__, __LINE__);
847848
Py_DECREF(self);
848-
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
849849
return NULL;
850850
}
851851
/* bpo43522 and OpenSSL < 1.1.1l: copy hostflags manually */

0 commit comments

Comments
 (0)