LiquidityFarming.sol Unbounded for loops can potentially freeze users’ funds in edge cases
The withdraw function calls a loop over an unbounded reward rate log, which can grow large if rewards are updated frequently and users are inactive. An attacker could cause a withdrawal transaction to revert due to out-of-gas, potentially freezing user funds.
Submitted by WatchPug
In the current implementation of withdraw(), it calls _sendRewardsForNft() at L243 which calls updatePool() at L129 which calls getUpdatedAccTokenPerShare() at L319.
getUpdatedAccTokenPerShare() will loop over rewardRateLog to calculate an up to date value of accTokenPerShare.
LiquidityFarming.sol#L270-L285
while (true) {
if (lastUpdatedTime >= counter) {
break;
}
unchecked {
accumulator +=
rewardRateLog[_baseToken][i].rewardsPerSecond *
(counter - max(lastUpdatedTime, rewardRateLog[_baseToken][i].timestamp));
}
counter = rewardRateLog[_baseToken][i].timestamp;
if (i == 0) {
break;
}
--i;
}
This won’t be a problem in the usual cases, however, if there is a baseToken that:
- the
rewardPerSecondget updated quite frequently; - the liquidityProviders are inactive (no deposits / withdrawals for a period of time)
Then by the time one of the liquidityProviders come to withdraw(), the tx may revert due to out-of-gas.
As the rewardRateLog is now accumulated to a large size that causes the loop costs more gas than the block gas limit.
There is a really easy fix for this, it will also make the code simpler:
Recommended Mitigation Steps
Consider removing rewardRateLog and change setRewardPerSecond() to:
function setRewardPerSecond(address _baseToken, uint256 _rewardPerSecond) public onlyOwner {
updatePool(baseToken);
rewardRate[_baseToken] = RewardsPerSecondEntry(_rewardPerSecond, block.timestamp);
emit LogRewardPerSecond(_baseToken, _rewardPerSecond);
}
ankurdubey521 (Biconomy) acknowledged
pauliax (judge) decreased severity to Medium and commented:
A valid concern, but I think it should be of Medium severity: “Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.”
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 unbounded growth of the rewardRateLog array combined with a loop over it in getUpdatedAccTokenPerShare, which can exceed the block gas limit.
Impact
In edge cases, withdrawals may revert due to out-of-gas, potentially freezing user funds; the judge rated this as Medium severity because assets are not at direct risk but protocol availability could be impacted.
What the attacker needed
Frequent reward rate updates; inactive liquidity providers; large rewardRateLog size
The fix
The recommendation is to remove rewardRateLog and update setRewardPerSecond to call updatePool and store only the latest reward rate, simplifying the code and avoiding the loop.
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.
Smart contracts have a limit on how much computation a single transaction can do, called the gas limit. If a function loops too many times, it runs out of gas and fails. Here, a list that grows over time is looped through during withdrawal, so if the list gets very long, withdrawals can fail and users cannot get their funds out.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.