Memory corruption in getBytes32FromBytes() can likely lead to loss of funds
The getBytes32FromBytes function in the LibBytes library has an incorrect out-of-bounds check: when the requested index equals the data length, it reads memory beyond the bytes array instead of returning zero. An attacker could cause callers to receive arbitrary incorrect data, potentially leading to incorrect pricing or loss of funds if used in reserve calculations.
Submitted by Trust
The LibBytes library is used to read and store uint128 types compactly for Well functions. The function getBytes32FromBytes() will fetch a specific index as bytes32.
/**
* @dev Read the `i`th 32-byte chunk from `data`.
*/
function getBytes32FromBytes(bytes memory data, uint256 i) internal pure returns (bytes32 _bytes) {
uint256 index = i * 32;
if (index > data.length) {
_bytes = ZERO_BYTES;
} else {
assembly {
_bytes := mload(add(add(data, index), 32))
}
}
}
If the index is out of bounds in the data structure, it returns ZERO_BYTES = bytes32(0). The issue is that the OOB check is incorrect. If index=data.length, the request is also OOB. For example: data.length=0 -> array is empty, data[0] is undefined. data.length=32 -> array has one bytes32, data[32] is undefined.
In other words, fetching the last element in the array will return whatever is stored in memory after the bytes structure.
Impact
Users of getBytes32FromBytes will receive arbitrary incorrect data. If used to fetch reserves like readUint128, this could easily cause severe damage, like incorrect pricing, or wrong logic that leads to loss of user funds.
PoC
The damage is easily demonstrated using the example below:
pragma solidity 0.8.17;
contract Demo {
event Here();
bytes32 private constant ZERO_BYTES = bytes32(0);
function corruption_POC(bytes memory data1, bytes memory data2) external returns (bytes32 _bytes) {
_bytes = getBytes32FromBytes(data1, 1);
}
function getBytes32FromBytes(bytes memory data, uint256 i) internal returns (bytes32 _bytes) {
uint256 index = i * 32;
if (index > data.length) {
_bytes = ZERO_BYTES;
} else {
emit Here();
assembly {
_bytes := mload(add(add(data, index), 32))
}
}
}
}
Calling corruption_POC with the following parameters:
{
"bytes data1": "0x2222222222222222222222222222222222222222222222222222222222222222",
"bytes data2": "0x3333333333333333333333333333333333333333333333333333333333333333"
}
The output bytes32 is:
{
"0": "bytes32: _bytes 0x0000000000000000000000000000000000000000000000000000000000000020"
}
The 0x20 value is in fact the size of the data2 bytes that resides in memory from the call to getBytes32FromBytes.
Recommended Mitigation Steps
Change the logic to:
if (index >= data.length) {
_bytes = ZERO_BYTES;
} else {
assembly {
_bytes := mload(add(add(data, index), 32))
}
}
publiuss (Basin) confirmed, but disagreed with severity and commented:
The report is valid, but the function is not used in the code base and will be removed. Because of this, it was never tested and/or intended for use. Recommend medium.
alcueca (Judge) decreased severity to Medium and commented:
The finding doesn’t impact code in scope in a way that would lead to loss of funds. Instead, it would affect only future code.
The function has been removed from the codebase.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The out-of-bounds check uses 'index > data.length' instead of 'index >= data.length', so an index equal to the length is treated as in-bounds and reads memory beyond the array.
Impact
Users of getBytes32FromBytes will receive arbitrary incorrect data, which could cause severe damage like incorrect pricing or wrong logic leading to loss of user funds, but the function is not used in the current codebase and will be removed.
What the attacker needed
Function must be used in production code
The fix
The function has been removed from the codebase.
How it was found
- Call corruption_POC with two bytes parameters, data1 and data2.
- The function calls getBytes32FromBytes(data1, 1).
- Because index equals data1.length, the check passes and the assembly reads memory after data1.
- The returned bytes32 is the size of data2 (0x20), showing memory corruption.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This is about a helper function that reads a 32-byte chunk from a byte array. The check that should stop it from reading past the end of the array is off by one, so when asked for the element just past the end, it reads whatever happens to be next in memory instead of returning zero. That wrong value could then be used in calculations, like for reserves, and cause problems.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.