Virtual swap balances don't take into account token prices
Virtual swap balances in GMX Synthetics are tracked as raw token amounts without converting between the collateral token and the virtual token using an exchange rate. An attacker could exploit price differences between tokens to manipulate impact discounts and drain them from legitimate traders.
Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/251
Found by
IllIllI
Summary
Virtual swap balances don't take into the fact that an exchange between the collateral token and the virtual token is taking place, necessitating an exchange rate.
Vulnerability Detail
Virtual position impacts are all based on the virtual token being the same token as the market's index token. With virtual swap impacts, they're not the same token - the market token is a market collateral token, and the virtual token is some other token, which likely has a different price. For example, the README mentions ETH/USDC as ETH/USDT, where USDC is paired with USDT. USDC and USDT sound like they should be equivalent, but looking at the monthly chart of the exchange rate between the two, it has been between 0.8601 and 1.2000 - a 20% difference at the margins. Further, if one of them were to de-peg, the difference may be even larger and for a much longer period of time.
This applies to swaps, as well as to position changes, since those also track virtual swap inventory, since the balance is changing.
Impact
Orders on some markets will get larger/smaller virtual discounts/penalties than they should, as compared to other markets using the same virtual impact pool. In addition to the basic accounting/fee issues associated with the difference, if the price difference is large enough, someone can swap through the market where the impact is smaller due to the exchange rate in order to push the impact more negative, and then simultaneously swap through the other market, where the same amount of funds would result in a larger positive impact than was incurred negatively in the other market, unfairly draining any impact discounts available to legitimate traders.
Code Snippet
The delta being applied is a token amount:
// File: gmx-synthetics/contracts/swap/SwapUtils.sol : SwapUtils._swap() #1
283 MarketUtils.applyDeltaToPoolAmount(
284 params.dataStore,
285 params.eventEmitter,
286 _params.market.marketToken,
287 _params.tokenIn,
288 @> (cache.amountIn + fees.feeAmountForPool).toInt256()
289 );
290
291 // the poolAmountOut excludes the positive price impact amount
292 // as that is deducted from the swap impact pool instead
293: MarketUtils.applyDeltaToPoolAmount(
and is stored unaltered as the virtual amount:
// File: gmx-synthetics/contracts/market/MarketUtils.sol : MarketUtils.applyDeltaToVirtualInventoryForSwaps() #2
1471 function applyDeltaToVirtualInventoryForSwaps(
1472 DataStore dataStore,
1473 EventEmitter eventEmitter,
1474 address market,
1475 address token,
1476 int256 delta
1477 ) internal returns (bool, uint256) {
1478 bytes32 marketId = dataStore.getBytes32(Keys.virtualMarketIdKey(market));
1479 if (marketId == bytes32(0)) {
1480 return (false, 0);
1481 }
1482
1483 uint256 nextValue = dataStore.applyBoundedDeltaToUint(
1484 Keys.virtualInventoryForSwapsKey(marketId, token),
1485 @> delta
1486 );
1487
1488 MarketEventUtils.emitVirtualSwapInventoryUpdated(eventEmitter, market, token, marketId, delta, nextValue);
1489
1490 return (true, nextValue);
1491: }
The same is true for deposits, decreases, increases, and withdrawals.
Tool used
Manual Review
Recommendation
Use oracle prices and convert the collateral token to the specific virtual token
Discussion
xvi10
added a note in https://github.com/gmx-io/gmx-synthetics/commit/3f17dd59b482e202b52652f8191581ca3827b18e
IllIllI000
https://github.com/gmx-io/gmx-synthetics/commit/3f17dd59b482e202b52652f8191581ca3827b18e The commit does not fix the issue, and instead acknowledges it via code comments, explaining the issue and its potential negative effects, and says that a patch may be applied at a later time if the issue ever manifests and needs to be addressed. done
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 the code applies token amount deltas directly to virtual swap inventory without converting the collateral token amount to the virtual token using an oracle price.
Impact
Orders on some markets could receive incorrect virtual discounts or penalties, and if the price difference is large enough, an attacker could swap through markets to unfairly drain impact discounts available to legitimate traders.
What the attacker needed
Access to GMX Synthetics markets; price difference between tokens
The fix
The issue was not fixed; the project acknowledged it via code comments and may patch it later if it manifests.
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 some trading systems, swaps are tracked using a 'virtual' token that may not be the same as the token actually being swapped. If the system doesn't convert between the two tokens using their relative prices, the recorded balances can be wrong. This report shows that such a mistake could let someone game the system to get unfair discounts.
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.