VAITP Dataset

← Back to the dataset

CVE-2023-37271

RestrictedPython (before versions 6.1 and 5.3) allowed code execution escape via generator expressions

  • CVSS 9.9
  • CWE-913 Improper Control of Dynamically-Managed Code Resources
  • Authentication, Authorization, and Session Management
  • Local

RestrictedPython is a tool that helps to define a subset of the Python language which allows users to provide a program input into a trusted environment. RestrictedPython does not check access to stack frames and their attributes. Stack frames are accessible within at least generators and generator expressions, which are allowed inside RestrictedPython. Prior to versions 6.1 and 5.3, an attacker with access to a RestrictedPython environment can write code that gets the current stack frame in a generator and then walk the stack all the way beyond the RestrictedPython invocation boundary, thus breaking out of the restricted sandbox and potentially allowing arbitrary code execution in the Python interpreter. All RestrictedPython deployments that allow untrusted users to write Python code in the RestrictedPython environment are at risk. In terms of Zope and Plone, this would mean deployments where the administrator allows untrusted users to create and/or edit objects of type `Script (Python)`, `DTML Method`, `DTML Document` or `Zope Page Template`. This is a non-default configuration and likely to be extremely rare. The problem has been fixed in versions 6.1 and 5.3.

CVSS base score
9.9
Published
2023-07-11
OWASP
A04 Insecure Design
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Authentication, Authorization, and Session Management
Subcategory
Privilege Escalation
Accessibility scope
Local
Impact
Arbitrary Code Execution
Affected component
RestrictedPython
Fixed by upgrading
Yes

Solution

Update to RestrictedPython version 6.1 or 5.3 or higher.

Vulnerable code sample

def vulnerable_generator():
    def inner():
        # Attempt to access the stack frame
        import inspect
        frame = inspect.currentframe().f_back
        return frame.f_globals

    yield from inner()

# Simulating execution in a RestrictedPython environment
for item in vulnerable_generator():
    print(item)  # This could potentially expose global variables or functions

Patched code sample

from RestrictedPython import compile_restricted

def safe_execute(code):
    # Compile the code in a restricted environment
    byte_code = compile_restricted(code, '<string>', 'exec')

    # Execute the compiled code in a restricted namespace
    exec(byte_code, {'__builtins__': {}})

# Example of potentially dangerous code that attempts to access stack frames
user_code = """
def generator():
    yield 1
    yield 2

for value in generator():
    print(value)
"""

# Safe execution of user code
safe_execute(user_code)

Published fix

Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.

Cite this entry

@misc{vaitp:cve202337271,
  title        = {{RestrictedPython (before versions 6.1 and 5.3) allowed code execution escape via generator expressions}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2023},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2023-37271},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-37271/}}
}

Download this entry as JSON Download the whole dataset

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 ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.