Incorrect totalsupply value will be returned due to erroneous return data decode implementation
The totalSupply function in DualVMToken incorrectly decodes the return data from a Starknet ERC20 contract as a single uint256 instead of a pair of uint128 values, causing it to return only the lower 128 bits. This can lead to incorrect total supply values, potentially causing fund loss or accounting errors in applications that rely on it.
oakcobalt Follow with RadiantLabs and gumgumzum
Originally disclosed on Code4rena
Submitted by oakcobalt, also found by RadiantLabs and gumgumzum
The returned value of totalSupply() in a starknet ERC20 contract is expected to fit in uint256, which is expressed in (uint128, uint128) with the first uint128 representing the lower 128bits.
The issue is current implementation of DualVMToken::totalSupply incorrectly decodes returnData as uint256 instead of (uint128, uint128). Because the first uint128 is the lower 128bits of a uint256 number. This means, totalSupply will return an incorrect value because it only reads the lower 128bits.
//kakarot/solidity_contracts/lib/kakarot-lib/src/CairoLib.sol
function totalSupply() external view returns (uint256) {
bytes memory returnData = starknetToken.staticcallCairo("total_supply");
|> return abi.decode(returnData, (uint256));
}
Impacts
totalSupply will return incorrect value. Due to totalSupply is critical in many defi or accounting based logic, this potentially leads to fund loss and errorneas accounting in any user application that uses DualVMToken.sol. Depending on the context of the application that calls DualVMToken.sol, the fund loss could be critical.
Recommended Mitigation Steps
In totalSupply(), change the decode following _balanceOf’s implementation:
...
(uint128 valueLow, uint128 valueHigh) = abi.decode(
returnData,
(uint128, uint128)
);
return uint256(valueLow) + (uint256(valueHigh) << 128);
Assessed type
Error
ClementWalter (Kakarot) confirmed and commented:
Severity: Low
Medium is appropriate here. Receiving an incorrect value for
TotalSupplycan cause untold amounts of hand wavy hypothetical misery.
ClementWalter (Kakarot) commented:
Ok for Medium.
2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
There might be paths where
totalSupplycan be used to trigger actions and eventually drain funds.
Kakarot mitigated:
This PR fixes an issue where
totalSupplywas skipping the high-part of the u256.
- Correctly deserialize return value.
- Update test to launch a token with populated (low, high) parts of the u256 struct.
Status: Mitigation confirmed.
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 code decodes the return data as a single 256-bit integer, but the Starknet contract returns two 128-bit integers (low and high parts), so only the low part is read.
Impact
totalSupply returns an incorrect value, which could lead to fund loss or erroneous accounting in applications that use DualVMToken, though the report notes this depends on the application's context.
What the attacker needed
Application uses DualVMToken.totalSupply
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.
Smart contracts on some networks store large numbers as two smaller pieces. The totalSupply function read only the first piece and ignored the second, so it reported a much smaller number than the real total. Applications that trust this number could make wrong decisions about how much money exists.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.