CVE-2022-41893: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. If `tf.raw_ops.TensorListResize` is given a nonscalar value for input `size`, it results `CHECK` fail which can be used to trigger a denial of service attack. We have patched the issue in GitHub commit 888e34b49009a4e734c27ab0c43b0b5102682c56. The fix will be included in TensorFlow 2.11. We will also cherrypick this commit on TensorFlow 2.10.1, 2.9.3, and TensorFlow 2.8.4, as these are also affected and still in supported range.
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-11-18T22:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/list_kernels.cc, tensorflow/python/kernel_tests/data_structures/list_ops_test.py
Commit date: 2022-09-26 15:12:42 -0700
Commit message: Fix tf.raw_ops.TensorListResize vulnerability with non-scalar input.

Check that the size input is valid.
Add graph/eager unit tests. Graph mode was already ok but eager mode was not.

Note: This fix will have to be cherry picked in r2.10, r2.9, and r2.8.
PiperOrigin-RevId: 477002316

Diff: diff --git a/tensorflow/core/kernels/list_kernels.cc b/tensorflow/core/kernels/list_kernels.cc
index 72443a594c860..d0f16d6f50bf5 100644
--- a/tensorflow/core/kernels/list_kernels.cc
+++ b/tensorflow/core/kernels/list_kernels.cc
@@ -373,10 +373,12 @@ class TensorListResize : public OpKernel {
   explicit TensorListResize(OpKernelConstruction* c) : OpKernel(c) {}
 
   void Compute(OpKernelContext* c) override {
     const TensorList* input_list = nullptr;
     OP_REQUIRES_OK(c, GetInputList(c, 0, &input_list));
+    OP_REQUIRES(c, TensorShapeUtils::IsScalar(c->input(1).shape()),
+                errors::InvalidArgument("size must be a scalar"));
     int32_t size = c->input(1).scalar<int32>()();
     OP_REQUIRES(
         c, size >= 0,
         errors::InvalidArgument(
             "TensorListSlice expects size to be non-negative. Got: ", size));
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 b4e21c1b1f7e8..2817ff78a02fa 100644
--- a/tensorflow/python/kernel_tests/data_structures/list_ops_test.py
+++ b/tensorflow/python/kernel_tests/data_structures/list_ops_test.py
@@ -1656,10 +1656,19 @@ class ListOpsTest(test_util.TensorFlowTestCase, parameterized.TestCase):
         "TensorListSlice expects size to be non-negative"):
       l = list_ops.tensor_list_from_tensor([1., 2., 3.], element_shape=[])
       l = list_ops.tensor_list_resize(l, -1)
       self.evaluate(l)
 
+  @test_util.run_in_graph_and_eager_modes
+  def testResizeWithNonScalarFails(self):
+    l = list_ops.tensor_list_from_tensor([3, 4, 5], element_shape=[])
+    size = np.zeros([0, 2, 3, 3])
+    with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
+                                r"Shape must be rank 0 but is rank \d+|"
+                                r"\w+ must be a scalar"):
+      self.evaluate(gen_list_ops.TensorListResize(input_handle=l, size=size))
+
   @test_util.run_deprecated_v1
   @test_util.enable_control_flow_v2
   def testSkipEagerResizeGrad(self):
     t = constant_op.constant([1., 2., 3.])
     l = list_ops.tensor_list_from_tensor(t, element_shape=[])

Fixed code:     OP_REQUIRES(c, TensorShapeUtils::IsScalar(c->input(1).shape()),
                errors::InvalidArgument("size must be a scalar"));
  @test_util.run_in_graph_and_eager_modes
  def testResizeWithNonScalarFails(self):
    l = list_ops.tensor_list_from_tensor([3, 4, 5], element_shape=[])
    size = np.zeros([0, 2, 3, 3])
    with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
                                r"Shape must be rank 0 but is rank \d+|"
                                r"\w+ must be a scalar"):
      self.evaluate(gen_list_ops.TensorListResize(input_handle=l, size=size))