Division by 0 in `ResourceScatterDiv` in TensorFlow
Description
TensorFlow is an end-to-end open source platform for machine learning. In affected versions the implementation of tf.raw_ops.ResourceScatterDiv is vulnerable to a division by 0 error. The implementation uses a common class for all binary operations but fails to treat the division by 0 case separately. We have patched the issue in GitHub commit 4aacb30888638da75023e6601149415b39763d76. 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.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
tensorflowPyPI | < 2.3.4 | 2.3.4 |
tensorflowPyPI | >= 2.4.0, < 2.4.3 | 2.4.3 |
tensorflowPyPI | >= 2.5.0, < 2.5.1 | 2.5.1 |
tensorflow-cpuPyPI | < 2.3.4 | 2.3.4 |
tensorflow-cpuPyPI | >= 2.4.0, < 2.4.3 | 2.4.3 |
tensorflow-cpuPyPI | >= 2.5.0, < 2.5.1 | 2.5.1 |
tensorflow-gpuPyPI | < 2.3.4 | 2.3.4 |
tensorflow-gpuPyPI | >= 2.4.0, < 2.4.3 | 2.4.3 |
tensorflow-gpuPyPI | >= 2.5.0, < 2.5.1 | 2.5.1 |
Affected products
1- Range: >= 2.5.0, < 2.5.1
Patches
14aacb3088863Disallow division by zero FPE in `tf.raw_ops.ResourceScatterDiv`
2 files changed · +37 −1
tensorflow/core/kernels/resource_variable_ops.cc+35 −0 modified@@ -873,6 +873,35 @@ TF_CALL_GPU_NUMBER_TYPES(REGISTER_GATHER_ND_GPU); #undef REGISTER_GATHER_ND_ALL_INDICES #undef REGISTER_GATHER_ND_FULL +namespace { + +template <typename Device> +bool isCPUDevice() { + return false; +} + +template <> +bool isCPUDevice<CPUDevice>() { + return true; +} + +template <typename T> +bool ValidateInput(const Tensor& updates) { + const auto updates_flat = updates.flat<T>(); + const T zero(0); + for (int i = 0; i < updates.NumElements(); i++) { + if (updates_flat(i) == zero) return false; + } + return true; +} + +template <> +bool ValidateInput<Variant>(const Tensor& updates) { + return true; +} + +} // namespace + template <typename Device, typename T, typename Index, scatter_op::UpdateOp op> class ResourceScatterUpdateOp : public OpKernel { public: @@ -939,6 +968,12 @@ class ResourceScatterUpdateOp : public OpKernel { " indexing: ", params->dim_size(0), " > ", std::numeric_limits<Index>::max())); + // Prevent division by 0 + if (isCPUDevice<Device>() && op == tensorflow::scatter_op::UpdateOp::DIV) { + OP_REQUIRES(c, ValidateInput<T>(updates), + errors::InvalidArgument("updates must not contain 0")); + } + if (N > 0) { auto indices_flat = indices.flat<Index>(); auto params_flat = params->flat_outer_dims<T>();
tensorflow/python/distribute/sharded_variable_test.py+2 −1 modified@@ -175,8 +175,9 @@ def func(): 'scatter_update') def test_scatter_ops_even_partition(self, op): v = variables_lib.Variable(array_ops.zeros((30, 1))) + # Make sure values does not contain 0 due to testing `scatter_div`! sparse_delta = ops.IndexedSlices( - values=constant_op.constant([[0.], [1.], [2.], [3.], [4.]]), + values=constant_op.constant([[1.], [2.], [3.], [4.], [5.]]), indices=constant_op.constant([0, 10, 12, 21, 22])) v0 = variables_lib.Variable(array_ops.zeros((10, 1)))
Vulnerability mechanics
Generated by null/stub on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
7- github.com/advisories/GHSA-ch4f-829c-v5pwghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2021-37642ghsaADVISORY
- github.com/pypa/advisory-database/tree/main/vulns/tensorflow-cpu/PYSEC-2021-555.yamlghsaWEB
- github.com/pypa/advisory-database/tree/main/vulns/tensorflow-gpu/PYSEC-2021-753.yamlghsaWEB
- github.com/pypa/advisory-database/tree/main/vulns/tensorflow/PYSEC-2021-264.yamlghsaWEB
- github.com/tensorflow/tensorflow/commit/4aacb30888638da75023e6601149415b39763d76ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/security/advisories/GHSA-ch4f-829c-v5pwghsax_refsource_CONFIRMWEB
News mentions
0No linked articles in our index yet.