`_accumulateExternalRewards()` could turn into an infinite loop if the check condition is true
The `_accumulateExternalRewards()` function in WstethLiquidityVault.sol contains a for loop where the increment `++i` is placed after a `continue` statement. If the sanity check `newBalance < rewardToken.lastBalance` is true, the `continue` skips the increment, causing an infinite loop that consumes all gas and blocks dependent functions.
RaymondFam Follow with shark
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-olympus-judging/issues/125
Found by
RaymondFam, shark
Summary
In WstethLiquidityVault.sol, the for loop in _accumulateExternalRewards() utilizes continue so it could proceed to the next iteration upon having a true condition in the sanity check. This will however turn the function into an infinite loop because ++i has been included at the end of the loop logic. As a result, this skipped increment leads to the same externalRewardTokens[i] repeatedly assigned to rewardToken where newBalance < rewardToken.lastBalance continues to equal true until the same executions make the gas run out.
Vulnerability Detail
Here is a typical scenario:
_accumulateExternalRewards()gets invoked via one of the functions embedding it, i.e.claimRewards(),_depositUpdateRewardState()or_withdrawUpdateRewardState()of SingleSidedLiquidityVault.sol.- It happens that
newBalance < rewardToken.lastBalancereturns true for a specific reward token. - Because
continuecomes before++i, this non-incremented iteration is repeatedly executed till gas is run out.
Impact
This will persistently cause DOS on _accumulateExternalRewards() for all function calls dependent on it. Depending on how big the deficiency is, the situation can only be remedied by:
- having the deficiency of contract balance on this particular reward token separately topped up at the expense of accounting mess up and/or the protocol resorting to a portion of its reward token(s) locked in the contract whenever this incident happens,
- waiting for a long enough time till the harvested reward is going to be larger than the deficiency entailed, or
- getting the contract deactivated to temporarily prevent further deposits, withdrawals, or reward claims which will nonetheless break other things when
deactivate()is called.
Note: The situation could be worse if more than 1 elements in the array ExternalRewardToken[] were similarly affected.
Code Snippet
File: WstethLiquidityVault.sol#L192-L216
function _accumulateExternalRewards() internal override returns (uint256[] memory) {
uint256 numExternalRewards = externalRewardTokens.length;
auraPool.rewardsPool.getReward(address(this), true);
uint256[] memory rewards = new uint256[](numExternalRewards);
for (uint256 i; i < numExternalRewards; ) {
ExternalRewardToken storage rewardToken = externalRewardTokens[i];
uint256 newBalance = ERC20(rewardToken.token).balanceOf(address(this));
// This shouldn't happen but adding a sanity check in case
if (newBalance < rewardToken.lastBalance) {
emit LiquidityVault_ExternalAccumulationError(rewardToken.token);
continue;
}
rewards[i] = newBalance - rewardToken.lastBalance;
rewardToken.lastBalance = newBalance;
unchecked {
++i;
}
}
return rewards;
}
Tool used
Manual Review
Recommendation
Consider having the affected code logic refactored as follows:
function _accumulateExternalRewards() internal override returns (uint256[] memory) {
uint256 numExternalRewards = externalRewardTokens.length;
auraPool.rewardsPool.getReward(address(this), true);
uint256[] memory rewards = new uint256[](numExternalRewards);
+ unchecked {
- for (uint256 i; i < numExternalRewards; ) {
+ for (uint256 i; i < numExternalRewards; ++i;) {
ExternalRewardToken storage rewardToken = externalRewardTokens[i];
uint256 newBalance = ERC20(rewardToken.token).balanceOf(address(this));
// This shouldn't happen but adding a sanity check in case
if (newBalance < rewardToken.lastBalance) {
emit LiquidityVault_ExternalAccumulationError(rewardToken.token);
continue;
}
rewards[i] = newBalance - rewardToken.lastBalance;
rewardToken.lastBalance = newBalance;
- unchecked {
- ++i;
- }
}
+ }
return rewards;
}
This will safely increment i when continue is hit and move on to the next i + 1 iteration while still having SafeMath unchecked for the entire scope of the for loop.
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
Impact
This could persistently cause a denial of service on `_accumulateExternalRewards()` and all functions that depend on it, potentially requiring contract deactivation or other disruptive measures.
What the attacker needed
NewBalance < rewardToken.lastBalance is true
The fix
The loop should be refactored to increment `i` before the `continue` statement, for example by moving `++i` into the for loop header.
How it was found
- `_accumulateExternalRewards()` is called via `claimRewards()`, `_depositUpdateRewardState()`, or `_withdrawUpdateRewardState()`.
- The condition `newBalance < rewardToken.lastBalance` evaluates to true for a reward token.
- The `continue` statement is executed, skipping the increment of `i`.
- The loop repeats the same iteration until gas is exhausted.
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 for loop repeats code for each item in a list. Normally, a counter goes up each time. Here, if a check fails, the code jumps back to the start without increasing the counter, so it gets stuck on the same item forever, using up all gas.
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.