VAITP Dataset

← Back to the dataset

CVE-2020-15193

TensorFlow < 2.2.1 and 2.3.1: Uninitialized memory corruption in dlpack.to_dlpack due to Python object mishandling

  • CVSS 7.1
  • CWE-908 Use of Uninitialized Resource
  • Input Validation and Sanitization
  • Local

In Tensorflow before versions 2.2.1 and 2.3.1, the implementation of `dlpack.to_dlpack` can be made to use uninitialized memory resulting in further memory corruption. This is because the pybind11 glue code assumes that the argument is a tensor. However, there is nothing stopping users from passing in a Python object instead of a tensor. The uninitialized memory address is due to a `reinterpret_cast` Since the `PyObject` is a Python object, not a TensorFlow Tensor, the cast to `EagerTensor` fails. The issue is patched in commit 22e07fb204386768e5bcbea563641ea11f96ceb8 and is released in TensorFlow versions 2.2.1, or 2.3.1.

CVSS base score
7.1
Published
2020-09-25
OWASP
A06 Vulnerable and Outdated Components
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Input Validation and Sanitization
Subcategory
Insecure Parsing or Deserialization
Accessibility scope
Local
Impact
Arbitrary Code Execution
Affected component
TensorFlow
Fixed by upgrading
Yes

Solution

Update TensorFlow to version 2.2.1 or 2.3.1.

Vulnerable code sample

import tensorflow as tf
import pybind11

# Vulnerable implementation of to_dlpack
def to_dlpack(obj):
    # This code does not check if 'obj' is a Tensor
    tensor_ptr = pybind11.cast(obj)  # Unsafe cast, assumes obj is a Tensor
    return tensor_ptr

# Example usage that triggers the vulnerability
# Passing a non-tensor object, which could lead to uninitialized memory access
vulnerable_output = to_dlpack("not_a_tensor")  # This could cause memory corruption

Patched code sample

import tensorflow as tf
import pybind11

# Simulating the vulnerability
def unsafe_to_dlpack(obj):
    # This simulates the unsafe behavior of assuming 'obj' is a Tensor
    if not isinstance(obj, tf.Tensor):
        raise TypeError("Expected a Tensor, got: {}".format(type(obj)))
    
    # Simulated reinterpret_cast that could lead to uninitialized memory access
    tensor_ptr = pybind11.cast(obj)
    return tensor_ptr

# Example usage that would trigger the vulnerability
try:
    unsafe_to_dlpack("not_a_tensor")  # Passing a string instead of a Tensor
except TypeError as e:
    print(e)

# Fixing the vulnerability
def safe_to_dlpack(obj):
    # Check if the input is a Tensor before proceeding
    if not isinstance(obj, tf.Tensor):
        raise TypeError("Expected a Tensor, got: {}".format(type(obj)))

    # Safe conversion to dlpack
    tensor_ptr = pybind11.cast(obj)
    return tensor_ptr

# Example usage with the fix
try:
    safe_to_dlpack("not_a_tensor")  # This will now raise a TypeError
except TypeError as e:
    print(e)

Cite this entry

@misc{vaitp:cve202015193,
  title        = {{TensorFlow < 2.2.1 and 2.3.1: Uninitialized memory corruption in dlpack.to_dlpack due to Python object mishandling}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2020},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2020-15193},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2020-15193/}}
}

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.