CVE-2021-37640: TensorFlow - Division by 0 Exception in tf.raw_ops.SparseReshape

tensorflow Updated 02 Oct 2025

Summary

06/03/2024
Yes i
Medium
Not published

CVE-2021-37640: Integer division by 0 in sparse reshaping in TensorFlow. Affects tensorflow across 3 version ranges.

>= 2.3.0, < 2.3.4 >= 2.4.0, < 2.4.3 >= 2.5.0, < 2.5.1

Fixed in 2.3.4, 2.4.3 or 2.5.1, depending on the release line you run. How to fix ↓

Also tracked for

The same CVE-2021-37640 is tracked separately for each ecosystem and vendor. Its fix information is the same; the package and affected versions differ.

Free scan

Check if this vulnerability affects your application

Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.

Check my app free

No signup required · Results in about 60 seconds

Sponsored Devnack Penetration testing for web, API and mobile Manual testing by engineers who build software, not an auditor's checklist. Trusted by teams at Swift and Google Talk to Devnack

Background

TensorFlow is an end-to-end open source platform for machine learning. It provides a comprehensive ecosystem of tools, libraries, and resources for building and deploying machine learning models. TensorFlow is widely used in various industries and research fields.

Vulnerability Detail

The vulnerability (CVE-2021-37640) exists in the implementation of tf.raw_ops.SparseReshape in TensorFlow. The issue arises when the reshaping functor is called without checking if the input or target shape has a non-zero number of elements. This can lead to an integral division by 0 exception. An attacker could potentially exploit this vulnerability to cause a denial-of-service (DoS) condition or execute arbitrary code.

How To Fix

Fixed versions by release line

The minimum fixed version depends on the release line you run.

  • >= 2.3.0, < 2.3.4 2.3.4
  • >= 2.4.0, < 2.4.3 2.4.3
  • >= 2.5.0, < 2.5.1 2.5.1

Tip — Not sure which release line you're on? Vulert can find the fix for the version you are using. Scan free →

Workaround

As a workaround, you can add a check in your code to ensure that both the input and target shape have a non-zero number of elements before calling tf.raw_ops.SparseReshape. Here's an example in Python:

import tensorflow as tf

input_shape = ...  # shape of the input
output_shape = ...  # shape of the target

if tf.size(input_shape) > 0 and tf.size(output_shape) > 0:
    # Call tf.raw_ops.SparseReshape
    ...
else:
    # Handle the case where either input_shape or output_shape has zero elements
    ...

Conclusion

The vulnerability (CVE-2021-37640) in TensorFlow's tf.raw_ops.SparseReshape can result in a division by 0 exception. It is recommended to update to TensorFlow 2.6.0 or later to fix this issue. Alternatively, apply the cherrypicked commit on TensorFlow 2.5.1. Adding a check in your code to ensure non-zero input and target shapes can serve as a temporary workaround. For further support on vulnerability remediation, please contact DevNack.com.

See every vulnerability recorded for tensorflow →

Share this vulnerability

FAQ

TensorFlow is an open source platform for machine learning that provides a comprehensive set of tools and resources for building and deploying machine learning models.

This vulnerability can lead to a division by 0 exception in TensorFlow, potentially causing a denial-of-service (DoS) condition or enabling arbitrary code execution.

To fix this vulnerability, update TensorFlow to version 2.6.0 or later. Alternatively, apply the cherrypicked commit on TensorFlow 2.5.1. Details on the fix can be found in the provided GitHub commit.

Yes, you can add a check in your code to ensure that both the input and target shape have a non-zero number of elements before calling tf.raw_ops.SparseReshape. Refer to the provided workaround section for more details.

You can find more information about this vulnerability in the provided GitHub commit and the official TensorFlow documentation.

Related vulnerabilities

Recent vulnerabilities recorded for tensorflow.

How To Fix

Fixed versions by release line

The minimum fixed version depends on the release line you run.

  • >= 2.3.0, < 2.3.4 2.3.4
  • >= 2.4.0, < 2.4.3 2.4.3
  • >= 2.5.0, < 2.5.1 2.5.1

Tip — Not sure which release line you're on? Vulert can find the fix for the version you are using. Scan free →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback