Skip to content

Out of bounds read in Tensorflow

High severity GitHub Reviewed Published Feb 2, 2022 in tensorflow/tensorflow • Updated Feb 3, 2023

Package

pip tensorflow (pip)

Affected versions

< 2.5.3
>= 2.6.0, < 2.6.3
= 2.7.0

Patched versions

2.5.3
2.6.3
2.7.1
pip tensorflow-cpu (pip)
< 2.5.3
>= 2.6.0, < 2.6.3
= 2.7.0
2.5.3
2.6.3
2.7.1
pip tensorflow-gpu (pip)
< 2.5.3
>= 2.6.0, < 2.6.3
= 2.7.0
2.5.3
2.6.3
2.7.1

Description

Impact

The implementation of shape inference for ReverseSequence does not fully validate the value of batch_dim and can result in a heap OOB read:

import tensorflow as tf

@tf.function
def test():
  y = tf.raw_ops.ReverseSequence(
    input = ['aaa','bbb'],
    seq_lengths = [1,1,1],
    seq_dim = -10,
    batch_dim = -10 )
  return y
    
test()

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:

  const int32_t input_rank = c->Rank(input);
  if (batch_dim >= input_rank) {
    return errors::InvalidArgument( 
        "batch_dim must be < input rank: ", batch_dim, " vs. ", input_rank);
  }
  // ...
  
  DimensionHandle batch_dim_dim = c->Dim(input, batch_dim);

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:

  DimensionHandle Dim(ShapeHandle s, int64_t idx) {
    if (!s.Handle() || s->rank_ == kUnknownRank) {
      return UnknownDim();
    }
    return DimKnownRank(s, idx);
  } 
·
  static DimensionHandle DimKnownRank(ShapeHandle s, int64_t idx) {
    CHECK_NE(s->rank_, kUnknownRank);
    if (idx < 0) {
      return s->dims_[s->dims_.size() + idx];
    }
    return s->dims_[idx];
  }

Patches

We have patched the issue in GitHub commit 37c01fb5e25c3d80213060460196406c43d31995.

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.

For more information

Please consult our security guide for more information regarding the security model and how to contact us with issues and questions.

Attribution

This vulnerability has been reported by Yu Tian of Qihoo 360 AIVul Team.

References

@mihaimaruseac mihaimaruseac published to tensorflow/tensorflow Feb 2, 2022
Published by the National Vulnerability Database Feb 3, 2022
Reviewed Feb 3, 2022
Published to the GitHub Advisory Database Feb 9, 2022
Last updated Feb 3, 2023

Severity

High

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
Low
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
None
Availability
High

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:H

EPSS score

0.731%
(81st percentile)

Weaknesses

CVE ID

CVE-2022-21728

GHSA ID

GHSA-6gmv-pjp9-p8w8
Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.