Loss for protocol by incorrectly assuming the position has been fully closed
The backstop liquidation function assumes a position is fully closed when only one position remains and margin is negative, writing off the entire margin balance. However, the liquidation order is an IOC order that may be only partially filled, leaving a remaining position while the protocol absorbs the loss.
dhank Follow with 0xsagetony, axelot and 2 others
Originally disclosed on Code4rena
Submitted by dhank, also found by 0xsagetony, axelot, dan__vinci, and hgrano
perps/modules/LiquidatorPanel.sol #L262-L266
In the fn backstopLiquidate() the check for finding whether the position is fully closed before writing-off the margin balance is incomplete.
// realize bad debt if underwater and full close
if (cache.margin < 0 && cache.positions.length == 1) {
//@audit-issue backstop liquidate entire position but if the placed order is partially filled there will be remaining position amount.
fee += cache.margin;
delete cache.margin;
}
When liquidator calls backstopLiquidate(), the liquidator places a IOC type fill order with amount = position.amount.
perps/types/Market.sol #L185
But this doesn’t ensure that the entire position.amount will be traded in the given execution. Especially in case of backstop orderbook where less orders are expected in the orderBook compared to the standard order Book, it is not necessary that there will be enough orders in the orderBook that matches the given incoming liquidation orders size.
Since it is an IOC type order, it won’t revert even if the mentioned amount is not filled completely.
But the code incorrectly writes off the entire margin balance at the protocol’s expense, under the false assumption that the user no longer holds any position.
Recommended mitigation steps
Either place FOK orders in case of backstop liquidation or check whether the position.amount == 0 also before writing off the user’s margin balance.
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 check for full closure only verifies that one position remains and margin is negative, but does not verify that the position amount is zero after the IOC order, which may be partially filled.
Impact
The protocol could incur a loss by writing off margin for a position that is not fully closed, leaving residual exposure.
What the attacker needed
Backstop liquidation triggered; IOC order partially filled
The fix
The code should either use FOK orders for backstop liquidation or also check that the position amount is zero before writing off the margin balance.
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 perpetual futures protocol, a liquidator can close an underwater position. The code checks if only one position remains and the margin is negative, then assumes the position is fully closed and writes off the bad debt. But the order used to close it might only be partially filled, so part of the position remains open while the protocol has already taken the loss.
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.