Collateral cannot be claimed due to inverted comparison condition
The claimCollateral function in GMX Synthetics has an inverted comparison that reverts when the claimable amount is greater than or equal to the already claimed amount, so users can never claim their collateral. This causes a principal loss for users who are owed collateral due to capped price impacts.
IllIllI Follow with berndartmueller, drdr and 4 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/136
Found by
IllIllI, berndartmueller, drdr, float-audits, joestakey, rvierdiiev, stopthecap
Summary
Collateral given back to a user due to capped price impacts cannot be claimed due to inverted comparison condition
Vulnerability Detail
The check that ensures that the amount claimed is less than the amount available has its condition inverted, i.e. requires that the amount claimed is more than the amount available.
Impact
Since the claimed amount starts at zero, the user will never be able to claim any of this collateral back, which is a principal loss.
Needing to let users claim their collateral is mainly for when large liquidations cause there to be large impact amounts. While this is not an everyday occurrence, large price gaps and liquidations are a relatively common occurrence in crypto, happening every couple of months, so needing this functionality will be required in the near future. Fixing the issue would require winding down all open positions and requiring LPs to withdraw their collateral, and re-deploying with new code.
Code Snippet
// File: gmx-synthetics/contracts/market/MarketUtils.sol : MarketUtils.claimCollateral() #1
627 uint256 claimableAmount = dataStore.getUint(Keys.claimableCollateralAmountKey(market, token, timeKey, account));
628 uint256 claimableFactor = dataStore.getUint(Keys.claimableCollateralFactorKey(market, token, timeKey, account));
629 uint256 claimedAmount = dataStore.getUint(Keys.claimedCollateralAmountKey(market, token, timeKey, account));
630
631 uint256 adjustedClaimableAmount = Precision.applyFactor(claimableAmount, claimableFactor);
632 @> if (adjustedClaimableAmount >= claimedAmount) {
633 revert CollateralAlreadyClaimed(adjustedClaimableAmount, claimedAmount);
634: }
Tool used
Manual Review
Recommendation
diff --git a/gmx-synthetics/contracts/market/MarketUtils.sol b/gmx-synthetics/contracts/market/MarketUtils.sol
index 7624b69..67b167c 100644
--- a/gmx-synthetics/contracts/market/MarketUtils.sol
+++ b/gmx-synthetics/contracts/market/MarketUtils.sol
@@ -629,7 +629,7 @@ library MarketUtils {
uint256 claimedAmount = dataStore.getUint(Keys.claimedCollateralAmountKey(market, token, timeKey, account));
uint256 adjustedClaimableAmount = Precision.applyFactor(claimableAmount, claimableFactor);
- if (adjustedClaimableAmount >= claimedAmount) {
+ if (adjustedClaimableAmount <= claimedAmount) {
revert CollateralAlreadyClaimed(adjustedClaimableAmount, claimedAmount);
}
Discussion
xvi10
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
Impact
Users cannot claim any collateral back, resulting in a principal loss; the issue is not an everyday occurrence but will be required when large liquidations cause large impact amounts.
What the attacker needed
Large liquidations causing capped price impact
The fix
The comparison should be changed from >= to <= so that it reverts only when the claimed amount is greater than or equal to the claimable amount.
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 smart contract, a function lets users claim back collateral (money they put in). The code that checks if you've already claimed too much has its comparison backwards, so it always thinks you've claimed too much and blocks you. This means you can never get your collateral back, losing your money.
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.