CVE-2021-41219: TensorFlow - Undefined Behavior in Sparse Matrix Multiplication

tensorflow Updated 02 Oct 2025

Summary

06/03/2024
Yes i
High
Not published

CVE-2021-41219: Undefined behavior via nullptr reference binding in sparse matrix multiplication. Affects tensorflow across 3 version ranges.

< 2.4.4 >= 2.5.0, < 2.5.2 >= 2.6.0, < 2.6.1

Fixed in 2.4.4, 2.5.2 or 2.6.1, depending on the release line you run. How to fix ↓

Also tracked for

The same CVE-2021-41219 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 Devnack Penetration testing for web, API and mobile Manual testing by engineers who build software, not an auditor's checklist. Trusted by teams at Swift and Google Talk to Devnack

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 supports various operations, including sparse matrix multiplication, which is used in many machine learning algorithms.

Vulnerability Detail

The vulnerability (CVE-2021-41219) in TensorFlow's sparse matrix multiplication code arises when the dimensions of the input matrices are 0 or less. In such cases, an empty output tensor should be allocated, but nothing should be written to it. However, due to a programming error, attempts to write to this empty tensor can result in a heap out-of-bounds access. This can lead to undefined behavior and potential security risks. The issue affects multiple versions of TensorFlow, including 2.4.4, 2.5.2, and 2.6.0.

How To Fix

Fixed versions by release line

The minimum fixed version depends on the release line you run.

  • < 2.4.4 2.4.4
  • >= 2.5.0, < 2.5.2 2.5.2
  • >= 2.6.0, < 2.6.1 2.6.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, users can check the dimensions of the input matrices before performing sparse matrix multiplication. If any dimension is 0 or less, the operation should be skipped or handled differently to avoid the vulnerable code path. Here's an example in Python:

import tensorflow as tf

# Check dimensions
if a.shape[0] <= 0 or a.shape[1] <= 0 or b.shape[0] <= 0 or b.shape[1] <= 0:
    # Handle empty matrices
    result = tf.sparse.zeros((a.shape[0], b.shape[1]), dtype=tf.float32)
else:
    # Perform sparse matrix multiplication
    result = tf.sparse.matmul(a, b)

Conclusion

The vulnerability (CVE-2021-41219) in TensorFlow's sparse matrix multiplication code can lead to undefined behavior and potential security risks. It is crucial to update to the latest patched version of TensorFlow or apply the provided workaround to mitigate the vulnerability. For further support on vulnerability remediation, you can reach out to DevNack.com.

See every vulnerability recorded for tensorflow →

Share this vulnerability

FAQ

TensorFlow is an open source platform for machine learning developed by Google. It provides a comprehensive ecosystem of tools, libraries, and resources for developing and deploying machine learning models.

Sparse matrix multiplication is a mathematical operation used in various machine learning algorithms. It involves multiplying two matrices where one or both matrices have many zero elements.

The vulnerability occurs when the dimensions of the input matrices are 0 or less. This triggers a code path that attempts to write to an empty output tensor, leading to a heap out-of-bounds access and undefined behavior.

The vulnerability can result in undefined behavior, which may include crashes, memory corruption, or even potential security exploits. An attacker could potentially exploit this vulnerability to execute arbitrary code or gain unauthorized access to sensitive information.

To mitigate this vulnerability, it is recommended to update to the latest patched version of TensorFlow (2.7.0 or later). Alternatively, you can implement the provided workaround by checking the dimensions of the input matrices before performing sparse matrix multiplication.

Related vulnerabilities

Recent vulnerabilities recorded for tensorflow.

How To Fix

Fixed versions by release line

The minimum fixed version depends on the release line you run.

  • < 2.4.4 2.4.4
  • >= 2.5.0, < 2.5.2 2.5.2
  • >= 2.6.0, < 2.6.1 2.6.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