# Admin new issuance or user calling `Vault::redeemExpiredLv()` after `Psm::redeemWithCt()` will lead to stuck funds when trying to withdraw

VaultLib._liquidatedLp burns vault-held CT via lvRedeemRaWithCtDs after PSM checkpointed full CT supply, leaving RA/PA permanently unredeemable.

- Payout: Audit contest
- Severity: high
- Program: Cork Protocol
- Found by: 0x73696d616f
- Disclosed: 2024-08-22
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-cork-protocol-judging/issues/156
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-cork-protocol%3AH-8
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Depeg-swap VaultLib/PsmLib accounting (Solidity)
**Root cause:** PsmLib._separateLiquidity checkpoints ctAttributed using IERC20(ct).totalSupply() before the vault liquidates its LP; VaultLib._liquidatedLp then burns the vault's CT via lvRedeemRaWithCtDs, so the checkpointed CT exceeds circulating CT and pro-rata RA/PA redemption can never fully occur.
**Steps:**
1. Admin calls `ModuleCore::issueNewDs()` (or user calls `Psm::redeemWithCT()` before `Vault::redeemExpiredLv()`).
2. `PsmLibrary.onNewIssuance()` -> `_separateLiquidity()` stores `self.psm.poolArchive[prevIdx] = PsmPoolArchive(availableRa, availablePa, IERC20(ds.ct).totalSupply())` including vault-owned CT still in UniV2 LP.
3. `VaultLibrary.onNewIssuance()` -> `_liquidatedLp()` -> `__liquidateUnchecked()` burns LP, receives CT+RA, then calls `PsmLibrary.lvRedeemRaWithCtDs()` which burns the vault's CT via `ds.burnBothFromSelf()`.
4. Circulating CT now < checkpointed ctAttributed; `_calcRedeemAmount` uses stale totalCtIssued so RA/PA pro-rata redemption under-delivers and remainder is stuck.
**How found:** Code-reading of the issueNewDs call order: noticed PSM checkpoint uses full CT totalSupply before vault LP liquidation burns vault-held CT, so checkpointed ctAttributed overstates circulating CT.
**Exploitation:** No attacker capital needed; the ordering is triggered automatically by admin `issueNewDs()` or forced by any user calling `Psm::redeemWithCT()` before `Vault::redeemExpiredLv()`. The vault withdraws too much RA and never claims its PA share, leaving RA/PA in poolArchive permanently unredeemable and blocking users from redeeming CT for RA+PA.
**Insight:** When a protocol checkpoints a token's totalSupply for pro-rata accounting, verify no later step in the same transaction burns or removes supply that the checkpoint counted—especially supply held by contracts (LP positions) that get liquidated afterward.

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

**issueNewDs call order triggering bug**

```
PsmLibrary.onNewIssuance(state, ct, ds, ammPair, idx, prevIdx, repurchaseFeePrecentage);

    getRouterCore().onNewIssuance(id, idx, ds, ammPair, 0, ra, ct);

    VaultLibrary.onNewIssuance(state, prevIdx, getRouterCore(), getAmmRouter());
```

**Checkpoint of ct total supply in _separateLiquidity**

```
self.psm.poolArchive[prevIdx] = PsmPoolArchive(availableRa, availablePa, IERC20(ds.ct).totalSupply());
```

**lvRedeemRaWithCtDs burning vault ct after checkpoint**

```
PsmLibrary.lvRedeemRaWithCtDs(self, redeemAmount, dsId);

    // if the reserved DS is more than the CT that's available from liquidating the AMM LP
    // then there's no CT we can use to effectively redeem RA + PA from the PSM
    uint256 ctAttributedToPa = reservedDs >= ctAmm ? 0 : ctAmm - reservedDs;

    uint256 psmPa;
    uint256 psmRa;

    if (ctAttributedToPa != 0) {
        (psmPa, psmRa) = PsmLibrary.lvRedeemRaPaWithCt(self, ctAttributedToPa, dsId);
    }

    psmRa += redeemAmount;
```

