# Withdrawing all `lv` before expiry will lead to lost funds in the Vault

If all lv holders redeem early, totalLvIssued hits 0 and separateLiquidity divides by zero at expiry, permanently locking vault funds.

- Payout: Audit contest
- Severity: medium
- Program: Cork Protocol
- Found by: 0x73696d616f
- Disclosed: 2024-08-22
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-cork-protocol-judging/issues/211
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-cork-protocol%3AM-5
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:sherlock sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** VaultLib / MathHelper.separateLiquidity in Depeg-swap vault
**Root cause:** MathHelper.separateLiquidity computes ratePerLv = totalAmount*1e18/totalLvIssued before the guard that returns early when totalLvIssued == 0, so a zero lv supply reverts with division by zero.
**Steps:**
1. All lv holders call `Vault::redeemEarlyLv()` before expiry, driving totalLvIssued to 0.
2. Ds expires; no further deposits possible to raise lv supply.
3. At expiry `VaultLib::_liquidatedLp()` calls `VaultPoolLib::reserve()` -> `MathHelper.separateLiquidity(totalAmount, 0, totalLvWithdrawn)`.
4. `ratePerLv = (totalAmount * 1e18) / 0` reverts, blocking liquidation and stranding AMM/PSM funds.
**How found:** Code reading of the liquidation path: noticed the division by totalLvIssued occurs before the zero-supply guard, and reasoned that redeemEarly can zero out lv supply before expiry.
**Exploitation:** Not an attacker-driven exploit; a state-dependent liveness bug triggered by legitimate user behavior (all users redeeming early) combined with expiry, permanently locking vault liquidity.
**Insight:** In share/rate math, check the order of zero-guards versus division: a guard placed after the division is dead code. Look for paths (early redeem, full withdrawal) that can zero total supply before a later settlement/liquidation routine runs.

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

**separateLiquidity divides by totalLvIssued before zero guard**

```
function separateLiquidity(uint256 totalAmount, uint256 totalLvIssued, uint256 totalLvWithdrawn)
    external
    pure
    returns (uint256 attributedWithdrawal, uint256 attributedAmm, uint256 ratePerLv)
{
    // with 1e18 precision
    ratePerLv = ((totalAmount * 1e18) / totalLvIssued);

    // attribute all to AMM if no lv issued or withdrawn
    if (totalLvIssued == 0 || totalLvWithdrawn == 0) {
        return (0, totalAmount, ratePerLv);
    }
```

