Skip to content

[3.11] gh-144833: Fix use-after-free in SSL module when SSL_new() fails (GH-144843)#144861

Open
miss-islington wants to merge 1 commit intopython:3.11from
miss-islington:backport-c91638c-3.11
Open

[3.11] gh-144833: Fix use-after-free in SSL module when SSL_new() fails (GH-144843)#144861
miss-islington wants to merge 1 commit intopython:3.11from
miss-islington:backport-c91638c-3.11

Conversation

@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Feb 16, 2026

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

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>
Copy link
Contributor

@caje731 caje731 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

self->ssl = SSL_new(ctx);
PySSL_END_ALLOW_THREADS
if (self->ssl == NULL) {
_setSSLError(get_state_ctx(sslctx), NULL, 0, __FILE__, __LINE__);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my curiosity - any benefits to moving decref further down? The only thing I can think of is if get_state_ctx begins holding (indirect / chain) references to self again through its argument, moving the decref down is appropriately defensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

5 participants