UpdateSystemContract is not copying gasPriceByChainId state variable to the new upgraded which will halt ZRC20 token withdraw until system contract is updated accordingly
The UpdateSystemContract function fails to copy the gasPriceByChainId state variable to the new system contract, causing ZRC20 token withdrawals to revert until gas prices are manually updated. An attacker cannot directly exploit this, but it causes a temporary denial of service for withdrawals.
Submitted by dontonka
Zeta protocol allow to update SystemContract.sol using UpdateSystemContract function which require Admin Group 2 access (so the highest access).
There is a problem in this function which I consider Medium severity as it doesn’t update one key state variable gasPriceByChainId during the upgrade.
As shown in the code snippet below, it does update the two other state variable gasCoinZRC20ByChainId and gasZetaPoolByChainId by calling respectively setGasCoinZRC20 and setGasZetaPool, but missing the call to setGasPrice.
...
foreignCoins := k.GetAllForeignCoins(ctx)
tmpCtx, commit := ctx.CacheContext()
for _, fcoin := range foreignCoins {
zrc20Addr := ethcommon.HexToAddress(fcoin.Zrc20ContractAddress)
if zrc20Addr == (ethcommon.Address{}) {
k.Logger(ctx).Error("invalid zrc20 contract address", "address", fcoin.Zrc20ContractAddress)
continue
}
_, err = k.CallEVM(tmpCtx, *zrc20ABI, types.ModuleAddressEVM, zrc20Addr, BigIntZero, nil, true, false, "updateSystemContractAddress", newSystemContractAddr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call zrc20 contract method updateSystemContractAddress (%s)", err.Error())
}
if fcoin.CoinType == common.CoinType_Gas {
_, err = k.CallEVM(tmpCtx, *sysABI, types.ModuleAddressEVM, newSystemContractAddr, BigIntZero, nil, true, false, "setGasCoinZRC20", big.NewInt(fcoin.ForeignChainId), zrc20Addr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call system contract method setGasCoinZRC20 (%s)", err.Error())
}
_, err = k.CallEVM(tmpCtx, *sysABI, types.ModuleAddressEVM, newSystemContractAddr, BigIntZero, nil, true, false, "setGasZetaPool", big.NewInt(fcoin.ForeignChainId), zrc20Addr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call system contract method setGasZetaPool (%s)", err.Error())
}
}
}
...
Impact
UpdateSystemContract doesn’t update gasPriceByChainId state variable on the new system contract instance which will have a temporary and unexpected negative impact after a system contract update as withdraw on all ZRC20 token will revert, so halted, until the situation is mitigated by Observers calling GasPriceVoter on all the supported chains.
Proof of Concept
I added the following unit test inside TestKeeper_UpdateSystemContract, which confirm that gasPriceByChainId is not being copied on the new system contract, as the last line of the test fail because require.Equal(t, big.NewInt(41), queryGasPrice(chainID1, contract)) since the amount returned is 0, instead of 41.
t.Run("can update the system contract including gas price XD", func(t *testing.T) {
k, ctx, sdkk, zk := keepertest.FungibleKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin, observertypes.Policy_Type_group2)
queryZRC20SystemContract := func(contract common.Address) string {
abi, err := zrc20.ZRC20MetaData.GetAbi()
require.NoError(t, err)
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, contract, keeper.BigIntZero, nil, false, false, "SYSTEM_CONTRACT_ADDRESS")
require.NoError(t, err)
unpacked, err := abi.Unpack("SYSTEM_CONTRACT_ADDRESS", res.Ret)
require.NoError(t, err)
address, ok := unpacked[0].(common.Address)
require.True(t, ok)
return address.Hex()
}
queryGasPrice := func(chainID *big.Int, contract common.Address) *big.Int {
abi, err := systemcontract.SystemContractMetaData.GetAbi()
require.NoError(t, err)
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, contract, keeper.BigIntZero, nil, false, false, "gasPriceByChainId", chainID)
require.NoError(t, err)
unpacked, err := abi.Unpack("gasPriceByChainId", res.Ret)
require.NoError(t, err)
gasPrice, ok := unpacked[0].(*big.Int)
require.True(t, ok)
return gasPrice
}
verifyState := func(contract common.Address, chainID1, chainID2 *big.Int, gas1, gas2 common.Address) {
foundGas1, err := k.QuerySystemContractGasCoinZRC20(ctx, chainID1)
require.NoError(t, err)
require.Equal(t, gas1, foundGas1)
foundGas2, err := k.QuerySystemContractGasCoinZRC20(ctx, chainID2)
require.NoError(t, err)
require.Equal(t, gas2, foundGas2)
require.Equal(t, contract.Hex(), queryZRC20SystemContract(gas1))
require.Equal(t, contract.Hex(), queryZRC20SystemContract(gas2))
require.Equal(t, big.NewInt(41), queryGasPrice(chainID1, contract))
require.Equal(t, big.NewInt(52), queryGasPrice(chainID2, contract))
}
chains := zetacommon.DefaultChainsList()
require.True(t, len(chains) > 1)
require.NotNil(t, chains[0])
require.NotNil(t, chains[1])
chainID1 := chains[0].ChainId
chainID2 := chains[1].ChainId
wzeta, factory, router, _, oldSystemContract := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper)
gas1 := setupGasCoin(t, ctx, k, sdkk.EvmKeeper, chainID1, "foo", "foo")
gas2 := setupGasCoin(t, ctx, k, sdkk.EvmKeeper, chainID2, "bar", "bar")
// setupGasCoin is not filling gasPriceByChainId, doing it manually as follow
k.SetGasPrice(ctx, big.NewInt(chainID1), big.NewInt(41))
k.SetGasPrice(ctx, big.NewInt(chainID2), big.NewInt(52))
// verify everything is good on the current system contract.
verifyState(oldSystemContract, big.NewInt(chainID1), big.NewInt(chainID2), gas1, gas2)
// deploy a new system contracts
newSystemContract, err := k.DeployContract(ctx, systemcontract.SystemContractMetaData, wzeta, factory, router)
require.NoError(t, err)
require.NotEqual(t, oldSystemContract, newSystemContract)
// can update the system contract
_, err = msgServer.UpdateSystemContract(ctx, types.NewMsgUpdateSystemContract(admin, newSystemContract.Hex()))
require.NoError(t, err)
// can retrieve the system contract
sc, found := k.GetSystemContract(ctx)
require.True(t, found)
require.Equal(t, newSystemContract.Hex(), sc.SystemContract)
// verify everything is good on the new system contract.
verifyState(newSystemContract, big.NewInt(chainID1), big.NewInt(chainID2), gas1, gas2)
})
Test Output
=== RUN TestKeeper_UpdateSystemContract
--- FAIL: TestKeeper_UpdateSystemContract (0.07s)
=== RUN TestKeeper_UpdateSystemContract/can_update_the_system_contract_including_gas_price_XD
msg_server_update_system_contract_test.go:61:
Error Trace: /mnt/c/IX/GitProjects/platform-tools/ETH_course/C4/2023-11-zetachain/repos/node/x/fungible/keeper/msg_server_update_system_contract_test.go:61
/mnt/c/IX/GitProjects/platform-tools/ETH_course/C4/2023-11-zetachain/repos/node/x/fungible/keeper/msg_server_update_system_contract_test.go:98
Error: Not equal:
expected: 41
actual : 0
Diff:
--- Expected
+++ Actual
@@ -2,4 +2,3 @@
neg: (bool) false,
- abs: (big.nat) (len=1) {
- (big.Word) 41
+ abs: (big.nat) {
}
Test: TestKeeper_UpdateSystemContract/can_update_the_system_contract_including_gas_price_XD
--- FAIL: TestKeeper_UpdateSystemContract/can_update_the_system_contract_including_gas_price_XD (0.07s)
Now that this is confirmed, if we examine ZRC20::withdraw it will first call withdrawGasFee which will then call ISystem(SYSTEM_CONTRACT_ADDRESS).gasPriceByChainId(CHAIN_ID) which will return 0 as proven already by the unit test. As we can see after the condition will make this transaction revert as gasPrice == 0.
function withdrawGasFee() public view override returns (address, uint256) {
address gasZRC20 = ISystem(SYSTEM_CONTRACT_ADDRESS).gasCoinZRC20ByChainId(CHAIN_ID);
if (gasZRC20 == address(0)) {
revert ZeroGasCoin();
}
uint256 gasPrice = ISystem(SYSTEM_CONTRACT_ADDRESS).gasPriceByChainId(CHAIN_ID);
if (gasPrice == 0) {
revert ZeroGasPrice();
}
uint256 gasFee = gasPrice * GAS_LIMIT + PROTOCOL_FLAT_FEE;
return (gasZRC20, gasFee);
}
Recommended Mitigation Steps
I would recommend to fix the problem in UpdateSystemContract to include gasPriceByChainId as follow. These changes will make the unit test pass.
func (k msgServer) UpdateSystemContract(goCtx context.Context, msg *types.MsgUpdateSystemContract) (*types.MsgUpdateSystemContractResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if msg.Creator != k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_group2) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Deploy can only be executed by the correct policy account")
}
newSystemContractAddr := ethcommon.HexToAddress(msg.NewSystemContractAddress)
if newSystemContractAddr == (ethcommon.Address{}) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid system contract address (%s)", msg.NewSystemContractAddress)
}
// update contracts
zrc20ABI, err := zrc20.ZRC20MetaData.GetAbi()
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrABIGet, "failed to get zrc20 abi")
}
sysABI, err := systemcontract.SystemContractMetaData.GetAbi()
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrABIGet, "failed to get system contract abi")
}
+
+ sys, found := k.GetSystemContract(ctx)
+ if !found {
+ k.Logger(ctx).Error("system contract not found")
+ }
+ oldSystemContractAddress := sys.SystemContract
+ oldSystemContractAddr := ethcommon.HexToAddress(oldSystemContractAddress)
+ if oldSystemContractAddr == (ethcommon.Address{}) {
+ k.Logger(ctx).Error("invalid system contract address", "address", oldSystemContractAddress)
+ return nil, sdkerrors.Wrapf(types.ErrContractCall, "invalid system contract (%s)", err.Error())
+ }
+
foreignCoins := k.GetAllForeignCoins(ctx)
tmpCtx, commit := ctx.CacheContext()
for _, fcoin := range foreignCoins {
zrc20Addr := ethcommon.HexToAddress(fcoin.Zrc20ContractAddress)
if zrc20Addr == (ethcommon.Address{}) {
k.Logger(ctx).Error("invalid zrc20 contract address", "address", fcoin.Zrc20ContractAddress)
continue
}
_, err = k.CallEVM(tmpCtx, *zrc20ABI, types.ModuleAddressEVM, zrc20Addr, BigIntZero, nil, true, false, "updateSystemContractAddress", newSystemContractAddr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call zrc20 contract method updateSystemContractAddress (%s)", err.Error())
}
if fcoin.CoinType == common.CoinType_Gas {
_, err = k.CallEVM(tmpCtx, *sysABI, types.ModuleAddressEVM, newSystemContractAddr, BigIntZero, nil, true, false, "setGasCoinZRC20", big.NewInt(fcoin.ForeignChainId), zrc20Addr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call system contract method setGasCoinZRC20 (%s)", err.Error())
}
_, err = k.CallEVM(tmpCtx, *sysABI, types.ModuleAddressEVM, newSystemContractAddr, BigIntZero, nil, true, false, "setGasZetaPool", big.NewInt(fcoin.ForeignChainId), zrc20Addr)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call system contract method setGasZetaPool (%s)", err.Error())
}
+
+ res, err := k.CallEVM(ctx, *sysABI, types.ModuleAddressEVM, oldSystemContractAddr, BigIntZero, nil, false, false, "gasPriceByChainId", big.NewInt(fcoin.ForeignChainId))
+ if err != nil {
+ return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call system contract method gasPriceByChainId (%s)", err.Error())
+ }
+ unpacked, err := sysABI.Unpack("gasPriceByChainId", res.Ret)
+ if err != nil {
+ return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to unpack gasPriceByChainId response (%s)", err.Error())
+ }
+ gasPrice, ok := unpacked[0].(*big.Int)
+ if !ok {
+ return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to convert gasPriceByChainId response (%s)", err.Error())
+ }
+ _, err = k.CallEVM(tmpCtx, *sysABI, types.ModuleAddressEVM, newSystemContractAddr, BigIntZero, nil, true, false, "setGasPrice", big.NewInt(fcoin.ForeignChainId), gasPrice)
+ if err != nil {
+ return nil, sdkerrors.Wrapf(types.ErrContractCall, "failed to call system contract method setGasZetaPool (%s)", err.Error())
+ }
}
}
- sys, found := k.GetSystemContract(ctx)
- if !found {
- k.Logger(ctx).Error("system contract not found")
- }
- oldSystemContractAddress := sys.SystemContract
sys.SystemContract = newSystemContractAddr.Hex()
k.SetSystemContract(ctx, sys)
err = ctx.EventManager().EmitTypedEvent(
&types.EventSystemContractUpdated{
MsgTypeUrl: sdk.MsgTypeURL(&types.MsgUpdateSystemContract{}),
NewContractAddress: msg.NewSystemContractAddress,
OldContractAddress: oldSystemContractAddress,
Signer: msg.Creator,
},
)
if err != nil {
k.Logger(ctx).Error("failed to emit event", "error", err.Error())
return nil, sdkerrors.Wrapf(types.ErrEmitEvent, "failed to emit event (%s)", err.Error())
}
commit()
return &types.MsgUpdateSystemContractResponse{}, nil
}
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 UpdateSystemContract function does not call setGasPrice for each gas coin, so the new system contract's gasPriceByChainId mapping remains zero.
Impact
After a system contract update, all ZRC20 token withdrawals revert until observers manually update gas prices, causing a temporary denial of service.
What the attacker needed
Admin group 2 access; System contract upgrade
The fix
The UpdateSystemContract function should be modified to read the gas price from the old system contract and set it on the new one using setGasPrice.
How it was found
- Deploy a new system contract.
- Call UpdateSystemContract with the new contract address.
- Attempt to withdraw ZRC20 tokens; the transaction reverts due to zero gas price.
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.
When the system contract is upgraded, some settings are copied to the new contract, but one important setting (the gas price for each chain) is missed. This means that after an upgrade, trying to withdraw ZRC20 tokens fails because the contract thinks the gas price is zero. The issue is fixed by also copying the gas price during the upgrade.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.