Skip to content
Smart contracts Audit finding Intermediate

Function stabilize() might always revert because of overflow since Malt contract use solidity 0.8

The Malt protocol reads a cumulative price from a Uniswap V2 pool that intentionally overflows, but its own Solidity 0.8 code reverts on overflow when subtracting the previous value. This makes the stabilize() function always revert once the pool's cumulative price wraps around.

minhquanym Follow

Originally disclosed on Code4rena

Submitted by minhquanym

https://github.com/code-423n4/2023-02-malt/blob/700f9b468f9cf8c9c5cffaa1eba1b8dea40503f9/contracts/StabilityPod/StabilizerNode.sol#L161

https://github.com/code-423n4/2023-02-malt/blob/700f9b468f9cf8c9c5cffaa1eba1b8dea40503f9/contracts/DataFeed/MaltDataLab.sol#L326

Impact

MaltDataLab fetched priceCumulative directly from Uniswap V2 pool to calculate price of Malt token. However, it is noticed that Uniswap V2 pool use Solidity 0.5.16, which does not revert when overflow happen. In addition, it is actually commented in Uniswap code that

  • never overflows, and + overflow is desired

https://github.com/Uniswap/v2-core/blob/ee547b17853e71ed4e0101ccfd52e70d5acded58/contracts/UniswapV2Pair.sol#L77-L81

if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
    // * never overflows, and + overflow is desired
    price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
    price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
}

However, MaltDataLab contracts use Solidity 0.8 and will revert when overflow. It will break the stabilize() function and always revert since stabilize() call to MaltDataLab contract to get state.

Please note that, with Solidity 0.5.16, when result of addition bigger than max(uint256), it will overflow without any errors. For example, max(uint256) + 2 = 1.

So when price0CumulativeLast is overflow, the new value of price0CumulativeLast will be smaller than old value. As the result, when MaltDataLab doing a subtraction to calculate current price, it might get revert.

Proof of Concept

Function stabilize() will call to MaltDataLab.trackPool() first:

https://github.com/code-423n4/2023-02-malt/blob/700f9b468f9cf8c9c5cffaa1eba1b8dea40503f9/contracts/StabilityPod/StabilizerNode.sol#L163

function stabilize() external nonReentrant onlyEOA onlyActive whenNotPaused {
    // Ensure data consistency
    maltDataLab.trackPool();
    ...
}

Function trackPool() used a formula that will revert when priceCumulative overflow in Uniswap pool.

https://github.com/code-423n4/2023-02-malt/blob/700f9b468f9cf8c9c5cffaa1eba1b8dea40503f9/contracts/DataFeed/MaltDataLab.sol#L323-L329

price = FixedPoint
    .uq112x112(
      uint224(
        // @audit might overflow with solidity 0.8.0
        (priceCumulative - maltPriceCumulativeLast) /
          (blockTimestampLast - maltPriceTimestampLast)
      )
    )

Scenario:

  1. maltPriceCumulativeLast = max(uint256 - 10) and price = 10, timeElapsed = 10. So the new priceCumulative = max(uint256 - 10) + 10 * 10 = 99 (overflow)
  2. When doing calculation in Malt protocol, priceCumulative < maltPriceCumulativeLast, so priceCumulative - maltPriceCumulativeLast will revert and fail

Recommended Mitigation Steps

Consider using unchecked block to match handle overflow calculation in Uniswap V2.

0xScotch (Malt) confirmed


The original report as published on Code4rena, by minhquanym. 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

Likely the MaltDataLab contract performs unchecked subtraction of a Uniswap V2 cumulative price that is designed to overflow, without using an unchecked block to match Uniswap's behaviour.

Impact

Once the Uniswap cumulative price overflows, the stabilize() function will always revert, preventing the protocol from stabilising the Malt token price.

What the attacker needed

Uniswap V2 pool cumulative price overflows; Malt protocol uses Solidity 0.8

The fix

The code should use an unchecked block for the overflow-prone subtraction to match Uniswap V2's intentional overflow behaviour.

How it was found

  1. The stabilize() function calls MaltDataLab.trackPool() to fetch pool data.
  2. trackPool() subtracts the stored previous cumulative price from the current Uniswap cumulative price.
  3. When the Uniswap cumulative price has overflowed and is now smaller than the stored value, the subtraction reverts under Solidity 0.8.
  4. The revert causes stabilize() to fail.

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.

A Uniswap pool keeps a running total of prices that is allowed to wrap around to zero when it gets too big, like an odometer rolling over. The Malt contract reads that total and subtracts the last value it saw, but because it uses a newer version of Solidity that treats wrap-around as an error, the subtraction fails and the stabilize function stops working.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by minhquanym

View all

28 more disclosed reports, best first.

More from Malt Protocol - Versus contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.