Liquidation will never work with non-zero discounts
The liquidation function in MochiVault.sol subtracts the passed-in debt amount from the position's stored debt, but the stored debt is discounted while the passed-in amount is not, causing an underflow. As a result, any discount profile that applies a non-zero discount will make all liquidations revert, preventing collateral from being liquidated.
Originally disclosed on Code4rena
Submitted by harleythedog
Impact
Right now, there is only one discount profile in the github repo: the ”NoDiscountProfile” which does not discount the debt at all. This specific discount profile works correctly, but I claim that any other discount profile will result in liquidation never working.
Suppose that we instead have a discount profile where discount() returns any value strictly larger than 0. Now, suppose someone wants to trigger a liquidation on a position. First, triggerLiquidation will be called (within DutchAuctionLiquidator.sol). The variable “debt” is initialized as equal to vault.currentDebt(\_nftId). Notice that currentDebt(\_ndfId) (within MochiVault.sol) simply scales the current debt of the position using the liveDebtIndex() function, but there is no discounting being done within the function - this will be important.
Back within the triggerLiquidation function, the variable “collateral” is simply calculated as the total collateral of the position. Then, the function calls vault.liquidate(\_nftId, collateral, debt), and I claim that this will never work due to underflow. Indeed, the liquidate function will first update the debt of the position (due to the updateDebt(\_id) modifier). The debt of the position is thus updated using lines 99-107 in MochiVault.sol. We can see that the details\[\_id].debt is updated in the exact same way as the calculations for currentDebt(\_nftId), however, there is the extra subtraction of the discountedDebt on line 107.
Eventually we will reach line 293 in MochiVault.sol. However, since we discounted the debt in the calculation of details\[\_id].debt, but we did not discount the debt for the passed in parameter _usdm (and thus is strictly larger in value), line 293 will always error due to an underflow. In summary, any discount profile that actually discounts the debt of the position will result in all liquidations erroring out due to this underflow. Since no positions will be liquidatable, this represents a major flaw in the contract as then no collateral can be liquidated so the entire functionality of the contract is compromised.
Proof of Concept
Retracing the steps as I have described above, we can see that any call to triggerLiquidation will result in:
details\[\_id].debt -= \_usdm;
throwing an error since _usdm will be larger than details\[\_id].debt.
Recommended Mitigation Steps
An easy fix is to simply change:
details\[\_id].debt -= \_usdm;
to be:
details\[\_id].debt = 0;
as liquidating a position should probably just be equivalent to repaying all of the debt in the position.
Side Note: If there are no other discount profiles planned to be added other than ”NoDiscountProfile”, then I would recommend deleting all of the discount logic entirely, since NoDiscountProfile doesn’t actually do anything.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the liquidation function subtracts the full debt amount from a stored debt value that has already been reduced by a discount, causing an arithmetic underflow.
Impact
Any discount profile that actually discounts the debt will cause all liquidations to revert, so no collateral can be liquidated and the contract's liquidation functionality is broken.
What the attacker needed
A discount profile with non-zero discount
The fix
The fix should set the position's debt to zero instead of subtracting the passed-in amount, or remove the discount logic entirely if only the no-discount profile is used.
How it was found
- A discount profile returns a value strictly larger than 0.
- Someone calls triggerLiquidation on a position.
- The function sets debt to vault.currentDebt, which does not apply the discount.
- The function calls vault.liquidate with the undiscounted debt.
- Inside liquidate, the debt is updated with the discount applied.
- The subtraction of the undiscounted debt from the discounted stored debt underflows and reverts.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract lets people borrow against collateral. If they become unsafe, a liquidator can repay their debt and take the collateral. This report shows that if a discount is applied to the debt, the liquidation code tries to subtract a larger number from a smaller one, which crashes the transaction, so no one can ever liquidate.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.