Due to logical error when changing leverage the system also refunds the margin that should be backing the unrealized loss the account holds.
When a user changes leverage on an open position, the system refunds margin without accounting for unrealised losses, allowing the user to withdraw margin that should cover those losses. This can create bad debt for the protocol, covered by the insurance fund.
Stormy Follow with dhank, lonelybones and 1 other
Originally disclosed on Code4rena
Submitted by Stormy, also found by dhank, lonelybones, and Mike_Bello90
On short explanation, when changing the leverage of already opened position, the system calculates the new intended margin based on the current position notional size and the changed leverage value. The intended margin is then checked against the position’s total equity (margin + upnl) to ensure that the position remains above the minimum open margin value.
cache.fundingPayment = ClearingHouseLib.realizeFundingPayment(cache.assets, cache.positions);
cache.newMargin = clearingHouse.getIntendedMargin(cache.assets, cache.positions);
// assert open margin requirement met
clearingHouse.assertOpenMarginRequired({
assets: cache.assets,
positions: cache.positions,
margin: cache.newMargin.toInt256()
});
clearingHouse.setPositions({
tradedAsset: "",
account: account,
subaccount: subaccount,
assets: cache.assets,
positions: cache.positions
});
// settle delta between new and prev margin & new and prev orderbook collateral
collateralDelta = StorageLib.loadCollateralManager().settleNewLeverage({
account: account,
subaccount: subaccount,
collateralDeltaFromBook: cache.collateralDeltaFromBook,
newMargin: cache.newMargin.toInt256(),
fundingPayment: cache.fundingPayment
});
The function settleNewLeverage is supposed to correctly account the margin balance of the user when leverage is changed. However the function does not take into account the unrealized PnL when calculating the new margin balance. As a result, the system can refund all excess margin including the margin that is effectively used to cover ongoing unrealized losses the account holds.
Simple example with Bob:
- Bob has 20k long position at 1x leverage.
- The market moves against Bob and the position now has -10k uPnL.
- When changing leverage, the new intended margin for the position to be open is 5k.
- Under normal behaviour the system should refund (20k - 10k) - 5k = 5k refund.
- However when settling the new leverage, the system only keeps the new intended position margin.
- So the margin balance covering the loss of the -10k uPnL loss is also refunded to Bob.
- Due to this logical error faulty bad debt is inflicted to the system and covered by the insurance fund.
function settleNewLeverage(
CollateralManager storage self,
address account,
uint256 subaccount,
int256 collateralDeltaFromBook,
int256 newMargin,
int256 fundingPayment
) internal returns (int256 collateralDelta) {
int256 currentMargin = self.margin[account][subaccount] - fundingPayment;
int256 collateralDeltaFromPosition = newMargin - currentMargin;
collateralDelta = collateralDeltaFromPosition + collateralDeltaFromBook;
self.handleCollateralDelta(account, collateralDelta);
self.margin[account][subaccount] = newMargin;
}
Recommended mitigation steps
Refactor the function settleNewLeverage to consider the account’s uPnL as well. The uPnL should be taken into account only if its negative value indicates that there is unrealized loss owned by the account holder.
function settleNewLeverage(
CollateralManager storage self,
address account,
uint256 subaccount,
int256 collateralDeltaFromBook,
int256 newMargin,
int256 fundingPayment,
int256 upnl
) internal returns (int256 collateralDelta) {
int256 effectiveUpnl = upnl < 0 ? upnl : int256(0);
int256 currentMargin = self.margin[account][subaccount] + effectiveUpnl - fundingPayment;
int256 collateralDeltaFromPosition = newMargin - currentMargin;
collateralDelta = collateralDeltaFromPosition + collateralDeltaFromBook;
self.handleCollateralDelta(account, collateralDelta);
self.margin[account][subaccount] = newMargin;
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 settleNewLeverage function calculates the new margin balance without including the position's unrealised profit and loss, so it refunds margin that is effectively covering unrealised losses.
Impact
The user can withdraw margin that should cover unrealised losses, potentially creating bad debt for the protocol that is covered by the insurance fund.
What the attacker needed
An open position with margin; ability to change leverage; position has negative unrealised PnL
The fix
The settleNewLeverage function should be refactored to include the negative unrealised PnL when calculating the current margin, so that only truly excess margin is refunded.
How it was found
- Open a long position with 1x leverage.
- The market moves against the position, creating a negative unrealised PnL.
- Change the leverage, causing the system to recalculate the intended margin.
- The system refunds excess margin without deducting the unrealised loss, leaving the position undercollateralised.
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 contract, a trader posts margin as collateral. If the position loses money, that loss is covered by the margin. When changing leverage, the system recalculates how much margin is needed and refunds any excess. Here, it forgot to subtract the current loss, so it refunded too much, leaving the protocol short.
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.