Skip to content
Business logic Audit finding Advanced

Pool amount adjustments for collateral decreases aren't undone if swaps are successful

A temporary pool amount adjustment used for swap pricing was not cleared when a swap succeeded, because the function returned early. This left the adjustment in storage, causing subsequent swaps to be priced incorrectly, giving some users undeserved discounts and others undeserved penalties.

IllIllI Follow with bin2chen and stent

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/235

Found by

IllIllI, bin2chen, stent

Summary

Pool amount adjustments, which are added to storage as a temporary accounting tracking mechanism, aren't undone if swaps are successful

Vulnerability Detail

swapProfitToCollateralToken() uses Keys.poolAmountAdjustmentKey() to temporarily store an adjustment to the pool amount, and undoes the adjustment at the end of the function, after the try-catch block. However, the function bypasses the end of the function in the succeess case, because it returns early, and therefore doesn't undo the adjustment.

Impact

The value is looked up and used in getNextPoolAmountsParams() for swap orders that occur afterwards. The adjusted amount will be included until someone does another swap of the profit to a collateral token, at which point it will have the new value (and not be reset unless there is an exception). The adjustment ends up being used in the calculation of swap impact, so all subsequent swaps will be priced incorrectly, giving some users discounts they don't deserve, and others a penalty that they don't deserve, when doing swaps.

Code Snippet

Adjustment is added to storage, but isn't undone if swapHandler.swap() is successful:

Solidity
// File: gmx-synthetics/contracts/position/DecreasePositionCollateralUtils.sol : DecreasePositionCollateralUtils.swapProfitToCollateralToken()   #1

452                // adjust the pool amount by the poolAmountDelta so that the price impact of the swap will be
453                // more accurately calculated
454 @>             params.contracts.dataStore.setInt(Keys.poolAmountAdjustmentKey(params.market.marketToken, pnlToken), poolAmountDelta);
455    
456                try params.contracts.swapHandler.swap(
457                    SwapUtils.SwapParams(
...
470                    )
471                ) returns (address /* tokenOut */, uint256 swapOutputAmount) {
472 @>                 return (true, swapOutputAmount);
473                } catch Error(string memory reason) {
474                    emit SwapUtils.SwapReverted(reason, "");
475                } catch (bytes memory reasonBytes) {
476                    (string memory reason, /* bool hasRevertMessage */) = ErrorUtils.getRevertMessage(reasonBytes);
477                    emit SwapUtils.SwapReverted(reason, reasonBytes);
478                }
479    
480 @>             params.contracts.dataStore.setInt(Keys.poolAmountAdjustmentKey(params.market.marketToken, pnlToken), 0);
481            }
482    
483            return (false, 0);
484:       }

https://github.com/sherlock-audit/2023-04-gmx/blob/main/gmx-synthetics/contracts/position/DecreasePositionCollateralUtils.sol#L452-L484

Tool used

Manual Review

Recommendation

Undo the adjustment before returning

Discussion

xvi10

fixed in https://github.com/gmx-io/gmx-synthetics/pull/155/commits/cd9f9c2f0f025c819f1080332514d6a2dedc05fc

the previous flow of updating a global variable seemed more of a workaround to fix the pool amount for the swap

the code was updated to use a more straightforward solution of updating the pool amount directly before performing the swap

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 an early return in the success branch of the swap try-catch block bypassed the code that reset the temporary pool amount adjustment.

Impact

All subsequent swaps could be priced incorrectly, giving some users discounts and others penalties they do not deserve.

What the attacker needed

Swap profit to collateral token; subsequent swaps occur

The fix

The adjustment should be undone before returning; the project fixed it by updating the pool amount directly before the swap instead of using a temporary global variable.

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 smart contract, a temporary value was saved to storage to help calculate swap prices. The code was supposed to reset it after the swap, but if the swap worked, the function exited early and skipped the reset. That leftover value then affected later swaps, making them cost more or less than they should.

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.