Closed c4-bot-9 closed 4 months ago
raymondfam marked the issue as insufficient quality report
raymondfam marked the issue as primary issue
Hypothetical example devoid of a concrete flow of interacting with the coded contracts.
hansfriese marked the issue as unsatisfactory: Insufficient proof
Lines of code
https://github.com/code-423n4/2024-03-dittoeth/blob/91faf46078bb6fe8ce9f55bcb717e5d2d302d22e/contracts/libraries/LibOrders.sol#L853-L866
Vulnerability details
Description
cancelBid
,cancelAsk
, andcancelShort
functions are responsible for canceling orders from their respective ordersbooks. They take theasset
andid
of the order as inputs and directly remove the order from the orderbook without checking if it is currently being matched.cancelBid
This retrieves the bid order, checks if it's not already canceled or matched, updates the user's escrowed balance, and then calls the internal
cancelOrder
function to remove the order from the orderbook.Vulnerability Details
When an order is in the process of being matched by the
bidMatchAlgo
orsellMatchAlgo
functions, and simultaneously, thecancelBid
,cancelAsk
, orcancelShort
function is called for the same order.Due to the lack of checks and locks, if the cancel function is called while the order is being matched, it can lead to inconsistencies in the orderbook state. The order may be partially filled and then cancelled, resulting in an incorrect remaining amount or a discrepancy between the user's balances and the order status.
For example, consider the following scenario:
bidMatchAlgo
function.cancelBid
function is called for the same bid order.cancelBid
function removes the order from the orderbook, even though it is still being matched.This can lead to a situation where the order is partially filled by the matching process, but then gets cancelled, leaving the remaining amount in an inconsistent state.
Impact
Proof of Concept
Scenario:
bidMatchAlgo
function starts processing User A's bid order and matches it with User B's ask order.cancelBid
function to cancel their bid order.Steps:
User A creates a bid order
User B creates an ask order
The
bidMatchAlgo
function starts matching User A's bid order with User B's ask orderWhile the matching process is in progress, User A calls the
cancelBid
functionWe can see
cancelBid
function removes User A's bid order from the orderbook, even though it is currently being matched with User B's ask order.Additional information to consider
Recommended Mitigation Steps
Add a 'filling' status to orders:
Add a reentrancy guard to the
bidMatchAlgo
andsellMatchAlgo
functions to prevent any external calls (including to the cancel functions) while a match is in progress.In the matching functions, update the order statuses to 'matched' before making any external calls or interactions.
Assessed type
Invalid Validation