Vulnerability: Division by zero in TFLite's convolution code ### Impact
TFLite's [convolution code](https://github.com/tensorflow/tensorflow/blob/09c73bca7d648e961dd05898292d91a8322a9d45/tensorflow/lite/kernels/conv.cc) has multiple division where the divisor is controlled by the user and not checked to be non-zero. For example:

```cc 
const int input_size = NumElements(input) / SizeOfDimension(input, 0);
```

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

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 members of the Aivul Team from Qihoo 360.
Vulnerability ID: GHSA-3qgw-p4fm-x7gf
CWE: CWE-369
Severity score: 2.5
Project: https://github.com/tensorflow/tensorflow
Published: 2021-05-21
File: tensorflow/lite/kernels/conv.cc
Commit message: Prevent division by 0.

PiperOrigin-RevId: 370962554
Change-Id: I0b9b62f4d8e1046dd88f9433f8dfeaf61a901680
Commit author: Mihai Maruseac

Vulnerable version (before fix):
 TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,     // Only one scale factor per batch is typically necessary. See optimized     // implementation for why we need to allocate for the height of the inputs     // flattened to 2D.     const int height = NumElements(input) / channels_in;     int scaling_dims[1] = {height};     if (!TfLiteIntArrayEqualsArray(scaling_factors->dims, 1, scaling_dims)) { TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,       input_offsets->type = kTfLiteInt32;       input_offsets->allocation_type = kTfLiteArenaRw;       // See above comment for the need to allocate for height of inputs.       const int height = NumElements(input) / channels_in;       const int input_offset_dims[1] = {height};       if (!TfLiteIntArrayEqualsArray(input_offsets->dims, 1, TfLiteStatus EvalHybridPerChannel(TfLiteContext* context, TfLiteNode* node,   CalculateActivationRange(params->activation, &output_activation_min,                            &output_activation_max);    const int input_size = NumElements(input) / SizeOfDimension(input, 0);   const int batch_size = SizeOfDimension(input, 0);   TfLiteTensor* quantized_input_tensor;   TF_LITE_ENSURE_OK(context,                     GetTemporarySafe(context, node, data->input_quantized_index, TfLiteStatus EvalHybrid(TfLiteContext* context, TfLiteNode* node,   CalculateActivationRange(params->activation, &output_activation_min,                            &output_activation_max);    const int input_size = NumElements(input) / SizeOfDimension(input, 0);   const int batch_size = SizeOfDimension(input, 0);    const float* input_ptr = GetTensorData<float>(input);   TfLiteTensor* quantized_input_tensor;

Fixed version (after fix):
 TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,     // Only one scale factor per batch is typically necessary. See optimized     // implementation for why we need to allocate for the height of the inputs     // flattened to 2D.     TF_LITE_ENSURE(context, channels_in != 0);     const int height = NumElements(input) / channels_in;     int scaling_dims[1] = {height};     if (!TfLiteIntArrayEqualsArray(scaling_factors->dims, 1, scaling_dims)) { TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,       input_offsets->type = kTfLiteInt32;       input_offsets->allocation_type = kTfLiteArenaRw;       // See above comment for the need to allocate for height of inputs.       TF_LITE_ENSURE(context, channels_in != 0);       const int height = NumElements(input) / channels_in;       const int input_offset_dims[1] = {height};       if (!TfLiteIntArrayEqualsArray(input_offsets->dims, 1, TfLiteStatus EvalHybridPerChannel(TfLiteContext* context, TfLiteNode* node,   CalculateActivationRange(params->activation, &output_activation_min,                            &output_activation_max);    const int batch_size = SizeOfDimension(input, 0);   TF_LITE_ENSURE(context, batch_size != 0);   const int input_size = NumElements(input) / batch_size;   TfLiteTensor* quantized_input_tensor;   TF_LITE_ENSURE_OK(context,                     GetTemporarySafe(context, node, data->input_quantized_index, TfLiteStatus EvalHybrid(TfLiteContext* context, TfLiteNode* node,   CalculateActivationRange(params->activation, &output_activation_min,                            &output_activation_max);    const int batch_size = SizeOfDimension(input, 0);   TF_LITE_ENSURE(context, batch_size != 0);   const int input_size = NumElements(input) / batch_size;    const float* input_ptr = GetTensorData<float>(input);   TfLiteTensor* quantized_input_tensor;

Patch:
patch: @@ -545,6 +545,7 @@ TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,
     // Only one scale factor per batch is typically necessary. See optimized
     // implementation for why we need to allocate for the height of the inputs
     // flattened to 2D.
+    TF_LITE_ENSURE(context, channels_in != 0);
     const int height = NumElements(input) / channels_in;
     int scaling_dims[1] = {height};
     if (!TfLiteIntArrayEqualsArray(scaling_factors->dims, 1, scaling_dims)) {
@@ -587,6 +588,7 @@ TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,
       input_offsets->type = kTfLiteInt32;
       input_offsets->allocation_type = kTfLiteArenaRw;
       // See above comment for the need to allocate for height of inputs.
+      TF_LITE_ENSURE(context, channels_in != 0);
       const int height = NumElements(input) / channels_in;
       const int input_offset_dims[1] = {height};
       if (!TfLiteIntArrayEqualsArray(input_offsets->dims, 1,
@@ -886,8 +888,9 @@ TfLiteStatus EvalHybridPerChannel(TfLiteContext* context, TfLiteNode* node,
   CalculateActivationRange(params->activation, &output_activation_min,
                            &output_activation_max);
 
-  const int input_size = NumElements(input) / SizeOfDimension(input, 0);
   const int batch_size = SizeOfDimension(input, 0);
+  TF_LITE_ENSURE(context, batch_size != 0);
+  const int input_size = NumElements(input) / batch_size;
   TfLiteTensor* quantized_input_tensor;
   TF_LITE_ENSURE_OK(context,
                     GetTemporarySafe(context, node, data->input_quantized_index,
@@ -989,8 +992,9 @@ TfLiteStatus EvalHybrid(TfLiteContext* context, TfLiteNode* node,
   CalculateActivationRange(params->activation, &output_activation_min,
                            &output_activation_max);
 
-  const int input_size = NumElements(input) / SizeOfDimension(input, 0);
   const int batch_size = SizeOfDimension(input, 0);
+  TF_LITE_ENSURE(context, batch_size != 0);
+  const int input_size = NumElements(input) / batch_size;
 
   const float* input_ptr = GetTensorData<float>(input);
   TfLiteTensor* quantized_input_tensor;
path: tensorflow/lite/kernels/conv.cc