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.
Originally disclosed on Code4rena
Submitted by minhquanym
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
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:
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.
price = FixedPoint
.uq112x112(
uint224(
// @audit might overflow with solidity 0.8.0
(priceCumulative - maltPriceCumulativeLast) /
(blockTimestampLast - maltPriceTimestampLast)
)
)
Scenario:
maltPriceCumulativeLast = max(uint256 - 10)andprice = 10, timeElapsed = 10. So the newpriceCumulative = max(uint256 - 10) + 10 * 10 = 99 (overflow)- When doing calculation in Malt protocol,
priceCumulative < maltPriceCumulativeLast, sopriceCumulative - maltPriceCumulativeLastwill revert and fail
Recommended Mitigation Steps
Consider using unchecked block to match handle overflow calculation in Uniswap V2.
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
- The stabilize() function calls MaltDataLab.trackPool() to fetch pool data.
- trackPool() subtracts the stored previous cumulative price from the current Uniswap cumulative price.
- When the Uniswap cumulative price has overflowed and is now smaller than the stored value, the subtraction reverts under Solidity 0.8.
- 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.