Submitted by ItsNio
The contract decreases users debts but may not take the full worth in collateral from the user, leading to the contract losing potential funds from the missing collateral.
During the liquidate() function call, the function _updateBorrowAndCollateralShare() is eventually invoked. This function liquidates a users debt and collateral based on the value of the collateral they own.
In particular, the equivalent amount of debt, availableBorrowPart is calculated from the users collateral on line 225 through the computeClosingFactor() function call.
Then, an additional fee through the liquidationBonusAmount is applied to the debt, which is then compared to the users debt on line 240. The minimum of the two is assigned borrowPart, which intuitively means the maximum amount of debt that can be removed from the users debt.
borrowPart is then increased by a bonus through liquidationMultiplier, and then converted to generate collateralShare, which represents the amount of collateral equivalent in value to borrowPart (plus some fees and bonus).
This new collateralShare may be more than the collateral that the user owns. In that case, the collateralShare is simply decreased to the users collateral.
collateralShare is then removed from the users collateral.
The problem lies in that although the collateralShare is equivalent to the borrowPart, or the debt removed from the users account, it could be worth more than the collateral that the user owns in the first place. Hence, the contract loses out on funds, as debt is removed for less than it is actually worth.
To demonstrate, we provide a runnable POC.
Add the require statement to line 261. This require statement essentially reverts the contract when the if condition satisfies. The if condition holds true when the collateralShare is greater that the users collateral, which is the target bug.
Once the changes have been made, add the following test into the singularity.test.ts test in tapioca-bar-audit/test
As demonstrated, the function call reverts due to the require statement added in the preconditions.
One potential mitigation for this issue would be to calculate the borrowPart depending on the existing users collateral factoring in the fees and bonuses. The collateralShare with the fees and bonuses should not exceed the users collateral.
cryptotechmaker (Tapioca) confirmed
