MovingAverage.setSampleMemory() may broke MovingAverage, making the value of exchangeRate in StabilizerNode.stabilize() being extremely wrong
The setSampleMemory function in MovingAverage.sol incorrectly updates the sample memory, causing the samples index to be misaligned. This leads to getValueWithLookback returning incorrect values, potentially zero or causing a revert, which can severely affect the exchange rate in StabilizerNode.stabilize().
Submitted by WatchPug
function setSampleMemory(uint256 _sampleMemory)
external
onlyRole(ADMIN_ROLE, "Must have admin privs")
{
require(_sampleMemory > 0, "Cannot have sample memroy of 0");
if (_sampleMemory > sampleMemory) {
for (uint i = sampleMemory; i < _sampleMemory; i++) {
samples.push();
}
counter = counter % _sampleMemory;
} else {
activeSamples = _sampleMemory;
// TODO handle when list is smaller Tue 21 Sep 2021 22:29:41 BST
}
sampleMemory = _sampleMemory;
}
In the current implementation, when sampleMemory is updated, the samples index will be malposition, making getValueWithLookback() get the wrong samples, so that returns the wrong value.
Proof of Concept
- When initial sampleMemory is
10 - After
movingAverage.update(1e18)being called for 120 times - The admin calls
movingAverage.setSampleMemory(118)and set sampleMemory to118
The current movingAverage.getValueWithLookback(sampleLength * 10) returns 0.00000203312 e18, while it’s expeceted to be 1e18
After setSampleMemory(), getValueWithLookback() may also return 0or revert FullMath: FULLDIV_OVERFLOW at L134.
Recommendation
Consider removing setSampleMemory function.
Alex the Entreprenerd (judge) commented:
I agree that calling
setSampleMemorywill cause issues, and can cause opportunities to further extract value. However this can be triggered by an admin action. I’ll think about the severity, but as of now, because it is contingent on admin privilege, will downgrade to Medium
Alex the Entreprenerd (judge) commented:
I stand by my decision of Medium Severity. While the consequences can be troublesome, they are contingent on the admin breaking / griefing the system
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 setSampleMemory function fails to properly adjust the counter and activeSamples when increasing sampleMemory, causing misalignment in the samples array.
Impact
The incorrect moving average could lead to wrong exchange rate calculations in StabilizerNode.stabilize(), potentially allowing value extraction, but this requires an admin action.
What the attacker needed
Admin privilege
The fix
The function setSampleMemory should be removed to prevent the issue.
How it was found
- Set initial sampleMemory to 10.
- Call movingAverage.update(1e18) 120 times.
- Admin calls movingAverage.setSampleMemory(118).
- Call movingAverage.getValueWithLookback(sampleLength * 10) and observe incorrect value.
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 calculation that smooths out data over time by averaging the last few values. The contract lets an admin change how many past values are kept, but the code that updates the list of stored values has a bug: it doesn't correctly adjust the internal counter, so the average is computed from the wrong set of numbers. This can make the average wildly incorrect, which then affects other parts of the system that rely on it.
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.