An infinite loop in MultiFeeDistribution.sol withdraw
The withdraw function in MultiFeeDistribution.sol enters an infinite loop when a user tries to withdraw more than their unlocked balance, because a zero-amount entry in the earnings array causes the loop counter never to advance. An attacker could trigger this to block the withdraw process for affected users.
novamanbg Follow with novamanbg and 0x40saoirse
Originally disclosed on Code4rena
Submitted by novamanbg, also found by novamanbg and 0x40saoirse
An infinite loop will block the withdraw process.
Proof of Concept
In the MultiFeeDistribution.sol the withdraw function will start going through user’s locked amounts if they do not have enough unlocked to cover their withdraw request:
if (amount <= bal.unlocked) {
bal.unlocked = bal.unlocked - amount;
} else {
uint256 remaining = amount - bal.unlocked;
if (bal.earned < remaining) revert InvalidEarned();
bal.unlocked = 0;
uint256 sumEarned = bal.earned;
uint256 i;
for (i = 0; ; ) {
uint256 earnedAmount = _userEarnings[_address][i].amount;
if (earnedAmount == 0) continue;
However, as you can see it will stay at 0 and the following check will execute:
if (earnedAmount == 0) continue;
This continues and will start a new iteration of the loop; however, it will still be 0 and this loop will never end. As a result, claiming from locked amounts with penalty will not be possible.
Recommended Mitigation Steps
Rewrite the code the following way:
if (earnedAmount == 0) {
i++;
continue;
}
Assessed type
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
Root cause
The loop uses a continue statement without incrementing the index variable when it encounters a zero-amount entry, so the same zero entry is read forever.
Impact
Withdrawing from locked amounts with a penalty becomes impossible for affected users, effectively blocking that part of the withdraw process.
What the attacker needed
User has locked tokens with a penalty; withdraw amount exceeds unlocked balance
The fix
The code should be changed so that the loop index is incremented before continuing when a zero-amount entry is found.
How it was found
- Call withdraw with an amount greater than the user's unlocked balance.
- The code enters the else branch and starts looping over the user's earnings array.
- When it reaches an entry with amount zero, the continue statement skips the increment.
- The loop repeats on the same zero entry indefinitely.
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 has a function that lets users take out their locked tokens by paying a penalty. The code uses a loop to go through a list of past earnings, but if it finds an empty entry it skips to the next round without moving forward, so it gets stuck forever. This means the withdrawal never completes and the function runs out of 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.