]> granicus.if.org Git - python/commitdiff
[3.5] bpo-30594: Fixed refcounting in newPySSLSocket (GH-1992) (#1993)
authorNathaniel J. Smith <njs@pobox.com>
Thu, 8 Jun 2017 11:14:44 +0000 (04:14 -0700)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 8 Jun 2017 11:14:44 +0000 (14:14 +0300)
If pass a server_hostname= that fails IDNA decoding to SSLContext.wrap_socket or SSLContext.wrap_bio, then the SSLContext object had a spurious Py_DECREF called on it, eventually leading to segfaults.
(cherry picked from commit 65ece7ca2366308fa91a39a8dfa255e6bdce3cca)

Modules/_ssl.c

index 9f79d17f6d3f4a3d5258e028b4f54e05093b8d33..3b2d3add2821bf3c1c8c93e07eac710e77e6f16f 100644 (file)
@@ -570,6 +570,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
     self->ssl = NULL;
     self->Socket = NULL;
     self->ctx = sslctx;
+    Py_INCREF(sslctx);
     self->shutdown_seen_zero = 0;
     self->handshake_done = 0;
     self->owner = NULL;
@@ -584,8 +585,6 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
         self->server_hostname = hostname;
     }
 
-    Py_INCREF(sslctx);
-
     /* Make sure the SSL error state is initialized */
     (void) ERR_get_state();
     ERR_clear_error();