# CDPVault.sol#liquidatePositionBadDebt() doesn't correctly handle profit and loss

LoopFi CDPVault bad-debt liquidation miscomputes profit/loss passed to PoolV3, burning excess treasury shares and mischarging lpETH stakers.

- Payout: Audit contest
- Severity: high
- Program: LoopFi
- Found by: pkqs90
- Disclosed: 2025-02-17
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-10-loopfi-findings/issues/2
- Library page: https://bugbountylibrary.com/r/c4%3A2024-10-loopfi%3AH-02
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:incomplete-fix technique:source-review vuln:accounting-error vuln:liquidation

**Target:** CDPVault/PoolV3 Solidity contracts (bad-debt liquidation accounting)
**Root cause:** liquidatePositionBadDebt passes debtData.debt as repaidAmount and profit=repayAmount-debt instead of accruedInterest, and PoolV3 uses else-if so profit and loss can't both apply, so loss is overstated and treasury shares over-burned.
**Steps:**
1. Debt 100, accruedInterest 50, calcTotalDebt=150; collateral discounted repayAmount=90.
2. loss = 150-90 = 60; repayAmount<debt so profit=0.
3. pool.repayCreditAccount(100, 0, 60) burns 60 shares from treasury instead of 10.
4. If repayAmount=101, profit=1 mints 1 share; gap of 61 shares for an 11-unit difference.
5. Fix: pass debtData.accruedInterest as profit and change `else if (loss>0)` to separate `if (loss>0)`.
**How found:** Warden reviewed the prior 2024-07 H-12 fix and noticed it was incomplete: profit still derived from debtData.debt and PoolV3 retained the else-if branch, then constructed numeric scenarios showing the share gap.
**Exploitation:** Triggered by liquidating an underwater position via liquidatePositionBadDebt; loss/profit mismatch burns excess treasury shares, shifting loss onto lpETH holders rather than stakers.
**Insight:** When auditing a patched finding, verify the fix covers both the caller's argument semantics and the callee's branch structure; else-if between profit and loss silently drops one when both are nonzero.

## Proof of concept (copied exactly from the original report)

**Bad debt liquidation profit/loss calculation in CDPVault**

```
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]
```

**PoolV3 repayCreditAccount profit/loss if-else branch**

```
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]
```

