An Issue stems in withdrawMargin()
function which is UD60X18 type(usigned 60.18decimal fixed-point) to an SD59X18(signed 59.18). The current approach cast it to int128 first, which is risky because UD60X18 can represent values upto 1.15e59, but int128 can only go up 1.7e38. If liquidationFeeUsdX18 exceeds int128 max, this cast will overflow causing incorrect value or reverts.
Here the checks if the margin balance without unrealised PnL is less than the liquidation fee. The liquidation fee in the contract is stored as UD60X18 but when making comparisons it needs to be an SD59X18 to match margin balance type.
If the liquidationFeeUsdx18 exceeds int128's capacity the coversion overflows to a negative value. aalowing users to withdraw collateral even when their balance is insufficient.
Manual review
Replace unsafe cast with UD60X18 to SD59X18 conversion using PRBMaths library.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.