Skip to content
DoS Audit finding Intermediate

Dos with revert via unbounded loop

The function _removeAnySharesFor contains an unbounded loop over a user's stake entries, so if a user accumulates many small stakes, the loop can consume excessive gas and revert. This could prevent legitimate withdrawals or other operations that rely on this function, potentially freezing funds.

lllll Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

57703 sc medium dos with revert via unbounded loop

Submitted on Oct 28th 2025 at 10:07:09 UTC by @lllll for Audit Comp | Belong

{% hint style="danger" %} Vulnerability: Potential Denial of Service (DoS) — unbounded loop in _removeAnySharesFor may cause transactions to run out of gas and revert, preventing legitimate withdrawals or other operations that rely on this function. {% endhint %}

Description

Brief / Intro

The function _removeAnySharesFor contains an unbounded loop that iterates over the stakes[staker] array and removes shares until the requested shares amount is satisfied. If a user accumulates a very large number of stake entries (i.e., stakes[staker].length becomes very large), calling _removeAnySharesFor can consume an arbitrarily large amount of gas and eventually run out of gas and revert.

Vulnerability Details

The implementation iterates over userStakes and removes elements via swap-and-pop or decrements an element. The loop condition is unbounded:

JavaScript
for (uint256 i; i < userStakes.length && remaining > 0;) {
    ...
}

Each iteration can modify the array length, and in the worst case (many small stakes), the number of iterations grows with the number of entries. A malicious or inadvertent accumulation of many small stakes can therefore make the function revert due to gas exhaustion.

Impact Details

If this function is used during withdraw/exit flows, affected users may be unable to withdraw staked assets, potentially resulting in permanent freezing of funds for those users.

References

Vulnerable snippet:

JavaScript
function _removeAnySharesFor(address staker, uint256 shares) internal {
    Stake[] storage userStakes = stakes[staker];
    uint256 remaining = shares;

    for (uint256 i; i < userStakes.length && remaining > 0;) {
        uint256 stakeShares = userStakes[i].shares;
        if (stakeShares <= remaining) {
            remaining -= stakeShares;
            userStakes[i] = userStakes[userStakes.length - 1];
            userStakes.pop();
            // don't ++i: a new element is now at index i
        } else {
            userStakes[i].shares = stakeShares - remaining;
            remaining = 0;
            unchecked {
                ++i;
            }
        }
    }
}

Proof of Concept

JavaScript
function _addStake(address staker, uint256 shares) external {
    stakes[staker].push(Stake({shares: shares, timestamp: block.timestamp}));
}

function _removeAnySharesFortest(address staker, uint256 shares) external {
    _removeAnySharesFor(staker, shares);
}

function testDoS_removeAnySharesFor() public {
    address staker = address(0x123);

    for(uint160 i = 0; i < 2000; i++) {
        staking._addStake(staker, 1e18);
    }
    
    uint256 gasBefore = gasleft();
    staking._removeAnySharesFortest(staker, 2000 * 1e18);
    uint256 gasUsed = gasBefore - gasleft();

    console.log("gas used:", gasUsed);
    assertLt(gasUsed, 1_000_000, "Gas too high, DoS potential!");
}

The original report as published on Immunefi, by lllll. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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 loop iterates over an array whose length is controlled by the user, without a cap, so gas usage grows with the number of entries.

Impact

If this function is used during withdraw or exit flows, affected users may be unable to withdraw staked assets, potentially resulting in permanent freezing of funds for those users.

What the attacker needed

User accumulates many small stakes; withdrawal relies on _removeAnySharesFor

How it was found

  1. Add many small stakes for a single staker.
  2. Call _removeAnySharesFor with the total shares.
  3. Observe that gas used exceeds the block gas limit and the transaction reverts.

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 function that processes a list of stakes uses a loop that runs once for each stake. If someone has thousands of tiny stakes, the loop takes too long and runs out of gas, which is the fee paid to run code. Then the transaction fails, and the user cannot withdraw their funds.

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.

More from Belong

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.