CVE-2022-35981: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. `FractionalMaxPoolGrad` validates its inputs with `CHECK` failures instead of with returning errors. If it gets incorrectly sized inputs, the `CHECK` failure can be used to trigger a denial of service attack. We have patched the issue in GitHub commit 8741e57d163a079db05a7107a7609af70931def4. 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-16T22:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/fractional_max_pool_op.cc, tensorflow/python/kernel_tests/nn_ops/fractional_max_pool_op_test.py
Commit date: 2022-07-18 15:02:30 -0700
Commit message: Fix security vulnerability with FractionalMaxPoolGrad

PiperOrigin-RevId: 461722693

Diff: diff --git a/tensorflow/core/kernels/fractional_max_pool_op.cc b/tensorflow/core/kernels/fractional_max_pool_op.cc
index 0722c408fba9d..375786615ebe6 100644
--- a/tensorflow/core/kernels/fractional_max_pool_op.cc
+++ b/tensorflow/core/kernels/fractional_max_pool_op.cc
@@ -17,16 +17,17 @@ limitations under the License.
 #include <algorithm>
 #include <cmath>
 #include <random>
 #include <vector>
 
-#include "tensorflow/core/kernels/fractional_pool_common.h"
-
 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
 #include "tensorflow/core/framework/numeric_op.h"
 #include "tensorflow/core/framework/op_kernel.h"
+#include "tensorflow/core/framework/op_requires.h"
+#include "tensorflow/core/kernels/fractional_pool_common.h"
 #include "tensorflow/core/lib/random/random.h"
+#include "tensorflow/core/platform/errors.h"
 #include "tensorflow/core/platform/logging.h"
 #include "tensorflow/core/platform/mutex.h"
 #include "tensorflow/core/util/guarded_philox_random.h"
 
 namespace tensorflow {
@@ -350,11 +351,13 @@ class FractionalMaxPoolGradOp : public OpKernel {
         output_size[2] * output_size[1] * output_size[0]);
     const int64_t num_reshaped_cols =
         output_size[2] * output_size[1] * output_size[0];
     for (int64_t i = 0; i < num_reshaped_cols; ++i) {
       for (int64_t j = 0; j < output_size[3]; ++j) {
-        DCHECK_EQ(tensor_out_dup_mat(j, i), tensor_out_mat(j, i));
+        OP_REQUIRES(context, tensor_out_dup_mat(j, i) == tensor_out_mat(j, i),
+                    errors::InvalidArgument(
+                        "tensor_out_dup is not the same as tensor_out"));
       }
     }
 
     Tensor* output = nullptr;
     OP_REQUIRES_OK(context, context->forward_input_or_allocate_output(
@@ -367,15 +370,16 @@ class FractionalMaxPoolGradOp : public OpKernel {
     int num_total_outputs = out_backprop_flat.size();
     int num_total_inputs = input_backprop_flat.size();
 
     for (int index = 0; index < num_total_outputs; ++index) {
       int input_backprop_index = out_arg_max_flat(index);
-      // According to maxpooling_op.cc, the performance impact below is small.
-      CHECK(input_backprop_index >= 0 &&
-            input_backprop_index < num_total_inputs)
-          << "Invalid input backprop index: " << input_backprop_index << ", "
-          << num_total_inputs;
+      OP_REQUIRES(
+          context,
+          input_backprop_index >= 0 && input_backprop_index < num_total_inputs,
+          errors::InvalidArgument(
+              "Invalid input backprop index: ", input_backprop_index, ", ",
+              num_total_inputs));
       input_backprop_flat(input_backprop_index) += out_backprop_flat(index);
     }
   }
 
  private:
diff --git a/tensorflow/python/kernel_tests/nn_ops/fractional_max_pool_op_test.py b/tensorflow/python/kernel_tests/nn_ops/fractional_max_pool_op_test.py
index 5acacdbb7463b..1594844ab49e7 100644
--- a/tensorflow/python/kernel_tests/nn_ops/fractional_max_pool_op_test.py
+++ b/tensorflow/python/kernel_tests/nn_ops/fractional_max_pool_op_test.py
@@ -122,11 +122,11 @@ class FractionalMaxPoolTest(test.TestCase):
       overlapping: Use overlapping when pooling.
 
     Returns:
       None
     """
-    with self.cached_session() as sess:
+    with self.cached_session():
       p, r, c = nn_ops.fractional_max_pool_v2(
           input_tensor,
           pooling_ratio,
           pseudo_random,
           overlapping,
@@ -153,11 +153,11 @@ class FractionalMaxPoolTest(test.TestCase):
       print("-" * 70)
       print("Testing FractionalMaxPool with overlapping = {}".format(
           overlapping))
       rand_mat = self._PRNG.randint(10, size=tensor_shape)
       pooling_ratio = [1, math.sqrt(2), math.sqrt(2), 1]
-      with self.cached_session() as sess:
+      with self.cached_session():
         p, r, c = nn_ops.fractional_max_pool_v2(
             rand_mat,
             pooling_ratio,
             pseudo_random,
             overlapping,
@@ -628,8 +628,31 @@ class FractionalMaxPoolGradTest(test.TestCase):
       self.assertShapeEqual(
           np.reshape(expected_input_backprop_overlapping, input_size), r)
       self.assertAllClose(expected_input_backprop_overlapping,
                           input_backprop_overlapping)
 
+  def testInvalidSeqRaiseErrorForFractionalMaxPoolGrad(self):
+    with self.assertRaises(errors.InvalidArgumentError):
+      with self.cached_session() as _:
+        overlapping = True
+        orig_input = constant_op.constant(
+            .453409232, shape=[1, 7, 13, 1], dtype=dtypes.float32)
+        orig_output = constant_op.constant(
+            .453409232, shape=[1, 7, 13, 1], dtype=dtypes.float32)
+        out_backprop = constant_op.constant(
+            .453409232, shape=[1, 7, 13, 1], dtype=dtypes.float32)
+        row_pooling_sequence = constant_op.constant(
+            0, shape=[5], dtype=dtypes.int64)
+        col_pooling_sequence = constant_op.constant(
+            0, shape=[5], dtype=dtypes.int64)
+        t = gen_nn_ops.FractionalMaxPoolGrad(
+            orig_input=orig_input,
+            orig_output=orig_output,
+            out_backprop=out_backprop,
+            row_pooling_sequence=row_pooling_sequence,
+            col_pooling_sequence=col_pooling_sequence,
+            overlapping=overlapping)
+        self.evaluate(t)
+
 
 if __name__ == "__main__":
   test.main()

Vulnerable code: #include "tensorflow/core/kernels/fractional_pool_common.h"

        DCHECK_EQ(tensor_out_dup_mat(j, i), tensor_out_mat(j, i));
      // According to maxpooling_op.cc, the performance impact below is small.
      CHECK(input_backprop_index >= 0 &&
            input_backprop_index < num_total_inputs)
          << "Invalid input backprop index: " << input_backprop_index << ", "
          << num_total_inputs;
    with self.cached_session() as sess:
      with self.cached_session() as sess:
Fixed code: #include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/kernels/fractional_pool_common.h"
#include "tensorflow/core/platform/errors.h"
        OP_REQUIRES(context, tensor_out_dup_mat(j, i) == tensor_out_mat(j, i),
                    errors::InvalidArgument(
                        "tensor_out_dup is not the same as tensor_out"));
      OP_REQUIRES(
          context,
          input_backprop_index >= 0 && input_backprop_index < num_total_inputs,
          errors::InvalidArgument(
              "Invalid input backprop index: ", input_backprop_index, ", ",
              num_total_inputs));
    with self.cached_session():
      with self.cached_session():
  def testInvalidSeqRaiseErrorForFractionalMaxPoolGrad(self):
    with self.assertRaises(errors.InvalidArgumentError):
      with self.cached_session() as _:
        overlapping = True
        orig_input = constant_op.constant(
            .453409232, shape=[1, 7, 13, 1], dtype=dtypes.float32)
        orig_output = constant_op.constant(
            .453409232, shape=[1, 7, 13, 1], dtype=dtypes.float32)
        out_backprop = constant_op.constant(
            .453409232, shape=[1, 7, 13, 1], dtype=dtypes.float32)
        row_pooling_sequence = constant_op.constant(
            0, shape=[5], dtype=dtypes.int64)
        col_pooling_sequence = constant_op.constant(
            0, shape=[5], dtype=dtypes.int64)
        t = gen_nn_ops.FractionalMaxPoolGrad(
            orig_input=orig_input,
            orig_output=orig_output,
            out_backprop=out_backprop,
            row_pooling_sequence=row_pooling_sequence,
            col_pooling_sequence=col_pooling_sequence,
            overlapping=overlapping)
        self.evaluate(t)