`FluidLocker::_getUnlockingPercentage()` incorrectly divides one of the components of the formula by `S`, leading to always having `80%` penalty
The unlocking percentage formula in FluidLocker incorrectly divides a square root term by the scaling factor, causing the term to always evaluate to zero. As a result, users always receive only a 20% unlocking percentage, incurring an 80% penalty regardless of the unlock period.
056Security Follow with 0x73696d616f, Drynooo and 7 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-11-superfluid-locking-contract-judging/issues/20
Found by
056Security, 0x73696d616f, Drynooo, IvanFitro, Kyosi, Z3R0, ke1caM, newspacexyz, redbeans, zhenyazhd
Summary
FluidLocker::_getUnlockingPercentage() calculates the percentage to unlock, which is the amount given to the user, while the remaining goes to other stakers. The formula incorrectly divides Math.sqrt(unlockPeriod * _SCALER) by SCALER:
function _getUnlockingPercentage(uint128 unlockPeriod) internal pure returns (uint256 unlockingPercentageBP) {
unlockingPercentageBP = (
_PERCENT_TO_BP
* (
((80 * _SCALER) / Math.sqrt(540 * _SCALER)) * (Math.sqrt(unlockPeriod * _SCALER) / _SCALER //@audit this _SCALER should not be here)
+ 20 * _SCALER
)
) / _SCALER;
}
Root Cause
In FluidLocker:388, it incorrectly divides the term (Math.sqrt(unlockPeriod * _SCALER) by _SCALER.
Internal pre-conditions
None.
External pre-conditions
None.
Attack Path
- User unlocks their Fluid from the locker with a duration bigger than 0, unvesting it through the fountain.
Impact
User suffers a big loss, even if they unlock with the maximum period, they will still get 80% penalty.
PoC
The component (Math.sqrt(unlockPeriod * _SCALER) / _SCALER) <= sqrt(540 * 24 * 3600 * 1e18) / 1e18 = 0 is always null, so only 20*_SCALER is left, which always yields a 20% unlocking percentage.
Mitigation
Remove the extra _SCALER.
function _getUnlockingPercentage(uint128 unlockPeriod) internal pure returns (uint256 unlockingPercentageBP) {
unlockingPercentageBP = (
_PERCENT_TO_BP
* (
((80 * _SCALER) / Math.sqrt(540 * _SCALER)) * (Math.sqrt(unlockPeriod * _SCALER))
+ 20 * _SCALER
)
) / _SCALER;
}
Discussion
0xPilou
Fix for this issue is included in the following PR : https://github.com/superfluid-finance/fluid/pull/6
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/superfluid-finance/fluid/pull/6
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 formula incorrectly divides the square root term by the scaling factor, causing it to always evaluate to zero.
Impact
User suffers a big loss, even if they unlock with the maximum period, they will still get `80%` penalty.
What the attacker needed
User has locked Fluid tokens; User unlocks with duration > 0
The fix
The extra `_SCALER` should be removed from the formula, and the protocol team fixed this issue in a pull request.
How it was found
- User unlocks their Fluid from the locker with a duration bigger than 0, unvesting it through the fountain.
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 how much of a locked token a user can unlock based on how long they lock it. A mistake in the maths makes one part of the formula always zero, so users always get only 20% back and lose 80% as a penalty, even if they lock for the maximum time.