Skip to content
Business logic Audit finding Intermediate

Leverage Increase Breaks Margin Removal Invariant

The setPositionLeverage function lacks the same margin safety check as removeMargin, so a user can increase leverage to reduce the required margin and effectively withdraw margin from a profitable position that removeMargin would block. This bypasses the protocol's margin safety mechanism, potentially increasing liquidation risk.

BenRai Follow with 0xterrah, Bigsam and 1 other

Originally disclosed on Code4rena

Submitted by BenRai, also found by 0xterrah, Bigsam, and Rhaydden

perps/PerpManager.sol #L176-L219

The setPositionLeverage() function allows users to bypass margin removal restrictions by increasing leverage, effectively extracting margin that would otherwise be blocked by removeMargin()’s safety checks.

The perpetual trading system implements margin management through two key functions: removeMargin() and setPositionLeverage(). While removeMargin() enforces strict restrictions to prevent excessive margin extraction from profitable positions, setPositionLeverage() lacks equivalent safeguards, creating a bypass mechanism.

The removeMargin() function enforces margin requirements through assertPostWithdrawalMarginRequired() which ensures that margin + upnl >= max(intendedMargin, totalNotional / 10). This restriction is designed to prevent users from extracting too much margin from profitable positions, maintaining adequate collateralization.

However, setPositionLeverage() only validates against assertOpenMarginRequired(), which uses a different calculation: margin + upnl >= minOpenMargin. The key difference is that assertOpenMarginRequired() uses getMinOpenMargin() which calculates margin requirements based on maxOpenLeverage of each market rather than the user’s actual leverage, while assertPostWithdrawalMarginRequired() uses the user’s actual leverage via getIntendedMargin().

When a user increases leverage:

  1. The intendedMargin decreases (since intendedMargin = currentNotional / newLeverage)
  2. The settleNewLeverage() function adjusts the margin balance to match the new intended margin
  3. This effectively allows margin extraction that would be blocked by removeMargin()

[!NOTE] The assertPostWithdrawalMarginRequired() function includes the critical line: intendedMargin = intendedMargin.max(totalNotional / 10) which prevents excessive margin extraction from high-profit positions, but this protection is absent in setPositionLeverage().

This vulnerability allows users to bypass critical margin safety mechanisms designed to protect the protocol from margin reduction based on unrealized pnl. Users can extract margin from profitable positions that would otherwise be restricted, potentially leading to increased liquidation risk and protocol instability. The bypass undermines the core risk management system of the perpetual trading platform.

Recommended mitigation steps

To prevent excessive margin reduction for profitable positions, ensure that a reduction of leverage is always possible. But if the leverage is increased, make sure to apply the same margin restrictions as removeMargin(). Keep in mind, that this might prevent the user from increasing his leverage above 10 if there is little to no unrealized pnl in the account.

Alternatively ensure that the indentedMargin with the new leverage is fully covered by the existing margin of the account, ignoring the unrealized pnl. This would still allow the user to increase his leverage (which is legitimate) but ensure that there is still enough “real” margin in the account to account for the intended leverage.

View detailed Proof of Concept


The original report as published on Code4rena, by BenRai. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

Likely the setPositionLeverage function uses a different margin requirement calculation (assertOpenMarginRequired) that does not enforce the same minimum margin floor as removeMargin's assertPostWithdrawalMarginRequired.

Impact

Users could extract margin from profitable positions that would otherwise be restricted, potentially leading to increased liquidation risk and protocol instability.

What the attacker needed

A position with unrealised profit; ability to call setPositionLeverage

The fix

The setPositionLeverage function should apply the same margin restrictions as removeMargin when leverage is increased, or ensure the intended margin is fully covered by existing margin ignoring unrealised profit.

How it was found

  1. Have a position with unrealised profit.
  2. Call setPositionLeverage to increase leverage.
  3. The intended margin decreases and the margin balance is adjusted, extracting margin that removeMargin would block.

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 trading system, users must keep a minimum amount of collateral (margin) in their position. The removeMargin function stops users from taking out too much margin when they have profits, but the setPositionLeverage function lets them increase leverage, which lowers the required margin and lets them take out that margin anyway. This means the safety check can be skipped.

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.

More by BenRai

View all

13 more disclosed reports, best first.

More from GTE Perps and Launchpad

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.