CVE-2022-36001: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. When `DrawBoundingBoxes` receives an input `boxes` that is not of dtype `float`, it gives a `CHECK` fail that can trigger a denial of service attack. We have patched the issue in GitHub commit da0d65cdc1270038e72157ba35bf74b85d9bda11. 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/image/draw_bounding_box_op.cc, tensorflow/python/kernel_tests/image_ops/draw_bounding_box_op_test.py
Commit date: 2022-07-18 22:47:37 -0700
Commit message: Fix dtype bug in draw bounding boxes.

Boxes always needs to be type `float`.

PiperOrigin-RevId: 461800676

Diff: diff --git a/tensorflow/core/kernels/image/draw_bounding_box_op.cc b/tensorflow/core/kernels/image/draw_bounding_box_op.cc
index 0ce1f3fa8a829..d1a5b59146fde 100644
--- a/tensorflow/core/kernels/image/draw_bounding_box_op.cc
+++ b/tensorflow/core/kernels/image/draw_bounding_box_op.cc
@@ -117,11 +117,11 @@ class DrawBoundingBoxesOp : public OpKernel {
     output->tensor<T, 4>() = images.tensor<T, 4>();
     auto canvas = output->tensor<T, 4>();
 
     for (int64_t b = 0; b < batch_size; ++b) {
       const int64_t num_boxes = boxes.dim_size(1);
-      const auto tboxes = boxes.tensor<T, 3>();
+      const auto tboxes = boxes.tensor<float, 3>();
       for (int64_t bb = 0; bb < num_boxes; ++bb) {
         int64_t color_index = bb % color_table.size();
         const int64_t min_box_row =
             static_cast<float>(tboxes(b, bb, 0)) * (height - 1);
         const int64_t min_box_row_clamp =
diff --git a/tensorflow/python/kernel_tests/image_ops/draw_bounding_box_op_test.py b/tensorflow/python/kernel_tests/image_ops/draw_bounding_box_op_test.py
index 5b695e861136b..91fcd9351680a 100644
--- a/tensorflow/python/kernel_tests/image_ops/draw_bounding_box_op_test.py
+++ b/tensorflow/python/kernel_tests/image_ops/draw_bounding_box_op_test.py
@@ -48,15 +48,20 @@ class DrawBoundingBoxOpTest(test.TestCase):
     image[0:height, width - 1, 0:depth] = color
     image[0, 0:width, 0:depth] = color
     image[height - 1, 0:width, 0:depth] = color
     return image
 
-  def _testDrawBoundingBoxColorCycling(self, img, colors=None):
+  def _testDrawBoundingBoxColorCycling(self,
+                                       img,
+                                       dtype=dtypes.float32,
+                                       colors=None):
     """Tests if cycling works appropriately.
 
     Args:
       img: 3-D numpy image on which to draw.
+      dtype: image dtype (float, half).
+      colors: color table.
     """
     color_table = colors
     if colors is None:
       # THIS TABLE MUST MATCH draw_bounding_box_op.cc
       color_table = np.asarray([[1, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1],
@@ -80,11 +85,11 @@ class DrawBoundingBoxOpTest(test.TestCase):
       bboxes = np.asarray([0, 0, 1, 1])
       bboxes = np.vstack([bboxes for _ in range(num_boxes)])
       bboxes = math_ops.cast(bboxes, dtypes.float32)
       bboxes = array_ops.expand_dims(bboxes, 0)
       image = ops.convert_to_tensor(image)
-      image = image_ops_impl.convert_image_dtype(image, dtypes.float32)
+      image = image_ops_impl.convert_image_dtype(image, dtype)
       image = array_ops.expand_dims(image, 0)
       image = image_ops.draw_bounding_boxes(image, bboxes, colors=colors)
       with self.cached_session(use_gpu=False) as sess:
         op_drawn_image = np.squeeze(sess.run(image), 0)
         self.assertAllEqual(test_drawn_image, op_drawn_image)
@@ -116,8 +121,16 @@ class DrawBoundingBoxOpTest(test.TestCase):
     image = np.zeros([10, 10, 4], "float32")
     colors = np.asarray([[0.5, 0, 0.5, 1], [0.5, 0.5, 0, 1], [0.5, 0, 0, 1],
                          [0, 0, 0.5, 1]])
     self._testDrawBoundingBoxColorCycling(image, colors=colors)
 
+  def testDrawBoundingBoxHalf(self):
+    """Test if RGBA color cycling works correctly with provided colors."""
+    image = np.zeros([10, 10, 4], "float32")
+    colors = np.asarray([[0.5, 0, 0.5, 1], [0.5, 0.5, 0, 1], [0.5, 0, 0, 1],
+                         [0, 0, 0.5, 1]])
+    self._testDrawBoundingBoxColorCycling(
+        image, dtype=dtypes.half, colors=colors)
+
 
 if __name__ == "__main__":
   test.main()

Vulnerable code:       const auto tboxes = boxes.tensor<T, 3>();
  def _testDrawBoundingBoxColorCycling(self, img, colors=None):
      image = image_ops_impl.convert_image_dtype(image, dtypes.float32)
Fixed code:       const auto tboxes = boxes.tensor<float, 3>();
  def _testDrawBoundingBoxColorCycling(self,
                                       img,
                                       dtype=dtypes.float32,
                                       colors=None):
      dtype: image dtype (float, half).
      colors: color table.
      image = image_ops_impl.convert_image_dtype(image, dtype)
  def testDrawBoundingBoxHalf(self):
    """Test if RGBA color cycling works correctly with provided colors."""
    image = np.zeros([10, 10, 4], "float32")
    colors = np.asarray([[0.5, 0, 0.5, 1], [0.5, 0.5, 0, 1], [0.5, 0, 0, 1],
                         [0, 0, 0.5, 1]])
    self._testDrawBoundingBoxColorCycling(
        image, dtype=dtypes.half, colors=colors)