Skip to content

Commit cfca05f

Browse files
committed
gh-142516: fix reference leaks in ssl.SSLContext objects
1 parent 03e6457 commit cfca05f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Modules/_ssl.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ typedef struct {
328328
int post_handshake_auth;
329329
#endif
330330
PyObject *msg_cb;
331-
PyObject *keylog_filename;
331+
PyObject *keylog_filename; // can be anything accepted by Py_fopen()
332332
BIO *keylog_bio;
333333
/* Cached module state, also used in SSLSocket and SSLSession code. */
334334
_sslmodulestate *state;
@@ -358,7 +358,7 @@ typedef struct {
358358
PySSLContext *ctx; /* weakref to SSL context */
359359
char shutdown_seen_zero;
360360
enum py_ssl_server_or_client socket_type;
361-
PyObject *owner; /* Python level "owner" passed to servername callback */
361+
PyObject *owner; /* weakref to Python level "owner" passed to servername callback */
362362
PyObject *server_hostname;
363363
/* Some SSL callbacks don't have error reporting. Callback wrappers
364364
* store exception information on the socket. The handshake, read, write,
@@ -2444,6 +2444,10 @@ static int
24442444
PySSL_clear(PyObject *op)
24452445
{
24462446
PySSLSocket *self = PySSLSocket_CAST(op);
2447+
Py_CLEAR(self->Socket);
2448+
Py_CLEAR(self->ctx);
2449+
Py_CLEAR(self->owner);
2450+
Py_CLEAR(self->server_hostname);
24472451
Py_CLEAR(self->exc);
24482452
return 0;
24492453
}
@@ -2468,10 +2472,7 @@ PySSL_dealloc(PyObject *op)
24682472
SSL_set_shutdown(self->ssl, SSL_SENT_SHUTDOWN | SSL_get_shutdown(self->ssl));
24692473
SSL_free(self->ssl);
24702474
}
2471-
Py_XDECREF(self->Socket);
2472-
Py_XDECREF(self->ctx);
2473-
Py_XDECREF(self->server_hostname);
2474-
Py_XDECREF(self->owner);
2475+
(void)PySSL_clear(op);
24752476
PyObject_GC_Del(self);
24762477
Py_DECREF(tp);
24772478
}
@@ -3594,6 +3595,11 @@ context_traverse(PyObject *op, visitproc visit, void *arg)
35943595
PySSLContext *self = PySSLContext_CAST(op);
35953596
Py_VISIT(self->set_sni_cb);
35963597
Py_VISIT(self->msg_cb);
3598+
Py_VISIT(self->keylog_filename);
3599+
#ifndef OPENSSL_NO_PSK
3600+
Py_VISIT(self->psk_client_callback);
3601+
Py_VISIT(self->psk_server_callback);
3602+
#endif
35973603
Py_VISIT(Py_TYPE(self));
35983604
return 0;
35993605
}

0 commit comments

Comments
 (0)