---
title: "Denial of service and locked funds"
url: https://dontgetrekt.today/dev/denial-of-service
section: "In what order does it run?"
---

# Denial of service and locked funds

One user, one failed transfer or one long loop can block a function for everyone. Funds stay locked in the contract.

## How the attack works

1. A function loops over all users or all positions. When the list grows, the loop needs more gas than a block allows.
2. A function sends ETH to many receivers in a loop. One receiver is a contract that always reverts, so the whole call reverts.
3. Variants: an attacker sends ETH with `selfdestruct` to break a `balance == x` check; a griefing call fills a queue.

## Vulnerable code

```solidity
function distribute() external {
    for (uint256 i; i < users.length; i++) {       // unbounded loop
        payable(users[i]).transfer(share);          // one revert blocks all
    }
}
```

## Fixed code

```solidity
mapping(address => uint256) public owed;

function claim() external {                         // pull, not push
    uint256 amt = owed[msg.sender];
    owed[msg.sender] = 0;
    (bool ok,) = msg.sender.call{value: amt}("");
    require(ok, "send failed");
}
```

## Review checklist

- Use pull payments: each user claims their own funds.
- Do not loop over arrays that users can make longer. If you must loop, process in batches.
- Do not use exact balance checks (`address(this).balance == x`).
- Make sure an admin pause cannot lock user funds forever: give users a way to exit.

## How to test for it

- Test the loop with a large number of entries and check the gas.
- Add a receiver that always reverts, and check that other users can still claim.

## Real exploits (1)

- 2022-04-23 Akutar NFT ($34.0M): Akutar NFT had about $34M locked forever because a contract bidder could make processRefunds() revert and a wrong require in claimProjectFunds() blocked the project's withdrawal. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-04/AkutarNFT_exp.sol)

## References

- [SWC-113: DoS with failed call](https://swcregistry.io/docs/SWC-113/)
- [SWC-128: DoS with block gas limit](https://swcregistry.io/docs/SWC-128/)
