CDPVault.sol#liquidatePositionBadDebt() doesn’t correctly handle profit and loss
The liquidatePositionBadDebt function in CDPVault incorrectly calculates profit and loss when liquidating bad debt, and PoolV3's repayCreditAccount cannot handle both profit and loss being non-zero. This causes incorrect accounting for lpETH stakers, potentially leading to losses for them.
When liquidating bad debt, the profit and loss is not correctly handled. This will cause incorrect accounting to lpETH stakers.
Bug Description
Note: This is based on the 2024-07 Loopfi audit H-12 issue. This protocol team applied a fix, but the fix is incomplete.
There are two issues that needs to be fixed in the new codebase:
The profit that is passed in pool.repayCreditAccount(debtData.debt, profit, loss); should actually use debtData.accruedInterest. This is because we should first “assume” full debt and interest is paid off, and calculate the loss part independently.
The loss is correctly calculated in PoolV3#repayCreditAccount, but the if-else branch is incorrectly implemented. Currently, it can’t handle the case where both profit and loss is non-zero. This would cause a issue that the loss will not be accounted, and will ultimately cause loss to lpETH holders (loss will be implicitly added to the users who hold lpETH) instead of lpETH stakers.
The second fix was also suggested in the original issue, but it isn’t applied.
CDPVault.sol:
takeCollateral = position.collateral;
repayAmount = wmul(takeCollateral, discountedPrice);
uint256 loss = calcTotalDebt(debtData) - repayAmount;
uint256 profit;
if (repayAmount > debtData.debt) {
@> profit = repayAmount - debtData.debt;
}
...
@> pool.repayCreditAccount(debtData.debt, profit, loss); // U:[CM-11]
// transfer the collateral amount from the vault to the liquidator
token.safeTransfer(msg.sender, takeCollateral);
User originally taken out a debt of 100, and interest grows to 50, so debtData.debt = 100, debtData.accruedInterest = 50, calcTotalDebt(debtData) = 150).
User collateral is only 100, and after multiplying discountPrice, the repayAmount is only 90. Bad debt occurs.
loss = calcTotalDebt(debtData) - repayAmount is equal to 150 - 90 = 60.
Since repayAmount < debtData.debt, we would have profit = 0.
This means for PoolV3#repayCreditAccount, 60 shares would be burned from the treasury, while instead it should be 10 (because original debt was 100, repaid is 90, 100 - 90 = 10).
You can also see that if repayAmount was 101, we would calculate profit = 1, and in PoolV3#repayCreditAccount we would mint 1 share instead. This means there is a 61 (1 - (-60) = 61) gap in treasury shares when the repaid amount diff is only 11 (101 - 90 = 11), which does not make any sense.
function calcTotalDebt(DebtData memory debtData) internal pure returns (uint256) {
return debtData.debt + debtData.accruedInterest; //+ debtData.accruedFees;
}
function liquidatePositionBadDebt(address owner, uint256 repayAmount) external whenNotPaused {
...
takeCollateral = position.collateral;
repayAmount = wmul(takeCollateral, discountedPrice);
@> uint256 loss = calcTotalDebt(debtData) - repayAmount;
uint256 profit;
if (repayAmount > debtData.debt) {
@> profit = repayAmount - debtData.debt;
}
...
@> pool.repayCreditAccount(debtData.debt, profit, loss); // U:[CM-11]
// transfer the collateral amount from the vault to the liquidator
token.safeTransfer(msg.sender, takeCollateral);
}
@pkqs90 - Could you please point out the incomplete fix? This is important, since if there is no indication that the sponsor intended to fix it, it would be out of scope (according to this announcement).
The suggested fix was also mentioned the original report for H-12.
The original report as published on Code4rena, by pkqs90. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the profit passed to repayCreditAccount should be the accrued interest, and the if-else branch in PoolV3 prevents handling both profit and loss simultaneously.
Impact
This could cause incorrect accounting, leading to losses for lpETH holders instead of lpETH stakers.
What the attacker needed
Bad debt liquidation occurs
The fix
The fix should change the profit argument to debtData.accruedInterest and modify PoolV3 to use separate if statements for profit and loss.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When a borrower's collateral is worth less than their debt, the system must handle the shortfall. This report shows that the code that calculates how much is lost and how much profit is made is wrong, so the wrong people end up paying for the loss.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.