Escrow record not cleared on cancellation and order fill
The escrow record for a market buy order is not cleared when the order is cancelled or filled, allowing an operator to take escrow funds that should no longer be available. This can lead to insolvency as the operator can withdraw other users' funds.
Source: https://github.com/sherlock-audit/2023-06-dinari-judging/issues/56
Found by
Delvir0, bin2chen, bitsurfer, chainNue, ctf_sec, dirk_y, hals, volodya
Summary
In DirectBuyIssuer.sol, a market buy requires the operator to take the payment token as escrow prior to filling the order. Checks are in place so that the math works out in terms of how much escrow has been taken vs the order's remaining fill amount. However, if the user cancels the order or fill the order, the escrow record is not cleared.
The escrow record will exists as a positive amount which can lead to accounting issues.
Vulnerability Detail
Take the following example:
- Operator broadcasts a
takeEscrow()transaction around the same time that the user callsrequestCancel()for the order - Operator also broadcasts a
cancelOrder()transaction - If the
cancelOrder()transaction is mined before thetakeEscrow()transaction, then the contract will transfer out token when it should not be able to.
takeEscrow() simply checks that the getOrderEscrow[orderId] is less than or equal to the requested amount:
bytes32 orderId = getOrderIdFromOrderRequest(orderRequest, salt);
uint256 escrow = getOrderEscrow[orderId];
if (amount > escrow) revert AmountTooLarge();
// Update escrow tracking
getOrderEscrow[orderId] = escrow - amount;
// Notify escrow taken
emit EscrowTaken(orderId, orderRequest.recipient, amount);
// Take escrowed payment
IERC20(orderRequest.paymentToken).safeTransfer(msg.sender, amount);
Cancelling the order does not clear the getOrderEscrow record:
function _cancelOrderAccounting(OrderRequest calldata order, bytes32 orderId, OrderState memory orderState)
internal
virtual
override
{
// Prohibit cancel if escrowed payment has been taken and not returned or filled
uint256 escrow = getOrderEscrow[orderId];
if (orderState.remainingOrder != escrow) revert UnreturnedEscrow();
// Standard buy order accounting
super._cancelOrderAccounting(order, orderId, orderState);
}
}
This can lead to an good-faith and trusted operator accidentally taking funds from the contract that should not be able to leave.
coming up with the fact that the transaction does not have deadline or expiration date:
consider the case below:
- a good-faith operator send a transaction, takeEscrow
- the transaction is pending in the mempool for a long long long time
- then user fire a cancel order request
- the operator help user cancel the order
- the operator send a transcation cancel order
- cancel order transaction land first
- the takeEscrow transaction lands
because escrow state is not clear up, the fund (other user's fund) is taken
It's also worth noting that the operator would not be able to call returnEscrow() because the order state has already been cleared by the cancellation. getRemainingOrder() would return 0.
function returnEscrow(OrderRequest calldata orderRequest, bytes32 salt, uint256 amount)
external
onlyRole(OPERATOR_ROLE)
{
// No nonsense
if (amount == 0) revert ZeroValue();
// Can only return unused amount
bytes32 orderId = getOrderIdFromOrderRequest(orderRequest, salt);
uint256 remainingOrder = getRemainingOrder(orderId);
uint256 escrow = getOrderEscrow[orderId];
// Unused amount = remaining order - remaining escrow
if (escrow + amount > remainingOrder) revert AmountTooLarge();
Impact
- Insolvency due to pulling escrow that should not be allowed to be taken
Code Snippet
Tool used
Manual Review
Recommendation
Clear the escrow record upon canceling the order.
Discussion
jaketimothy
Fixed in
ctf-sec
The fix reset the escrow balance after cancelling
the protocol may want to consider handling the escrow accounting properly when the order is filled as well (such as reset the escrow balance to 0)
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 escrow record is not cleared when an order is cancelled or filled, leaving a positive escrow balance that can be withdrawn later.
Impact
The operator can take escrow funds that should not be allowed to be taken, leading to insolvency.
What the attacker needed
Operator role; user cancels order; transaction ordering
The fix
The escrow record should be cleared upon cancelling the order, and the protocol may also consider resetting the escrow balance when the order is filled.
How it was found
- Operator broadcasts a takeEscrow transaction around the same time that the user calls requestCancel for the order.
- Operator also broadcasts a cancelOrder transaction.
- If the cancelOrder transaction is mined before the takeEscrow transaction, then the contract will transfer out token when it should not be able to.
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 this system, an operator takes a payment from a buyer and holds it in escrow until the order is filled. The bug is that when the order is cancelled, the record of that escrow is not removed, so the operator can still take the money even though the order is gone. This means the operator could accidentally take funds that belong to other users.
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.