{"id":1028,"cve":"CVE-2024-3219","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2024-3219\/","cwe":"CWE-306","cwe_number":306,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/306","cvss_score":5.1,"published":"2024-07-29","owasp":"A01 Broken Access Control","odc":"Interface","cdc":"Incorrect Interface","category":"Race Conditions","subcategory":"Time-of-Check to Time-of-Use","scope":"Local","impact":"Unauthorized Access","summary":"Local socket connection race vulnerability in Python's socket module.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2024-3219\/","description":"The\n \u201csocket\u201d module provides a pure-Python fallback to the \nsocket.socketpair() function for platforms that don\u2019t support AF_UNIX, \nsuch as Windows. This pure-Python implementation uses AF_INET or \nAF_INET6 to create a local connected pair of sockets. The connection \nbetween the two sockets was not verified before passing the two sockets \nback to the user, which leaves the server socket vulnerable to a \nconnection race from a malicious local peer.\n\nPlatforms that support AF_UNIX such as Linux and macOS are not affected by this vulnerability. Versions prior to CPython 3.5 are not affected due to the vulnerable API not being included.","solution":"Upgrade to CPython 3.5 or later to mitigate the vulnerability.","affected_component":"Python 3.5","fixed_by_upgrading":true,"vulnerable_code":"import socket\n\ndef create_socket_pair():\n    \"\"\"Vulnerable function that demonstrates the security issue.\"\"\"\n    # Create a pair of connected sockets using AF_INET\n    sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n    # Bind the sockets to localhost and a free port\n    sock1.bind(('localhost', 0))\n    sock2.bind(('localhost', 0))\n\n    # Get the port number assigned to sock1\n    port = sock1.getsockname()[1]\n\n    # Here, the connection is not verified before returning the sockets\n    sock2.connect(('localhost', port))\n\n    return sock1, sock2\n\n# Example usage\nsock1, sock2 = create_socket_pair()\nprint(\"Socket pair created (vulnerable to connection race).\")","patched_code":"import socket\n\ndef create_socket_pair():\n    \"\"\"Secure function that fixes the vulnerability.\"\"\"\n    # Create a pair of connected sockets using AF_INET\n    sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n    # Bind the sockets to localhost and a free port\n    sock1.bind(('localhost', 0))\n    sock2.bind(('localhost', 0))\n\n    # Get the port number assigned to sock1\n    port = sock1.getsockname()[1]\n\n    # Connect sock2 to sock1\n    sock2.connect(('localhost', port))\n\n    # Listen on sock1 to accept the connection from sock2\n    sock1.listen(1)\n    conn, _ = sock1.accept()\n\n    return conn, sock2\n\n# Example usage\nconn, sock2 = create_socket_pair()\nprint(\"Connected sockets created successfully.\")","payload_needed":true,"payload":"```python\nimport socket\nimport threading\n\n# Malicious payload to exploit the connection race\ndef malicious_socket():\n    # Create a socket and connect to the server socket\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock.connect(('localhost', 12345))  # Assuming the server is bound to this port\n    # Send malicious data\n    sock.sendall(b'Malicious data')\n    sock.close()\n\n# Start the malicious thread\nthread = threading.Thread(target=malicious_socket)\nthread.start()\n```","payload_status":"available","published_fix":[{"repository":"python\/cpython","commit_url":"https:\/\/github.com\/python\/cpython\/commit\/06fa244666ec6335a3b9bf2367e31b42b9a89b20","commit_subject":"[3.9] gh-122133: Authenticate socket connection for `socket.socketpair()` fallback (GH-122134) (#122428)","units":[{"file":"Lib\/socket.py","function":null}]},{"repository":"python\/cpython","commit_url":"https:\/\/github.com\/python\/cpython\/commit\/5df322e91a40909e6904bbdbc0c3a6b6a9eead39","commit_subject":"[3.12] gh-122133: Rework pure Python socketpair tests to avoid use of importlib.reload. (GH-122493) (GH-122505)","units":[{"file":"Lib\/socket.py","function":null}]}],"references":null}