DeFiFoundry
50,000 USDC
View results
Submission Details
Severity: low
Invalid

The `getOpenInterest` function has a precision loss issue

Summary

Due to the divisor division calculation being performed first and the results being added to get the return value, precision loss may occur when longToken == shortToken.

Vulnerability Details

contracts/libraries/gmx/MarketUtils.sol#L126

For example, when dataStore.getUint(openInterestKey(market, collateralToken, isLong)) returns an odd number like 13, the return value will be 13 / 2 = 6.

function getOpenInterest(
IDataStore dataStore,
address market,
address collateralToken,
bool isLong,
uint256 divisor
) internal view returns (uint256) {
return dataStore.getUint(openInterestKey(market, collateralToken, isLong)) / divisor;
}
function getOpenInterest(
IDataStore dataStore,
MarketProps memory market,
bool isLong
) internal view returns (uint256) {
// @audit Because of `longToken == shortToken`, divisor = 2
uint256 divisor = getPoolDivisor(market.longToken, market.shortToken);
// @audit Because of `longToken == shortToken`
// openInterestUsingLongTokenAsCollateral = openInterestUsingShortTokenAsCollateral
uint256 openInterestUsingLongTokenAsCollateral = getOpenInterest(dataStore, market.marketToken, market.longToken, isLong, divisor);
uint256 openInterestUsingShortTokenAsCollateral = getOpenInterest(dataStore, market.marketToken, market.shortToken, isLong, divisor);
// @audit Return value is 6 + 6 = 12, but not 13
return openInterestUsingLongTokenAsCollateral + openInterestUsingShortTokenAsCollateral;
}

Impact

The precision loss problem will make the return value of the getOpenInterest function smaller than normal.

Tools Used

Recommendations

This precision loss issue can be avoided by moving the divisor division operation to the end:

function getOpenInterest(
IDataStore dataStore,
MarketProps memory market,
bool isLong
) internal view returns (uint256) {
uint256 divisor = getPoolDivisor(market.longToken, market.shortToken);
uint256 openInterestUsingLongTokenAsCollateral = getOpenInterest(dataStore, market.marketToken, market.longToken, isLong);
uint256 openInterestUsingShortTokenAsCollateral = getOpenInterest(dataStore, market.marketToken, market.shortToken, isLong);
// @audit Return value is (13 + 13) / 2 = 13, no rounding issue
return (openInterestUsingLongTokenAsCollateral + openInterestUsingShortTokenAsCollateral) / divisor;
}
Updates

Lead Judging Commences

n0kto Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational or Gas

Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelihood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.

Suppositions

There is no real proof, concrete root cause, specific impact, or enough details in those submissions. Examples include: "It could happen" without specifying when, "If this impossible case happens," "Unexpected behavior," etc. Make a Proof of Concept (PoC) using external functions and realistic parameters. Do not test only the internal function where you think you found something.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.