This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
stent - Calc.boundedAdd used intitally but later regular subtraction used #269
Labels
Medium
A valid Medium severity issue
Reward
A payout will be made for this issue
Sponsor Confirmed
The sponsor acknowledged this issue is valid
Won't Fix
The sponsor confirmed this issue will not be fixed
stent
medium
Calc.boundedAdd used intitally but later regular subtraction used
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 useCalc.boundedSubtract
but rather a regular-
, which could cause overflow errors.Vulnerability Detail
In
MarketUtils.getNextFundingAmountPerSize
the funding amount per size variables in theGetNextFundingAmountPerSizeResult
variable are calculated usingCalc.boundedAdd(a,b)
. If the inputs to the function are(type(int256).min+10,-12)
, for example, then the value returned will betype(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 negatingtype(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
orCalc.boundedSub
.Impact
MarketDecrease orders that would be expected to pass would fail, causing them to be cancelled.
Code Snippet
https://github.com/gmx-io/gmx-synthetics/blob/a2e331f6d0a3b59aaac5ead975b206840369a723/contracts/market/MarketUtils.sol#L1074
https://github.com/gmx-io/gmx-synthetics/blob/a2e331f6d0a3b59aaac5ead975b206840369a723/contracts/utils/Calc.sol#L90
https://github.com/gmx-io/gmx-synthetics/blob/a2e331f6d0a3b59aaac5ead975b206840369a723/contracts/market/MarketUtils.sol#L1345
Tool used
Manual Review
Recommendation
Use
Calc.boundedAdd
&Calc.boundedSub
on all calculations involving addition and subtraction with funding amount per size variables.The text was updated successfully, but these errors were encountered: