CVE-2021-41196: Integer Underflow (Wrap or Wraparound) (CWE-191)
value: TensorFlow is an open source platform for machine learning. In affected versions the Keras pooling layers can trigger a segfault if the size of the pool is 0 or if a dimension is negative. This is due to the TensorFlow's implementation of pooling operations where the values in the sliding window are not checked to be strictly positive. 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 subtracts one value from another, such that the result is less than the minimum allowable integer value, which produces a value that is not equal to the correct result.
CVSS v3 base score: 5.5
Published: 2021-11-05T20:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/pooling_ops_3d.cc, tensorflow/python/kernel_tests/pooling_ops_3d_test.py
Commit date: 2021-10-06 08:14:33 -0700
Commit message: Merge pull request #51975 from yongtang:51936-max_pool3d

PiperOrigin-RevId: 401245519
Change-Id: I67d2cbb0e21729b94186ca9bf82450ff93132ff2

Diff: diff --cc tensorflow/core/kernels/pooling_ops_3d.cc
index af00b6368c7ee,a4709bbf02c10..d4dc87c7e3f86
--- a/tensorflow/core/kernels/pooling_ops_3d.cc
+++ b/tensorflow/core/kernels/pooling_ops_3d.cc
@@@ -139,10 -139,15 +139,15 @@@ class Pooling3DOp : public UnaryOp<T> 
      }
      OP_REQUIRES_OK(context, context->GetAttr("ksize", &ksize_));
      OP_REQUIRES(context, ksize_.size() == 5,
                  errors::InvalidArgument("Sliding window ksize field must "
                                          "specify 5 dimensions"));
 -    bool non_negative = std::all_of(
 -        ksize_.begin(), ksize_.end(), [](int k) { return k > 0; });
++    bool non_negative =
++        std::all_of(ksize_.begin(), ksize_.end(), [](int k) { return k > 0; });
+     OP_REQUIRES(context, non_negative,
+                 errors::InvalidArgument("Sliding window ksize field must "
+                                         "have non-negative dimensions"));
      OP_REQUIRES_OK(context, context->GetAttr("strides", &stride_));
      OP_REQUIRES(context, stride_.size() == 5,
                  errors::InvalidArgument("Sliding window stride field must "
                                          "specify 5 dimensions"));
      OP_REQUIRES_OK(context, context->GetAttr("padding", &padding_));
diff --cc tensorflow/python/kernel_tests/pooling_ops_3d_test.py
index ecb87b2077c5d,895fb21ca08d7..203d3ad2f280f
--- a/tensorflow/python/kernel_tests/pooling_ops_3d_test.py
+++ b/tensorflow/python/kernel_tests/pooling_ops_3d_test.py
@@@ -12,13 -12,18 +12,14 @@@
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  """Functional tests for 3d pooling operations."""
  
 -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 errors
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import gradient_checker
  from tensorflow.python.ops import gradients_impl
  from tensorflow.python.ops import nn_ops
  import tensorflow.python.ops.nn_grad  # pylint: disable=unused-import
@@@ -499,8 -504,25 +500,21 @@@ class PoolingTest(test.TestCase)
          output_sizes=[1, 3, 6, 2, 1],
          window=(3, 3, 3),
          strides=(1, 1, 1),
          padding="SAME")
  
+   def testMaxPool3DZeroPoolSize(self):
+     # Test case for GitHub issue 51936.
+     for f in [nn_ops.max_pool3d, nn_ops.avg_pool3d]:
+       with self.session():
+         with self.assertRaises((errors.InvalidArgumentError, ValueError)):
+           input_sizes = [3, 4, 10, 11, 12]
+ 
+           input_data = 1.
+           input_tensor = constant_op.constant(
+               input_data, shape=input_sizes, name="input")
 -          pool_3d = f(
 -              input_tensor,
 -              ksize=[2, 2, 0],
 -              strides=1,
 -              padding="VALID")
++          pool_3d = f(input_tensor, ksize=[2, 2, 0], strides=1, padding="VALID")
+           self.evaluate(pool_3d)
+ 
  
  if __name__ == "__main__":
    test.main()

Fixed code: +    bool non_negative =
+        std::all_of(ksize_.begin(), ksize_.end(), [](int k) { return k > 0; });
     OP_REQUIRES(context, non_negative,
                 errors::InvalidArgument("Sliding window ksize field must "
                                         "have non-negative dimensions"));
   def testMaxPool3DZeroPoolSize(self):
     # Test case for GitHub issue 51936.
     for f in [nn_ops.max_pool3d, nn_ops.avg_pool3d]:
       with self.session():
         with self.assertRaises((errors.InvalidArgumentError, ValueError)):
           input_sizes = [3, 4, 10, 11, 12]
 
           input_data = 1.
           input_tensor = constant_op.constant(
               input_data, shape=input_sizes, name="input")
+          pool_3d = f(input_tensor, ksize=[2, 2, 0], strides=1, padding="VALID")
           self.evaluate(pool_3d)