# Liquidation doesn't account for penalty when calculating collateral to give, allowing users to profit by borrowing and self-liquidating

LoopFi CDPVault computes liquidator collateral from full repayAmount ignoring the liquidation penalty, letting a borrower self-liquidate at a discount and profit.

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

**Target:** CDPVault.liquidatePosition lending vault
**Root cause:** takeCollateral is computed as repayAmount/discountedPrice, but only repayAmount - penalty actually reduces debt and is transferred to the pool, so the penalty is never reflected in collateral given out.
**Steps:**
1. Deploy CDPVault with liquidationRatio 1.05, penalty 0.99, discount 0.92, liquidationRatio 1.05.
2. Deposit 100e18 collateral and borrow 95e18 WETH via `_modifyCollateralAndDebt(vault, 100 ether, 95 ether)`.
3. Warp 30 days so accrued interest makes position unsafe (debt ~95.79e18 > collateral*ratio).
4. Compute `repayFull = wmul(collateral, wmul(spot, 0.92))` = 92e18 and approve vault.
5. Call `vault.liquidatePosition(position, repayFull)`; receive full 100e18 collateral while only 92e18 WETH spent.
**How found:** Read the natspec claiming the penalty mitigates profitable self-liquidation, then compared the debt-reduction math (`deltaDebt = repayAmount * penalty`) and pool transfer (`repayAmount - penalty`) against the collateral payout (`repayAmount / discountedPrice`) and noticed the penalty was omitted from the payout.
**Exploitation:** Attacker deposits collateral, borrows near the max, waits for interest accrual (or price movement) to make the position unsafe, then self-liquidates by repaying the discounted amount. Because takeCollateral ignores the penalty, the attacker recovers the full collateral for less than the debt, netting the difference from lenders/protocol.
**Insight:** When a protocol documents an anti-self-liquidation penalty, verify every place the penalty should appear: debt reduction, token transfer, and collateral payout. A mismatch between the amount that reduces debt and the amount of collateral released is the tell.

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

**Liquidation collateral calculation missing penalty**

```
uint256 discountedPrice = wmul(spotPrice_, liqConfig_.liquidationDiscount);
        if (spotPrice_ == 0) revert CDPVault__liquidatePosition_invalidSpotPrice();
        // Ensure that there's no bad debt
        if (calcTotalDebt(debtData) > wmul(position.collateral, spotPrice_)) revert CDPVault__BadDebt();

        // compute collateral to take, debt to repay and penalty to pay
@>      uint256 takeCollateral = wdiv(repayAmount, discountedPrice);
        uint256 deltaDebt = wmul(repayAmount, liqConfig_.liquidationPenalty);
        uint256 penalty = wmul(repayAmount, WAD - liqConfig_.liquidationPenalty);
```

