Skip to content
Crypto Audit finding Intermediate

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));
    }

https://github.com/kkrt-labs/kakarot/blob/7411a5520e8a00be6f5243a50c160e66ad285563/solidity_contracts/src/CairoPrecompiles/DualVmToken.sol#L82

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

LSDan (judge) commented:

Medium is appropriate here. Receiving an incorrect value for TotalSupply can 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 totalSupply can be used to trigger actions and eventually drain funds.

Kakarot mitigated:

This PR fixes an issue where totalSupply was 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.


The original report as published on Code4rena, by oakcobalt. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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.

More by oakcobalt

View all

50 more disclosed reports, best first.

More from Kakarot

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.