When a position is closed, the execution fees for the canceled stop orders are lost for the user
When a position is decreased, all stop orders for that position are cancelled without refunding the execution fees paid by the user. An attacker could not directly exploit this, but users lose funds when closing positions or when liquidated.
mstpr-brainbot Follow with pashap9990
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/50
Found by
mstpr-brainbot, pashap9990
Summary
When a position is decreased fully or partially, all the stop orders for that particular position will be canceled. Normally, the cancel order flow returns the execution fee paid by the user. However, this type of cancellation does not do that. As a result, all the STOP_LOSS and TAKE_PROFIT order execution fees are lost for the user.
Vulnerability Detail
When a position is decreased partially or in full, the DecreasePositionProcess::decreasePosition function will remove all the hanging close orders with the following line:
CancelOrderProcess.cancelStopOrders(
cache.position.account,
symbolProps.code,
cache.position.marginToken,
cache.position.isCrossMargin,
CancelOrderProcess.CANCEL_ORDER_POSITION_CLOSE,
params.requestId
);
As we can observe in CancelOrderProcess::cancelStopOrders function the order removed from the accounts storage and global storage:
function cancelStopOrders(
address account,
bytes32 symbol,
address marginToken,
bool isCrossMargin,
bytes32 reasonCode,
uint256 excludeOrder
) external {
.
.
if (
orderInfo.symbol == symbol &&
orderInfo.marginToken == marginToken &&
Order.Type.STOP == orderInfo.orderType &&
orderInfo.isCrossMargin == isCrossMargin
) {
-> accountProps.delOrder(orderIds[i]);
-> orderPros.remove(orderIds[i]);
.
}
}
}
When the order is removed from storage user can no longer cancel it and get back the execution fee. All the stop loss and take profit orders execution fees are lost for the user.
Impact
When user decides to close positions they will lose the execution fees. It can interpreted as user mistake however, If the account is liquidated than it can't be users mistake and the execution fees are lost regardless.
Code Snippet
Tool used
Manual Review
Recommendation
Refund the execution fees as it's done in a normal cancel order
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/22
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 decreasePosition function calls cancelStopOrders, which removes orders from storage without triggering the refund logic used in normal cancellations.
Impact
Users lose the execution fees for all stop-loss and take-profit orders when they close a position, and this loss also occurs during liquidation.
What the attacker needed
User has open position with stop orders; position is decreased or liquidated
The fix
The protocol team fixed this issue by refunding the execution fees as done in a normal cancel 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 some trading systems, users pay a small fee when placing stop-loss or take-profit orders. If those orders are later cancelled, the fee should be returned. Here, when a position is closed, the system cancels all related stop orders but forgets to refund the fees, so users lose money.
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.