CVE-2021-37646: TensorFlow - Bad alloc in StringNGrams caused by integer conversion

tensorflow Updated 02 Oct 2025

Summary

25/08/2021
Yes i
Moderate
5.5

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

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.

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 MarkAI Find prospects already talking about the problem you solve AI watches the communities your customers use to find qualified prospects, then alerts you when a conversation is worth joining. See MarkAI

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.4 2.3.4
    pip install --upgrade "tensorflow==2.3.4"
  • >= 2.4.0, < 2.4.3 2.4.3
    pip install --upgrade "tensorflow==2.4.3"
  • >= 2.5.0, < 2.5.1 2.5.1
    pip 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 →

Share this vulnerability

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.

Related vulnerabilities

Recent vulnerabilities recorded for tensorflow.

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.4 2.3.4
    pip install --upgrade "tensorflow==2.3.4"
  • >= 2.4.0, < 2.4.3 2.4.3
    pip install --upgrade "tensorflow==2.4.3"
  • >= 2.5.0, < 2.5.1 2.5.1
    pip 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 →

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