CVE-2021-41199: Integer Overflow or Wraparound (CWE-190)
value: TensorFlow is an open source platform for machine learning. In affected versions if `tf.image.resize` is called with a large input argument then the TensorFlow process will crash due to a `CHECK`-failure caused by an overflow. The number of elements in the output tensor is too much for the `int64_t` type and the overflow is detected via a `CHECK` statement. This aborts the process. The fix will be included in TensorFlow 2.7.0. We will also cherrypick this commit on TensorFlow 2.6.1, TensorFlow 2.5.2, and TensorFlow 2.4.4, as these are also affected and still in supported range.
CWE description: The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
CVSS v3 base score: 5.5
Published: 2021-11-05T20:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/util/image_resizer_state.h, tensorflow/python/ops/image_ops_test.py
Commit date: 2021-08-17 17:19:53 -0700
Commit message: Merge pull request #51497 from yongtang:46914-image.resize

PiperOrigin-RevId: 391409572
Change-Id: I027c4901c9717ae7ee8266e5f57baba950b3e1e3

Diff: diff --cc tensorflow/core/util/image_resizer_state.h
index 31764bdd87976,2cb5715260ed8..48e51705b4743
--- a/tensorflow/core/util/image_resizer_state.h
+++ b/tensorflow/core/util/image_resizer_state.h
@@@ -141,15 -141,21 +141,19 @@@ struct ImageResizerState 
  
    // Calculates all the required variables, and allocates the output.
    void ValidateAndCreateOutput(OpKernelContext* context) {
      ValidateAndCalculateOutputSize(context);
      if (!context->status().ok()) return;
-     OP_REQUIRES_OK(
-         context,
-         context->allocate_output(
-             0, TensorShape({batch_size, out_height, out_width, channels}),
-             &output));
+ 
+     TensorShape shape;
+     // Guard against shape overflow
+     OP_REQUIRES_OK(context, shape.AddDimWithStatus(batch_size));
+     OP_REQUIRES_OK(context, shape.AddDimWithStatus(out_height));
+     OP_REQUIRES_OK(context, shape.AddDimWithStatus(out_width));
+     OP_REQUIRES_OK(context, shape.AddDimWithStatus(channels));
+ 
 -    OP_REQUIRES_OK(
 -        context,
 -        context->allocate_output( 0, shape, &output));
++    OP_REQUIRES_OK(context, context->allocate_output(0, shape, &output));
    }
  
    int64_t batch_size;
    int64_t out_height;
    int64_t out_width;
diff --cc tensorflow/python/ops/image_ops_test.py
index d589c7525b0ec,2eb82a213f515..fe0d6022d9c9e
--- a/tensorflow/python/ops/image_ops_test.py
+++ b/tensorflow/python/ops/image_ops_test.py
@@@ -3159,10 -3159,18 +3159,18 @@@ class ResizeImagesV2Test(test_util.Tens
      x_shape = [299, 299, 3]
      x = np.random.uniform(size=x_shape)
  
      self._assertResizeCheckShape(x, x_shape, [320, 320], [320, 320, 3])
  
+   def testLargeDim(self):
+     with self.session():
+       with self.assertRaises(errors.InternalError):
+         x = np.ones((5, 1, 1, 2))
 -        v = image_ops.resize_images_v2(
 -          x, [1610637938, 1610637938], image_ops.ResizeMethod.BILINEAR)
++        v = image_ops.resize_images_v2(x, [1610637938, 1610637938],
++                                       image_ops.ResizeMethod.BILINEAR)
+         _ = self.evaluate(v)
+ 
  
  class ResizeImagesTest(test_util.TensorFlowTestCase,
                         parameterized.TestCase):
  
    METHODS = [

Vulnerable code:      OP_REQUIRES_OK(
         context,
         context->allocate_output(
             0, TensorShape({batch_size, out_height, out_width, channels}),
             &output));
Fixed code:  
     TensorShape shape;
     // Guard against shape overflow
     OP_REQUIRES_OK(context, shape.AddDimWithStatus(batch_size));
     OP_REQUIRES_OK(context, shape.AddDimWithStatus(out_height));
     OP_REQUIRES_OK(context, shape.AddDimWithStatus(out_width));
     OP_REQUIRES_OK(context, shape.AddDimWithStatus(channels));
 
+    OP_REQUIRES_OK(context, context->allocate_output(0, shape, &output));
   def testLargeDim(self):
     with self.session():
       with self.assertRaises(errors.InternalError):
         x = np.ones((5, 1, 1, 2))
+        v = image_ops.resize_images_v2(x, [1610637938, 1610637938],
+                                       image_ops.ResizeMethod.BILINEAR)
         _ = self.evaluate(v)