short side of getReservedUsd does not work for market that has the same collateral token
The getReservedUsd function for the short side does not account for price changes when the collateral token is the same as the index token, potentially leading to under-reserved funds and market insolvency. An attacker could exploit this by opening short positions in such a market, increasing the risk of insolvency if the collateral token's price drops.
Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/198
Found by
0xGoodess
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
Tool used
Manual Review
Recommendation
Consider apply both long and short calculations of reserveUsd with relation to the indexTokenPrice.
Discussion
IllIllI000
leaning towards invalid, but will let sponsor verify
xvi10
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
IllIllI000
@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
the issue does not seem valid to me and the current contract code seems reasonable, an example:
- there is $50m worth of ETH in the pool and the reserve factor is set to 0.25
- a max of $12.5m shorts can be opened
- when validating the reserve we check that the max short open interest does not exceed this
- 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
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
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
Considering this issue as a valid medium based on the above comments that there is a possibility of market insolvency
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 short-side reserve calculation uses open interest in USD without adjusting for the collateral token's price, which is incorrect when the collateral token is the same as the index token and its price fluctuates.
Impact
If a market uses a non-stablecoin as collateral for shorts, the reserved USD may not cover potential losses when the collateral price drops, potentially leading to market insolvency.
What the attacker needed
Market with same collateral for long and short; non-stablecoin collateral
The fix
The recommendation is to apply both long and short calculations of reservedUsd with relation to the indexTokenPrice, but the sponsor argued it is a configuration issue and the fix was not implemented.
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 trading market, the system reserves a portion of funds to cover potential losses. For short positions, the code assumes the collateral is a stablecoin, so it doesn't adjust the reserve when the collateral's price changes. If the collateral is a volatile token like ETH, the reserve may become insufficient, risking market insolvency.
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.