Vulnerability: Heap buffer overflow in `Conv3DBackprop*` ### Impact
Missing validation between arguments to `tf.raw_ops.Conv3DBackprop*` operations can result in heap buffer overflows:

```python
import tensorflow as tf

input_sizes = tf.constant([1, 1, 1, 1, 2], shape=[5], dtype=tf.int32)
filter_tensor = tf.constant([734.6274508233133, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0,
                            -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0,
                            -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0], shape=[4, 1, 6, 1, 1], dtype=tf.float32)
out_backprop = tf.constant([-10.0], shape=[1, 1, 1, 1, 1], dtype=tf.float32)

tf.raw_ops.Conv3DBackpropInputV2(input_sizes=input_sizes, filter=filter_tensor, out_backprop=out_backprop, strides=[1, 89, 29, 89, 1], padding='SAME', data_format='NDHWC', dilations=[1, 1, 1, 1, 1])
```
```python
import tensorflow as tf

input_values = [-10.0] * (7 * 7 * 7 * 7 * 7)
input_values[0] = 429.6491056791816
input_sizes = tf.constant(input_values, shape=[7, 7, 7, 7, 7], dtype=tf.float32)
filter_tensor = tf.constant([7, 7, 7, 1, 1], shape=[5], dtype=tf.int32)
out_backprop = tf.constant([-10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0], shape=[7, 1, 1, 1, 1], dtype=tf.float32)
  
tf.raw_ops.Conv3DBackpropFilterV2(input=input_sizes, filter_sizes=filter_tensor, out_backprop=out_backprop, strides=[1, 37, 65, 93, 1], padding='VALID', data_format='NDHWC', dilations=[1, 1, 1, 1, 1])
```

This is because the [implementation](https://github.com/tensorflow/tensorflow/blob/4814fafb0ca6b5ab58a09411523b2193fed23fed/tensorflow/core/kernels/conv_grad_shape_utils.cc#L94-L153) assumes that the `input`, `filter_sizes` and `out_backprop` tensors have the same shape, as they are accessed in parallel.

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

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 securityguide](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-wcv5-qrj6-9pfm
CWE: CWE-787
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: Validate some shape requirements for `Conv3DBackpropFilter*` and `Conv3DBackpropInput*` ops.

Older versions of Eigen might otherwise crash / produce OOB read on specially crafted inputs.

PiperOrigin-RevId: 369293977
Change-Id: I58f51445a93936d7cf8e616f75de17677df36718
Commit author: Mihai Maruseac

Vulnerable version (before fix):
 class Conv3DBackpropInputOp : public OpKernel {       input_shape = context->input(0).shape();     }      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context, ConvBackpropComputeDimensions(                                 "Conv3DBackpropInputOp", /*num_spatial_dims=*/3, class Conv3DCustomBackpropInputOp : public OpKernel {       input_shape = context->input(0).shape();     }      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context, ConvBackpropComputeDimensions(                                 "Conv3DBackpropInputOp", /*num_spatial_dims=*/3, class Conv3DBackpropFilterOp : public OpKernel {       filter_shape = context->input(1).shape();     }      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context,                    ConvBackpropComputeDimensions( class Conv3DCustomBackpropFilterOp : public OpKernel {       filter_shape = context->input(1).shape();     }      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context,                    ConvBackpropComputeDimensions(

Fixed version (after 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 "                                 "number of channels. Got ",                                 input_shape.dim_size(4), " for input and ",                                 filter_shape.dim_size(3), " for filter_sizes"));     OP_REQUIRES(         context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),         errors::InvalidArgument("out_backprop and filter_sizes must have the "                                 "same number of channels. Got ",                                 out_backprop_shape.dim_size(4),                                 " for out_backprop and ",                                 filter_shape.dim_size(4), " for filter_sizes"));      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context, ConvBackpropComputeDimensions(                                 "Conv3DBackpropInputOp", /*num_spatial_dims=*/3, 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 "                                 "number of channels. Got ",                                 input_shape.dim_size(4), " for input and ",                                 filter_shape.dim_size(3), " for filter_sizes"));     OP_REQUIRES(         context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),         errors::InvalidArgument("out_backprop and filter_sizes must have the "                                 "same number of channels. Got ",                                 out_backprop_shape.dim_size(4),                                 " for out_backprop and ",                                 filter_shape.dim_size(4), " for filter_sizes"));      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context, ConvBackpropComputeDimensions(                                 "Conv3DBackpropInputOp", /*num_spatial_dims=*/3, 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 "                                 "number of channels. Got ",                                 input_shape.dim_size(4), " for input and ",                                 filter_shape.dim_size(3), " for filter_sizes"));     OP_REQUIRES(         context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),         errors::InvalidArgument("out_backprop and filter_sizes must have the "                                 "same number of channels. Got ",                                 out_backprop_shape.dim_size(4),                                 " for out_backprop and ",                                 filter_shape.dim_size(4), " for filter_sizes"));      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context,                    ConvBackpropComputeDimensions( 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 "                                 "number of channels. Got ",                                 input_shape.dim_size(4), " for input and ",                                 filter_shape.dim_size(3), " for filter_sizes"));     OP_REQUIRES(         context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),         errors::InvalidArgument("out_backprop and filter_sizes must have the "                                 "same number of channels. Got ",                                 out_backprop_shape.dim_size(4),                                 " for out_backprop and ",                                 filter_shape.dim_size(4), " for filter_sizes"));      ConvBackpropDimensions dims;     OP_REQUIRES_OK(context,                    ConvBackpropComputeDimensions(

Patch:
patch: @@ -239,6 +239,20 @@ 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 "
+                                "number of channels. Got ",
+                                input_shape.dim_size(4), " for input and ",
+                                filter_shape.dim_size(3), " for filter_sizes"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),
+        errors::InvalidArgument("out_backprop and filter_sizes must have the "
+                                "same number of channels. Got ",
+                                out_backprop_shape.dim_size(4),
+                                " for out_backprop and ",
+                                filter_shape.dim_size(4), " for filter_sizes"));
+
     ConvBackpropDimensions dims;
     OP_REQUIRES_OK(context, ConvBackpropComputeDimensions(
                                 "Conv3DBackpropInputOp", /*num_spatial_dims=*/3,
@@ -346,6 +360,20 @@ 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 "
+                                "number of channels. Got ",
+                                input_shape.dim_size(4), " for input and ",
+                                filter_shape.dim_size(3), " for filter_sizes"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),
+        errors::InvalidArgument("out_backprop and filter_sizes must have the "
+                                "same number of channels. Got ",
+                                out_backprop_shape.dim_size(4),
+                                " for out_backprop and ",
+                                filter_shape.dim_size(4), " for filter_sizes"));
+
     ConvBackpropDimensions dims;
     OP_REQUIRES_OK(context, ConvBackpropComputeDimensions(
                                 "Conv3DBackpropInputOp", /*num_spatial_dims=*/3,
@@ -696,6 +724,20 @@ 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 "
+                                "number of channels. Got ",
+                                input_shape.dim_size(4), " for input and ",
+                                filter_shape.dim_size(3), " for filter_sizes"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),
+        errors::InvalidArgument("out_backprop and filter_sizes must have the "
+                                "same number of channels. Got ",
+                                out_backprop_shape.dim_size(4),
+                                " for out_backprop and ",
+                                filter_shape.dim_size(4), " for filter_sizes"));
+
     ConvBackpropDimensions dims;
     OP_REQUIRES_OK(context,
                    ConvBackpropComputeDimensions(
@@ -808,6 +850,20 @@ 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 "
+                                "number of channels. Got ",
+                                input_shape.dim_size(4), " for input and ",
+                                filter_shape.dim_size(3), " for filter_sizes"));
+    OP_REQUIRES(
+        context, out_backprop_shape.dim_size(4) == filter_shape.dim_size(4),
+        errors::InvalidArgument("out_backprop and filter_sizes must have the "
+                                "same number of channels. Got ",
+                                out_backprop_shape.dim_size(4),
+                                " for out_backprop and ",
+                                filter_shape.dim_size(4), " for filter_sizes"));
+
     ConvBackpropDimensions dims;
     OP_REQUIRES_OK(context,
                    ConvBackpropComputeDimensions(
path: tensorflow/core/kernels/conv_grad_ops_3d.cc