Vulnerability: Division by 0 in `Conv3DBackprop*` ### Impact
The `tf.raw_ops.Conv3DBackprop*` operations fail to validate that the input tensors are not empty. In turn, this would result in a division by 0:

```python
import tensorflow as tf

input_sizes = tf.constant([0, 0, 0, 0, 0], shape=[5], dtype=tf.int32)
filter_tensor = tf.constant([], shape=[0, 0, 0, 1, 0], dtype=tf.float32)
out_backprop = tf.constant([], shape=[0, 0, 0, 0, 0], dtype=tf.float32)
                            
tf.raw_ops.Conv3DBackpropInputV2(input_sizes=input_sizes, filter=filter_tensor, out_backprop=out_backprop, strides=[1, 1, 1, 1, 1], padding='SAME', data_format='NDHWC', dilations=[1, 1, 1, 1, 1])
```
```python
import tensorflow as tf

input_sizes = tf.constant([1], shape=[1, 1, 1, 1, 1], dtype=tf.float32)
filter_tensor = tf.constant([0, 0, 0, 1, 0], shape=[5], dtype=tf.int32)
out_backprop = tf.constant([], shape=[1, 1, 1, 1, 0], dtype=tf.float32)

tf.raw_ops.Conv3DBackpropFilterV2(input=input_sizes, filter_sizes=filter_tensor, out_backprop=out_backprop, strides=[1, 1, 1, 1, 1], padding='SAME', data_format='NDHWC', dilations=[1, 1, 1, 1, 1])
```

This is because the [implementation](https://github.com/tensorflow/tensorflow/blob/a91bb59769f19146d5a0c20060244378e878f140/tensorflow/core/kernels/conv_grad_ops_3d.cc#L430-L450) does not check that the divisor used in computing the shard size is not zero:

```cc
  const int64 size_A = output_image_size * dims.out_depth;
  const int64 size_B = filter_total_size * dims.out_depth;
  const int64 size_C = output_image_size * filter_total_size;
  const int64 work_unit_size = size_A + size_B + size_C;
  ...
  const size_t shard_size =
      use_parallel_contraction
        ? 1
        : (target_working_set_size + work_unit_size - 1) / work_unit_size;
```

Thus, if attacker controls the input sizes, they can trigger a denial of service via a division by zero error.

### Patches
We have patched the issue in GitHub commit [311403edbc9816df80274bd1ea8b3c0c0f22c3fa](https://github.com/tensorflow/tensorflow/commit/311403edbc9816df80274bd1ea8b3c0c0f22c3fa).

The fix will be included in TensorFlow 2.5.0. We will also cherrypick this commit on TensorFlow 2.4.2, TensorFlow 2.3.3, TensorFlow 2.2.3 and TensorFlow 2.1.4, as these are also affected and still in supported range.

### For more information
Please consult [our security guide](https://github.com/tensorflow/tensorflow/blob/master/SECURITY.md) for more information regarding the security model and how to contact us with issues and questions.

### Attribution
This vulnerability has been reported by Yakun Zhang and Ying Wang of Baidu X-Team.
Vulnerability ID: GHSA-c968-pq7h-7fxv
CWE: CWE-369
Severity score: 2.5
Project: https://github.com/tensorflow/tensorflow
Published: 2021-05-21
File: tensorflow/core/kernels/conv_grad_ops_3d.cc
Commit message: Eliminate a division by 0 in 3D convolutions.

Also prevent a CHECK failed introduced in the most recent change.

PiperOrigin-RevId: 369322073
Change-Id: I4f609c028f89565fb2b49c3fdd20b63496582bae
Commit author: Mihai Maruseac

Vulnerable version (before fix):
 class Conv3DBackpropInputOp : public OpKernel {       input_shape = context->input(0).shape();     }      OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropInputOp : public OpKernel {       input_shape = context->input(0).shape();     }      OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropInputOp : public OpKernel {     // contraction compared to sharding and matmuls.     const bool use_parallel_contraction = dims.batch_size == 1;      const size_t shard_size =         use_parallel_contraction             ? 1 class Conv3DBackpropFilterOp : public OpKernel {       filter_shape = context->input(1).shape();     }      OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropFilterOp : public OpKernel {       filter_shape = context->input(1).shape();     }      OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropFilterOp : public OpKernel {      const int64 work_unit_size = size_A + size_B + size_C;      const size_t shard_size =         (target_working_set_size + work_unit_size - 1) / work_unit_size; 

Fixed version (after fix):
 class Conv3DBackpropInputOp : public OpKernel {       input_shape = context->input(0).shape();     }      OP_REQUIRES(context, input_shape.dims() == 5,                 errors::InvalidArgument("input tensor must have 5 dimensions"));     OP_REQUIRES(         context, filter_shape.dims() == 5,         errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));     OP_REQUIRES(         context, out_backprop_shape.dims() == 5,         errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));     OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropInputOp : public OpKernel {       input_shape = context->input(0).shape();     }      OP_REQUIRES(context, input_shape.dims() == 5,                 errors::InvalidArgument("input tensor must have 5 dimensions"));     OP_REQUIRES(         context, filter_shape.dims() == 5,         errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));     OP_REQUIRES(         context, out_backprop_shape.dims() == 5,         errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));     OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropInputOp : public OpKernel {     // contraction compared to sharding and matmuls.     const bool use_parallel_contraction = dims.batch_size == 1;      OP_REQUIRES(         context, work_unit_size > 0,         errors::InvalidArgument("input, filter_sizes and out_backprop tensors "                                 "must all have at least 1 element"));      const size_t shard_size =         use_parallel_contraction             ? 1 class Conv3DBackpropFilterOp : public OpKernel {       filter_shape = context->input(1).shape();     }      OP_REQUIRES(context, input_shape.dims() == 5,                 errors::InvalidArgument("input tensor must have 5 dimensions"));     OP_REQUIRES(         context, filter_shape.dims() == 5,         errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));     OP_REQUIRES(         context, out_backprop_shape.dims() == 5,         errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));     OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropFilterOp : public OpKernel {       filter_shape = context->input(1).shape();     }      OP_REQUIRES(context, input_shape.dims() == 5,                 errors::InvalidArgument("input tensor must have 5 dimensions"));     OP_REQUIRES(         context, filter_shape.dims() == 5,         errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));     OP_REQUIRES(         context, out_backprop_shape.dims() == 5,         errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));     OP_REQUIRES(         context, input_shape.dim_size(4) == filter_shape.dim_size(3),         errors::InvalidArgument("input and filter_sizes must have the same " class Conv3DCustomBackpropFilterOp : public OpKernel {      const int64 work_unit_size = size_A + size_B + size_C;      OP_REQUIRES(         context, work_unit_size > 0,         errors::InvalidArgument("input, filter_sizes and out_backprop tensors "                                 "must all have at least 1 element"));      const size_t shard_size =         (target_working_set_size + work_unit_size - 1) / work_unit_size; 

Patch:
patch: @@ -239,6 +239,14 @@ class Conv3DBackpropInputOp : public OpKernel {
       input_shape = context->input(0).shape();
     }
 
+    OP_REQUIRES(context, input_shape.dims() == 5,
+                errors::InvalidArgument("input tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, filter_shape.dims() == 5,
+        errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dims() == 5,
+        errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));
     OP_REQUIRES(
         context, input_shape.dim_size(4) == filter_shape.dim_size(3),
         errors::InvalidArgument("input and filter_sizes must have the same "
@@ -360,6 +368,14 @@ class Conv3DCustomBackpropInputOp : public OpKernel {
       input_shape = context->input(0).shape();
     }
 
+    OP_REQUIRES(context, input_shape.dims() == 5,
+                errors::InvalidArgument("input tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, filter_shape.dims() == 5,
+        errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dims() == 5,
+        errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));
     OP_REQUIRES(
         context, input_shape.dim_size(4) == filter_shape.dim_size(3),
         errors::InvalidArgument("input and filter_sizes must have the same "
@@ -444,6 +460,11 @@ class Conv3DCustomBackpropInputOp : public OpKernel {
     // contraction compared to sharding and matmuls.
     const bool use_parallel_contraction = dims.batch_size == 1;
 
+    OP_REQUIRES(
+        context, work_unit_size > 0,
+        errors::InvalidArgument("input, filter_sizes and out_backprop tensors "
+                                "must all have at least 1 element"));
+
     const size_t shard_size =
         use_parallel_contraction
             ? 1
@@ -724,6 +745,14 @@ class Conv3DBackpropFilterOp : public OpKernel {
       filter_shape = context->input(1).shape();
     }
 
+    OP_REQUIRES(context, input_shape.dims() == 5,
+                errors::InvalidArgument("input tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, filter_shape.dims() == 5,
+        errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dims() == 5,
+        errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));
     OP_REQUIRES(
         context, input_shape.dim_size(4) == filter_shape.dim_size(3),
         errors::InvalidArgument("input and filter_sizes must have the same "
@@ -850,6 +879,14 @@ class Conv3DCustomBackpropFilterOp : public OpKernel {
       filter_shape = context->input(1).shape();
     }
 
+    OP_REQUIRES(context, input_shape.dims() == 5,
+                errors::InvalidArgument("input tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, filter_shape.dims() == 5,
+        errors::InvalidArgument("filter_sizes tensor must have 5 dimensions"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dims() == 5,
+        errors::InvalidArgument("out_backprop tensor must have 5 dimensions"));
     OP_REQUIRES(
         context, input_shape.dim_size(4) == filter_shape.dim_size(3),
         errors::InvalidArgument("input and filter_sizes must have the same "
@@ -936,6 +973,11 @@ class Conv3DCustomBackpropFilterOp : public OpKernel {
 
     const int64 work_unit_size = size_A + size_B + size_C;
 
+    OP_REQUIRES(
+        context, work_unit_size > 0,
+        errors::InvalidArgument("input, filter_sizes and out_backprop tensors "
+                                "must all have at least 1 element"));
+
     const size_t shard_size =
         (target_working_set_size + work_unit_size - 1) / work_unit_size;
path: tensorflow/core/kernels/conv_grad_ops_3d.cc