Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

0xGoodess - short side of getReservedUsd does not work for market that has the same collateral token #198

Open
sherlock-admin opened this issue Jun 5, 2023 · 7 comments
Labels
Medium A valid Medium severity issue Reward A payout will be made for this issue Sponsor Disputed The sponsor disputed this issue's validity Won't Fix The sponsor confirmed this issue will not be fixed

Comments

@sherlock-admin
Copy link
Contributor

0xGoodess

medium

short side of getReservedUsd does not work for market that has the same collateral token

Summary

short side of getReservedUsd does not work for market that has the same collateral token

Vulnerability Detail

Consider the case of ETH / USD market with both long and short collateral token as ETH.

the available amount to be reserved (ETH) would CHANGE with the price of ETH.

    function getReservedUsd(
        DataStore dataStore,
        Market.Props memory market,
        MarketPrices memory prices,
        bool isLong
    ) internal view returns (uint256) {
        uint256 reservedUsd;
        if (isLong) {
            // for longs calculate the reserved USD based on the open interest and current indexTokenPrice
            // this works well for e.g. an ETH / USD market with long collateral token as WETH
            // the available amount to be reserved would scale with the price of ETH
            // this also works for e.g. a SOL / USD market with long collateral token as WETH
            // if the price of SOL increases more than the price of ETH, additional amounts would be
            // automatically reserved
            uint256 openInterestInTokens = getOpenInterestInTokens(dataStore, market, isLong);
            reservedUsd = openInterestInTokens * prices.indexTokenPrice.max;
        } else {
            // for shorts use the open interest as the reserved USD value
            // this works well for e.g. an ETH / USD market with short collateral token as USDC
            // the available amount to be reserved would not change with the price of ETH
            reservedUsd = getOpenInterest(dataStore, market, isLong);
        }

        return reservedUsd;
    }

Impact

reservedUsd does not work when long and short collateral tokens are the same.

Code Snippet

https://github.com/sherlock-audit/2023-04-gmx/blob/main/gmx-synthetics/contracts/market/MarketUtils.sol#L1415-L1439

Tool used

Manual Review

Recommendation

Consider apply both long and short calculations of reserveUsd with relation to the indexTokenPrice.

@github-actions github-actions bot added the Medium A valid Medium severity issue label Jun 9, 2023
@IllIllI000
Copy link
Collaborator

leaning towards invalid, but will let sponsor verify

@xvi10
Copy link

xvi10 commented Jun 12, 2023

it would not be advisable to use non-stablecoins to back short positions

in case a non-stablecoin is used to back short positions, the amount to be reserved may not need to be changed since the reserve ratio should still validate if the total open interest is a reasonable ratio of the pool's USD value

@xvi10 xvi10 added the Sponsor Disputed The sponsor disputed this issue's validity label Jun 12, 2023
@IllIllI000
Copy link
Collaborator

@xvi10 inadvisable, and the stipulation that open interest be a 'reasonable ratio' seem to indication that this bug is possible, and should therefore remain open - am I misunderstanding what you've said

@xvi10
Copy link

xvi10 commented Jun 14, 2023

the issue does not seem valid to me and the current contract code seems reasonable, an example:

  1. there is $50m worth of ETH in the pool and the reserve factor is set to 0.25
  2. a max of $12.5m shorts can be opened
  3. when validating the reserve we check that the max short open interest does not exceed this
  4. if the price of ETH decreases and is now worth $40m, the validation would be that the max open interest does not exceed $10m

the pending pnl of the short positions would increase if the price of ETH decreases, which is a problem with choosing to use a non-stablecoin to back short positions rather than an issue with the validation

in that case the cap of trader pnl and ADL could help to reduce the risk of the market becoming insolvent, but it would be better to avoid the situation by not using a non-stablecoin to back short positions

@IllIllI000
Copy link
Collaborator

IllIllI000 commented Jun 14, 2023

which is a problem with choosing to use a non-stablecoin to back short positions rather than an issue with the validation

I can see that argument, but you seem to be indicating that users are allowed to do it anyway, and markets becoming insolvent seems like a situation that should be prevented. I'll let Sherlock decide

@xvi10
Copy link

xvi10 commented Jun 14, 2023

markets have a risk of becoming insolvent, capping trader pnl and ADL helps, reducing the risk of insolvency is left up to the market creator to configure the backing tokens and parameters

@hrishibhat
Copy link

Considering this issue as a valid medium based on the above comments that there is a possibility of market insolvency

@sherlock-admin sherlock-admin added the Reward A payout will be made for this issue label Jun 20, 2023
@xvi10 xvi10 added the Won't Fix The sponsor confirmed this issue will not be fixed label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Medium A valid Medium severity issue Reward A payout will be made for this issue Sponsor Disputed The sponsor disputed this issue's validity Won't Fix The sponsor confirmed this issue will not be fixed
Projects
None yet
Development

No branches or pull requests

4 participants