Closed github-actions[bot] closed 1 year ago
Disagreed with severity because this is only a potential issue for users who borow nearly all of their borrow limit, meaning they knowingly put themselves in risk of liquidation
Closing issue as it's describing the financial risks of the protocol
unforgiven
high
User can get liquidated just after the borrowing in the same block
Summary
when users call
borrow()
and borrow some loan tokens, code allows users to borrow up touserCollateralRatioMantissa <= _currentCollateralRatioMantissa
but when calculatingdebtShare
for user code rounds up and it would cause the user debt to be higher than actual debt in the same block and it can makeuserCollateralRatioMantissa > _currentCollateralRatioMantissa
and user debt can be liquidated in the same block.Vulnerability Detail
This is
borrow()
code:As you can see code uses
_shares = tokenToShares(amount, _currentTotalDebt, _debtSharesSupply, true)
to calculate user debt share balance. it would calculate user debt share balance by rounding up and set it to thedebtSharesBalanceOf[]
This isliquidate()
code:As you can see it uses
getDebtOf(debtSharesBalanceOf[borrower], _debtSharesSupply, _currentTotalDebt)
to calculate user debt and ifuserCollateralRatioMantissa > _currentCollateralRatioMantissa
it allows user debt to be liquidated. This would cause issue if user decides to borrow allowed maximum amount whereuserCollateralRatioMantissa == _currentCollateralRatioMantissa
. because code calculates debt share by rounding up and set the value of thedebtSharesBalanceOf[]
then code would calculate higher debt amount by callinggetDebtOf()
and that higher debt amount would causeuserCollateralRatioMantissa > _currentCollateralRatioMantissa
. imagine this scenario:currentCollateralRatio
is 5 (total collateral is 20)borrow(50)
to borrow 50 token anduserCollateralRatio
would be 5 (50/10).currentCollateralRatio
would be 150/30 = 50. so code would allow borrwoingl.round_UP(50 * 3 / 100) = 2
. so user debt share would be 2 and total share would be 5 and total debt would be 150.getDebtOf()
for the user would return2 * 150 / 5 = 60
and user collateral ratio would be60 / 10 = 6
which is higher thancurrentCollateralRatio
.This bug would create a MEV where miners can liquidates users immediately if users try to create maximal loan. and possible sandwich attack opportunities. bots can liquidate users debts right after they create a valid debt.
the issue would happen even if
userCollateralRatioMantissa
was so close to_currentCollateralRatioMantissa
when borrowing and it doesn't require it to be equal. as interest in the same block doesn't happen so users should be liquidates in the same block where the interest for that block has already been calculated and they created a valid debt.Impact
Users and other contract that create a valid debt with maximum allowed debt would be liquidated in the same block and lose funds.
Code Snippet
https://github.com/Surge-fi/surge-protocol-v1/blob/b7cb1dc2a2dcb4bf22c765a4222d7520843187c6/src/Pool.sol#L480-L485 https://github.com/Surge-fi/surge-protocol-v1/blob/b7cb1dc2a2dcb4bf22c765a4222d7520843187c6/src/Pool.sol#L572-L574
Tool used
Manual Review
Recommendation
have different threshold for allowed
userCollateralRatioMantissa
when borrowing and for liquidation. the liquidation threshold should be higher than allowed borrow threshold.