VAITP Dataset

← Back to the dataset

CVE-2024-12254

Python 3.12.0+ asyncio.writelines() may not drain write buffers, causing memory exhaustion on macOS/Linux.

  • CVSS 8.7
  • CWE-400
  • Resource Management
  • Local

Starting in Python 3.12.0, the asyncio._SelectorSocketTransport.writelines() method would not "pause" writing and signal to the Protocol to drain the buffer to the wire once the write buffer reached the "high-water mark". Because of this, Protocols would not periodically drain the write buffer potentially leading to memory exhaustion. This vulnerability likely impacts a small number of users, you must be using Python 3.12.0 or later, on macOS or Linux, using the asyncio module with protocols, and using .writelines() method which had new zero-copy-on-write behavior in Python 3.12.0 and later. If not all of these factors are true then your usage of Python is unaffected.

CVSS base score
8.7
Published
2024-12-06
OWASP
A07 Resource Exhaustion
Orthogonal defect classification
Timing/Serialization
Code defect classification
Timing Issues
Category
Resource Management
Subcategory
Resource Exhaustion
Accessibility scope
Local
Impact
Denial of Service (DoS)
Affected component
Python
Fixed by upgrading
Yes

Solution

Upgrade to Python 3.12.1 or later.

Vulnerable code sample

import asyncio

class ModifiedProtocol(asyncio.Protocol):
    def __init__(self):
        self.buffer = bytearray()

    def connection_made(self, transport):
        self.transport = transport

    def data_received(self, data):
        pass 

    def connection_lost(self, exc):
        pass 

    def write_data(self, data):
        self.buffer.extend(data)


async def main():
    loop = asyncio.get_event_loop()
    transport, protocol = await loop.create_connection(lambda: ModifiedProtocol(), '127.0.0.1', 8888)

    large_data = b'a' * (1024 * 1024 * 10) 

    protocol.write_data(large_data) 

    loop.run_until_complete(asyncio.sleep(1))

    transport.close()
    loop.close()


if __name__ == "__main__":
    import socket
    try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind(('127.0.0.1',8888))
      s.listen(1)
    except OSError:
      print("Port 8888 is in use, try using another port")
      exit()

    asyncio.run(main())

Patched code sample

import asyncio

class ModifiedProtocol(asyncio.Protocol):
    def __init__(self, max_buffer_size=1024 * 1024): 
        self.buffer = bytearray()
        self.max_buffer_size = max_buffer_size

    def connection_made(self, transport):
        self.transport = transport

    def data_received(self, data):
        if len(self.buffer) + len(data) > self.max_buffer_size:
            self.transport.close()
            print("Data size limit exceeded, connection closed.")
            return

        self.buffer.extend(data)
        print(f"Received data, current buffer size: {len(self.buffer)} bytes")

    def connection_lost(self, exc):
        if exc:
            print(f"Connection lost: {exc}")
        else:
            print("Connection closed gracefully.")

    def write_data(self, data):
        if len(self.buffer) + len(data) > self.max_buffer_size:
            print("Buffer size exceeded, unable to write more data.")
            return

        self.buffer.extend(data)
        print(f"Data written to buffer, buffer size: {len(self.buffer)} bytes")


async def main():
    loop = asyncio.get_event_loop()
    transport, protocol = await loop.create_connection(
        lambda: ModifiedProtocol(),
        '127.0.0.1',
        8888
    )

    large_data = b'a' * (1024 * 1024 * 10)
    protocol.write_data(large_data)

    await asyncio.sleep(1)
    transport.close()
    loop.close()


if __name__ == "__main__":
    import socket
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('127.0.0.1', 8888))
        s.listen(1)
    except OSError:
        print("Port 8888 is in use, try using another port.")
        exit()

    asyncio.run(main())

Cite this entry

@misc{vaitp:cve202412254,
  title        = {{Python 3.12.0+ asyncio.writelines() may not drain write buffers, causing memory exhaustion on macOS/Linux.
}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2024},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2024-12254},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-12254/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::