CVE-2022-35991: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. When `TensorListScatter` and `TensorListScatterV2` receive an `element_shape` of a rank greater than one, they give a `CHECK` fail that can trigger a denial of service attack. We have patched the issue in GitHub commit bb03fdf4aae944ab2e4b35c7daa051068a8b7f61. 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-16T23:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/list_kernels.h, tensorflow/python/kernel_tests/data_structures/list_ops_test.py
Commit date: 2022-07-21 13:43:22 -0700
Commit message: Check for element_shape in TensorListScatter and TensorListScatterV2

PiperOrigin-RevId: 462460346

Diff: diff --git a/tensorflow/core/kernels/list_kernels.h b/tensorflow/core/kernels/list_kernels.h
index 51a3eb306e74d..96872c5486a33 100644
--- a/tensorflow/core/kernels/list_kernels.h
+++ b/tensorflow/core/kernels/list_kernels.h
@@ -893,10 +893,15 @@ class TensorListScatter : public OpKernel {
     AllocatorAttributes attr;
     attr.set_on_host(true);
     OP_REQUIRES_OK(c, c->allocate_output(0, {}, &output_tensor, attr));
     Tensor indices = c->input(1);
     PartialTensorShape element_shape;
+    OP_REQUIRES(
+        c, !TensorShapeUtils::IsMatrixOrHigher(c->input(2).shape()),
+        errors::InvalidArgument(
+            "TensorListScatter: element_shape must be at most rank 1 but has ",
+            "the shape of ", c->input(2).shape().DebugString()));
     OP_REQUIRES_OK(c, TensorShapeFromTensor(c->input(2), &element_shape));
     // TensorListScatterV2 passes the num_elements input, TensorListScatter does
     // not.
     int num_elements = c->num_inputs() >= 4 ? c->input(3).scalar<int>()() : -1;
     OP_REQUIRES(c, num_elements >= -1,
diff --git a/tensorflow/python/kernel_tests/data_structures/list_ops_test.py b/tensorflow/python/kernel_tests/data_structures/list_ops_test.py
index 796786aa3061a..7e7ad79e78a33 100644
--- a/tensorflow/python/kernel_tests/data_structures/list_ops_test.py
+++ b/tensorflow/python/kernel_tests/data_structures/list_ops_test.py
@@ -479,10 +479,34 @@ class ListOpsTest(test_util.TensorFlowTestCase, parameterized.TestCase):
     l = gen_list_ops.tensor_list_scatter_v2(
         c0, [1, 3], list_ops._build_element_shape([]), num_elements=5)
     # TensorListScatter should return a list with size num_elements.
     self.assertAllEqual(list_ops.tensor_list_length(l), 5)
 
+  def testScatterFailsWhenElementShapeIsNotVector(self):
+    c0 = constant_op.constant([1.0, 2.0])
+    # In Eager mode, InvalidArgumentError is generated by the Compute function.
+    # In graph mode, ValueError is generated by the shape function.
+    with self.assertRaisesRegex(
+        (errors.InvalidArgumentError, ValueError),
+        "must be at most rank 1"):
+      l = gen_list_ops.tensor_list_scatter(
+          # Wrong element_shape. Should be at most rank 1.
+          c0, [1, 3], element_shape=[[1]])
+      self.evaluate(l)
+
+  def testScatterV2FailsWhenElementShapeIsNotVector(self):
+    c0 = constant_op.constant([1.0, 2.0])
+    # In Eager mode, InvalidArgumentError is generated by the Compute function.
+    # In graph mode, ValueError is generated by the shape function.
+    with self.assertRaisesRegex(
+        (errors.InvalidArgumentError, ValueError),
+        "must be at most rank 1"):
+      l = gen_list_ops.tensor_list_scatter_v2(
+          # Wrong element_shape. Should be at most rank 1.
+          c0, [1, 3], element_shape=[[1]], num_elements=2)
+      self.evaluate(l)
+
   def testScatterFailsWhenIndexLargerThanNumElements(self):
     c0 = constant_op.constant([1.0, 2.0])
     with self.assertRaisesRegex(
         errors.InvalidArgumentError,
         "TensorListScatter: Trying to scatter at index 3 in list with size 3"):

Fixed code:     OP_REQUIRES(
        c, !TensorShapeUtils::IsMatrixOrHigher(c->input(2).shape()),
        errors::InvalidArgument(
            "TensorListScatter: element_shape must be at most rank 1 but has ",
            "the shape of ", c->input(2).shape().DebugString()));
  def testScatterFailsWhenElementShapeIsNotVector(self):
    c0 = constant_op.constant([1.0, 2.0])
    # In Eager mode, InvalidArgumentError is generated by the Compute function.
    # In graph mode, ValueError is generated by the shape function.
    with self.assertRaisesRegex(
        (errors.InvalidArgumentError, ValueError),
        "must be at most rank 1"):
      l = gen_list_ops.tensor_list_scatter(
          # Wrong element_shape. Should be at most rank 1.
          c0, [1, 3], element_shape=[[1]])
      self.evaluate(l)

  def testScatterV2FailsWhenElementShapeIsNotVector(self):
    c0 = constant_op.constant([1.0, 2.0])
    # In Eager mode, InvalidArgumentError is generated by the Compute function.
    # In graph mode, ValueError is generated by the shape function.
    with self.assertRaisesRegex(
        (errors.InvalidArgumentError, ValueError),
        "must be at most rank 1"):
      l = gen_list_ops.tensor_list_scatter_v2(
          # Wrong element_shape. Should be at most rank 1.
          c0, [1, 3], element_shape=[[1]], num_elements=2)
      self.evaluate(l)