Calc.boundedAdd used intitally but later regular subtraction used
The report identifies an inconsistency in the GMX Synthetics smart contract where a funding amount per size calculation uses a bounded addition function to prevent overflow, but a later subtraction uses a regular minus operator. This could cause an arithmetic overflow when negating the minimum integer value, leading to failed MarketDecrease orders.
Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/269
Found by
stent
Summary
In MarketDecrease order flow the funding amount per size calculation uses Calc.boundedAdd but further down the call path there is a calculation that does not use Calc.boundedSubtract but rather a regular -, which could cause overflow errors.
Vulnerability Detail
In MarketUtils.getNextFundingAmountPerSize the funding amount per size variables in the GetNextFundingAmountPerSizeResult variable are calculated using Calc.boundedAdd(a,b). If the inputs to the function are (type(int256).min+10,-12), for example, then the value returned will be type(int256).min i.e. it will not cause an arithmetic overflow, like it would if the values were just added normally with +.
Further down the call path of MarketDecrease in MarketUtils.getFundingFeeAmount the funding amount is negated, and negating type(int256).min results in an arithmetic overflow.
If the point of defending overflow of the funding amount per size variables was to allow execution to pass without failure then all calculations involving the funding amount per size variables should use Calc.boundedAdd or Calc.boundedSub.
Impact
MarketDecrease orders that would be expected to pass would fail, causing them to be cancelled.
Code Snippet
// contract & func: MarketUtils.getNextFundingAmountPerSize
result.fundingAmountPerSize_LongCollateral_LongPosition = Calc.boundedAdd(
result.fundingAmountPerSize_LongCollateral_LongPosition,
cache.fps.fundingAmountPerSizeDelta_LongCollateral_LongPosition.toInt256()
);
// contract: Calc
function boundedAdd(int256 a, int256 b) internal pure returns (int256) {
// if either a or b is zero or if the signs are different there should not be any overflows
if (a == 0 || b == 0 || (a < 0 && b > 0) || (a > 0 && b < 0)) {
return a + b;
}
// if adding `b` to `a` would result in a value less than the min int256 value
// then return the min int256 value
if (a < 0 && b <= type(int256).min - a) {
return type(int256).min;
}
// if adding `b` to `a` would result in a value more than the max int256 value
// then return the max int256 value
if (a > 0 && b >= type(int256).max - a) {
return type(int256).max;
}
return a + b;
}
// contract: MarketUtils
function getFundingFeeAmount(
int256 latestFundingAmountPerSize,
int256 positionFundingAmountPerSize,
uint256 positionSizeInUsd
) internal pure returns (int256) {
int256 fundingDiffFactor = (latestFundingAmountPerSize - positionFundingAmountPerSize);
Tool used
Manual Review
Recommendation
Use Calc.boundedAdd & Calc.boundedSub on all calculations involving addition and subtraction with funding amount per size variables.
Discussion
IllIllI000
will let sponsor review
xvi10
it seems the market could get bricked if reaching this state, despite attempts at avoiding that, the likelihood of this should be quite small
if the market reaches this state, the market may not function correctly, it may be preferable to update the funding fee factors to prevent this state instead of handling this in the contracts, to avoid the complexity of handling this case for this scope
the contracts could be updated in the future to properly handle this case
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 root cause is inconsistent use of bounded arithmetic functions for funding amount per size calculations, allowing a regular subtraction to overflow when negating the minimum integer value.
Impact
MarketDecrease orders that would be expected to pass could fail and be cancelled, potentially bricking the market if the state is reached, though the likelihood is small.
What the attacker needed
Funding amount per size reaches extreme values; MarketDecrease order is placed
The fix
The contracts should be updated to use bounded addition and subtraction for all calculations involving funding amount per size variables, or the funding fee factors should be updated to prevent the problematic state.
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.
Smart contracts often use special functions to safely add or subtract numbers and avoid errors when the result is too large or too small. Here, one part of the code uses such a safe function, but another part uses a normal subtraction, which can cause an error if the number becomes extremely negative. This error could make some orders fail.
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.