Skip to content
Race condition Audit finding Advanced

Non-deterministic gas consumption due to shared StateDB pointer in bank keeper affecting consensus

The bank keeper holds a shared pointer to the EVM StateDB that is set during read-only operations like gas estimation, and its presence changes gas computation for later bank transactions. This can cause different nodes to compute different gas amounts for the same transaction, leading to consensus failure.

0x41 Follow

Originally disclosed on Code4rena

Submitted by 0x41

An issue exists in Nibiru’s implementation of the bank keeper and its interaction with the EVM’s StateDB. The NibiruBankKeeper maintains a pointer field to StateDB that gets updated during read-only EVM operations (like eth_estimateGas), which then affects the gas computation of subsequent bank transactions.

The issue arises because the StateDB pointer in NibiruBankKeeper is modified during read-only operations, and the presence or absence of this pointer affects program flow in bank operations through nil checks:

func (bk *NibiruBankKeeper) SyncStateDBWithAccount(ctx sdk.Context, acc sdk.AccAddress) {
    // If there's no StateDB set, it means we're not in an EthereumTx.
    if bk.StateDB == nil {
        return
    }
    // ... state updates
}

This can lead to consensus failures as different nodes may compute different gas amounts for the same transaction (depending on if they previously executed a read only query via RPC), which should never happen.

Proof of Concept

The vulnerability can be demonstrated through the following sequence:

  1. Initial state: Execute a bank send transaction and record gas used.
// Initial bank send
sendMsg := banktypes.NewMsgSend(sender, receiver, coins)
gasUsed1 := executeTx(sendMsg) // Records initial gas usage
  1. Trigger a read-only operation that modifies the StateDB pointer.
// This can modify NibiruBankKeeper.StateDB depending on the tx content
client.EstimateGas(ethTx)
  1. Execute the same bank send transaction again.
gasUsed2 := executeTx(sendMsg) // Different gas usage than gasUsed1 because bk.StateDB is no longer nil

The key problematic code is in bank_extension.go:

type NibiruBankKeeper struct {
    bankkeeper.BaseKeeper
    StateDB *statedb.StateDB  // This shared pointer causes the issue
}

func (evmKeeper *Keeper) NewStateDB(
    ctx sdk.Context, txConfig statedb.TxConfig,
) *statedb.StateDB {
    stateDB := statedb.New(ctx, evmKeeper, txConfig)
    evmKeeper.Bank.StateDB = stateDB // Modifies shared state
    return stateDB
}

Recommended mitigation steps

There are several ways to fix this issue:

  1. Clone the StateDB for read-only operations:
func (k Keeper) EstimateGas(ctx sdk.Context, msg core.Message) (uint64, error) {
    originalStateDB := k.Bank.StateDB
    k.Bank.StateDB = originalStateDB.Copy()
    defer func() {
        k.Bank.StateDB = originalStateDB
    }()
    // ... estimation logic
}
  1. Use context to pass StateDB instead of keeping it as a field:
type NibiruBankKeeper struct {
    bankkeeper.BaseKeeper
}

func (bk *NibiruBankKeeper) SyncStateDBWithAccount(
    ctx sdk.Context,
    stateDB *statedb.StateDB,
    acc sdk.AccAddress,
) {
    if stateDB == nil {
        return
    }
    // ... state updates
}
  1. Implement a proper snapshot/restore mechanism:
type BankKeeperState struct {
    stateDB *statedb.StateDB
}

func (bk *NibiruBankKeeper) Snapshot() *BankKeeperState {
    return &BankKeeperState{stateDB: bk.StateDB}
}

func (bk *NibiruBankKeeper) Restore(state *BankKeeperState) {
    bk.StateDB = state.stateDB
}

The solution must ensure:

  • Deterministic gas computation across all nodes.
  • Proper isolation between read-only and state-modifying operations.

Lambda (warden) commented:

It is possible that I am missing something obvious and this is indeed invalid, but I still think that this is (was) a valid critical issue that could have caused consensus failure because of non-deterministic gas usage across nodes. While looking at the current Nibiri code, I even noticed that it was fixed in the meantime here. However, this was in a later version than the frozen code, i.e., the frozen commit still had the issue.

berndartmueller (judge) commented:

@Lambda - After a more comprehensive second look, I agree that this is an issue. This finding was initially dismissed as invalid due to the lack of a comprehensive write-up and PoC.

While this issue has been separately discovered by the sponsor, the code in scope of the audit still contained the flaw. Therefore, it’s still a valid submission, with High severity justified, given that it can cause consensus failures.

k-yang (Nibiru) confirmed and commented:

Addressed by https://github.com/NibiruChain/nibiru/pull/2173.

Nibiru mitigated:

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

Status: Mitigation confirmed.


The original report as published on Code4rena, by 0x41. 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, the bank keeper stores a mutable pointer to StateDB as a field, and read-only operations set it without isolation, so nil checks in bank operations behave differently depending on prior calls.

Impact

Different nodes could compute different gas amounts for the same transaction, potentially causing consensus failure; the report notes the issue was later fixed in a newer version and mitigated in the frozen code.

What the attacker needed

Node executes a read-only EVM query; subsequent bank transaction

The fix

The issue was mitigated by ensuring only one copy of StateDB exists when executing Ethereum transactions, and the sponsor confirmed the mitigation.

How it was found

  1. Execute a bank send transaction and record the gas used.
  2. Trigger a read-only operation such as gas estimation that modifies the StateDB pointer.
  3. Execute the same bank send transaction again and observe different gas usage.

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 blockchain relies on every computer (node) calculating exactly the same result for each transaction. Here, a shared piece of memory was changed by a harmless read-only request, so later transactions could cost different amounts of gas on different nodes. That disagreement can break the chain's agreement on the state.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by 0x41

View all

5 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.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.