VAITP Dataset

← Back to the dataset

CVE-2022-21728

Tensorflow ReverseSequence Negative batch_dim Heap OOB Read

  • CVSS 8.1
  • CWE-125 Out-of-bounds Read
  • Memory Corruption
  • Remote

Tensorflow is an Open Source Machine Learning Framework. The implementation of shape inference for `ReverseSequence` does not fully validate the value of `batch_dim` and can result in a heap OOB read. There is a check to make sure the value of `batch_dim` does not go over the rank of the input, but there is no check for negative values. Negative dimensions are allowed in some cases to mimic Python's negative indexing (i.e., indexing from the end of the array), however if the value is too negative then the implementation of `Dim` would access elements before the start of an array. The fix will be included in TensorFlow 2.8.0. We will also cherrypick this commit on TensorFlow 2.7.1, TensorFlow 2.6.3, and TensorFlow 2.5.3, as these are also affected and still in supported range.

CVSS base score
8.1
Published
2022-02-03
OWASP
A03 Injection
Orthogonal defect classification
Checking
Code defect classification
Missing Check
Category
Memory Corruption
Subcategory
Out-of-Bound Accesses
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
TensorFlow
Fixed by upgrading
Yes

Solution

Ensure that negative values for batch_dim are properly checked and handled to prevent out-of-bounds access. Upgrade to TensorFlow 2.8.0 and is also being backported to TensorFlow 2.7.1, TensorFlow 2.6.3, and TensorFlow 2.5.3 to cover affected and supported versions

Vulnerable code sample

import tensorflow as tf

def reverse_sequence(input_tensor, seq_lengths, batch_dim):
    input_rank = tf.rank(input_tensor)

    if batch_dim < 0:
        if abs(batch_dim) >= input_rank:
            raise ValueError("Absolute value of batch_dim exceeds the rank of the input tensor")
    elif batch_dim >= input_rank:
        raise ValueError("batch_dim exceeds the rank of the input tensor")

    return tf.reverse_sequence(input_tensor, seq_lengths, seq_axis=batch_dim)

input_tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
seq_lengths = tf.constant([2, 3])
batch_dim = -1

try:
    output = reverse_sequence(input_tensor, seq_lengths, batch_dim)
    print(output)
except ValueError as e:
    print(f"Vulnerability triggered: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

Patched code sample

import tensorflow as tf

def reverse_sequence(input_tensor, seq_lengths, seq_axis, batch_axis):
    input_rank = tf.rank(input_tensor).numpy()

    if seq_axis < 0:
        seq_axis += input_rank
    if batch_axis < 0:
        batch_axis += input_rank

    if not (0 <= seq_axis < input_rank):
        raise ValueError("Invalid seq_axis: out of bounds for input tensor rank")
    if not (0 <= batch_axis < input_rank):
        raise ValueError("Invalid batch_axis: out of bounds for input tensor rank")
    if seq_axis == batch_axis:
        raise ValueError("seq_axis and batch_axis must be different")

    return tf.reverse_sequence(input_tensor, seq_lengths, seq_axis=seq_axis, batch_axis=batch_axis)

input_tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
seq_lengths = tf.constant([2, 3])
seq_axis = 1
batch_axis = 0

try:
    output = reverse_sequence(input_tensor, seq_lengths, seq_axis, batch_axis)
    print(output.numpy())
except ValueError as e:
    print(f"Validation error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Cite this entry

@misc{vaitp:cve202221728,
  title        = {{
Tensorflow ReverseSequence Negative batch_dim Heap OOB Read}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2022},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2022-21728},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-21728/}}
}
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 ::