Skip to content
Business logic Audit finding Intermediate

Incorrect `nextOpenInterest` values set for `priceImpact`

The function that calculates the next open interest values for long and short positions only updates the side of the user's action, leaving the other side as zero. This causes incorrect price impact calculations, potentially leading to large negative price impact and loss of user funds.

float-audits Follow

Originally disclosed on Sherlock

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

Found by

float-audits

Summary

Logic for setting nextOpenInterest values is incorrect for long and short, depending on which side the action was in. These incorrect values for nextOpenInterest values lead to incorrect calculation of priceImpact for the user.

Vulnerability Detail

Inside PositionPricingUtils.sol the following chain of function calls can be observed: getPriceImpactUsd()->getNextOpenInterest()->getNextOpenInterestParams()

getNextOpenInterestParams() is intended to return the next open interest values for long and short after taking into account the action of the user. The default values for the nextLongOpenInterest and nextShortOpenInterest are 0. However it only updates the next open interest of the side on which the action is on, therefore the next open interest of the other side is incorrectly returned as 0.

Should the open interest values be really large, then the implication of having 0 value as next open interest value for one side is quite significant.

This causes the consequent price impact calculations based on current and next open interest values to be materially incorrect.

This causes boolean value isSameSideRebalance in _getPriceImpactUsd() function to have an incorrect value.

For a user who could actually be improving the balance in the market and should be given a positive price impact, this calculation would lead to negative price impact that is very large in magnitude and hence loss of user funds.

Impact

Example below:

isLong = true;

sizeDeltaUsd = 2;

longOpenInterest = 10; // next should be 12 shortOpenInterest = 13; // next should be 13

nextLongOpenInterest = 12; nextShortOpenInterest = 0;

longOpenInterest <= shortOpenInterest = true; nextLongOpenInterest <= nextShortOpenInterest = false;

isSameSideRebalance = false; // should be true

priceImpact calculated based on the nextDiffUsd value of 12 (instead of 1)

Code Snippet

LoC: https://github.com/sherlock-audit/2023-02-gmx/blob/main/gmx-synthetics/contracts/pricing/PositionPricingUtils.sol#L310-L340

Solidity
function getNextOpenInterestParams(
    GetPriceImpactUsdParams memory params,
    uint256 longOpenInterest,
    uint256 shortOpenInterest
) internal pure returns (OpenInterestParams memory) {
    uint256 nextLongOpenInterest;
    uint256 nextShortOpenInterest;

    if (params.isLong) {
        if (params.usdDelta < 0 && (-params.usdDelta).toUint256() > longOpenInterest) {
            revert UsdDeltaExceedsLongOpenInterest(params.usdDelta, longOpenInterest);
        }

        nextLongOpenInterest = Calc.sumReturnUint256(longOpenInterest, params.usdDelta);
    } else {
        if (params.usdDelta < 0 && (-params.usdDelta).toUint256() > shortOpenInterest) {
            revert UsdDeltaExceedsShortOpenInterest(params.usdDelta, shortOpenInterest);
        }

        nextShortOpenInterest = Calc.sumReturnUint256(shortOpenInterest, params.usdDelta);
    }

    OpenInterestParams memory openInterestParams = OpenInterestParams(
        longOpenInterest,
        shortOpenInterest,
        nextLongOpenInterest,
        nextShortOpenInterest
    );

    return openInterestParams;
}

Tool used

Manual Review

Recommendation

Remove these lines in the if-brackets:

  • nextLongOpenInterest = Calc.sumReturnUint256(longOpenInterest, params.usdDelta);
  • nextShortOpenInterest = Calc.sumReturnUint256(shortOpenInterest, params.usdDelta);

And add these lines before openInterestParams assignment in getNextOpenInterestParams(): nextLongOpenInterest = isLong ? longOpenInterest + sizeDeltaUsd : longOpenInterest nextShortOpenInterest = !isLong ? shortOpenInterest + sizeDeltaUsd : shortOpenInterest

Discussion

xvi10

Fix in https://github.com/gmx-io/gmx-synthetics/commit/b04edccc87babc619dd769c66b6af03313885bd3

The original report as published on Sherlock, by float-audits. 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

The function initialises next open interest values to zero and only updates the side of the action, failing to carry over the unchanged side's current open interest.

Impact

Users who improve market balance could receive a large negative price impact, leading to loss of funds.

What the attacker needed

None

The fix

The fix was applied in a commit that corrects the calculation of next open interest values.

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.

Open interest is the total value of open positions in a market. When a user trades, the system should update both the long and short open interest values to calculate the price impact of the trade. Here, the code only updated one side, leaving the other at zero, which made the price impact calculation wrong and could unfairly penalise users.

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 float-audits

View all

1 more disclosed report, 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.