VAITP Dataset

← Back to the dataset

CVE-2022-45907

PyTorch eval vulnerability allows arbitrary code execution

  • CVSS 9.8
  • CWE-94
  • Input Validation and Sanitization
  • Remote

In PyTorch before trunk/89695, torch.jit.annotations.parse_type_line can cause arbitrary code execution because eval is used unsafely.

CVSS base score
9.8
Published
2022-11-26
OWASP
A08 Software and Data Integrity Failures
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Input Validation and Sanitization
Subcategory
Insecure Parsing or Deserialization
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
PyTorch
Fixed by upgrading
Yes

Solution

Update PyTorch to version trunk/89695 or higher.

Vulnerable code sample

def unsafe_eval(expr):
    # This function uses eval unsafely, allowing arbitrary code execution
    return eval(expr)

# Example usage
type_line = "__import__('os').system('echo Vulnerable!')"  # Example of a malicious expression
result = unsafe_eval(type_line)  # This will execute the command

Patched code sample

import ast

def safe_eval(expr):
    # Only allow a limited set of operations for safety
    allowed_nodes = {
        ast.Expression,
        ast.Num,
        ast.Str,
        ast.List,
        ast.Tuple,
        ast.Dict,
        ast.Name,
        ast.Load,
        ast.BinOp,
        ast.Add,
        ast.Sub,
        ast.Mult,
        ast.Div,
        ast.Pow,
        ast.UAdd,
        ast.USub,
    }
    
    tree = ast.parse(expr, mode='eval')
    
    for node in ast.walk(tree):
        if type(node) not in allowed_nodes:
            raise ValueError("Unsafe operation detected")
    
    return eval(compile(tree, filename='', mode='eval'))

# Example usage
type_line = "1 + 2"  # Example of a safe expression
result = safe_eval(type_line)
print(result)  # Output: 3

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.

Payload

curl -X POST -d "config=__import__('os').system('cat /etc/passwd')" http://localhost:5000/update_config

Cite this entry

@misc{vaitp:cve202245907,
  title        = {{PyTorch eval vulnerability allows arbitrary code execution}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2022},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2022-45907},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-45907/}}
}

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.