Vulnerability: Missing validation crashes `QuantizeAndDequantizeV4Grad` ### Impact
The implementation of [`tf.raw_ops.QuantizeAndDequantizeV4Grad`](https://github.com/tensorflow/tensorflow/blob/f3b9bf4c3c0597563b289c0512e98d4ce81f886e/tensorflow/core/kernels/quantize_and_dequantize_op.cc#L148-L226) does not fully validate the input arguments. This results in a `CHECK`-failure which can be used to trigger a denial of service attack:

```python
import tensorflow as tf

tf.raw_ops.QuantizeAndDequantizeV4Grad(
  gradients=tf.constant(1, shape=[2,2], dtype=tf.float64),
  input=tf.constant(1, shape=[2,2], dtype=tf.float64),
  input_min=tf.constant([], shape=[0], dtype=tf.float64),
  input_max=tf.constant(-10, shape=[], dtype=tf.float64),
  axis=-1)
```

The code assumes `input_min` and `input_max` are scalars but there is no validation for this.

### Patches
We have patched the issue in GitHub commit [098e7762d909bac47ce1dbabe6dfd06294cb9d58](https://github.com/tensorflow/tensorflow/commit/098e7762d909bac47ce1dbabe6dfd06294cb9d58).
    
The fix will be included in TensorFlow 2.9.0. We will also cherrypick this commit on TensorFlow 2.8.1, TensorFlow 2.7.2, and TensorFlow 2.6.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 Neophytos Christou from Secure Systems Lab at Brown University.
Vulnerability ID: GHSA-h2wq-prv9-2f56
CWE: CWE-20
Severity score: 5.5
Project: https://github.com/tensorflow/tensorflow
Published: 2022-05-24
File: tensorflow/core/kernels/quantize_and_dequantize_op.cc
Commit message: Fix tf.raw_ops.QuantizeAndDequantizeV4Grad vulnerability with invalid input_min or input_max.

Check that argument is actually a scalar before treating it as such.

PiperOrigin-RevId: 445198280
Commit author: Alan Liu

Vulnerable version (before fix):
 class QuantizeAndDequantizeV4GradientOp : public OpKernel {     OP_REQUIRES(ctx,                 input_min_tensor.dims() == 0 || input_min_tensor.dims() == 1,                 errors::InvalidArgument(                     "Input min tensor must have dimension 1. Recieved ",                     input_min_tensor.dims(), "."));     const Tensor& input_max_tensor = ctx->input(3);     OP_REQUIRES(ctx,                 input_max_tensor.dims() == 0 || input_max_tensor.dims() == 1,                 errors::InvalidArgument(                     "Input max tensor must have dimension 1. Recieved ",                     input_max_tensor.dims(), "."));     if (axis_ != -1) {       OP_REQUIRES( class QuantizeAndDequantizeV4GradientOp : public OpKernel {                    ctx->allocate_output(2, min_max_shape, &input_max_backprop));      if (axis_ == -1) {       functor::QuantizeAndDequantizeOneScaleGradientFunctor<Device, T> f;       f(ctx->eigen_device<Device>(), gradient.template flat<T>(),         input.template flat<T>(), input_min_tensor.scalar<T>(),

Fixed version (after fix):
 class QuantizeAndDequantizeV4GradientOp : public OpKernel {     OP_REQUIRES(ctx,                 input_min_tensor.dims() == 0 || input_min_tensor.dims() == 1,                 errors::InvalidArgument(                     "Input min tensor must have dimension 0 or 1. Received ",                     input_min_tensor.dims(), "."));     const Tensor& input_max_tensor = ctx->input(3);     OP_REQUIRES(ctx,                 input_max_tensor.dims() == 0 || input_max_tensor.dims() == 1,                 errors::InvalidArgument(                     "Input max tensor must have dimension 0 or 1. Received ",                     input_max_tensor.dims(), "."));     if (axis_ != -1) {       OP_REQUIRES( class QuantizeAndDequantizeV4GradientOp : public OpKernel {                    ctx->allocate_output(2, min_max_shape, &input_max_backprop));      if (axis_ == -1) {       OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(input_min_tensor.shape()),                   errors::InvalidArgument(                       "input_min must be a scalar if axis is unspecified"));       OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(input_max_tensor.shape()),                   errors::InvalidArgument(                       "input_max must be a scalar if axis is unspecified"));       functor::QuantizeAndDequantizeOneScaleGradientFunctor<Device, T> f;       f(ctx->eigen_device<Device>(), gradient.template flat<T>(),         input.template flat<T>(), input_min_tensor.scalar<T>(),

Patch:
patch: @@ -174,13 +174,13 @@ class QuantizeAndDequantizeV4GradientOp : public OpKernel {
     OP_REQUIRES(ctx,
                 input_min_tensor.dims() == 0 || input_min_tensor.dims() == 1,
                 errors::InvalidArgument(
-                    "Input min tensor must have dimension 1. Recieved ",
+                    "Input min tensor must have dimension 0 or 1. Received ",
                     input_min_tensor.dims(), "."));
     const Tensor& input_max_tensor = ctx->input(3);
     OP_REQUIRES(ctx,
                 input_max_tensor.dims() == 0 || input_max_tensor.dims() == 1,
                 errors::InvalidArgument(
-                    "Input max tensor must have dimension 1. Recieved ",
+                    "Input max tensor must have dimension 0 or 1. Received ",
                     input_max_tensor.dims(), "."));
     if (axis_ != -1) {
       OP_REQUIRES(
@@ -203,6 +203,12 @@ class QuantizeAndDequantizeV4GradientOp : public OpKernel {
                    ctx->allocate_output(2, min_max_shape, &input_max_backprop));
 
     if (axis_ == -1) {
+      OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(input_min_tensor.shape()),
+                  errors::InvalidArgument(
+                      "input_min must be a scalar if axis is unspecified"));
+      OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(input_max_tensor.shape()),
+                  errors::InvalidArgument(
+                      "input_max must be a scalar if axis is unspecified"));
       functor::QuantizeAndDequantizeOneScaleGradientFunctor<Device, T> f;
       f(ctx->eigen_device<Device>(), gradient.template flat<T>(),
         input.template flat<T>(), input_min_tensor.scalar<T>(),
path: tensorflow/core/kernels/quantize_and_dequantize_op.cc