Fee not decayed if past decayTime
The fee decay function incorrectly returns the full last fee instead of zero when the decay period has fully elapsed. An attacker could pay a higher fee than intended, but the report does not claim any direct exploit.
Submitted by cmichel
The ThreePieceWiseLinearPriceCurve.calculateDecayedFee function is supposed to decay the lastFeePercent over time. This is correctly done in the decay > 0 && decay < decayTime case, but for the decay > decayTime case it does not decay at all but should set it to 0 instead..
if (decay > 0 && decay < decayTime) {
// @audit if decay is close to decayTime, this fee will be zero. but below it'll be 1. the more time passes, the higher the decay. but then decay > decayTime should return 0.
fee = lastFeePercent.sub(lastFeePercent.mul(decay).div(decayTime));
} else {
fee = lastFeePercent;
}
Recommended Mitigation Steps
It seems wrong to handle the decay == 0 case (decay happened in same block) the same way as the decay >= decayTime case (decay happened long time ago) as is done in the else branch. I believe it should be like this instead:
// decay == 0 case should be full lastFeePercent
if(decay < decayTime) {
fee = lastFeePercent.sub(lastFeePercent.mul(decay).div(decayTime));
} else {
// reset to zero if decay >= decayTime
fee = 0;
}
kingyetifinance (Yeti finance) confirmed:
@LilYeti: Good find. The fee would be reset to not 0 in this case.
0xtruco (Yeti finance) commented:
Resolved in https://github.com/code-423n4/2021-12-yetifinance/pull/14.
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 else branch handles both the case where no time has passed and the case where the full decay time has passed, so it never resets the fee to zero.
Impact
The fee is not reset to zero after the decay time, so users could be charged the full last fee instead of zero.
What the attacker needed
None
The fix
The code was fixed by changing the condition to check if decay is less than decayTime and setting fee to zero otherwise.
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 smart contract calculates a fee that should decrease over time. When enough time has passed, the fee should be zero, but a mistake in the code makes it stay at the old high value. This means users might pay more than they should.
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.