Vulnerability: Division by 0 in `ResourceGather` ### Impact
An attacker can trigger a crash via a floating point exception in `tf.raw_ops.ResourceGather`:

```python
import tensorflow as tf

tensor = tf.constant(value=[[]],shape=(0,1),dtype=tf.uint32)
v = tf.Variable(tensor)
tf.raw_ops.ResourceGather(
  resource=v.handle,
  indices=[0],
  dtype=tf.uint32,
  batch_dims=1,
  validate_indices=False)
```

The [implementation](https://github.com/tensorflow/tensorflow/blob/f24faa153ad31a4b51578f8181d3aaab77a1ddeb/tensorflow/core/kernels/resource_variable_ops.cc#L725-L731) computes the value of a value, `batch_size`, and then divides by it without checking that this value is not 0. 

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

The fix will be included in TensorFlow 2.6.0. We will also cherrypick this commit on TensorFlow 2.5.1, TensorFlow 2.4.3, and TensorFlow 2.3.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-qjj8-32p7-h289
CWE: CWE-369
Severity score: 5.5
Project: https://github.com/tensorflow/tensorflow
Published: 2021-08-25
File: tensorflow/core/kernels/resource_variable_ops.cc
Commit message: Prevent division by 0 in `resource_variable_ops.cc`

PiperOrigin-RevId: 387939939
Change-Id: Ib04902d63756633999959a70613f2eaa30c2c151
Commit author: Mihai Maruseac

Vulnerable version (before fix):
 class ResourceGatherOp : public OpKernel {         copy_functor(c->eigen_device<Device>(), tmp_indices.flat<Index>(),                      indices.flat<Index>());          AddBatchOffsets(&tmp_indices, params);         op_indices = &tmp_indices;       }  class ResourceGatherOp : public OpKernel {   // Example: batch_dims = 1, indices = [[0, 1, 2], [0, 1, 2]]   // If indexing into a params dimension of size 4, then the indices will become   // [0, 1, 2, 4, 5, 6]   void AddBatchOffsets(Tensor* indices, const Tensor& params) {     int64_t batch_size = 1;  // The size of all batch dimensions.     for (int idx = 0; idx < batch_dims_; ++idx) {       batch_size *= params.dim_size(idx);     }      auto indices_flat = indices->flat<Index>();     int64_t const index_inner_size = indices->NumElements() / batch_size;

Fixed version (after fix):
 class ResourceGatherOp : public OpKernel {         copy_functor(c->eigen_device<Device>(), tmp_indices.flat<Index>(),                      indices.flat<Index>());          AddBatchOffsets(c, &tmp_indices, params);         if (!c->status().ok()) return;         op_indices = &tmp_indices;       }  class ResourceGatherOp : public OpKernel {   // Example: batch_dims = 1, indices = [[0, 1, 2], [0, 1, 2]]   // If indexing into a params dimension of size 4, then the indices will become   // [0, 1, 2, 4, 5, 6]   void AddBatchOffsets(OpKernelContext* ctx, Tensor* indices,                        const Tensor& params) {     int64_t batch_size = 1;  // The size of all batch dimensions.     for (int idx = 0; idx < batch_dims_; ++idx) {       batch_size *= params.dim_size(idx);     }     OP_REQUIRES(         ctx, batch_size != 0,         errors::InvalidArgument(             "Inner size of indices would result in batch_size of 0 and a ",             "division by 0 in the implementation. This is illegal"));      auto indices_flat = indices->flat<Index>();     int64_t const index_inner_size = indices->NumElements() / batch_size;

Patch:
patch: @@ -710,7 +710,8 @@ class ResourceGatherOp : public OpKernel {
         copy_functor(c->eigen_device<Device>(), tmp_indices.flat<Index>(),
                      indices.flat<Index>());
 
-        AddBatchOffsets(&tmp_indices, params);
+        AddBatchOffsets(c, &tmp_indices, params);
+        if (!c->status().ok()) return;
         op_indices = &tmp_indices;
       }
 
@@ -742,11 +743,17 @@ class ResourceGatherOp : public OpKernel {
   // Example: batch_dims = 1, indices = [[0, 1, 2], [0, 1, 2]]
   // If indexing into a params dimension of size 4, then the indices will become
   // [0, 1, 2, 4, 5, 6]
-  void AddBatchOffsets(Tensor* indices, const Tensor& params) {
+  void AddBatchOffsets(OpKernelContext* ctx, Tensor* indices,
+                       const Tensor& params) {
     int64_t batch_size = 1;  // The size of all batch dimensions.
     for (int idx = 0; idx < batch_dims_; ++idx) {
       batch_size *= params.dim_size(idx);
     }
+    OP_REQUIRES(
+        ctx, batch_size != 0,
+        errors::InvalidArgument(
+            "Inner size of indices would result in batch_size of 0 and a ",
+            "division by 0 in the implementation. This is illegal"));
 
     auto indices_flat = indices->flat<Index>();
     int64_t const index_inner_size = indices->NumElements() / batch_size;
path: tensorflow/core/kernels/resource_variable_ops.cc