CVE-2022-35934: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. The implementation of tf.reshape op in TensorFlow is vulnerable to a denial of service via CHECK-failure (assertion failure) caused by overflowing the number of elements in a tensor. This issue has been patched in GitHub commit 61f0f9b94df8c0411f0ad0ecc2fec2d3f3c33555. The fix will be included in TensorFlow 2.10.0. We will also cherrypick this commit on TensorFlow 2.9.1, TensorFlow 2.8.1, and TensorFlow 2.7.2, as these are also affected and still in supported range. There are no known workarounds for this issue.
CWE description: The product contains an assert() or similar statement that can be triggered by an attacker, which leads to an application exit or other behavior that is more severe than necessary.
CVSS v3 base score: 7.5
Published: 2022-09-16T20:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/reshape_op.h, tensorflow/python/kernel_tests/array_ops/array_ops_test.py
Commit date: 2022-05-19 20:34:09 -0700
Commit message: Fix failed check in tf.reshape.

Passing in too many dimensions causes an assertion
failure and a crash. Check input as a precondition and
fail gracefully.

PiperOrigin-RevId: 449893553

Diff: diff --git a/tensorflow/core/kernels/reshape_op.h b/tensorflow/core/kernels/reshape_op.h
index cd8ffefdff227..9f6dd2c156cea 100644
--- a/tensorflow/core/kernels/reshape_op.h
+++ b/tensorflow/core/kernels/reshape_op.h
@@ -43,10 +43,15 @@ class ReshapeOp : public OpKernel {
         (TensorShapeUtils::IsVector(sizes.shape()) ||
          // TODO(rmlarsen): Disallow legacy use of scalars to represent shape.
          TensorShapeUtils::IsScalar(sizes.shape())),
         errors::InvalidArgument("sizes input must be 1-D, not ",
                                 sizes.shape().DebugString()));
+    OP_REQUIRES(
+        context, sizes.NumElements() < TensorShape::MaxDimensions(),
+        errors::InvalidArgument("too many dimensions: must be < ",
+                                TensorShape::MaxDimensions(), ", but received ",
+                                sizes.NumElements()));
 
     // Compute the output shape.  Determine product of specified
     // dimensions, and find the index of the unspecified one.
     TensorShape shape;
     int64_t product = 1;
diff --git a/tensorflow/python/kernel_tests/array_ops/array_ops_test.py b/tensorflow/python/kernel_tests/array_ops/array_ops_test.py
index 4d3e9bd79598e..e66654e1157ef 100644
--- a/tensorflow/python/kernel_tests/array_ops/array_ops_test.py
+++ b/tensorflow/python/kernel_tests/array_ops/array_ops_test.py
@@ -349,10 +349,19 @@ class OperatorShapeTest(test_util.TensorFlowTestCase):
   def testExpandDimsWithNonScalarDim(self):
     with self.assertRaisesRegex(Exception,
                                 "must be a tensor with a single value"):
       array_ops.expand_dims(1, axis=[0, 1])
 
+  def testReshapeWithManyDims(self):
+    with self.assertRaisesRegex(errors.InvalidArgumentError,
+                                "too many dimensions"):
+      self.evaluate(
+          array_ops.reshape(
+              tensor=[[1]],
+              shape=constant_op.constant([1 for i in range(254)],
+                                         dtype=dtypes.int64)))
+
 
 @test_util.with_eager_op_as_function
 class ReverseV2Test(test_util.TensorFlowTestCase):
 
   def testReverse0DimAuto(self):

Fixed code:     OP_REQUIRES(
        context, sizes.NumElements() < TensorShape::MaxDimensions(),
        errors::InvalidArgument("too many dimensions: must be < ",
                                TensorShape::MaxDimensions(), ", but received ",
                                sizes.NumElements()));
  def testReshapeWithManyDims(self):
    with self.assertRaisesRegex(errors.InvalidArgumentError,
                                "too many dimensions"):
      self.evaluate(
          array_ops.reshape(
              tensor=[[1]],
              shape=constant_op.constant([1 for i in range(254)],
                                         dtype=dtypes.int64)))