# PositionAction.sol#onCreditFlashLoan may end up with stuck funds for EXACT_IN primary swaps

In LoopFi's decreaseLever EXACT_IN path, over-repayment of debt is capped by CDPVault, leaving residual debt tokens stuck in PositionAction with no recovery path.

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

**Target:** PositionAction.sol onCreditFlashLoan / CDPVault.modifyCollateralAndDebt
**Root cause:** onCreditFlashLoan computes residualAmount = swapAmountOut - subDebt and attempts to repay it, but CDPVault caps repayment at calcTotalDebt; the excess debt tokens remain in PositionAction with no sweep to residualRecipient.
**Steps:**
1. Call decreaseLever with primarySwap.swapType == EXACT_IN on a PositionAction4626 position.
2. onCreditFlashLoan repays subDebt, then _onDecreaseLever withdraws collateral and redeems/exits pools returning a dynamic withdrawnCollateral.
3. primarySwap.amount is set to withdrawnCollateral and swapped; swapAmountOut is dynamic.
4. residualAmount = swapAmountOut - subDebt is approved and passed as -deltaDebt to modifyCollateralAndDebt.
5. If residualAmount >= calcTotalDebt, CDPVault caps debtToDecrease to maxRepayment, transferring only that amount.
6. Remaining residualAmount - maxRepayment debt tokens stay in PositionAction, inaccessible to the user.
**How found:** Code review of the new diff: noticed CDPVault caps deltaDebt repayment at maxRepayment while PositionAction assumes full residualAmount is consumed; traced dynamic output sources (redeem, EXACT_BPT_IN_FOR_ONE_TOKEN_OUT) and frontrun partial liquidation as realistic triggers.
**Exploitation:** Not an attack; a user-triggered accounting flaw. Preconditions: EXACT_IN swap, dynamic collateral output (ERC4626 redeem or Balancer/Pendle pool exit) or partial liquidation frontrun reducing debt, causing swap output to exceed remaining debt. Result: excess debt tokens stranded in the contract.
**Insight:** When a downstream contract silently caps an input (repayment capped at total debt), any caller computing a residual from a dynamic swap output can strand the difference; check that capped amounts are refunded to the user rather than left in an intermediate contract.

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

**CDPVault caps debt repayment to maxRepayment**

```
uint256 maxRepayment = calcTotalDebt(debtData);
@>          if (debtToDecrease >= maxRepayment) {
                debtToDecrease = maxRepayment;
                deltaDebt = -toInt256(debtToDecrease);
            }

            uint256 scaledDebtDecrease = wmul(debtToDecrease, poolUnderlyingScale);
            poolUnderlying.safeTransferFrom(creditor, address(pool), scaledDebtDecrease);
```

**PositionAction4626 dynamic collateral withdrawal via redeem**

```
tokenOut = IERC4626(leverParams.collateralToken).redeem(withdrawnCollateral, address(this), address(this));

        if (leverParams.auxAction.args.length != 0) {
            _delegateCall(
                address(poolAction),
                abi.encodeWithSelector(poolAction.exit.selector, leverParams.auxAction)
            );

            tokenOut = IERC20(IERC4626(leverParams.collateralToken).asset()).balanceOf(address(this));
```

