Skip to content
DoS Audit finding Intermediate

_updateTwav() and _getTwav() will revert when cumulativePrice overflows

The contract stores cumulative price observations by adding a new value to the previous cumulative total, and reads them back by subtracting two totals. Because these additions and subtractions are checked for overflow, the contract will revert once the cumulative value exceeds the integer limit, breaking the time-weighted average price functions.

peritoflores Follow

Originally disclosed on Code4rena

Submitted by peritoflores, also found by WatchPug

https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/Twav/Twav.sol#L28

https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/Twav/Twav.sol#L40

Impact

Contract will break when cumulativeValuation overflows.

Proof of Concept

Cumulative prices are designed to work with overflows/underflows because in the end the difference is important.

In _updateTwav() when _prevCumulativeValuation + (_valuation *_timeElapsed) overflows the contract will not work anymore.

twavObservations[twavObservationsIndex] = TwavObservation(_blockTimestamp, _prevCumulativeValuation + (_valuation * _timeElapsed)); //add the previous observation to make it cumulative @audit overflow breaks the contract

Same problem in _getTwav()

  _twav = (_twavObservationCurrent.cumulativeValuation - _twavObservationPrev.cumulativeValuation) / (_twavObservationCurrent.timestamp - _twavObservationPrev.timestamp);@audit same overflow breaks the contract

}

Similar issues

https://github.com/code-423n4/2022-04-phuture-findings/issues/62

Recommended Mitigation

Add unchecked keyword in every line you add / subtract cumulative prices.

mundhrakeshav (Nibbl) acknowledged

HardlyDifficult (judge) decreased severity to Medium and commented:

Without a better POC of the issue occurring it’s hard to justify this is High risk. e.g. maybe it could be forced by spamming updateTWAV, but it’s not clear if that would require extremely large values or an unrealistic number of transactions.

Related to https://github.com/code-423n4/2022-06-nibbl-findings/issues/178, that one includes unchecking the price in the recommendation but the rest of the description focuses on timestamp overflows while this one looks at price overflows.


The original report as published on Code4rena, by peritoflores. 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 cumulative price arithmetic uses checked addition and subtraction, so an overflow or underflow reverts instead of wrapping as cumulative price maths intends.

Impact

Once the cumulative valuation overflows, the update and read functions revert, so the contract stops working; the judge noted no proof that this is realistically reachable and lowered it to Medium.

What the attacker needed

Cumulative valuation grows past the integer limit

The fix

The cumulative price additions and subtractions should be wrapped in unchecked blocks so they can overflow and underflow as intended.

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 cumulative price is a running total that keeps growing over time; to get an average you subtract two totals. In this smart contract language, adding past the maximum number causes an error and stops the function. The author says the running total should be allowed to wrap around, because only the difference between two totals matters.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by peritoflores

View all

4 more disclosed reports, best first.

More from Nibbl contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.