# _calculateMaxBorrowCollateral calculates repay incorrectly and can lead to set token liquidation

MorphoLeverageStrategyExtension applies unutilizedLeveragePercentage when computing netRepayLimit, so once borrowBalance exceeds the reduced threshold, ripcord repay calls underflow and revert, blocking deleveraging and forcing set token liquidation.

- Payout: Audit contest
- Severity: medium
- Program: Morpho X Index
- Found by: 0x52
- Disclosed: 2024-10-02
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-10-morpho-x-index-judging/issues/26
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-morpho-x-index%3AM-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:incomplete-fix technique:regression vuln:accounting-error vuln:dos vuln:liquidation

**Target:** MorphoLeverageStrategyExtension.sol (_calculateMaxBorrowCollateral / ripcord)
**Root cause:** netRepayLimit is computed as liquidationThreshold reduced by unutilizedLeveragePercentage; when borrowBalance exceeds that reduced value, the subtraction at L1134 underflows and reverts, so repay/deleverage paths always revert.
**Steps:**
1. Deploy leveraged set with nonzero unutilizedLeveragePercentage (default for Index Coop tokens).
2. Collateral price drops rapidly, lowering liquidationThreshold.
3. borrowBalance grows past liquidationThreshold * (1 - unutilizedLeveragePercentage).
4. Call MorphoLeverageStrategyExtension#ripcord to repay; _calculateMaxBorrowCollateral underflows at L1134 and reverts.
5. Deleveraging impossible; position reaches liquidation, incurring liquidation fee (~5%+ scaled by leverage).
**How found:** Compared against a prior valid Index Coop finding (2023-05 Index #254) fixed in the Aave leverage extension; noticed the same unutilizedLeveragePercentage adjustment was reintroduced in the Morpho extension's netRepayLimit calculation, then traced the underflow at L1134.
**Exploitation:** Not an attacker-driven exploit; a liveness/accounting flaw. Under a sharp collateral price drop, the keeper's ripcord repay transaction reverts due to underflow, preventing timely deleveraging and allowing the position to be liquidated, costing the set token the liquidation penalty multiplied by leverage.
**Escalation:** Loss scales with leverage: 3x loses ~15% NAV, 5x ~25% NAV from the ~5% liquidation fee.
**Insight:** When a codebase has multiple leverage extensions (Aave, Morpho, etc.), diff the deleveraging/repay math across them: a fix applied in one adapter is often not ported to siblings, so re-check the same subtraction/percentage adjustment for underflow in the new adapter.

