# CVE-2021-29542: Heap buffer overflow in `StringNGrams`

- Severity: Low 2.5 (CVSS:3.1, scored by GitHub_M) `CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:N/I:N/A:L`
- Known to be exploited: not in CISA's catalogue, which lists only what CISA has confirmed
- Likelihood of exploitation in the next 30 days (EPSS): 0.20%, higher than 10% of CVEs (2026-12-01)
- Public exploit code or proof of concept: one link below, marked "exploit"
- Published: 2021-05-14T19:11:17.000Z
- Modified: 2026-09-08T08:48:10.898Z
- Also known as: GHSA-4hrh-9vmp-2jgg, BIT-TENSORFLOW-2021-29542, PYSEC-2021-179, PYSEC-2021-470, PYSEC-2021-668
- Weaknesses: CWE-131, CWE-787
- Assigned by: GitHub_M

## Description

TensorFlow is an end-to-end open source platform for machine learning. An attacker can cause a heap buffer overflow by passing crafted inputs to `tf.raw_ops.StringNGrams`. This is because the implementation(https://github.com/tensorflow/tensorflow/blob/1cdd4da14282210cc759e468d9781741ac7d01bf/tensorflow/core/kernels/string_ngrams_op.cc#L171-L185) fails to consider corner cases where input would be split in such a way that the generated tokens should only contain padding elements. If input is such that `num_tokens` is 0, then, for `data_start_index=0` (when left padding is present), the marked line would result in reading `data[-1]`. The fix will be included in TensorFlow 2.5.0. We will also cherrypick this commit on TensorFlow 2.4.2, TensorFlow 2.3.3, TensorFlow 2.2.3 and TensorFlow 2.1.4, as these are also affected and still in supported range.

(As written by CVE List.)

## Affected

- tensorflow tensorflow: < 2.1.4; >= 2.2.0, < 2.2.3; >= 2.3.0, < 2.3.3; >= 2.4.0, < 2.4.2
- tensorflow (PyPI): before 2.1.4; from 2.2.0 before 2.2.3; from 2.3.0 before 2.3.3; from 2.4.0 before 2.4.2; before ba424dd8f16f7110eea526a8086f1a155f14f22b
- tensorflow-cpu (PyPI): before 2.1.4; from 2.2.0 before 2.2.3; from 2.3.0 before 2.3.3; from 2.4.0 before 2.4.2; before ba424dd8f16f7110eea526a8086f1a155f14f22b
- tensorflow-gpu (PyPI): before 2.1.4; from 2.2.0 before 2.2.3; from 2.3.0 before 2.3.3; from 2.4.0 before 2.4.2; before ba424dd8f16f7110eea526a8086f1a155f14f22b
- google tensorflow: before 2.1.4; from 2.2.0 before 2.2.3; from 2.3.0 before 2.3.3; from 2.4.0 before 2.4.2
- tensorflow (Bitnami): before 2.1.4; from 2.2.0 before 2.2.3; from 2.3.0 before 2.3.3; from 2.4.0 before 2.4.2

## Every score

- GitHub_M: CVSS:3.1 2.5 Low `CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:N/I:N/A:L`
- GitHub: CVSS:3.1 2.5 Low `CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:N/I:N/A:L`
- GitHub: CVSS:4.0 Low `CVSS:4.0/AV:L/AC:L/AT:P/PR:L/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N`
- NVD: CVSS:3.1 5.5 Medium `CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:H`
- NVD: CVSS:2.0 2.1 Low `AV:L/AC:L/Au:N/C:N/I:N/A:P`
- Bitnami Vulnerability Database: rating Medium

## References

- <https://github.com/tensorflow/tensorflow/security/advisories/GHSA-4hrh-9vmp-2jgg> (exploit, advisory, patch, third-party-advisory, web)
- <https://github.com/tensorflow/tensorflow/commit/ba424dd8f16f7110eea526a8086f1a155f14f22b> (advisory, fix, patch, third-party-advisory, web)
- <https://nvd.nist.gov/vuln/detail/CVE-2021-29542> (advisory, web)
- <https://github.com/pypa/advisory-database/tree/main/vulns/tensorflow-cpu/PYSEC-2021-470.yaml> (web)
- <https://github.com/pypa/advisory-database/tree/main/vulns/tensorflow-gpu/PYSEC-2021-668.yaml> (web)
- <https://github.com/pypa/advisory-database/tree/main/vulns/tensorflow/PYSEC-2021-179.yaml> (web)
- <https://github.com/tensorflow/tensorflow> (package)

## Sources

- CVE List: [CVE-2021-29542](https://www.cve.org/CVERecord?id=CVE-2021-29542), CVE Terms of Use, modified 2024-08-03
- GitHub Advisory Database: [GHSA-4hrh-9vmp-2jgg](https://github.com/advisories/GHSA-4hrh-9vmp-2jgg), CC-BY-4.0, modified 2026-07-08
- NVD: [CVE-2021-29542](https://nvd.nist.gov/vuln/detail/CVE-2021-29542), Public domain, modified 2026-06-17
- PyPA Advisory Database: [PYSEC-2021-179](https://osv.dev/vulnerability/PYSEC-2021-179), CC-BY-4.0, modified 2023-12-06
- PyPA Advisory Database: [PYSEC-2021-470](https://osv.dev/vulnerability/PYSEC-2021-470), CC-BY-4.0, modified 2023-12-06
- PyPA Advisory Database: [PYSEC-2021-668](https://osv.dev/vulnerability/PYSEC-2021-668), CC-BY-4.0, modified 2023-12-06
- Bitnami Vulnerability Database: [BIT-TENSORFLOW-2021-29542](https://osv.dev/vulnerability/BIT-TENSORFLOW-2021-29542), Apache-2.0, modified 2026-09-08

---

Page: https://cve.day/CVE-2021-29542 · Whole record: https://cve.day/CVE-2021-29542.json · Rebuilt by cve.day 2026-11-25T22:58:01.372Z
