Moving average precision is lost
The moving average calculation in a smart contract loses precision because the difference between prices is divided by the number of observations before being added to the average. An attacker could exploit this to cause the moving average to deviate from the true value, potentially leaking value over time.
Submitted by hyh, also found by CertoraInc, d3e4, and rbserver
Now the precision is lost in moving average calculations as the difference is calculated separately and added each time, while it typically can be small enough to lose precision in the division involved.
For example, 10000 moves of 990 size, numObservations = 1000. This will yield 0 on each update, while must yield 9900 increase in the moving average.
Proof of Concept
Moving average is calculated with the addition of the difference:
// Calculate new moving average
if (currentPrice > earliestPrice) {
_movingAverage += (currentPrice - earliestPrice) / numObs;
} else {
_movingAverage -= (earliestPrice - currentPrice) / numObs;
}
/ numObs can lose precision as currentPrice - earliestPrice is usually small.
It is returned on request as is:
/// @notice Get the moving average of OHM in the Reserve asset over the defined window (see movingAverageDuration and observationFrequency).
function getMovingAverage() external view returns (uint256) {
if (!initialized) revert Price_NotInitialized();
return _movingAverage;
}
Recommended Mitigation Steps
Consider storing the cumulative sum, while returning sum / numObs on request:
/// @notice Get the moving average of OHM in the Reserve asset over the defined window (see movingAverageDuration and observationFrequency).
function getMovingAverage() external view returns (uint256) {
if (!initialized) revert Price_NotInitialized();
- return _movingAverage;
+ return _movingAverage / numObservations;
}
Oighty (Olympus) disagreed with severity and commented:
Keeping track of the observations as a sum and then dividing is a good suggestion. The price values have 18 decimals and the max discrepancy introduced is very small (10**-15) with expected parameter ranges. The potential risk to the protocol seems low though.
Please notice that discrepancy here is unbounded, i.e. the logic itself does not have any max discrepancy, the divergence between fact and recorded value can pile up over time without a limit.
If you do imply that markets should behave in some way that minuses be matched with pluses, then I must say that they really shouldn’t.
Debating between QA and Medium on this one. I am going to award it as medium because there is a potential to leak some value due to this imprecision compounding over time.
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 moving average is updated by adding a divided difference instead of accumulating the sum and dividing only when queried, causing precision loss.
Impact
The moving average can deviate from the true value without bound, potentially leaking value over time as the imprecision compounds.
What the attacker needed
None
The fix
The moving average should be calculated by storing the cumulative sum and dividing by the number of observations only when the average is requested.
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 moving average is a way to smooth out price changes by averaging many observations. The code updates the average by adding a small change each time, but it divides that change by the number of observations first, which can round down to zero. Over many updates, the average can drift away from the correct value, which might be exploited to gain an unfair advantage.