# Wrong accounting of locked RA when repurchasing DS+PA with RA

Cork PSM repurchase double-counts the fee's RA in locked balance, inflating ra.locked and over-distributing rewards, leaving last redeemers unable to withdraw.

- Payout: Audit contest
- Severity: high
- 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/155
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-cork-protocol%3AH-7
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:sherlock sev:high tech:solidity technique:source-review vuln:accounting-error vuln:reward-distribution

**Target:** PsmLib.repurchase / PSM RA locked accounting (Solidity)
**Root cause:** repurchase() locks the full RA amount including the fee via lockUnchecked, then provideLiquidityWithFee mints CT+DS from that fee and unsafeIssueToLv increments ra.locked again by the CT amount, double-counting the fee.
**Steps:**
1. PSM starts with 1000 RA locked.
2. Alice repurchases 100 DS+PA providing 100 RA; 5% fee = 5 RA.
3. repurchase() calls `self.psm.balances.ra.lockUnchecked(amount, buyer)` locking all 100 RA -> locked = 1100.
4. provideLiquidityWithFee -> __provideLiquidityWithRatio -> __provideLiquidity mints CT+DS from ~3 of the 5 fee RA.
5. unsafeIssueToLv calls `self.psm.balances.ra.incLocked(amount)` adding 3 more -> locked = 1103 while real RA balance is 1100.
6. Observe ra.locked exceeds actual RA held; rewards computed from locked are inflated.
**How found:** Code review of repurchase() flow: noticed lockUnchecked accounts the full amount including fee, then traced provideLiquidityWithFee -> unsafeIssueToLv which increments locked again for the same fee RA.
**Exploitation:** Any user repurchasing DS+PA with RA triggers the double-count; repeated repurchases inflate ra.locked relative to real RA, so reward distribution overpays early redeemers and leaves insufficient RA for later redeemers.
**Escalation:** Repeated repurchases amplify the discrepancy; first withdrawers drain funds, last withdrawers cannot redeem.
**Insight:** When a fee is carved from user-supplied funds and then re-minted into LP tokens, check whether the same value is counted in locked/accounting twice across the deposit and liquidity-provision paths.

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

**repurchase locks RA unchecked then provides liquidity with fee**

```
self.psm.balances.ra.lockUnchecked(amount, buyer);

        // transfer user attrubuted DS + PA
        // PA
        (, address pa) = self.info.underlyingAsset();
        IERC20(pa).safeTransfer(buyer, received);

        // DS
        IERC20(ds._address).transfer(buyer, received);

        // Provide liquidity
        VaultLibrary.provideLiquidityWithFee(self, fee, flashSwapRouter, ammRouter);
```

**unsafeIssueToLv increments locked RA by CT amount**

```
function unsafeIssueToLv(State storage self, uint256 amount) internal {
        uint256 dsId = self.globalAssetIdx;

        DepegSwap storage ds = self.ds[dsId];

        self.psm.balances.ra.incLocked(amount);

        ds.issue(address(this), amount);
```

