_getFirstSample returns wrong sample if count < sampleMemory
The `_getFirstSample` helper in a moving-average contract computed the first sample index as if the ring buffer had already wrapped, so before the buffer filled it pointed at a zero-initialised slot instead of index zero. This produced an incorrect time-weighted average price, which the sponsor said could leak value but did not put funds directly at risk.
Submitted by cmichel
The MovingAverage.sol contract defines several variables that in the end make the samples array act as a ring buffer:
sampleMemory: The total length (buffer size) of thesamplesarray.samplesis initialized withsampleMemoryzero observations.counter: The pending sample index (modulosampleMemory)
The _getFirstSample function computes the first sample as (counter + 1) % sampleMemory which returns the correct index only if the ring buffer is full, i.e., it wraps around. (in the counter + 1 >= sampleMemory).
If the samples array does not wrap around yet, the zero index should be returned instead.
Impact
Returning counter + 1 if counter + 1 < sampleMemory returns a zero initialized samples observation index. This then leads to a wrong computation of the TWAP.
Recommended Mitigation Steps
Add an additional check for if (counter + 1 < sampleMemory) return 0 in _getFirstSample.
0xScotch (sponsor) confirmed and disagreed with severity:
Funds aren’t directly at risk. I believe this is medium severity
Alex the Entreprenerd (judge) commented:
Personally am not sure the first sample after wrapping would be
counter + 1(why notcounter % sampleMemory)That said, I do agree that before wrapping, the first item in the array is at the 0 index
I agree that the protocol is not behaving properly so I can understand the high severity, that said I also agree with the Sponsor’s statement, the worst case would be a leak of value, so Medium severity seems the most appropriate
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 index calculation for the oldest sample did not handle the not-yet-wrapped case, where the first sample is simply at index zero.
Impact
The wrong average price could cause a leak of value, though the sponsor stated funds were not directly at risk.
What the attacker needed
Buffer not yet wrapped
The fix
The function should return zero when the buffer has not yet wrapped, by adding a check before the modulo calculation.
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.
The contract keeps recent price observations in a fixed-size list that is reused in a loop, like seats around a circular table. A helper that finds the oldest observation assumed the table was already full, so early on it read an empty seat instead of the first real one. That made the calculated average price wrong.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.