# State changes are overwritten during anchor serialization when two accounts are the same

In Woofi Solana swap, when quote pool equals from/to pool, Anchor serialization overwrites reserve updates, corrupting quote pool accounting.

- Payout: Audit contest
- Severity: medium
- Program: Woofi Solana Deployment
- Found by: S3v3ru5
- Disclosed: 2024-08-26
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-woofi-solana-deployment-judging/issues/73
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-woofi-solana-deployment%3AM-3
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:rust tech:solana technique:source-review vuln:accounting-error

**Target:** Solana swap instruction / WooPool account state
**Root cause:** swap passes woopool_quote as the same account as woopool_from or woopool_to; Anchor deserializes each Account into separate in-memory copies and serializes them back sequentially, so the last-written (quote) copy overwrites the earlier reserve update.
**Steps:**
1. Call swap with woopool_to == woopool_quote (base-to-quote) or woopool_from == woopool_quote (quote-to-base).
2. Instruction adds from_amount to from reserve, subtracts to_amount from to reserve, subtracts swap_fee from quote reserve, adds swap_fee to unclaimed fee.
3. Anchor serializes woopool_to then woopool_quote; since same account, quote copy overwrites to-pool changes.
4. Observe via test: USDC pool reserve goes 200000 -> 195769 (only fee deducted), missing to_amount=136775 deduction.
**How found:** Researcher reasoned about Anchor's #[derive(Accounts)] deserialize-modify-serialize model (analogous to Solidity memory copies) and noticed swap accepts three pool accounts that can alias, then confirmed with a test printing reserve before/after.
**Exploitation:** Any user performing a base-to-quote or quote-to-base swap triggers the overwrite; no capital or special privileges needed. Repeated swaps progressively desynchronize quote pool reserve from actual vault balance, blocking valid swaps and admin withdraw/claim_fee.
**Insight:** On Solana/Anchor, when an instruction takes multiple Account<'info,T> of the same type, check whether callers can pass the same pubkey for two of them; each gets its own in-memory copy and the last serialized write silently discards earlier mutations.

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

**Test printing toPool reserve before/after swap**

```
let toPoolDataBefore = null;
      try {
        toPoolDataBefore = await program.account.wooPool.fetch(toPoolParams.woopool);
      } catch (e) {
          console.log(e);
          console.log("fetch failed");
          return;
      }
      console.log("toPool reserve before swap:" + toPoolDataBefore.reserve);
      console.log("toPool unclaimed fee before swap:" + toPoolDataBefore.unclaimedFee);

and Add the following at line 357 at the end of the that test (after `swap` is called`:
        let toPoolDataAfter = null;
        try {
          toPoolDataAfter = await program.account.wooPool.fetch(toPoolParams.woopool);
        } catch (e) {
            console.log(e);
            console.log("fetch failed");
            return;
        }
        console.log("toPool reserve after swap:" + toPoolDataAfter.reserve);
        console.log("toPool unclaimed fee after swap:" + toPoolDataAfter.unclaimedFee);
```

**Test output showing reserve only reduced by swapFee**

```
toPool reserve before swap:200000
toPool unclaimed fee before swap:0
price - 14597424250
feasible - 1
price - 0
feasible - 0
toAmount:136775
swapFee:4231
toTokenAccount amount:136775
toTokenAccount decimals:6
toPool reserve after swap:195769
toPool unclaimed fee after swap:4231
```

