# Users will steal excess funds from the Vault due to `VaultPoolLib::redeem()` not always decreasing `self.withdrawalPool.raBalance` and `self.withdrawalPool.paBalance`

VaultPoolLib::redeem() fails to decrement withdrawalPool ra/pa balances when redeeming excess from AMM pool, double-counting funds and letting users drain the Vault.

- 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/144
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-cork-protocol%3AH-6
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Vault/VaultPoolLib redemption logic (Solidity)
**Root cause:** __tryRedeemExcessFromAmmPool() calls the view-only __tryRedeemfromWithdrawalPool() instead of __redeemfromWithdrawalPool(), so withdrawalPool.raBalance/paBalance are never decremented when excess is redeemed from the AMM pool.
**Steps:**
1. User calls `Vault::redeemExpiredLv()` with `userEligible < amount`, triggering `VaultPoolLib::redeem()` -> `__redeemExcessFromAmmPool()`.
2. Observe only `self.ammLiquidityPool.balance` decreases; `self.withdrawalPool.raBalance` and `paBalance` remain unchanged.
3. New issuance calls `VaultPoolLib::reserve()`, which recomputes exchange rates from the stale (un-decremented) withdrawal pool balances.
4. `raExchangeRate`/`paExchangeRate` are inflated, letting users redeem more than their share and draining the Vault.
**How found:** Code review of the redemption path: noticed __tryRedeemExcessFromAmmPool uses the view-only __tryRedeemfromWithdrawalPool variant, so state balances are not updated, then traced how reserve() consumes those stale balances.
**Exploitation:** Attacker redeems a partial amount after expiry so excess is pulled from the AMM pool without decrementing withdrawal pool accounting; on next issuance the inflated exchange rates let them (and others) withdraw more than entitled, leaving honest users unable to withdraw.
**Insight:** When a library has paired 'try' (view) and state-mutating variants of the same accounting function, check every call site uses the mutating one wherever balances must be decremented; stale accounting surfaces later in exchange-rate recomputation.

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

**Vulnerable __tryRedeemExcessFromAmmPool not decreasing withdrawal pool balances**

```
function __tryRedeemExcessFromAmmPool(VaultPool storage self, uint256 amountAttributed, uint256 excessAmount)
    internal
    view
    returns (uint256 ra, uint256 pa, uint256 withdrawnFromAmm)
{
    (ra, pa) = __tryRedeemfromWithdrawalPool(self, amountAttributed);

    withdrawnFromAmm =
        MathHelper.calculateRedeemAmountWithExchangeRate(excessAmount, self.withdrawalPool.raExchangeRate); //@audit PA is ignored here

    ra += withdrawnFromAmm;
}
```

