Single-sided deposits that are auto-adjusted may have their collateral value cut in half
In the deposit logic, when a single-sided deposit triggers an auto-adjustment that increases the adjusted short amount, the adjusted long amount is never assigned and stays at zero. An attacker or user submitting such an order without a minimum output could receive roughly half the expected market tokens, while sane slippage settings would simply make the transaction revert.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/164
Found by
IllIllI, joestakey, koxuan, rvierdiiev
Summary
Deposits of only the long collateral token, when doing so results in an auto-adjustment of the order to minimize price impact, creating a larger adjusted short amount, does not properly track the new adjusted long amount.
Vulnerability Detail
When such an adjustment is maid, the adjusted long amount is never updated, so it remains at the uninitialized value of zero.
Impact
The long portion of the collateral will be zero. If the user submitted the order without specifying a minimum number of market tokens to receive, the amount they receive may be half of what it should have been. If they provide sane slippage amounts, the transaction will revert, and the feature will essentially be broken.
Code Snippet
Rather than storing the result of the subtraction to the adjustedLongTokenAmount variable, the difference is subtracted from it, and the result is not stored:
// File: gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol : ExecuteDepositUtils.getAdjustedLongAndShortTokenAmounts() #1
389 @> uint256 adjustedLongTokenAmount;
390 uint256 adjustedShortTokenAmount;
...
402 uint256 diff = poolShortTokenAmount - poolLongTokenAmount;
403
404 if (diff < poolShortTokenAmount) {
405 adjustedShortTokenAmount = diff + (longTokenAmount - diff) / 2;
406 @> adjustedLongTokenAmount - longTokenAmount - adjustedShortTokenAmount;
407 } else {
408 adjustedLongTokenAmount = 0;
409 adjustedShortTokenAmount = longTokenAmount;
410 }
411 }
412
413 return (adjustedLongTokenAmount, adjustedShortTokenAmount);
414: }
Tool used
Manual Review
Recommendation
diff --git a/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol b/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol
index 03467e4..1a96f47 100644
--- a/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol
+++ b/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol
@@ -403,7 +403,7 @@ library ExecuteDepositUtils {
if (diff < poolShortTokenAmount) {
adjustedShortTokenAmount = diff + (longTokenAmount - diff) / 2;
- adjustedLongTokenAmount - longTokenAmount - adjustedShortTokenAmount;
+ adjustedLongTokenAmount = longTokenAmount - adjustedShortTokenAmount;
} else {
adjustedLongTokenAmount = 0;
adjustedShortTokenAmount = longTokenAmount;
Discussion
xvi10
Fix in https://github.com/gmx-io/gmx-synthetics/commit/d4066840f925c43116eed3c2c83ae2e44be39eed
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 a typo where a subtraction expression was written instead of an assignment, so the computed adjusted long amount was discarded and the variable kept its default zero value.
Impact
The long portion of the collateral becomes zero, so without a minimum output the user may receive about half the expected tokens, and with sane slippage the transaction reverts, effectively breaking the feature.
What the attacker needed
Single-sided deposit of the long token; auto-adjustment path is triggered; no minimum output specified
The fix
The assignment was corrected so the adjusted long amount is stored as the long token amount minus the adjusted short amount; the text notes a fix commit.
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.
When you deposit into a liquidity pool, the code sometimes splits your deposit between two tokens to reduce price impact. A coding mistake meant one half of that split was never recorded, so it was treated as zero. The result is that the user gets far fewer tokens than they should, or the transaction fails if they set a sensible minimum.
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.