_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.
Originally disclosed on Code4rena
Submitted by peritoflores, also found by WatchPug
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.
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.