CVE-2021-29542: TensorFlow Heap Buffer Overflow in tf.raw_ops.StringNGrams
Summary
CVE-2021-29542: TensorFlow is an end-to-end open source platform for machine learning. Affects tensorflow-gpu across 5 version ranges.
< 2.1.4
>= 2.2.0, < 2.2.3
>= 2.3.0, < 2.3.3
>= 2.4.0, < 2.4.2
< ba424dd8f16f7110eea526a8086f1a155f14f22b
Fixed in 4 releases; the minimum fixed version depends on the release line you run. How to fix ↓
Also tracked for
- tensorflow · Bitnami
- tensorflow · PyPI
- tensorflow · PyPI
- tensorflow-cpu · PyPI
The same CVE-2021-29542 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 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 includes various operations and functions to manipulate and process data, including the tf.raw_ops.StringNGrams function.
Vulnerability Detail
The vulnerability (CVE-2021-29542) in TensorFlow arises from a heap buffer overflow in the tf.raw_ops.StringNGrams function. An attacker can exploit this vulnerability by passing crafted inputs to the function. The issue occurs due to a lack of proper input validation and handling of corner cases in the implementation. Specifically, when the input is split in such a way that the generated tokens should only contain padding elements and the num_tokens parameter is set to 0, the implementation fails to handle this scenario correctly. As a result, the code attempts to read data from an invalid memory location, leading to a heap buffer overflow.
How To Fix
Latest published version: 2.12.0 as of 22 Oct 2026
pip install --upgrade "tensorflow-gpu==2.12.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-gpu==2.1.4" -
>= 2.2.0, < 2.2.32.2.3pip install --upgrade "tensorflow-gpu==2.2.3" -
>= 2.3.0, < 2.3.32.3.3pip install --upgrade "tensorflow-gpu==2.3.3" -
>= 2.4.0, < 2.4.22.4.2pip install --upgrade "tensorflow-gpu==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
There is no specific workaround provided for this vulnerability. However, users can mitigate the risk by ensuring they are using one of the fixed versions of TensorFlow (2.5.0, 2.4.2, 2.3.3, 2.2.3, or 2.1.4).
Conclusion
The vulnerability (CVE-2021-29542) in TensorFlow's tf.raw_ops.StringNGrams function can be exploited by an attacker to cause a heap buffer overflow. To mitigate this vulnerability, users should update their TensorFlow installations to one of the fixed versions (2.5.0, 2.4.2, 2.3.3, 2.2.3, or 2.1.4). It is recommended to follow the official TensorFlow documentation or use package managers like pip to update the TensorFlow package. For further support on vulnerability remediation, you can contact DevNack.com.
See every vulnerability recorded for tensorflow-gpu →
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 be exploited by an attacker to cause a heap buffer overflow in TensorFlow, potentially leading to arbitrary code execution or a denial of service (DoS) condition.
The vulnerability affects TensorFlow versions 2.1.4 and later, including TensorFlow 2.5.0. Versions prior to 2.1.4 are not affected.
Yes, the vulnerability has been fixed in TensorFlow versions 2.5.0, 2.4.2, 2.3.3, 2.2.3, and 2.1.4. Users are advised to update their TensorFlow installations to one of the fixed versions.
You can find more information about this vulnerability in the official TensorFlow documentation or by referring to the CVE-2021-29542 advisory.
References
Related vulnerabilities
Recent vulnerabilities recorded for tensorflow-gpu.