CVE-2022-35969: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. The implementation of `Conv2DBackpropInput` requires `input_sizes` to be 4-dimensional. Otherwise, it gives a `CHECK` failure which can be used to trigger a denial of service attack. We have patched the issue in GitHub commit 50156d547b9a1da0144d7babe665cf690305b33c. 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-16T21:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/conv_grad_input_ops.h, tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py
Commit date: 2022-07-26 12:13:16 -0700
Commit message: Add security vulnerability test for raw_ops.Conv2DBackpropInput

PiperOrigin-RevId: 463395218

Diff: diff --git a/tensorflow/core/kernels/conv_grad_input_ops.h b/tensorflow/core/kernels/conv_grad_input_ops.h
index a9db8a9795ff8..6fdd72e0014b0 100644
--- a/tensorflow/core/kernels/conv_grad_input_ops.h
+++ b/tensorflow/core/kernels/conv_grad_input_ops.h
@@ -420,10 +420,15 @@ class Conv2DBackpropInputOp : public OpKernel {
   void Compute(OpKernelContext* context) override {
     const Tensor& input_sizes = context->input(0);
     const Tensor& filter = context->input(1);
     const Tensor& out_backprop = context->input(2);
 
+    OP_REQUIRES(
+        context, out_backprop.dims() == 4,
+        errors::InvalidArgument("input_sizes must be 4-dimensional, got: ",
+                                out_backprop.dims()));
+
     TensorShape input_shape;
     OP_REQUIRES_OK(context,
                    Conv2DBackpropComputeInputShape(input_sizes, filter.shape(),
                                                    out_backprop.shape(),
                                                    data_format_, &input_shape));
@@ -525,10 +530,14 @@ class Conv2DCustomBackpropInputOp : public OpKernel {
 
   void Compute(OpKernelContext* context) override {
     const Tensor& input_sizes = context->input(0);
     const Tensor& filter = context->input(1);
     const Tensor& out_backprop = context->input(2);
+    OP_REQUIRES(
+        context, out_backprop.dims() == 4,
+        errors::InvalidArgument("input_sizes must be 4-dimensional, got: ",
+                                out_backprop.dims()));
 
     TensorShape input_shape;
     OP_REQUIRES_OK(context,
                    Conv2DBackpropComputeInputShape(input_sizes, filter.shape(),
                                                    out_backprop.shape(),
diff --git a/tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py b/tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py
index 0527215618f26..2392944fc4170 100644
--- a/tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py
+++ b/tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py
@@ -30,10 +30,11 @@ from tensorflow.python.framework import errors_impl
 from tensorflow.python.framework import ops
 from tensorflow.python.framework import test_util
 from tensorflow.python.layers import convolutional
 from tensorflow.python.ops import array_ops
 from tensorflow.python.ops import control_flow_ops
+from tensorflow.python.ops import gen_nn_ops
 from tensorflow.python.ops import gradient_checker
 from tensorflow.python.ops import gradients_impl
 from tensorflow.python.ops import math_ops
 from tensorflow.python.ops import nn_impl
 from tensorflow.python.ops import nn_ops
@@ -1317,11 +1318,11 @@ class Conv2DTest(test.TestCase):
                                          padding, data_format, use_gpu, err):
     x1 = self._CreateNumpyTensor(input_sizes)
     x2 = self._CreateNumpyTensor(filter_sizes)
     default_dilations = (dilations[0] == 1 and dilations[1] == 1)
     if default_dilations or use_gpu:
-      with self.cached_session(use_gpu=use_gpu) as sess:
+      with self.cached_session(use_gpu=use_gpu):
         if data_format == "NCHW":
           input_sizes = test_util.NHWCToNCHW(input_sizes)
         t1 = constant_op.constant(x1, shape=input_sizes)
         t2 = constant_op.constant(x2, shape=filter_sizes)
         full_strides = [1] + strides + [1]
@@ -1363,11 +1364,11 @@ class Conv2DTest(test.TestCase):
                                           padding, data_format, use_gpu, err):
     x1 = self._CreateNumpyTensor(input_sizes)
     x2 = self._CreateNumpyTensor(filter_sizes)
     default_dilations = (dilations[0] == 1 and dilations[1] == 1)
     if default_dilations or use_gpu:
-      with self.cached_session(use_gpu=use_gpu) as sess:
+      with self.cached_session(use_gpu=use_gpu):
         if data_format == "NCHW":
           input_sizes = test_util.NHWCToNCHW(input_sizes)
         t1 = constant_op.constant(x1, shape=input_sizes)
         t2 = constant_op.constant(x2, shape=filter_sizes)
         full_strides = [1] + strides + [1]
@@ -2626,10 +2627,31 @@ class Conv2DTest(test.TestCase):
               input_val, [18, 18, 3, 2],
               out_backprop_val,
               strides=[1, 1, 1, 1],
               padding=[[0, 0], [-1, 0], [0, 0], [0, 0]]))
 
+  def testConv2DBackpropInputInvalidOutBackpropRaiseError(self):
+    with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
+      with self.cached_session():
+        input_sizes = constant_op.constant([65534, 65534],
+                                           shape=[2],
+                                           dtype=dtypes.int32)
+        filters = constant_op.constant(
+            0.159749106, shape=[3, 3, 2, 2], dtype=dtypes.float32)
+        out_backprop = constant_op.constant(0, shape=[], dtype=dtypes.float32)
+        t = gen_nn_ops.conv2d_backprop_input(
+            input_sizes=input_sizes,
+            filter=filters,
+            out_backprop=out_backprop,
+            strides=[1, 1, 1, 1],
+            padding="SAME",
+            use_cudnn_on_gpu=True,
+            explicit_paddings=[],
+            data_format="NHWC",
+            dilations=[1, 1, 1, 1])
+        self.evaluate(t)
+
 
 @test_util.run_all_without_tensor_float_32("Avoid TF32 conv on GPU")
 class DepthwiseConv2DTest(test.TestCase):
 
   def _VerifyValues(self, tensor_in_sizes, filter_in_sizes, stride, padding,
@@ -2653,11 +2675,11 @@ class DepthwiseConv2DTest(test.TestCase):
       total_size_2 *= s
     # Initializes the input tensor with array containing incrementing
     # numbers from 1.
     x1 = [f * 1.0 for f in range(1, total_size_1 + 1)]
     x2 = [f * 1.0 for f in range(1, total_size_2 + 1)]
-    with self.cached_session() as sess:
+    with self.cached_session():
       t1 = constant_op.constant(x1, shape=tensor_in_sizes)
       t1.set_shape(tensor_in_sizes)
       t2 = constant_op.constant(x2, shape=filter_in_sizes)
       conv = nn_impl.depthwise_conv2d(
           t1, t2, strides=[1, stride, stride, 1], padding=padding)
@@ -2924,11 +2946,11 @@ class DeepConv2DTest(test.TestCase):
       padding: Padding type.
     """
     x1 = np.random.rand(*tensor_in_sizes).astype(np.float32)
     x2 = np.random.rand(*filter_in_sizes).astype(np.float32)
 
-    with self.cached_session(use_gpu=False) as sess:
+    with self.cached_session(use_gpu=False):
       t1 = constant_op.constant(x1, shape=tensor_in_sizes)
       t2 = constant_op.constant(x2, shape=filter_in_sizes)
       strides = [1] + conv_strides + [1]
 
       conv = nn_ops.conv2d(t1, t2, strides=strides, padding=padding)

Vulnerable code:       with self.cached_session(use_gpu=use_gpu) as sess:
      with self.cached_session(use_gpu=use_gpu) as sess:
    with self.cached_session() as sess:
    with self.cached_session(use_gpu=False) as sess:
Fixed code:     OP_REQUIRES(
        context, out_backprop.dims() == 4,
        errors::InvalidArgument("input_sizes must be 4-dimensional, got: ",
                                out_backprop.dims()));

    OP_REQUIRES(
        context, out_backprop.dims() == 4,
        errors::InvalidArgument("input_sizes must be 4-dimensional, got: ",
                                out_backprop.dims()));
from tensorflow.python.ops import gen_nn_ops
      with self.cached_session(use_gpu=use_gpu):
      with self.cached_session(use_gpu=use_gpu):
  def testConv2DBackpropInputInvalidOutBackpropRaiseError(self):
    with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
      with self.cached_session():
        input_sizes = constant_op.constant([65534, 65534],
                                           shape=[2],
                                           dtype=dtypes.int32)
        filters = constant_op.constant(
            0.159749106, shape=[3, 3, 2, 2], dtype=dtypes.float32)
        out_backprop = constant_op.constant(0, shape=[], dtype=dtypes.float32)
        t = gen_nn_ops.conv2d_backprop_input(
            input_sizes=input_sizes,
            filter=filters,
            out_backprop=out_backprop,
            strides=[1, 1, 1, 1],
            padding="SAME",
            use_cudnn_on_gpu=True,
            explicit_paddings=[],
            data_format="NHWC",
            dilations=[1, 1, 1, 1])
        self.evaluate(t)

    with self.cached_session():
    with self.cached_session(use_gpu=False):