CVE-2021-41226: TensorFlow - Heap Out-of-Bounds Access in SparseBinCount
Summary
CVE-2021-41226: TensorFlow is an open source platform for machine learning. Affects tensorflow across 5 version ranges.
< 2.4.4
>= 2.5.0, < 2.5.2
>= 2.6.0, < 2.6.1
>= 2.7.0rc0, < 2.7.0
< f410212e373eb2aec4c9e60bf3702eba99a38aba
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-41226 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 platform for machine learning. It provides a comprehensive ecosystem of tools, libraries, and resources for developing and deploying machine learning models. TensorFlow allows users to build and train neural networks, perform data preprocessing, and evaluate model performance. It is widely used in various domains, including computer vision, natural language processing, and reinforcement learning. However, in certain versions of TensorFlow, there is a vulnerability in the implementation of the SparseBinCount function.
Vulnerability Detail
The vulnerability in TensorFlow's SparseBinCount function arises due to missing validation between the elements of the values argument and the shape of the sparse output. This flaw can lead to a heap out-of-bounds access, potentially resulting in memory corruption or arbitrary code execution. Attackers may exploit this vulnerability to gain unauthorized access, modify data, or launch further attacks on the affected system. The issue has been identified and addressed in TensorFlow 2.7.0, with fixes also backported to TensorFlow 2.6.1, TensorFlow 2.5.2, and TensorFlow 2.4.4, which are still within the supported range.
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.4.42.4.4pip install --upgrade "tensorflow==2.4.4" -
>= 2.5.0, < 2.5.22.5.2pip install --upgrade "tensorflow==2.5.2" -
>= 2.6.0, < 2.6.12.6.1pip install --upgrade "tensorflow==2.6.1" -
>= 2.7.0rc0, < 2.7.02.7.0pip install --upgrade "tensorflow==2.7.0"
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 consider implementing input validation checks before using the SparseBinCount function. By ensuring that the values argument aligns with the expected shape of the sparse output, the risk of encountering the heap out-of-bounds access can be mitigated. Here is an example of how input validation can be performed in Python:
import tensorflow as tf
def safe_sparse_bin_count(values, minlength=None, maxlength=None):
if minlength is not None and len(values) < minlength:
raise ValueError('Length of values is less than the specified minlength.')
if maxlength is not None and len(values) > maxlength:
raise ValueError('Length of values exceeds the specified maxlength.')
return tf.sparse.bincount(values, minlength=minlength, maxlength=maxlength)
Conclusion
In conclusion, TensorFlow versions prior to 2.7.0 are vulnerable to a heap out-of-bounds access in the SparseBinCount function. This vulnerability can have serious security implications, including unauthorized access and arbitrary code execution. It is crucial to update TensorFlow to the fixed versions (2.7.0, 2.6.1, 2.5.2, or 2.4.4) to mitigate the risk. For further support on vulnerability remediation, please contact DevNack.com.
See every vulnerability recorded for tensorflow →
FAQ
TensorFlow is an open source platform for machine learning that provides tools and resources for developing and deploying machine learning models.
SparseBinCount is a function in TensorFlow that performs a sparse bin count operation. It counts the occurrences of each value in a sparse tensor and returns a dense tensor with the counts.
A heap out-of-bounds access occurs when a program tries to read from or write to memory outside the bounds of a dynamically allocated heap buffer. This can lead to memory corruption, crashes, or arbitrary code execution.
The vulnerability in TensorFlow's SparseBinCount function can potentially allow attackers to gain unauthorized access, modify data, or execute arbitrary code on the affected system. It is important to apply the necessary updates to mitigate these risks.
You can use Vulert's playground at Vulert Scanner to check if your application is affected by this vulnerability. Vulert monitors and alerts in real-time for open-source vulnerabilities, using just the Pipfile.lock file as input.
References
Related vulnerabilities
Recent vulnerabilities recorded for tensorflow.