CVE-2021-41197: Integer Overflow or Wraparound (CWE-190)
value: TensorFlow is an open source platform for machine learning. In affected versions TensorFlow allows tensor to have a large number of dimensions and each dimension can be as large as desired. However, the total number of elements in a tensor must fit within an `int64_t`. If an overflow occurs, `MultiplyWithoutOverflow` would return a negative result. In the majority of TensorFlow codebase this then results in a `CHECK`-failure. Newer constructs exist which return a `Status` instead of crashing the binary. This is similar to CVE-2021-29584. 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/python/kernel_tests/attention_ops_test.py
Commit date: 2021-10-06 08:19:47 -0700
Commit message: Merge pull request #51658 from yongtang:51618-tf.image.extract_glimpse

PiperOrigin-RevId: 401246064
Change-Id: I9e64ab662b1a0bc8c6be4dcc4f0e620db6708d22

Diff: diff --cc tensorflow/python/kernel_tests/attention_ops_test.py
index 3ebb5ba4f19db,34b6da4c6a8e6..7baa7597bf71f
--- a/tensorflow/python/kernel_tests/attention_ops_test.py
+++ b/tensorflow/python/kernel_tests/attention_ops_test.py
@@@ -12,14 -12,19 +12,15 @@@
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  """Tests for image.extract_glimpse()."""
  
 -from __future__ import absolute_import
 -from __future__ import division
 -from __future__ import print_function
 -
  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.ops import array_ops
  from tensorflow.python.ops import gen_image_ops
  from tensorflow.python.ops import image_ops
  from tensorflow.python.platform import test
  
@@@ -295,8 -300,17 +296,20 @@@ class ExtractGlimpseTest(test.TestCase)
            self.evaluate(result1)[0, :, :, 0])
        self.assertAllEqual(
            np.asarray([[5, 6, 7], [10, 11, 12], [15, 16, 17]]),
            self.evaluate(result2)[0, :, :, 0])
  
+   def testGlimpseNegativeInput(self):
 -    img = np.arange(9).reshape([1,3,3,1])
++    img = np.arange(9).reshape([1, 3, 3, 1])
+     with self.test_session():
+       with self.assertRaises((errors.InternalError, ValueError)):
+         result = image_ops.extract_glimpse_v2(
 -            img, size=[1023, -63], offsets=[1023, 63],
 -            centered=False, normalized=False)
++            img,
++            size=[1023, -63],
++            offsets=[1023, 63],
++            centered=False,
++            normalized=False)
+         self.evaluate(result)
+ 
  
  if __name__ == '__main__':
    test.main()

Fixed code:    def testGlimpseNegativeInput(self):
+    img = np.arange(9).reshape([1, 3, 3, 1])
     with self.test_session():
       with self.assertRaises((errors.InternalError, ValueError)):
         result = image_ops.extract_glimpse_v2(
+            img,
+            size=[1023, -63],
+            offsets=[1023, 63],
+            centered=False,
+            normalized=False)
         self.evaluate(result)