CVE-2021-29597: Division by zero in TFLite's implementation of SpaceToBatchNd in TensorFlow
Summary
CVE-2021-29597: Division by zero in TFLite's implementation of SpaceToBatchNd. Affects tensorflow across 4 version ranges.
< 2.1.4
>= 2.2.0, < 2.2.3
>= 2.3.0, < 2.3.3
>= 2.4.0, < 2.4.2
Fixed in 4 releases; the minimum fixed version depends on the release line you run. How to fix ↓
Also tracked for
- tensorflow · Bitnami
- tensorflow-cpu · PyPI
- tensorflow-gpu · PyPI
The same CVE-2021-29597 is tracked separately for each ecosystem and vendor. Its fix information is the same; the package and affected versions differ.
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.
No signup required · Results in about 60 seconds
Background
TensorFlow is an open-source machine learning framework that provides a wide range of tools and libraries for developing and deploying machine learning models. It is widely used in various industries and has a large community of developers. One of the key components of TensorFlow is TFLite, which is a lightweight version of TensorFlow designed for mobile and embedded devices. TFLite includes various operators, including the SpaceToBatchNd operator, which is used for reshaping tensors by splitting them into blocks and rearranging the blocks. However, a vulnerability has been discovered in the implementation of the SpaceToBatchNd operator in TFLite, which can lead to a division by zero error.
Vulnerability Detail
The vulnerability in TFLite's implementation of the SpaceToBatchNd operator arises from a division by zero error. The vulnerable code can be found in the space_to_batch_nd.cc file of the TensorFlow Lite library. The code attempts to calculate the output size of the operator by dividing the final dimension size by the corresponding block shape dimension. However, it fails to check if the block shape dimension is zero, leading to a division by zero error. An attacker can exploit this vulnerability by crafting a model with a block input dimension set to zero, causing the division by zero error to occur during the execution of the model. The impact of this vulnerability can vary depending on the specific usage of the SpaceToBatchNd operator in a TensorFlow application, but it has the potential to cause crashes, unexpected behavior, or even remote code execution.
How To Fix
Latest published version: 2.21.0 as of 22 Oct 2026
pip install --upgrade "tensorflow==2.21.0"
Upgrading to it may include breaking changes; the versions below are the minimum fixed release for this vulnerability.
If you can't move to the latest
The minimum fixed version depends on the release line you run.
-
< 2.1.42.1.4pip install --upgrade "tensorflow==2.1.4" -
>= 2.2.0, < 2.2.32.2.3pip install --upgrade "tensorflow==2.2.3" -
>= 2.3.0, < 2.3.32.3.3pip install --upgrade "tensorflow==2.3.3" -
>= 2.4.0, < 2.4.22.4.2pip install --upgrade "tensorflow==2.4.2"
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, users can manually check the block shape dimensions before using the SpaceToBatchNd operator and ensure that none of the dimensions are set to zero. This can be done by inspecting the input data or using appropriate validation checks. If a zero value is detected, alternative approaches or operators can be used to achieve the desired tensor reshaping. Here is an example code snippet that demonstrates the workaround:
import tensorflow as tf
block_shape = [2, 0, 3]
input_data = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
if any(dim == 0 for dim in block_shape):
# Handle the zero dimension case
# Alternative approach or operator can be used
...
else:
output_data = tf.nn.space_to_batch_nd(input_data, block_shape)
...
Note: The above code is a simplified example and may need to be adapted based on the specific usage of the SpaceToBatchNd operator in the application.
Conclusion
In conclusion, a division by zero vulnerability has been discovered in TFLite's implementation of the SpaceToBatchNd operator in TensorFlow. This vulnerability can be exploited by crafting a model with a zero value in the block input dimension, leading to a division by zero error. The impact of the vulnerability can range from crashes to remote code execution. Users should update to the patched versions of TensorFlow to mitigate this vulnerability. For further support on vulnerability remediation, please contact DevNack.com.
See every vulnerability recorded for tensorflow →
FAQ
TensorFlow is an open-source machine learning framework that provides tools and libraries for developing and deploying machine learning models. It is widely used in various industries and has a large community of developers.
TFLite is a lightweight version of TensorFlow designed for mobile and embedded devices. It includes various operators, including the SpaceToBatchNd operator.
The SpaceToBatchNd operator is used for reshaping tensors by splitting them into blocks and rearranging the blocks.
The impact of the vulnerability can vary depending on the specific usage of the SpaceToBatchNd operator in a TensorFlow application, but it has the potential to cause crashes, unexpected behavior, or even remote code execution.
The vulnerability has been patched in TensorFlow commit 6d36ba65577006affb272335b7c1abd829010708. Users should update to the patched versions of TensorFlow once they are released.
References
Related vulnerabilities
Recent vulnerabilities recorded for tensorflow.