EMA Data Point From Unlock Is Discarded
The exponential moving average (EMA) calculation in the yield distribution contract ignores the initial data points set during unlock, because the distribution counter starts at 0 and is incremented to 1 before the EMA formula is called, causing the formula to return only the latest value. This leads to incorrect EMA values and thus incorrect yield distribution.
JigglypuffAndPikachu Follow with Tendency, lemonmon and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/201
Found by
JigglypuffAndPikachu, Tendency, lemonmon, pseudoArtist
Summary
The ema calculation fails to incorporate the emaJTT and emaSTT amounts which are initally set in unlock.
Vulnerability Details
Let's carefully consider the EMA calculation. Firstly during the unlock, the emaSTT and emaJTT are set to the current values. This acts as our first data point. Since this is the only data point, this doesn't have any averaging which is why MATH.ema() is not called.
Now, after unlocking consider when distributeYield is called for the first time after unlocking. emaSTT and emaJTT should incorporate the first data point (which was recorded during unlock) with the new totalSupply of STT and JTT.
Now let's show why the contract will actually ignore the emaSTT/JTT set during unlock during the first yield distribution:
The distributionCounter is 0 when distributeYield is called, but due to the line distributionCounter += 1;, 1 is passed as the 3rd parameter to the ema formula in emaSTT = MATH.ema(emaSTT, aSTT, retrospectiveDistributions.min(distributionCounter));
When N == 1 in the ema formula, the function will return only eV, which is only the latest data point. It completely ignores bV which is the data point during the unlock:
function ema(uint256 bV, uint256 cV, uint256 N) external pure returns (uint256 eV) {
assert(N != 0);
uint256 M = (WAD * 2).floorDiv(N + 1); //When N = 1, M = WAD
eV = ((M * cV) + (WAD - M) * bV).floorDiv(WAD); //Substituting M = WAD, eV = cV
}
Impact
Incorrect EMA calculation which leads to incorrect yield distribution
Code Snippet
Tool used
Manual Review
Recommendation
The distributionCounter should actually be 2 rather than 1 during the first pass into the MATH.ema() formula. However then the varibale would not really correspond to the number of times distribution was called, so it might need to be renamed.
Discussion
pseudonaut
Valid, distributionCounter must be set to 1 initially
sherlock-admin3
1 comment(s) were left on this issue during the judging contest.
panprog commented:
borderline low/medium, while true, the impact is questionable as there can be situations when current method is better than fixed one, so this is basically a choice of N=1 or N=2 - both are valid, just whatever developer prefers to better fit the protocol needs.
panprog
Keeping this medium as sponsor confirmed it's valid (thus considering that N=2 is the intended value, but it's N=1 right now). The impact then is unintended values used in further calculations.
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Zivoe/zivoe-core-foundry/pull/252
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 distribution counter is incremented before being passed to the EMA function, so the first call uses N=1 instead of N=2, causing the EMA to ignore the initial data point.
Impact
Incorrect EMA calculation which leads to incorrect yield distribution.
What the attacker needed
None
The fix
The distributionCounter should be set to 1 initially so that the first call to MATH.ema uses N=2, incorporating the initial data point; the protocol team fixed this issue.
How it was found
- During unlock, emaSTT and emaJTT are set to current values as the first data point.
- After unlocking, distributeYield is called for the first time.
- distributionCounter is 0, then incremented to 1 before being passed to MATH.ema.
- MATH.ema with N=1 returns only the latest value, ignoring the initial data point.
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.
An exponential moving average (EMA) is a way to smooth out data over time by giving more weight to recent values. In this contract, the EMA is used to decide how yield is distributed. The bug is that the first time the EMA is calculated after unlocking, it ignores the starting value and only uses the new value, so the average is wrong from the start.
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.