CVE-2022-41883: Out-of-bounds Read (CWE-125)
value: TensorFlow is an open source platform for machine learning. When ops that have specified input sizes receive a differing number of inputs, the executor will crash. We have patched the issue in GitHub commit f5381e0e10b5a61344109c1b7c174c68110f7629. 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 reads data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 7.5
Published: 2022-11-18T21:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/common_runtime/eager/execute.cc, tensorflow/python/kernel_tests/data_structures/dynamic_stitch_op_test.py
Commit date: 2022-09-30 13:39:28 -0700
Commit message: Fix OOB error when op input sizes do not match.

In cases where op input sizes are specified as in
```
REGISTER_OP("DynamicStitch")
    .Input("indices: N * int32")
    .Input("data: N * T")
    .Output("merged: T")
    .Attr("N : int >= 1")
    .Attr("T : type")
    .SetShapeFn(DynamicStitchShapeFunction);
```
if differing number of inputs are provided (e.g. 3 for `indices` and 4 for `data`)
we can get a crash in the executor when parsing the inputs, even before the kernel
called.  Here we avoid this by checking the return code for the argument id and
exit early.

PiperOrigin-RevId: 478068540

Diff: diff --git a/tensorflow/core/common_runtime/eager/execute.cc b/tensorflow/core/common_runtime/eager/execute.cc
index 9ec18b8f99fdb..5e747d436489b 100644
--- a/tensorflow/core/common_runtime/eager/execute.cc
+++ b/tensorflow/core/common_runtime/eager/execute.cc
@@ -311,10 +311,14 @@ bool IsHostMemoryArg(const EagerOperation& op, const NodeDef* node_def,
   if (node_def == nullptr) return false;
   if (kernel_def == nullptr || op_device == nullptr) return false;
   const auto& host_memory_args = kernel_def->host_memory_arg();
   const OpDef& op_def = OpRegistry::Global()->LookUp(op.Name())->op_def;
   const int arg_id = OpPortIdToArgId(*node_def, op_def.input_arg(), port_id);
+  // Fail if argument ID not found.
+  if (arg_id < 0) {
+    return false;
+  }
   return std::find(host_memory_args.begin(), host_memory_args.end(),
                    op_def.input_arg(arg_id).name()) != host_memory_args.end();
 }
 
 Status GetDeviceForInput(const EagerOperation& op, const EagerContext& ctx,
diff --git a/tensorflow/python/kernel_tests/data_structures/dynamic_stitch_op_test.py b/tensorflow/python/kernel_tests/data_structures/dynamic_stitch_op_test.py
index 28dc151aec6eb..05e0cb6be2925 100644
--- a/tensorflow/python/kernel_tests/data_structures/dynamic_stitch_op_test.py
+++ b/tensorflow/python/kernel_tests/data_structures/dynamic_stitch_op_test.py
@@ -16,10 +16,11 @@
 
 import numpy as np
 
 from tensorflow.python.framework import constant_op
 from tensorflow.python.framework import dtypes
+from tensorflow.python.framework import errors
 from tensorflow.python.framework import test_util
 from tensorflow.python.ops import array_ops
 from tensorflow.python.ops import data_flow_ops
 from tensorflow.python.ops import math_ops
 from tensorflow.python.ops import gradients_impl
@@ -306,8 +307,31 @@ class ParallelDynamicStitchTest(DynamicStitchTestBase, test.TestCase):
                                      stitched_grad)
     self.assertEqual(grads[:3], [None] * 3)  # Indices have no gradients
     for datum, grad in zip(data, self.evaluate(grads[3:])):
       self.assertAllEqual(7.0 * self.evaluate(datum), grad)
 
+  @test_util.run_in_graph_and_eager_modes
+  def testMismatchedDataAndIndexListSizes(self):
+    indices = [
+        constant_op.constant([2]),
+        constant_op.constant([1]),
+        constant_op.constant([0]),
+        constant_op.constant([3]),
+    ]
+    data = [
+        constant_op.constant([1.0]),
+        constant_op.constant([2.0]),
+        constant_op.constant([3.0]),
+        constant_op.constant([4.0])
+    ]
+    with self.assertRaisesRegex(
+        (ValueError, errors.InvalidArgumentError),
+        "expected inputs .* do not match|List argument .* must match"):
+      self.evaluate(data_flow_ops.dynamic_stitch(indices[0:2], data))
+
+    with self.assertRaisesRegex(
+        (ValueError, errors.InvalidArgumentError),
+        "expected inputs .* do not match|List argument .* must match"):
+      self.evaluate(data_flow_ops.dynamic_stitch(indices, data[0:2]))
 
 if __name__ == "__main__":
   test.main()

Fixed code:   // Fail if argument ID not found.
  if (arg_id < 0) {
    return false;
  }
from tensorflow.python.framework import errors
  @test_util.run_in_graph_and_eager_modes
  def testMismatchedDataAndIndexListSizes(self):
    indices = [
        constant_op.constant([2]),
        constant_op.constant([1]),
        constant_op.constant([0]),
        constant_op.constant([3]),
    ]
    data = [
        constant_op.constant([1.0]),
        constant_op.constant([2.0]),
        constant_op.constant([3.0]),
        constant_op.constant([4.0])
    ]
    with self.assertRaisesRegex(
        (ValueError, errors.InvalidArgumentError),
        "expected inputs .* do not match|List argument .* must match"):
      self.evaluate(data_flow_ops.dynamic_stitch(indices[0:2], data))

    with self.assertRaisesRegex(
        (ValueError, errors.InvalidArgumentError),
        "expected inputs .* do not match|List argument .* must match"):
      self.evaluate(data_flow_ops.dynamic_stitch(indices, data[0:2]))