_globalPositions.marginDepositedTotal might be set to a extreme big number
Summary
There is a invalid validation before updating _globalPositions.marginDepositedTotal, it's value might be set to a extreme big number due to overflow. In this case, the protocol would be bricked.
Vulnerability Detail
The issue arise on L232 of FlatcoinVault.sol, the validation of int256(_globalPositions.marginDepositedTotal) > _fundingFees is invalid. let's say _globalPositions.marginDepositedTotal = 100 and _fundingFees = -101, they meet the above validation, but then uint256(int256(_globalPositions.marginDepositedTotal) + _fundingFees) = uint256(100 - 101) = uint256(-1) = type(uint256).max. Unintentional overflow occurs and _globalPositions.marginDepositedTotal is being set to a extreme big number.
In this case, any subsequent operations for increasing net global margin would fail, which includes openPosition, increasePositionMargin and updateGlobalPositionPnL.
KingNFT
medium
_globalPositions.marginDepositedTotal
might be set to a extreme big numberSummary
There is a invalid validation before updating
_globalPositions.marginDepositedTotal
, it's value might be set to a extreme big number due to overflow. In this case, the protocol would be bricked.Vulnerability Detail
The issue arise on L232 of
FlatcoinVault.sol
, the validation ofint256(_globalPositions.marginDepositedTotal) > _fundingFees
is invalid. let's say_globalPositions.marginDepositedTotal = 100
and_fundingFees = -101
, they meet the above validation, but thenuint256(int256(_globalPositions.marginDepositedTotal) + _fundingFees) = uint256(100 - 101) = uint256(-1) = type(uint256).max
. Unintentional overflow occurs and_globalPositions.marginDepositedTotal
is being set to a extreme big number.Impact
In this case, any subsequent operations for increasing net global margin would fail, which includes
openPosition
,increasePositionMargin
andupdateGlobalPositionPnL
.Code Snippet
https://github.com/sherlock-audit/2023-12-flatmoney/blob/bba4f077a64f43fbd565f8983388d0e985cb85db/flatcoin-v1/src/FlatcoinVault.sol#L232
Tool used
Manual Review
Recommendation
Duplicate of #195