Skip to content
Business logic Audit finding Intermediate

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().

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/code-423n4/2021-11-malt/blob/c3a204a2c0f7c653c6c2dda9f4563fd1dc1cecf3/src/contracts/MovingAverage.sol#L424-L442

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 to 118

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.

0xScotch (sponsor) confirmed

Alex the Entreprenerd (judge) commented:

I agree that calling setSampleMemory will 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

The original report as published on Code4rena, by WatchPug. 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 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

  1. Set initial sampleMemory to 10.
  2. Call movingAverage.update(1e18) 120 times.
  3. Admin calls movingAverage.setSampleMemory(118).
  4. 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.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Malt Protocol - Versus contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.