# CVE-2021-21401: Invalid free() call in Nanopb

> Attackers can trigger invalid free or realloc calls in Nanopb by sending crafted Protocol Buffers messages with specific oneof fields, potentially leading to crashes or code execution.

- **Severity:** High 7.1 (`CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:L`)
- **Exploited in the wild:** Not known
- **Login needed:** Any signed-in user
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 23 Mar 2021
- **Updated:** 3 Aug 2024
- **Also known as:** DEBIAN-CVE-2021-21401, GHSA-7mv5-5mxh-qg88, PYSEC-2021-432, UBUNTU-CVE-2021-21401

## What is CVE-2021-21401?

CVE-2021-21401 is a high-severity memory corruption vulnerability in nanopb, published on 23 Mar 2021. Any signed-in user can exploit it over the network, with no action from the victim.

### Official description

Nanopb is a small code-size Protocol Buffers implementation in ansi C. In Nanopb before versions 0.3.9.8 and 0.4.5, decoding a specifically formed message can cause invalid `free()` or `realloc()` calls if the message type contains an `oneof` field, and the `oneof` directly contains both a pointer field and a non-pointer field. If the message data first contains the non-pointer field and then the pointer field, the data of the non-pointer field is incorrectly treated as if it was a pointer value. Such message data rarely occurs in normal messages, but it is a concern when untrusted data is parsed. This has been fixed in versions 0.3.9.8 and 0.4.5. See referenced GitHub Security Advisory for more information including workarounds.

Weakness: CWE-763 (Release of Invalid Pointer or Reference)

## Which versions are affected by CVE-2021-21401?

5 open source packages ship an affected version, including nanopb. It affects 1 product.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `nanopb` | PyPI | All versions | 0.3.9.8, 0.4.5 |
| `nanopb` | PyPI | From 0.3.2 | 0.3.9.8 |
| `nanopb` | PyPI | From 0.3.2 | 0.3.9.8 |
| `nanopb` | PyPI | From 0.4.0 | 0.4.5 |
| `nanopb` | PyPI | From 0.4.0 | 0.4.5 |

Products: Nanopb Project Nanopb.

## How do I fix CVE-2021-21401?

Upgrade nanopb to a fixed version for your release line: 0.3.9.8 or 0.4.5.

## Is CVE-2021-21401 being exploited?

There is no known exploitation of CVE-2021-21401 in the wild and no public exploit code.

Public exploit code:

- https://github.com/uthrasri/CVE-2021-21401_nanopb-c_AOSP10_R33 (github_poc): https://github.com/uthrasri/CVE-2021-21401_nanopb-c_AOSP10_R33

## Other vulnerabilities in nanopb

- [CVE-2014-125106](https://insights.sbomify.com/CVE-2014-125106/): Integer overflow in n/a (critical 9.8)
- [CVE-2020-26243](https://insights.sbomify.com/CVE-2020-26243/): Memory leak in nanopb (high 7.5)
- [CVE-2020-5235](https://insights.sbomify.com/CVE-2020-5235/): Out-of-memory condition in Nanopb is potentially exploitable (medium 6.5)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2021-21401 and fix it if it is affected.

What it is: CVE-2021-21401 is a high-severity memory corruption vulnerability in nanopb, published on 23 Mar 2021. Any signed-in user can exploit it over the network, with no action from the victim.
Exploitation: There is no known exploitation of CVE-2021-21401 in the wild and no public exploit code.

Affected packages and fixed versions:
- nanopb (PyPI): affected all versions; fixed in 0.3.9.8, 0.4.5
- nanopb (PyPI): affected from 0.3.2; fixed in 0.3.9.8
- nanopb (PyPI): affected from 0.3.2; fixed in 0.3.9.8
- nanopb (PyPI): affected from 0.4.0; fixed in 0.4.5
- nanopb (PyPI): affected from 0.4.0; fixed in 0.4.5

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2021-21401/ (Markdown: https://insights.sbomify.com/CVE-2021-21401/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2021-21401
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2021-21401
- OSV: https://osv.dev/vulnerability/CVE-2021-21401

---
From sbomify Insights: https://insights.sbomify.com/CVE-2021-21401/
Summaries are generated and may contain mistakes; the official description above is the record.
