Users can get impact pool discounts while also increasing the virtual impact pool skew
The virtual impact pool is updated unconditionally even when the virtual impact is not used in price impact calculations, leading to a growing negative virtual impact. This can cause users to suddenly face a large virtual swap impact imbalance when a market becomes flat.
Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/246
Found by
IllIllI
Summary
Virtual impacts will remain consistently negative, even if most pools' impacts are balanced
Vulnerability Detail
applyDeltaToPoolAmount()/applyDeltaToVirtualInventoryForPositions() always modify the virtual impact, even if the virtual impact wasn't consulted
Impact
Imagine a different stablecoin is one of the tokens in each of 5 markets where the other side is Eth, all five markes are part of the same virtual market, and each of the markets' swap imbalance is such that users will get a discount for swapping from Eth into the stablecoin, and the global virtual swap balance for 'stable'/Eth is flat. Users will keep swapping from Eth into the stable, which will push the individual pool towards being flat, but will push the virtual swap inventory for stables to be more and more negative. As soon as one of the five markets becomes flat, any user trying to swap Eth into a stable will suddenly be hit with the huge virtual swap impact imbalance
Code Snippet
applyDeltaToPoolAmount() unconditionally updates the virtual inventory for swaps:
// File: gmx-synthetics/contracts/market/MarketUtils.sol : MarketUtils.applyDeltaToPoolAmount() #1
711 @> applyDeltaToVirtualInventoryForSwaps(
712 dataStore,
713 eventEmitter,
714 market,
715 token,
716 delta
717: );
but the actual price impact skips the virtual impact if one of the tokens doesn't have virtual inventory:
// File: gmx-synthetics/contracts/pricing/SwapPricingUtils.sol : SwapPricingUtils.getPriceImpactUsd() #2
119 @> if (!hasVirtualInventoryTokenA || !hasVirtualInventoryTokenB) {
120 return priceImpactUsd;
121: }
or if the impact was positive:
// File: gmx-synthetics/contracts/pricing/SwapPricingUtils.sol : SwapPricingUtils.getPriceImpactUsd() #3
96 // the virtual price impact calculation is skipped if the price impact
97 // is positive since the action is helping to balance the pool
98 //
99 // in case two virtual pools are unbalanced in a different direction
100 // e.g. pool0 has more WNT than USDC while pool1 has less WNT
101 // than USDT
102 // not skipping the virtual price impact calculation would lead to
103 // a negative price impact for any trade on either pools and would
104 // disincentivise the balancing of pools
105:@> if (priceImpactUsd >= 0) { return priceImpactUsd; }
Virtual position impacts have the same issue
Tool used
Manual Review
Recommendation
Don't adjust the virtual inventory for swaps if the virtual inventory wasn't consulted when calculating the impact.
Discussion
xvi10
fixed in https://github.com/gmx-io/gmx-synthetics/pull/155/commits/1b35173e915a7d3bb85f6c6b6e59a7210897854c
virtual swap price impact should be tracked by virtualInventoryForSwapsKey(virtualMarketId, isLongToken) instead of virtualInventoryForSwapsKey(virtualMarketId, token)
this would allow similar markets e.g. ETH/USDC, ETH/USDT to be associated together
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 virtual inventory is updated unconditionally in applyDeltaToPoolAmount() and applyDeltaToVirtualInventoryForPositions(), while the price impact calculation skips the virtual impact under certain conditions, causing a mismatch.
Impact
Users could face unexpectedly high fees when swapping into a stablecoin if the virtual impact pool becomes very negative, as described in the scenario.
What the attacker needed
Multiple markets sharing a virtual market; users swap to balance individual pools; virtual impact becomes negative
The fix
The virtual inventory should only be adjusted when it is actually consulted in the price impact calculation, and the tracking key should be based on the virtual market and token side rather than the individual token.
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 decentralised exchange, a virtual impact pool tracks imbalances across similar markets to adjust swap fees. The bug is that this pool is updated even when it's not used to calculate fees, so it can become very negative. Then, when a market becomes balanced, users suddenly get hit with a huge fee.
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.