PnL is incorrectly counted as collateral when determining whether to close positions automatically
The protocol incorrectly includes unrealised profit and loss (PnL) when calculating remaining collateral for a position. An attacker with a profitable position can reduce collateral to one wei, leaving the pool exposed to losses if the price gaps down.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/154
Found by
IllIllI
Summary
GMX uses MIN_COLLATERAL_USD to ensure that there is always enough collateral remaining in a position, so that if there are sudden large gaps in prices, there is enough collateral to cover potential losses.
Vulnerability Detail
When a position is reduced, the estimate of remaining collateral includes the total P&L as part of the collateral. If a position has a lot of profit, a nefarious owner of the position can reduce the collateral to one wei of the collateral token, and let the position run.
Impact
If there is a sudden gap down in price, as is common with crypto Bart price chart formations, the user only loses one wei, but the pool incurs losses because there is no collateral to cover price decreases. Once one wei is left in the position, there is no mechanism for a keeper to reduce the position's leverage, so the only chance thereafter to close the position is when it needs to be liquidated.
Code Snippet
PnL is counted as collateral:
// File: gmx-synthetics/contracts/position/PositionUtils.sol : PositionUtils.willPositionCollateralBeSufficient() #1
420 int256 remainingCollateralUsd = values.positionCollateralAmount.toInt256() * collateralTokenPrice.min.toInt256();
421
422 @> remainingCollateralUsd += values.positionPnlUsd;
423
424 if (values.realizedPnlUsd < 0) {
425 remainingCollateralUsd = remainingCollateralUsd + values.realizedPnlUsd;
426 }
427
428 if (remainingCollateralUsd < 0) {
429 return (false, remainingCollateralUsd);
430 }
431
432: int256 minCollateralUsdForLeverage = Precision.applyFactor(values.positionSizeInUsd, minCollateralFactor).toInt256();
The position is only closed if that total is below the minimum collateral dollar amount.
Tool used
Manual Review
Recommendation
Do not count PnL as part of the collateral, for the purposes of determining the minimum position collateral amount. The combined value may still be useful as a separate check.
Discussion
xvi10
PnL is counted as collateral to allow position increases / decreases if the pnl is sufficient to cover pending fees, the risk of the position having insufficient pnl to cover fees in case of a large change in price is similar to the risk of a position having insufficient collateral to cover fees
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 code adds the position's profit and loss (PnL) to the remaining collateral when checking if collateral is sufficient, instead of using only the actual collateral amount.
Impact
A trader with a profitable position can reduce collateral to one wei, and if the price gaps down, the pool incurs losses because there is no collateral to cover the decrease; the position cannot be reduced further by a keeper and can only be closed via liquidation.
What the attacker needed
A position with significant profit; ability to reduce position collateral
The fix
The recommendation is to not count PnL as part of the collateral when determining the minimum position collateral amount, though the combined value may still be used as a separate check.
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 leveraged trading, a position needs enough collateral (money set aside) to cover potential losses. This report shows that the system mistakenly counted unrealised profit as if it were collateral, so a trader could withdraw almost all their real collateral while keeping the position open. If the price then dropped suddenly, the trader would lose almost nothing, but the pool would bear 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.