# Attackers will steal the reserve from the `Vault` by receiving `ra` in `FlashSwapRouter::__swapDsforRa()`

FlashSwapRouter.__swapDsforRa sends the Ra reserve to the caller instead of the Vault, letting any user drain the Vault's Ra reserve.

- 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/161
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-cork-protocol%3AH-9
- 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:** FlashSwapRouter / Vault (Solidity, UniswapV2 flash swap)
**Root cause:** __flashSwap encodes msg.sender as the caller and __afterFlashswapSell transfers raAttributed to that caller, so the Ra meant to fund the Vault via provideLiquidityWithFlashSwapFee is sent to the user instead.
**Steps:**
1. Call `FlashSwapRouter::swapRaforDs()` triggering `_swapRaforDs()` which sells the reserve via `__swapDsforRa()`.
2. `__flashSwap()` encodes `msg.sender` into `data` and calls `univ2Pair.swap(...)`.
3. Uniswap pair invokes `uniswapV2Call()`, which decodes `caller = msg.sender` and calls `__afterFlashswapSell(self, amount, reserveId, dsId, caller, extraData)`.
4. `__afterFlashswapSell` executes `IERC20(ra).safeTransfer(caller, raAttributed)`, sending the Vault's Ra to the user.
**How found:** Code-reading of the flash swap call chain: traced __swapDsforRa -> __flashSwap -> univ2Pair.swap -> uniswapV2Call -> __afterFlashswapSell and noticed the encoded caller is msg.sender rather than the Vault owner, so the Ra transfer recipient is wrong.
**Exploitation:** Any user calls swapRaforDs(); the flash swap path routes the Ra that should fund the Vault's liquidity fee back to the caller, allowing repeated calls to drain the Vault's Ra reserve.
**Insight:** When a router performs a flash swap on behalf of a pool/vault, verify the recipient encoded in the callback data is the intended beneficiary (owner/vault), not msg.sender; trace every safeTransfer recipient through the callback chain.

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

**FlashSwapRouter __flashSwap and uniswapV2Call sending ra to caller**

```
function __flashSwap(...) internal {
    ...
    bytes memory data = abi.encode(reserveId, dsId, buyDs, msg.sender, extraData);

    univ2Pair.swap(amount0out, amount1out, address(this), data);
}

function uniswapV2Call(address sender, uint256 amount0, uint256 amount1, bytes calldata data) external {
    (Id reserveId, uint256 dsId, bool buyDs, address caller, uint256 extraData) =
        abi.decode(data, (Id, uint256, bool, address, uint256));
    ...
    if (buyDs) {
        ...
    } else {
        uint256 amount = amount0 == 0 ? amount1 : amount0;

        __afterFlashswapSell(self, amount, reserveId, dsId, caller, extraData);
    }
}

function __afterFlashswapSell(...) internal {
    ...
    IERC20(ra).safeTransfer(caller, raAttributed);
```

