Skip to content
Business logic Audit finding Intermediate

Wrong implementation of orderbook can make user can't get their fund back

The order book contract reuses order IDs after cancellation, causing new orders to overwrite existing ones. An attacker can place an order that overwrites another user's order, preventing the victim from withdrawing their funds.

Trumpero Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-09-knox-judging/issues/66

Found by

Trumpero

Lines of code

https://github.com/sherlock-audit/2022-09-knox/blob/main/knox-contracts/contracts/auction/OrderBook.sol#L240 https://github.com/sherlock-audit/2022-09-knox/blob/main/knox-contracts/contracts/auction/OrderBook.sol#L182-L190

Summary

When a user remove an order, next user call addLimitOrder can override the latest order with his/her order. It will make one who is owner of that latest order lose their fund.

Vulnerability Detail

Function _remove will decrease value of index.length by 1 when an order is removed

// url = https://github.com/sherlock-audit/2022-09-knox/blob/main/knox-contracts/contracts/auction/OrderBook.sol#L240
function _remove(Index storage index, uint256 id) internal returns (bool) {
    index.length = index.length > 0 ? index.length - 1 : 1;
    ...
}

Instead of reserving id of removed order to reuse for next created order, function _insert use the id of new order is index.length + 1

// url = https://github.com/sherlock-audit/2022-09-knox/blob/main/knox-contracts/contracts/auction/OrderBook.sol#L165-L172
function _insert(
    Index storage index,
    int128 price64x64,
    uint256 size,
    address buyer
) internal returns (uint256) {
    index.length = index.length > 0 ? index.length + 1 : 1;
    uint256 id = index.length;
    ...
}

It will override the latest order with new order's data.

For example

  • Alice create an order with price = 10 --> id = 1, index.length = 1
  • Bob create an order with price = 20 --> id = 2, index.length = 2
  • Alice cancel order id = 1 --> index.length = 1
  • Candice create new order with price = 30
    • At this time, new order will have id = index.length + 1 = 1 + 1 = 2. It will override the state of Bob's order: price from 20 -> 30

Impact

User whose order is overrided can't withdraw their refund ERC20 and their exercised tokens.

Code Snippet

it.only("bug", async() => {
    const totalContracts = await auction.getTotalContracts(epoch);

    const buyer1OrderSize = totalContracts.div(5);
    const buyer2OrderSize = totalContracts.div(5);
    const buyer3OrderSize = totalContracts.div(5);

    // buyer1 create order with price = 10
    await asset
      .connect(signers.buyer1)
      .approve(addresses.auction, ethers.constants.MaxUint256);
    await auction.addLimitOrder(epoch, 10, buyer1OrderSize);

    // buyer2 create order with price = 20
    await asset 
      .connect(signers.buyer2)
      .approve(addresses.auction, ethers.constants.MaxUint256);
    await auction
      .connect(signers.buyer2)
      .addLimitOrder(epoch, 20, buyer2OrderSize);

    // order with id = 2 have price = 20 
    expect( (await auction.getOrderById(epoch, 2)).price64x64 ).to.equal(20);

    // buyer1 cancel order with id = 1
    await auction.connect(signers.buyer1).cancelLimitOrder(epoch, 1);

    // buyer3 create order with price = 30
    await asset
      .connect(signers.buyer3)
      .approve(addresses.auction, ethers.constants.MaxUint256);
    await auction
      .connect(signers.buyer3)
      .addLimitOrder(epoch, 30, buyer3OrderSize);

    // order with id = 2 have price = 30 --> nervous 
    expect( (await auction.getOrderById(epoch, 2)).price64x64 ).to.equal(30);
});

To check with test, u can use this file https://gist.github.com/Trumpero/adbcd84c33f71856dbf379f581e8abbb I write one more describe ::Bug beside your original describe ::Auction in file Auction.behavior.ts (just too lazy to write a new one).

Tool used

Hardhat

Recommendation

Use an array to store unused (removed) id, then assign each id to the new limit order created instead of using index.length.

Discussion

0xCourtney

https://github.com/KnoxFinance/knox-contracts/pull/78

rcstanciu

Reply from @arbitrary-CodeBeholder


As mentioned above, by introducing this fix the existing method of looping based on length would have caus e an issue, but it appears to be correctly addressed in PR https://github.com/KnoxFinance/knox-contracts/pull/80.

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

Root cause

The contract uses the current length of the order index as the new order ID instead of tracking and reusing freed IDs, so after a cancellation the next order ID collides with an existing order.

Impact

The user whose order is overwritten cannot withdraw their refunded ERC20 tokens or their exercised tokens.

What the attacker needed

A user cancels an order; another user places a new order

The fix

The contract should track removed order IDs in a separate list and assign those IDs to new orders instead of using the index length.

How it was found

  1. Alice creates an order with price 10, receiving ID 1.
  2. Bob creates an order with price 20, receiving ID 2.
  3. Alice cancels her order with ID 1, reducing the index length to 1.
  4. Candice creates a new order with price 30, which gets ID 2 and overwrites Bob's order.

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.

In a smart contract, orders are stored with unique numbers called IDs. When an order is cancelled, the contract reduces the total count but does not remember the cancelled ID. The next new order gets an ID that may already belong to someone else, so it overwrites that person's order. This means the original owner can no longer access their funds.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Trumpero

View all

2 more disclosed reports, best first.

More from Knox Finance

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.