Skip to content
Business logic Audit finding Advanced

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.

pkqs90 Follow with 0xAlix2

Originally disclosed on Code4rena

Submitted by pkqs90, also found by 0xAlix2

https://github.com/code-423n4/2024-10-loopfi/blob/main/src/CDPVault.sol#L702

https://github.com/code-423n4/2024-10-loopfi/blob/main/src/PoolV3.sol#L593

Impact

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:

  1. 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.
  2. 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);

PoolV3.sol:

    function repayCreditAccount(
        uint256 repaidAmount,
        uint256 profit,
        uint256 loss
    )
        external
        override
        creditManagerOnly // U:[LP-2C]
        whenNotPaused // U:[LP-2A]
        nonReentrant // U:[LP-2B]
    {
        ...
        if (profit > 0) {
            _mint(treasury, _convertToShares(profit)); // U:[LP-14B]
@>      } else if (loss > 0) {
            address treasury_ = treasury;
            uint256 sharesInTreasury = balanceOf(treasury_);
            uint256 sharesToBurn = _convertToShares(loss);
            if (sharesToBurn > sharesInTreasury) {
                unchecked {
                    emit IncurUncoveredLoss({
                        creditManager: msg.sender,
                        loss: _convertToAssets(sharesToBurn - sharesInTreasury)
                    }); // U:[LP-14D]
                }
                sharesToBurn = sharesInTreasury;
            }
            _burn(treasury_, sharesToBurn); // U:[LP-14C,14D]
        }
        ...
    }

Recommended Mitigation Steps

In CDPVault, change to pool.repayCreditAccount(debtData.debt, debtData.accruedInterest, loss).

In PoolV3:

        if (profit > 0) {
            _mint(treasury, convertToShares(profit)); // U:[LP-14B]
+       }
+       if (loss > 0)
-       } else if (loss > 0) {
            ...
        }

0xtj24 (LoopFi) confirmed

Koolex (judge) commented:

Why Profit should be debtData.accruedInterest?

For the second part, could you please provide a case where profit and loss are non-zero in PJQA?

pkqs90 (warden) commented:

@Koolex - Here’s an example scenario:

  1. User originally taken out a debt of 100, and interest grows to 50, so debtData.debt = 100, debtData.accruedInterest = 50, calcTotalDebt(debtData) = 150).
  2. User collateral is only 100, and after multiplying discountPrice, the repayAmount is only 90. Bad debt occurs.
  3. loss = calcTotalDebt(debtData) - repayAmount is equal to 150 - 90 = 60.
  4. 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);
    }

PoolV3.sol:

    function repayCreditAccount(
        uint256 repaidAmount,
        uint256 profit,
        uint256 loss
    )
        external
        override
        creditManagerOnly // U:[LP-2C]
        whenNotPaused // U:[LP-2A]
        nonReentrant // U:[LP-2B]
    {
        uint128 repaidAmountU128 = repaidAmount.toUint128();

        DebtParams storage cmDebt = _creditManagerDebt[msg.sender];
        uint128 cmBorrowed = cmDebt.borrowed;
        if (cmBorrowed == 0) {
            revert CallerNotCreditManagerException(); // U:[LP-2C,14A]
        }

        if (profit > 0) {
            _mint(treasury, _convertToShares(profit)); // U:[LP-14B]
        } else if (loss > 0) {
            address treasury_ = treasury;
            uint256 sharesInTreasury = balanceOf(treasury_);
            uint256 sharesToBurn = _convertToShares(loss);
            if (sharesToBurn > sharesInTreasury) {
                unchecked {
                    emit IncurUncoveredLoss({
                        creditManager: msg.sender,
                        loss: _convertToAssets(sharesToBurn - sharesInTreasury)
                    }); // U:[LP-14D]
                }
                sharesToBurn = sharesInTreasury;
            }
            _burn(treasury_, sharesToBurn); // U:[LP-14C,14D]
        }...
    }

Koolex (judge) commented:

@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).

pkqs90 (warden) commented:

@Koolex - The 2024-07 code had pool.repayCreditAccount(debtData.debt, 0, loss); https://github.com/code-423n4/2024-07-loopfi/blob/main/src/CDPVault.sol#L624, and was later fixed to pool.repayCreditAccount(debtData.debt, profit, loss); https://github.com/code-423n4/2024-10-loopfi/blob/main/src/CDPVault.sol#L702.

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.

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.

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.

More by pkqs90

View all

25 more disclosed reports, best first.

More from LoopFi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.