From f4d1fc64887df4833127173e6b92e7a56154a28e Mon Sep 17 00:00:00 2001
From: Dmitriy Popov <dpopov@cloudlinux.com>
Date: Tue, 26 Dec 2023 14:44:16 +0200
Subject: [PATCH] 00388-2.7-CVE-2023-40217

Backport the following upstream commits:

    ---- 1 ----
    From 0cb0c238d520a8718e313b52cffc356a5a7561bf Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?=C5=81ukasz=20Langa?= <lukasz@langa.pl>
    Date: Tue, 22 Aug 2023 19:53:15 +0200
    Subject: [PATCH] gh-108310: Fix CVE-2023-40217: Check for & avoid the ssl
     pre-close flaw (#108315)
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit

    Instances of `ssl.SSLSocket` were vulnerable to a bypass of the TLS handshake
    and included protections (like certificate verification) and treating sent
    unencrypted data as if it were post-handshake TLS encrypted data.

    The vulnerability is caused when a socket is connected, data is sent by the
    malicious peer and stored in a buffer, and then the malicious peer closes the
    socket within a small timing window before the other peers’ TLS handshake can
    begin. After this sequence of events the closed socket will not immediately
    attempt a TLS handshake due to not being connected but will also allow the
    buffered data to be read as if a successful TLS handshake had occurred.

    Co-authored-by: Gregory P. Smith [Google LLC] <greg@krypto.org>

    ---- 2 ----
    From 64f99350351bc46e016b2286f36ba7cd669b79e3 Mon Sep 17 00:00:00 2001
    From: Victor Stinner <vstinner@python.org>
    Date: Wed, 23 Aug 2023 07:26:01 +0200
    Subject: [PATCH] gh-108342: Break ref cycle in SSLSocket._create() exc
     (#108344)

    Explicitly break a reference cycle when SSLSocket._create() raises an
    exception. Clear the variable storing the exception, since the
    exception traceback contains the variables and so creates a reference
    cycle.

    This test leak was introduced by the test added for the fix of #108310.

    ---- 3 ----
    From 592bacb6fc0833336c0453e818e9b95016e9fd47 Mon Sep 17 00:00:00 2001
    From: Victor Stinner <vstinner@python.org>
    Date: Wed, 23 Aug 2023 23:57:11 +0200
    Subject: [PATCH] gh-108342: Make ssl TestPreHandshakeClose more reliable
     (#108370)

    * In preauth tests of test_ssl, explicitly break reference cycles
      invoving SingleConnectionTestServerThread to make sure that the
      thread is deleted. Otherwise, the test marks the environment as
      altered because the threading module sees a "dangling thread"
      (SingleConnectionTestServerThread). This test leak was introduced
      by the test added for the fix of issue gh-108310.
    * Use support.SHORT_TIMEOUT instead of hardcoded 1.0 or 2.0 seconds
      timeout.
    * SingleConnectionTestServerThread.run() catchs TimeoutError
    * Fix a race condition (missing synchronization) in
      test_preauth_data_to_tls_client(): the server now waits until the
      client connect() completed in call_after_accept().
    * test_https_client_non_tls_response_ignored() calls server.join()
      explicitly.
    * Replace "localhost" with server.listener.getsockname()[0].

    ---- 4 ----
    From 7fa62f5471b71008cb98ca5abffa5be432dae39b Mon Sep 17 00:00:00 2001
    From: Lumir Balhar <lbalhar@redhat.com>
    Date: Wed, 11 Oct 2023 15:35:02 +0200
    Subject: [PATCH] gh-108310: Fix TestPreHandshakeClose tests in test_ssl

    The new class is part of the fix for CVE-2023-40217:
    https://github.com/python/cpython/commit/b4bcc06a9cfe13d96d5270809d963f8ba278f89b
    but it's not in the lists of tests so they're not
    executed. The new tests also need `SHORT_TIMEOUT`
    constant not available in test.support in 3.8.

Signed-off-by: Dmitriy Popov <dpopov@cloudlinux.com>
---
 Lib/ssl.py               |  41 ++++++-
 Lib/test/test_ssl.py     | 233 ++++++++++++++++++++++++++++++++++++++-
 Lib/test/test_support.py |   2 +
 3 files changed, 273 insertions(+), 3 deletions(-)

diff --git a/Lib/ssl.py b/Lib/ssl.py
index 743951f..d746279 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -91,6 +91,8 @@ class SSLSocket(socket):
                  ssl_version=PROTOCOL_SSLv23, ca_certs=None,
                  do_handshake_on_connect=True,
                  suppress_ragged_eofs=True):
+        self._sslobj = None
+        self._makefile_refs = 0
         socket.__init__(self, _sock=sock._sock)
         # The initializer for socket overrides the methods send(), recv(), etc.
         # in the instancce, which we don't need -- but we want to provide the
@@ -103,6 +105,9 @@ class SSLSocket(socket):
 
         if certfile and not keyfile:
             keyfile = certfile
+
+        sock_timeout = sock.gettimeout()
+
         # see if it's connected
         try:
             socket.getpeername(self)
@@ -111,6 +116,38 @@ class SSLSocket(socket):
                 raise
             # no, no connection yet
             self._sslobj = None
+            blocking = (sock.gettimeout() != 0)
+            self.setblocking(False)
+            try:
+                # We are not connected so this is not supposed to block, but
+                # testing revealed otherwise on macOS and Windows so we do
+                # the non-blocking dance regardless. Our raise when any data
+                # is found means consuming the data is harmless.
+                notconn_pre_handshake_data = self.recv(1)
+            except socket_error as e:
+                # EINVAL occurs for recv(1) on non-connected on unix sockets.
+                if e.errno not in (errno.ENOTCONN, errno.EINVAL):
+                    raise
+                notconn_pre_handshake_data = b''
+            self.setblocking(blocking)
+            if notconn_pre_handshake_data:
+                # This prevents pending data sent to the socket before it was
+                # closed from escaping to the caller who could otherwise
+                # presume it came through a successful TLS connection.
+                reason = "Closed before TLS handshake with data in recv buffer"
+                notconn_pre_handshake_data_error = SSLError(e.errno, reason)
+                # Add the SSLError attributes that _ssl.c always adds.
+                notconn_pre_handshake_data_error.reason = reason
+                notconn_pre_handshake_data_error.library = None
+                try:
+                    self.close()
+                except OSError:
+                    pass
+                try:
+                    raise notconn_pre_handshake_data_error
+                finally:
+                    # Explicitly break the reference cycle.
+                    notconn_pre_handshake_data_error = None
         else:
             # yes, create the SSL object
             self._sslobj = _ssl.sslwrap(self._sock, server_side,
@@ -118,6 +155,7 @@ class SSLSocket(socket):
                                         cert_reqs, ssl_version, ca_certs)
             if do_handshake_on_connect:
                 self.do_handshake()
+        self.settimeout(sock_timeout)  # Must come after setblocking() calls.
         self.keyfile = keyfile
         self.certfile = certfile
         self.cert_reqs = cert_reqs
@@ -125,7 +163,6 @@ class SSLSocket(socket):
         self.ca_certs = ca_certs
         self.do_handshake_on_connect = do_handshake_on_connect
         self.suppress_ragged_eofs = suppress_ragged_eofs
-        self._makefile_refs = 0
 
     def read(self, len=1024):
 
@@ -214,7 +251,7 @@ class SSLSocket(socket):
                     self.__class__)
             return self.read(buflen)
         else:
-            return socket.recv(self, buflen, flags)
+            return self._sock.recv(buflen, flags)
 
     def recv_into(self, buffer, nbytes=None, flags=0):
         if buffer and (nbytes is None):
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 3b868f0..f3711a3 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -6,10 +6,12 @@ from test import test_support
 import asyncore
 import socket
 import select
+import struct
 import errno
 import subprocess
 import time
 import gc
+import httplib
 import os
 import errno
 import pprint
@@ -17,6 +19,7 @@ import urllib, urlparse
 import shutil
 import traceback
 import weakref
+from contextlib import closing
 
 from BaseHTTPServer import HTTPServer
 from SimpleHTTPServer import SimpleHTTPRequestHandler
@@ -1227,6 +1230,234 @@ else:
                 t.join()
                 server.close()
 
+def set_socket_so_linger_on_with_zero_timeout(sock):
+    sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
+
+class TestPreHandshakeClose(unittest.TestCase):
+    """Verify behavior of close sockets with received data before to the handshake.
+    """
+
+    class SingleConnectionTestServerThread(threading.Thread):
+
+        def __init__(self,  name, call_after_accept, timeout=None):
+            self.call_after_accept = call_after_accept
+            self.received_data = b''  # set by .run()
+            self.wrap_error = None  # set by .run()
+            self.listener = None  # set by .start()
+            self.port = None  # set by .start()
+            if timeout is None:
+                self.timeout = test_support.SHORT_TIMEOUT
+            else:
+                self.timeout = timeout
+            threading.Thread.__init__(self, name=name)
+
+        def __enter__(self):
+            self.start()
+            return self
+
+        def __exit__(self, *args):
+            try:
+                if self.listener:
+                    self.listener.close()
+            except ssl.SSLError:
+                pass
+            self.join()
+            self.wrap_error = None  # avoid dangling references
+
+        def start(self):
+            self.listener = socket.socket()
+            self.port = test_support.bind_port(self.listener)
+            self.listener.settimeout(self.timeout)
+            self.listener.listen(1)
+            threading.Thread.start(self)
+
+        def run(self):
+            try:
+                conn, address = self.listener.accept()
+            except OSError as e:
+                if e.errno == errno.ETIMEDOUT:
+                    # on timeout, just close the listener
+                    return
+                else:
+                    raise
+            finally:
+                self.listener.close()
+
+            with closing(conn):
+                if self.call_after_accept(conn):
+                    return
+                try:
+                    tls_socket = ssl.wrap_socket(conn, keyfile=os.path.join(os.path.dirname(__file__), "certdata", "ssl_key.pem"), certfile=os.path.join(os.path.dirname(__file__), "certdata", "ssl_cert.pem"), cert_reqs=ssl.CERT_REQUIRED, server_side=True)
+                except ssl.SSLError as err:
+                    self.wrap_error = err
+                else:
+                    try:
+                        self.received_data = tls_socket.recv(400)
+                    except OSError:
+                        pass  # closed, protocol error, etc.
+
+    def setUp(self):
+        self.cleanups = []
+        self.addCleanup = self.cleanups.append
+
+    def tearDown(self):
+        while self.cleanups:
+            cleanup = self.cleanups.pop()
+            cleanup()
+
+    def test_preauth_data_to_tls_server(self):
+        server_accept_called = threading.Event()
+        ready_for_server_wrap_socket = threading.Event()
+
+        def call_after_accept(unused):
+            server_accept_called.set()
+            ready_for_server_wrap_socket.wait(test_support.SHORT_TIMEOUT)
+            if not ready_for_server_wrap_socket.is_set():
+                raise RuntimeError("wrap_socket event never set, test may fail.")
+            return False  # Tell the server thread to continue.
+
+        server = self.SingleConnectionTestServerThread(
+                call_after_accept=call_after_accept,
+                name="preauth_data_to_tls_server")
+        server.__enter__()  # starts it
+        self.addCleanup(server.__exit__)  # ... & unittest.TestCase stops it.
+
+        with closing(socket.socket()) as client:
+            client.connect(server.listener.getsockname())
+            # This forces an immediate connection close via RST on .close().
+            set_socket_so_linger_on_with_zero_timeout(client)
+            client.setblocking(False)
+
+            server_accept_called.wait()
+            client.send(b"DELETE /data HTTP/1.0\r\n\r\n")
+            client.close()  # RST
+
+        ready_for_server_wrap_socket.set()
+        server.join()
+
+        wrap_error = server.wrap_error
+        try:
+            self.assertEqual(b"", server.received_data)
+            self.assertTrue(isinstance(wrap_error, ssl.SSLError))
+            self.assertTrue("before TLS handshake with data" in wrap_error.args[1])
+            self.assertTrue("before TLS handshake with data" in wrap_error.reason)
+            self.assertNotEqual(0, wrap_error.args[0])
+            self.assertTrue(wrap_error.library is None, msg="attr must exist")
+        finally:
+            # gh-108342: Explicitly break the reference cycle
+            wrap_error = None
+            server = None
+
+    def test_preauth_data_to_tls_client(self):
+        server_can_continue_with_wrap_socket = threading.Event()
+        client_can_continue_with_wrap_socket = threading.Event()
+
+        def call_after_accept(conn_to_client):
+            server_can_continue_with_wrap_socket.wait(test_support.SHORT_TIMEOUT)
+            if not server_can_continue_with_wrap_socket.is_set():
+                print("ERROR: test client took too long")
+
+            # This forces an immediate connection close via RST on .close().
+            set_socket_so_linger_on_with_zero_timeout(conn_to_client)
+            conn_to_client.send(
+                    b"HTTP/1.0 307 Temporary Redirect\r\n"
+                    b"Location: https://example.com/someone-elses-server\r\n"
+                    b"\r\n")
+            conn_to_client.close()  # RST
+            client_can_continue_with_wrap_socket.set()
+            return True  # Tell the server to stop.
+
+        server = self.SingleConnectionTestServerThread(
+                call_after_accept=call_after_accept,
+                name="preauth_data_to_tls_client")
+        server.__enter__()  # starts it
+        self.addCleanup(server.__exit__)  # ... & unittest.TestCase stops it.
+
+        # Redundant; call_after_accept sets SO_LINGER on the accepted conn.
+        set_socket_so_linger_on_with_zero_timeout(server.listener)
+
+        with closing(socket.socket()) as client:
+            client.connect(server.listener.getsockname())
+            server_can_continue_with_wrap_socket.set()
+
+            client_can_continue_with_wrap_socket.wait(test_support.SHORT_TIMEOUT)
+
+            if not client_can_continue_with_wrap_socket.is_set():
+                self.fail("test server took too long")
+            try:
+                tls_client = ssl.wrap_socket(client)
+            except ssl.SSLError as err:
+                wrap_error = err
+                received_data = b""
+            else:
+                wrap_error = None
+                received_data = tls_client.recv(400)
+                tls_client.close()
+
+        server.join()
+        try:
+            self.assertEqual(b"", received_data)
+            self.assertTrue(isinstance(wrap_error, ssl.SSLError))
+            self.assertTrue("before TLS handshake with data" in wrap_error.args[1])
+            self.assertTrue("before TLS handshake with data" in wrap_error.reason)
+            self.assertNotEqual(0, wrap_error.args[0])
+            self.assertTrue(wrap_error.library is None, msg="attr must exist")
+        finally:
+            # gh-108342: Explicitly break the reference cycle
+            wrap_error = None
+            server = None
+
+    def test_https_client_non_tls_response_ignored(self):
+        server_responding = threading.Event()
+
+        class SynchronizedHTTPSConnection(httplib.HTTPSConnection):
+            def connect(self):
+                # Call clear text HTTP connect(), not the encrypted HTTPS (TLS)
+                # connect(): wrap_socket() is called manually below.
+                httplib.HTTPConnection.connect(self)
+
+                # Wait for our fault injection server to have done its thing.
+                if not server_responding.wait(test_support.SHORT_TIMEOUT) and test_support.verbose:
+                    sys.stdout.write("server_responding event never set.")
+                self.sock = ssl.wrap_socket(self.sock)
+
+        def call_after_accept(conn_to_client):
+            # This forces an immediate connection close via RST on .close().
+            set_socket_so_linger_on_with_zero_timeout(conn_to_client)
+            conn_to_client.send(
+                    b"HTTP/1.0 402 Payment Required\r\n"
+                    b"\r\n")
+            conn_to_client.close()  # RST
+            server_responding.set()
+            return True  # Tell the server to stop.
+
+        timeout = 2.0
+        server = self.SingleConnectionTestServerThread(
+                call_after_accept=call_after_accept,
+                name="non_tls_http_RST_responder",
+                timeout=timeout)
+        server.__enter__()  # starts it
+        self.addCleanup(server.__exit__)  # ... & unittest.TestCase stops it.
+        # Redundant; call_after_accept sets SO_LINGER on the accepted conn.
+        set_socket_so_linger_on_with_zero_timeout(server.listener)
+
+        connection = SynchronizedHTTPSConnection(
+                server.listener.getsockname()[0],
+                port=server.port,
+                timeout=timeout,
+        )
+        # There are lots of reasons this raises as desired, long before this
+        # test was added. Sending the request requires a successful TLS wrapped
+        # socket; that fails if the connection is broken. It may seem pointless
+        # to test this. It serves as an illustration of something that we never
+        # want to happen... properly not happening.
+        def failfunc():
+            connection.request("HEAD", "/test", headers={"Host": "localhost"})
+            response = connection.getresponse()
+        self.assertRaises(ssl.SSLError, failfunc)
+
+        server.join()
+
 
 def test_main(verbose=False):
     if skip_expected:
@@ -1246,7 +1477,7 @@ def test_main(verbose=False):
         not os.path.exists(NULLBYTECERT)):
         raise test_support.TestFailed("Can't read certificate files!")
 
-    tests = [BasicTests]
+    tests = [BasicTests, TestPreHandshakeClose]
 
     if test_support.is_resource_enabled('network'):
         tests.append(NetworkedTests)
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 8a4924b..5c1c00b 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -26,6 +26,8 @@ __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul
            "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
            "threading_cleanup", "reap_children", "strip_python_stderr"]
 
+SHORT_TIMEOUT = 30.0  # Added to make backporting from 3.x easier
+
 class Error(Exception):
     """Base class for regression test exceptions."""
 
-- 
2.34.1

