CVE-2022-35999: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. When `Conv2DBackpropInput` receives empty `out_backprop` inputs (e.g. `[3, 1, 0, 1]`), the current CPU/GPU kernels `CHECK` fail (one with dnnl, the other with cudnn). This can be used to trigger a denial of service attack. We have patched the issue in GitHub commit 27a65a43cf763897fecfa5cdb5cc653fc5dd0346. 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-16T23: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-20 13:35:18 -0700
Commit message: Fix GPU/CPU Conv2DBackpropInputOp check error.

For empty `out_backprop` inputs (e.g. `[3, 1, 0, 1]`), the current
CPU/GPU kernels fail (one with dnnl, the other with cudnn). Added
a shortcut path to return a zero input.

Note that the XLA test currently fails due to an incorrect result
size bug (b/239598470), so it is currently disabled.

PiperOrigin-RevId: 462217998

Diff: diff --git a/tensorflow/core/kernels/conv_grad_input_ops.h b/tensorflow/core/kernels/conv_grad_input_ops.h
index 88df14d395c73..a9db8a9795ff8 100644
--- a/tensorflow/core/kernels/conv_grad_input_ops.h
+++ b/tensorflow/core/kernels/conv_grad_input_ops.h
@@ -35,10 +35,11 @@ limitations under the License.
 #include "tensorflow/core/framework/tensor_shape.h"
 #include "tensorflow/core/framework/tensor_slice.h"
 #include "tensorflow/core/kernels/conv_2d.h"
 #include "tensorflow/core/kernels/conv_grad_ops.h"
 #include "tensorflow/core/kernels/conv_grad_shape_utils.h"
+#include "tensorflow/core/kernels/fill_functor.h"
 #ifdef TENSORFLOW_USE_LIBXSMM_CONVOLUTIONS
 #include "tensorflow/core/kernels/xsmm_conv2d.h"
 #endif
 #include "tensorflow/core/lib/core/errors.h"
 #include "tensorflow/core/lib/gtl/array_slice.h"
@@ -434,10 +435,19 @@ class Conv2DBackpropInputOp : public OpKernel {
     // If there is nothing to compute, return.
     if (input_shape.num_elements() == 0) {
       return;
     }
 
+    // If shapes are valid but `out_backprop` is empty, in_backprop should be
+    // set to all zeros.  Otherwise, cudnn/dnnl fail with an empty input.
+    if (out_backprop.NumElements() == 0) {
+      functor::SetZeroFunctor<Device, T> set_zero;
+      set_zero(context->eigen_device<Device>(),
+               in_backprop->template flat<T>());
+      return;
+    }
+
     // For now we take the stride from the second and third dimensions only (we
     // do not support striding on the batch or depth dimension).
     const int stride_rows = GetTensorDim(strides_, data_format_, 'H');
     const int stride_cols = GetTensorDim(strides_, data_format_, 'W');
     const int dilation_rows = GetTensorDim(dilations_, data_format_, 'H');
@@ -552,10 +562,19 @@ class Conv2DCustomBackpropInputOp : public OpKernel {
     // If there is nothing to compute, return.
     if (input_shape.num_elements() == 0) {
       return;
     }
 
+    // If shapes are valid but `out_backprop` is empty, in_backprop should be
+    // set to all zeros.  Otherwise, cudnn/dnnl fail with an empty input.
+    if (out_backprop.NumElements() == 0) {
+      functor::SetZeroFunctor<Device, T> set_zero;
+      set_zero(context->eigen_device<Device>(),
+               in_backprop->template flat<T>());
+      return;
+    }
+
 // TODO(ezhulenev): Remove custom kernel and move XSMM support to
 // LaunchConv2DBackpropInputOp functor.
 #if defined TENSORFLOW_USE_LIBXSMM_CONVOLUTIONS && \
     defined TENSORFLOW_USE_LIBXSMM_BACKWARD_CONVOLUTIONS
     int64 pad_top, pad_bottom;
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 91a35f9002b1f..907c3c93a2fd5 100644
--- a/tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py
+++ b/tensorflow/python/kernel_tests/nn_ops/conv_ops_test.py
@@ -1101,10 +1101,27 @@ class Conv2DTest(test.TestCase):
           expected=expected_output,
           data_format=data_format,
           use_gpu=use_gpu,
           err=1e-5)
 
+  @test_util.run_in_graph_and_eager_modes
+  @test_util.disable_xla("b/239598470")
+  def testConv2DBackpropInputDegenerateBackpropInput(self):
+    input_sizes = [3, 1, 1, 2]
+    expected_output = np.zeros(input_sizes).flatten()
+    for (data_format, use_gpu) in GetTestConfigs():
+      self._RunAndVerifyBackpropInput(
+          input_sizes=input_sizes,
+          filter_sizes=[1, 3, 2, 3],
+          output_sizes=[3, 1, 0, 3],
+          strides=[1, 2],
+          padding="VALID",
+          expected=expected_output,
+          data_format=data_format,
+          use_gpu=use_gpu,
+          err=1e-5)
+
   # Testing for backprops
   def _RunAndVerifyBackpropFilter(self,
                                   input_sizes,
                                   filter_sizes,
                                   output_sizes,

Fixed code: #include "tensorflow/core/kernels/fill_functor.h"
    // If shapes are valid but `out_backprop` is empty, in_backprop should be
    // set to all zeros.  Otherwise, cudnn/dnnl fail with an empty input.
    if (out_backprop.NumElements() == 0) {
      functor::SetZeroFunctor<Device, T> set_zero;
      set_zero(context->eigen_device<Device>(),
               in_backprop->template flat<T>());
      return;
    }

    // If shapes are valid but `out_backprop` is empty, in_backprop should be
    // set to all zeros.  Otherwise, cudnn/dnnl fail with an empty input.
    if (out_backprop.NumElements() == 0) {
      functor::SetZeroFunctor<Device, T> set_zero;
      set_zero(context->eigen_device<Device>(),
               in_backprop->template flat<T>());
      return;
    }

  @test_util.run_in_graph_and_eager_modes
  @test_util.disable_xla("b/239598470")
  def testConv2DBackpropInputDegenerateBackpropInput(self):
    input_sizes = [3, 1, 1, 2]
    expected_output = np.zeros(input_sizes).flatten()
    for (data_format, use_gpu) in GetTestConfigs():
      self._RunAndVerifyBackpropInput(
          input_sizes=input_sizes,
          filter_sizes=[1, 3, 2, 3],
          output_sizes=[3, 1, 0, 3],
          strides=[1, 2],
          padding="VALID",
          expected=expected_output,
          data_format=data_format,
          use_gpu=use_gpu,
          err=1e-5)