`initialCollateralDeltaAmount` is incorrectly interpreted as a USD value when calculating estimated remaining collateral
Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/249
Found by
Chinmay, IllIllI, bin2chen, ten-on-ten
Summary
Decrease orders have checks to ensure that if collateral is withdrawn, that there is enough left that the liquidation checks will still pass. The code that calculates the remaining collateral incorrectly adds a token amount to a USD value.
Vulnerability Detail
initialCollateralDeltaAmount is incorrectly interpreted as a USD value when calculating estimated remaining collateral which means, depending on the token's decimals, the collateral will either be accepted or not accepted, when it shouldn't be.
Impact
If the remaining collateral is over-estimated, the MIN_COLLATERAL_USD checks later in the function will pass, and the user will be able decrease their collateral, but then will immediately be liquidatable by liquidation keepers, since liquidation orders don't attempt to change the collateral amount.
If the remaining collateral is under-estimated, the user will be incorrectly locked into their position.
Code Snippet
initialCollateralDeltaAmount() isn't converted to a USD amount before being added to estimatedRemainingCollateralUsd, which is a USD amount:
// File: gmx-synthetics/contracts/position/DecreasePositionUtils.sol : DecreasePositionUtils.decreasePosition() #1
139 // the estimatedRemainingCollateralUsd subtracts the initialCollateralDeltaAmount
140 // since the initialCollateralDeltaAmount will be set to zero, the initialCollateralDeltaAmount
141 // should be added back to the estimatedRemainingCollateralUsd
142 @> estimatedRemainingCollateralUsd += params.order.initialCollateralDeltaAmount().toInt256();
143 params.order.setInitialCollateralDeltaAmount(0);
144 }
145
146 // if the remaining collateral including position pnl will be below
147 // the min collateral usd value, then close the position
148 //
149 // if the position has sufficient remaining collateral including pnl
150 // then allow the position to be partially closed and the updated
151 // position to remain open
152:@> if ((estimatedRemainingCollateralUsd + cache.estimatedRemainingPnlUsd) < params.contracts.dataStore.getUint(Keys.MIN_COLLATERAL_USD).toInt256()) {
Tool used
Manual Review
Recommendation
Convert to a USD amount before doing the addition
Discussion
xvi10
fixed in https://github.com/gmx-io/gmx-synthetics/commit/f8f2dd694269a8a795a45ed961fb9970e9ce3cc8
IllIllI000
https://github.com/gmx-io/gmx-synthetics/commit/f8f2dd694269a8a795a45ed961fb9970e9ce3cc8
The commit implements the suggested fix of converting the amount to a USD value, and uses the collateral token's min price as the conversion price. The resulting value is never written back to storage, and is only used as a minimum collateral threshold check, so using this less-favorable-to-the-user price is correct.
Looking at 97c826246b06fc977191b8090c970c0ff93cf88a, later changes moved the price lookup to the top of the decreasePosition() function, and changes from using MarketUtils.getMarketPricesForPosition() to using MarketUtils.getMarketPrices(), which means the primary price is now always used, rather than the latest price which may have been a custom or secondary price. According to the comments and fix for #240, there is no longer a custom or secondary price, so the change for this fix looks correct.
done
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A high-severity business logic bug in GMX Update. Sherlock made it public on 21 June 2023. Found in the 74 audit contest (finding H-5), also found by 3 other wardens.
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.