VAITP Dataset

← Back to the dataset

CVE-2022-25882

onnx < 1.13.0: Directory Traversal in external_data field

  • CVSS 7.5
  • CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
  • Input Validation and Sanitization
  • Local

Versions of the package onnx before 1.13.0 are vulnerable to Directory Traversal as the external_data field of the tensor proto can have a path to the file which is outside the model current directory or user-provided directory, for example "../../../etc/passwd"

CVSS base score
7.5
Published
2023-01-26
OWASP
A06 Vulnerable and Outdated Components
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Input Validation and Sanitization
Subcategory
Path Traversal
Accessibility scope
Local
Impact
Information Disclosure
Fixed by upgrading
Yes

Solution

Update onnx to version 1.13.0 or higher.

Vulnerable code sample

import onnx

def load_onnx_model(model_path):
    model = onnx.load(model_path)

    for tensor in model.graph.initializer:
        if tensor.external_data:
            external_data_path = tensor.external_data
            print(f"Loading external data from: {external_data_path}")

    return model

Patched code sample

import os
import hashlib
from pathlib import Path
from typing import Optional, Set
import onnx

def load_onnx_model(
    model_path: str,
    allowed_dir: str,
    expected_hash: Optional[str] = None,
    max_size: int = 100 * 1024 * 1024  # 100MB
) -> onnx.ModelProto:
    
    try:
        model_path = os.path.abspath(model_path)
        allowed_dir = os.path.abspath(allowed_dir)
        
        if not model_path.startswith(allowed_dir):
            raise ValueError("Model path outside allowed directory")
            
        if not os.path.isfile(model_path):
            raise ValueError("Model file not found")
            
        model_size = os.path.getsize(model_path)
        if model_size > max_size:
            raise ValueError(f"Model too large: {model_size} bytes")
            
        if expected_hash:
            with open(model_path, 'rb') as f:
                file_hash = hashlib.sha256(f.read()).hexdigest()
            if file_hash != expected_hash:
                raise ValueError("Model hash mismatch")
                
        model = onnx.load(model_path)
        
        onnx.checker.check_model(model)
        
        for tensor in model.graph.initializer:
            if tensor.HasField('data_location') and tensor.data_location == onnx.TensorProto.EXTERNAL:
                for entry in tensor.external_data:
                    if entry.key == 'location':
                        data_path = os.path.abspath(
                            os.path.join(os.path.dirname(model_path), entry.value)
                        )
                        if not data_path.startswith(allowed_dir):
                            raise ValueError(
                                f"External data path outside allowed directory: {entry.value}"
                            )
                            
        return model
        
    except onnx.checker.ValidationError as e:
        raise ValueError(f"Invalid ONNX model: {str(e)}")
    except Exception as e:
        raise ValueError(f"Error loading model: {str(e)}")

Cite this entry

@misc{vaitp:cve202225882,
  title        = {{onnx < 1.13.0: Directory Traversal in external_data field}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2023},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2022-25882},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-25882/}}
}
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 ::