OpenZeppelin Contracts base64 encoding may read from potentially dirty memory

Description

Impact

The Base64.encode function encodes a bytes input by iterating over it in chunks of 3 bytes. When this input is not a multiple of 3, the last iteration may read parts of the memory that are beyond the input buffer.

Although the encode function pads the output for these cases, up to 4 bits of data are kept between the encoding and padding, corrupting the output if these bits were dirty (i.e. memory after the input is not 0). These conditions are more frequent in the following scenarios:

  • A bytes memory struct is allocated just after the input and the first bytes of it are non-zero.
  • The memory pointer is set to a non-empty memory location before allocating the input.

Developers should evaluate whether the extra bits can be maliciously manipulated by an attacker.

Patches

Upgrade to 5.0.2 or 4.9.6.

References

This issue was reported by the Independent Security Researcher Riley Holterhus through Immunefi (@rileyholterhus on X)

Basic information

Type
reviewed
Severity
medium
Advisory on GitHub
Open advisory ↗
Repository advisory
Open repository advisory ↗
Source code
Browse source ↗
Published (advisory)
2024-02-29 20:09:53 UTC
Updated
2026-03-14 05:05:41 UTC
GitHub reviewed
2024-02-29 20:09:53 UTC
NVD published
2024-03-20

EPSS Score

Score Percentile
0.56% 67.52%

CVSS Scores

Base score Version Severity Vector
6.5 3.1
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:H Click to expand
Attack vector (AV:N)
Could be attacked over the internet or any normal routed network—not just someone sitting at the machine.
Attack complexity (AC:H)
Even with access, the exploit needs extra luck, timing, or a fussy environment to actually work.
Privileges required (PR:N)
No account or special rights needed—anonymous or random user is enough.
User interaction (UI:N)
Nobody has to click “OK” or open a trap file; it can work without a victim helping.
Scope (S:U)
Damage stays in the same “trust bubble” as the broken component—no big spill into unrelated systems.
Confidentiality (C:N)
Doesn’t really leak secrets in a meaningful way.
Integrity (I:L)
Attackers could change some data, but it’s limited—not everything goes.
Availability (A:H)
Could take the service down hard or make it unusable for people who depend on it.

Identifiers

CWEs

CWE id Name
CWE-125 Out-of-bounds Read

Credits

  • rholterhus (finder)

Affected packages (4)

Vulnerable version ranges and first patched releases as published by GitHub.

Ecosystem Package Vulnerable range First patched Vulnerable functions
npm @openzeppelin/contracts >= 4.5.0, < 4.9.6 4.9.6
npm @openzeppelin/contracts-upgradeable >= 5.0.0-rc.0, < 5.0.2 5.0.2
npm @openzeppelin/contracts >= 5.0.0-rc.0, < 5.0.2 5.0.2
npm @openzeppelin/contracts-upgradeable >= 4.5.0, < 4.9.6 4.9.6

References

cvelogic Threat Intelligence