Skip to content
Business logic Audit finding Intermediate

Wrong handling of ERC20 denoms in ERC20Keeper::BurnCoins

The BurnCoins function in the ERC20Keeper incorrectly sends the entire amount of coins to the community pool when any coin is an ERC20 denom, instead of only the matching coin. This causes double the intended amount to be sent to the community pool for each ERC20 denom, as the same coins are also burned in the next iteration.

0xAlix2 Follow

Originally disclosed on Code4rena

Submitted by 0xAlix2

MiniEVM is an optimistic rollup consumer chain powered by EVM, it allows users to interact with EVM/solidity contracts through Cosmos. Part of those contracts is ERC20, which allows users to interact with ERC20Factory to create their ERC20s through Cosmos call messages. When doing so, a new ERC20 is created, and of course, an address is generated say ADDRESS, on the Cosmos end this denom/token is saved as evm/ADDRESS (without the 0x prefix), and the corresponding EVM address is saved, this type of denoms are referred to as ERC20Denom. Another type of denoms created is through CreateERC20, these also interact with the factory, but these are saved as just denoms, in other words, if a uinit denom is created it is just saved and referred to as uinit, the Cosmos part handles the part of minting and burning of these.

When MintCoins is called in the ERC20 keeper, it checks if the denom is ERC20Denom, if so, nothing is done, this is because minting should be done by the ERC20 creator/owner. If not, then sudoMint is called, here:

func (k ERC20Keeper) MintCoins(ctx context.Context, addr sdk.AccAddress, amount sdk.Coins) error {
	// ... snip ...

	for _, coin := range amount {
		denom := coin.Denom
		if types.IsERC20Denom(denom) {
			return moderrors.Wrapf(types.ErrInvalidRequest, "cannot mint erc20 coin: %s", coin.Denom)
		}

		// ... snip ...

		inputBz, err := k.ERC20ABI.Pack("sudoMint", evmAddr, coin.Amount.BigInt())
		if err != nil {
			return types.ErrFailedToPackABI.Wrap(err.Error())
		}

		// ... snip ...
	}

	// ... snip ...
}

Similarly, in BurnCoins (here):

func (k ERC20Keeper) BurnCoins(ctx context.Context, addr sdk.AccAddress, amount sdk.Coins) error {
	// ... snip ...

	for _, coin := range amount {
		// if a coin is not generated from 0x1, then send the coin to community pool
		// because we don't have burn capability.
		if types.IsERC20Denom(coin.Denom) {
			if err := k.communityPoolKeeper.FundCommunityPool(ctx, amount, evmAddr.Bytes()); err != nil {
				return err
			}

			continue
		}

		// ... snip ...
	}

	// ... snip ...
}

If the denom is ERC20Denom, it is supposed to be sent to the community pool. However, there’s a huge flaw here, as it is sending the whole amount instead of coin.

If one of the coins returns true for types.IsERC20Denom(coin.Denom), then all the coins are sent to FundCommunityPool, not just the matching one. Then, the next iteration will send the coins again to 0x1 to be burned. If 2 EVM coins are provided, then double of each will be sent to the fund.

Proof of Concept

Add the following test in minievm/x/evm/keeper/erc20_test.go:

func Test_BurnTwice(t *testing.T) {
	ctx, input := createDefaultTestInput(t)
	_, _, addr := keyPubAddr()
	evmAddr := common.BytesToAddress(addr.Bytes())

	erc20Keeper, err := keeper.NewERC20Keeper(&input.EVMKeeper)
	require.NoError(t, err)

	fooContractAddr_1 := deployERC20(t, ctx, input, evmAddr, "foo")
	fooDenom_1, err := types.ContractAddrToDenom(ctx, &input.EVMKeeper, fooContractAddr_1)
	require.NoError(t, err)
	require.Equal(t, "evm/"+fooContractAddr_1.Hex()[2:], fooDenom_1)

	fooContractAddr_2 := deployERC20(t, ctx, input, evmAddr, "bar")
	fooDenom_2, err := types.ContractAddrToDenom(ctx, &input.EVMKeeper, fooContractAddr_2)
	require.NoError(t, err)
	require.Equal(t, "evm/"+fooContractAddr_2.Hex()[2:], fooDenom_2)

	cacheCtx, _ := ctx.CacheContext()
	err = erc20Keeper.MintCoins(cacheCtx, addr, sdk.NewCoins(
		sdk.NewCoin(fooDenom_1, math.NewInt(100)),
		sdk.NewCoin(fooDenom_2, math.NewInt(100)),
	))
	require.Error(t, err)

	mintERC20(t, ctx, input, evmAddr, evmAddr, sdk.NewCoin(fooDenom_1, math.NewInt(100)), false)
	mintERC20(t, ctx, input, evmAddr, evmAddr, sdk.NewCoin(fooDenom_2, math.NewInt(100)), false)

	amount, err := erc20Keeper.GetBalance(ctx, addr, fooDenom_1)
	require.NoError(t, err)
	require.Equal(t, math.NewInt(100), amount)

	amount, err = erc20Keeper.GetBalance(ctx, addr, fooDenom_2)
	require.NoError(t, err)
	require.Equal(t, math.NewInt(100), amount)

	// Community pool should have 0 balance
	require.Equal(t, math.NewInt(0), input.CommunityPoolKeeper.CommunityPool.AmountOf(fooDenom_1))
	require.Equal(t, math.NewInt(0), input.CommunityPoolKeeper.CommunityPool.AmountOf(fooDenom_2))

	// Burn 50 coins of each denom
	err = erc20Keeper.BurnCoins(ctx, addr, sdk.NewCoins(
		sdk.NewCoin(fooDenom_1, math.NewInt(50)),
		sdk.NewCoin(fooDenom_2, math.NewInt(50)),
	))
	require.NoError(t, err)

	// Community pool should have 100 (50*2) coins of each denom
	require.Equal(t, math.NewInt(100), input.CommunityPoolKeeper.CommunityPool.AmountOf(fooDenom_1))
	require.Equal(t, math.NewInt(100), input.CommunityPoolKeeper.CommunityPool.AmountOf(fooDenom_2))
}

Recommended mitigation steps

minievm/x/evm/keeper/erc20.go:

	// BurnCoins implements IERC20Keeper.
	func (k ERC20Keeper) BurnCoins(ctx context.Context, addr sdk.AccAddress, amount sdk.Coins) error {
		// ... snip ...

		for _, coin := range amount {
			// if a coin is not generated from 0x1, then send the coin to community pool
			// because we don't have burn capability.
			if types.IsERC20Denom(coin.Denom) {
-				if err := k.communityPoolKeeper.FundCommunityPool(ctx, amount, evmAddr.Bytes()); err != nil {
+				if err := k.communityPoolKeeper.FundCommunityPool(ctx, sdk.NewCoins(coin), evmAddr.Bytes()); err != nil {
					return err
				}

				continue
			}

			// ... snip ...
		}

		return nil
	}

leojin (Initia) confirmed and commented:

We fixed in this PR.

Initia mitigated:

PR addresses burn coins error.

Status: Mitigation confirmed. Full details in reports from berndartmueller, Franfran and 0xAlix2.


The original report as published on Code4rena, by 0xAlix2. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 code passes the entire 'amount' slice to FundCommunityPool instead of the single 'coin' being processed.

Impact

The community pool receives double the intended amount of ERC20 tokens, leading to an incorrect total supply and potential inflation of the community pool's balance.

What the attacker needed

Ability to call BurnCoins with multiple ERC20 denoms

The fix

The code was fixed to pass only the current coin to FundCommunityPool instead of the entire amount.

How it was found

  1. Deploy two ERC20 tokens and mint 100 of each to an address.
  2. Call BurnCoins with 50 of each denom.
  3. Observe that the community pool receives 100 of each denom instead of 50.

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 you burn tokens, the system is supposed to send only the ERC20 tokens to a community pool and burn the rest. But a mistake makes it send all tokens to the pool, and then also burn them, so the pool gets twice as many tokens as it should. This means the total supply of those tokens becomes incorrect.

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.

More by 0xAlix2

View all

39 more disclosed reports, best first.

More from Initia Cosmos

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.