Skip to content
Business logic Audit finding Advanced

Accounting breaks if end market appears multiple times in swap path

The swap logic in GMX Synthetics fails to transfer tokens to the next market when the next market is also the final destination, but still updates the pool's accounting as if tokens were received. An attacker could exploit this to drain collateral tokens from a pool, preventing LPs from withdrawing and users from exiting positions.

IllIllI Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/132

Found by

IllIllI

Summary

If a swap path goes through a market that also is the final destination market, share accounting breaks

Vulnerability Detail

When doing a swap, every market in the provided array of markets has _swap() called, with the input token amount currently residing in the market address of the market, which converts the token to the output token. When the next market isn't the final destination market, the output token is transferred to the next market in the array, to be processed on the next iteration, and the pool's balance of input/output tokens is updated (applyDeltaToPoolAmount()). If the market is the final destination market, no transfer is done, but the balance is still updated (to account for tokens being taken out as either a swap order, or a swap into a position's collateral token).

If a user does an increase order with a swap path where the final destination market appears multiple times in the swap path, markets that appear in the array, after the extra destination market, will have updated their accounting of received tokens, without actually receiving tokens.

Impact

Token accounting will be broken, because the destination market will have extra tokens that its tracking of its own pool amounts doesn't know about (and can't be updated to know about), and subsequent markets in the chain will have fewer tokens than their pool amount accounting believes it does.

An attacker, if they're willing to incur the expense of swap fees, can perform swaps back and forth through the same market multiple times (to minimize swap impact fees), and cause a pool to have zero collateral tokens remaining, meaning LP market tokens are unable to withdraw their funds because token transfers will revert, and nobody will be able to exit their positions for the affected markets, since no collateral tokens will be available. There also will be an undercollateralization, because the reserves calculation will be wrong.

Even through normal use, if a user does this, they'll be able to get their funds, since LPs are providing swap liquidity, but when it comes time to wind down the market, the last to withdraw will not be able to get their funds.

Code Snippet

Tokens aren't transferred to the next market if the next market is the destination market of the swap:

Solidity
// File: gmx-synthetics/contracts/swap/SwapUtils.sol : SwapUtils._swap()   #1

240            // the amountOut value includes the positive price impact amount
241 @>         if (_params.receiver != _params.market.marketToken) {
242                MarketToken(payable(_params.market.marketToken)).transferOut(
243                    cache.tokenOut,
244                    _params.receiver,
245                    cache.amountOut,
246                    _params.shouldUnwrapNativeToken
247                );
248:           }

https://github.com/sherlock-audit/2023-02-gmx/blob/main/gmx-synthetics/contracts/swap/SwapUtils.sol#L240-L248

Tool used

Manual Review

Recommendation

The for-loop calling _swap() already knows when it's processing the last swap, so set a boolean for this fact, and pass that variable into _swap(), and check the variable rather than using the current check

Discussion

xvi10

Fix in https://github.com/gmx-io/gmx-synthetics/pull/101

The original report as published on Sherlock, by IllIllI. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 checks whether the receiver is the market token to decide whether to transfer, instead of checking whether the current iteration is the last in the swap path, so an intermediate market that is also the final destination is incorrectly treated as the end and no transfer occurs.

Impact

An attacker willing to pay swap fees could cause a pool to have zero collateral tokens, so LPs cannot withdraw and users cannot exit positions; even normal use could leave the last LP unable to withdraw.

What the attacker needed

Willing to pay swap fees; ability to create orders with custom swap paths

The fix

The fix should pass a boolean indicating whether the current swap is the last in the loop and use that to decide whether to transfer tokens, rather than checking if the receiver is the market 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 swap can pass through several markets. Each market is supposed to pass the swapped tokens to the next one. This report shows that if the same market appears twice at the end of the path, the code skips the transfer but still records the tokens as received, so the books no longer match reality. An attacker can repeat this to empty a pool's real tokens while the records still show a balance.

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.

More by IllIllI

View all

119 more disclosed reports, best first.

More from GMX Update

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.