CVE-2021-37646: TensorFlow - Bad alloc in StringNGrams caused by integer conversion
Summary
CVE-2021-37646: Bad alloc in StringNGrams caused by integer conversion. Affects tensorflow across 3 version ranges.
< 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
- tensorflow · Bitnami
- tensorflow-cpu · PyPI
- tensorflow-gpu · PyPI
The same CVE-2021-37646 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 developed by Google. It provides a comprehensive ecosystem of tools, libraries, and resources for building and deploying machine learning models. TensorFlow includes various operations and functions for data processing, including the StringNGrams function. This function is used to generate n-grams from a given set of strings. However, a vulnerability has been discovered in the implementation of StringNGrams that can lead to a bad alloc error due to an integer conversion issue.
Vulnerability Detail
The vulnerability in StringNGrams is caused by an integer overflow issue. When the function is called with negative ngram_widths values, the implementation converts these signed integers to unsigned ones and then allocates memory based on the converted value. However, this conversion can result in a negative value being transformed into a large unsigned integer, leading to a bad alloc error. An attacker could potentially exploit this vulnerability to cause a denial-of-service condition or execute arbitrary code.
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.3.42.3.4pip install --upgrade "tensorflow==2.3.4" -
>= 2.4.0, < 2.4.32.4.3pip install --upgrade "tensorflow==2.4.3" -
>= 2.5.0, < 2.5.12.5.1pip install --upgrade "tensorflow==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 avoid passing negative ngram_widths values to the StringNGrams function. Ensure that the input values are within the valid range to prevent triggering the vulnerability. Here is an example of how to call the function with valid input:
import tensorflow as tf
tf.raw_ops.StringNGrams(
data=['',''],
data_splits=[0,2],
separator=' '*100,
ngram_widths=[0,0,0,0],
left_pad=' ',
right_pad=' ',
pad_width=100,
preserve_short_sequences=False)
Conclusion
The vulnerability in TensorFlow's StringNGrams function can lead to a bad alloc error due to an integer conversion issue. It is recommended to update to TensorFlow 2.6.0 or later to mitigate the risk. Ensure that negative ngram_widths values are not passed as input to the function. For further support on vulnerability remediation, you can contact DevNack.com.
See every vulnerability recorded for tensorflow →
FAQ
The vulnerability can lead to a bad alloc error in the StringNGrams function of TensorFlow. This can result in a denial-of-service condition or potential execution of arbitrary code.
To fix the vulnerability, update your TensorFlow installation to version 2.6.0 or later. Additionally, review and update any code that uses the StringNGrams function to ensure that negative ngram_widths values are not passed as input.
Yes, you can avoid passing negative ngram_widths values to the StringNGrams function as a workaround. Ensure that the input values are within the valid range.
There are no known instances of this vulnerability being exploited in the wild. However, it is still important to apply the patch and take necessary precautions to prevent potential attacks.
You can find more information about this vulnerability in the TensorFlow security guide and the GitHub advisory database. Links to these resources are provided in the vulnerability details.
References
Related vulnerabilities
Recent vulnerabilities recorded for tensorflow.