Users that have to claim collateral more than once for a time slot, may get the wrong total amount
When a user claims collateral for a time slot, the contract stores the factor-adjusted total as the amount already claimed instead of the raw claimable amount. If the claimable factor changes and the user claims again for the same slot, the accounting is wrong and the user can receive less collateral than they are owed.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/142
Found by
IllIllI
Summary
Users that have to claim collateral more than once for a given time slot, may get the wrong total amount, because the amount claimed is incorrectly set
Vulnerability Detail
When letting a user claim his/her collateral, the code looks up the claimable amount, does an adjustment based on a factor, sends that amount to the user, then updates the remaining amount claimable. The code incorrectly sets the factor-adjusted total claimable amount as the amount claimed, rather than the claimable amount.
Impact
Accounting of the claimed amount will be wrong, and the user will get less collateral back than they deserve, in some cases.
Code Snippet
// File: gmx-synthetics/contracts/market/MarketUtils.sol : MarketUtils.claimCollateral() #1
631 @> uint256 adjustedClaimableAmount = Precision.applyFactor(claimableAmount, claimableFactor);
632 if (adjustedClaimableAmount <= claimedAmount) { // @audit fixed comparison operator as mentioned in a separate issue
633 revert CollateralAlreadyClaimed(adjustedClaimableAmount, claimedAmount);
634 }
635
636 @> uint256 remainingClaimableAmount = adjustedClaimableAmount - claimedAmount;
637
638 dataStore.setUint(
639 Keys.claimedCollateralAmountKey(market, token, timeKey, account),
640 @> adjustedClaimableAmount
641 );
642
643 MarketToken(payable(market)).transferOut(
644 token,
645 receiver,
646 remainingClaimableAmount
647: );
- A user triggers claimable collateral for 1 Eth (
claimableAmount= 1) - A keeper sets
claimableFactorto 1.0 - The user calls claim, and gets the full 1 Eth, and
claimedAmountbecomes 1 (adjustedClaimableAmount) - A keeper sets
claimableFactorto 0.5 for that time slot - The user triggers more claimable collateral for 1 Eth (
claimableAmount= 2) for the same time slot - The user calls claim.
adjustedClaimableAmountis 2 * 0.5 = 1,remainingClaimableAmountis 1 - 1 = 0, so the user can't claim anything
The user should have been able to claim a total of 1.5 Eth, but was only able to claim the original 1 Eth, and then nothing more.
Tool used
Manual Review
Recommendation
diff --git a/gmx-synthetics/contracts/market/MarketUtils.sol b/gmx-synthetics/contracts/market/MarketUtils.sol
index 7624b69..3346296 100644
--- a/gmx-synthetics/contracts/market/MarketUtils.sol
+++ b/gmx-synthetics/contracts/market/MarketUtils.sol
@@ -637,7 +637,7 @@ library MarketUtils {
dataStore.setUint(
Keys.claimedCollateralAmountKey(market, token, timeKey, account),
- adjustedClaimableAmount
+ claimableAmount
);
MarketToken(payable(market)).transferOut(
Discussion
xvi10
the claimableFactor should be set only after the time slot for the timeKey has passed, there is no validation on the contract for this currently, this validation will be added if necessary, this feature is excluded from the current scope
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 claim function stores the factor-adjusted claimable total as the claimed amount rather than the raw claimable amount, so later claims for the same time slot are offset incorrectly.
Impact
The claimed-amount accounting is wrong and the user may receive less collateral back than they deserve; the maintainer notes the factor is meant to be set only after the time slot has passed, and that validation is not yet enforced.
What the attacker needed
Claimable factor changes within a time slot; user claims more than once for that slot
The fix
The stored claimed amount should be set to the raw claimable amount rather than the adjusted one; the maintainer also says validation should ensure the factor is only set after the time slot has passed.
How it was found
- A user accrues 1 unit of claimable collateral for a time slot and the claimable factor is 1.0.
- The user claims and receives the full 1 unit, and the stored claimed amount becomes 1.
- A keeper lowers the claimable factor to 0.5 for that same time slot.
- The user accrues another 1 unit for the same slot, making the raw claimable amount 2.
- The user claims again; the adjusted amount is 1, the remaining amount is 0, so nothing more is paid out.
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.
A smart contract lets people collect collateral they are owed for a period of time. It keeps a running note of how much has already been paid out, but it writes down the wrong number after a payout. If the payout rate changes and the person claims again, the contract thinks they were already paid more than they were, so it gives them too little.
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.