Solidity integer division might truncate. As a result, performing multiplication before division can sometimes avoid loss of precision.
Proof of Concept
In general, this is a problem due to precision.
In this case, it also affects balance of the protocol, what makes me suggest high severity, as users can withdraw more than expected as minimumCollateral would get rounded down.
The amount of collateral needed in minimumCollateral to be less than expected, making the user able to withdraw more than expected in getWithdrawalLimitInternal and getWithdrawalLimit
Lines of code
https://github.com/code-423n4/2022-10-inverse/blob/cc281e5800d5860c816138980f08b84225e430fe/src/Market.sol#L360
Vulnerability details
division before multiply
Impact
Solidity integer division might truncate. As a result, performing multiplication before division can sometimes avoid loss of precision.
Proof of Concept
In general, this is a problem due to precision.
In this case, it also affects balance of the protocol, what makes me suggest high severity, as users can withdraw more than expected as minimumCollateral would get rounded down.
The amount of collateral needed in
minimumCollateral
to be less than expected, making the user able to withdraw more than expected ingetWithdrawalLimitInternal
andgetWithdrawalLimit
https://github.com/code-423n4/2022-10-inverse/blob/cc281e5800d5860c816138980f08b84225e430fe/src/Market.sol#L360 https://github.com/code-423n4/2022-10-inverse/blob/cc281e5800d5860c816138980f08b84225e430fe/src/Market.sol#L377
Also, this would affect fees of the protocol in
liquidate()
asliquidationFee
can be truncated, making the amount collected less than expected.https://github.com/code-423n4/2022-10-inverse/blob/cc281e5800d5860c816138980f08b84225e430fe/src/Market.sol#L606
The
replenisherReward
, theliquidatorReward
to be less than expected affecting reward.https://github.com/code-423n4/2022-10-inverse/blob/cc281e5800d5860c816138980f08b84225e430fe/src/Market.sol#L563-L564
https://github.com/code-423n4/2022-10-inverse/blob/cc281e5800d5860c816138980f08b84225e430fe/src/Market.sol#L597-L598
Recommended Mitigation Steps
Reorder the operations. For more info: https://github.com/crytic/slither/wiki/Detector-Documentation#divide-before-multiply