# Inconsistent state management: ethereumTx StateDB overriding CallContract results

FunToken precompile's sendToBank state changes were lost when StateDB committed stale evmTxCtx, letting attacker double-spend ERC20 and mint unbacked Nibi.

- Payout: Audit contest
- Severity: high
- Program: Nibiru
- Found by: 0x007
- Disclosed: 2025-02-24
- Write-up quality: 8/10
- Original: https://github.com/code-423n4/2024-11-nibiru-findings/issues/24
- Library page: https://bugbountylibrary.com/r/c4%3A2024-11-nibiru%3AH-05
- Tags: area:web3 domain:web3 impact:funds-loss impact:integrity kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:business-logic

**Target:** EVM precompile / StateDB (FunToken sendToBank)
**Root cause:** Multiple StateDBs are created in ApplyEvmMsg but reset to the original near the end of the precompile call, losing intermediate storage changes; getStateObject always reads evmTxCtx instead of the newer cacheCtx.
**Steps:**
1. Start localnet via `just localnet`
2. Create FunToken: `nibid tx evm create-funtoken --bank-denom unibi --from validator ...`
3. Mint Nibi to EVM: `nibid tx evm convert-coin-to-evm <addr> 1000000000000unibi ...`
4. Deploy POC24 contract holding FunToken ERC20 balance
5. Call `attack()`: `funToken.sendToBank(erc20, balance, recipient)` then `erc20.transfer(0x...dEaD, 1)`
6. Observe contract still holds 999.999999 ERC20 while recipient bank balance increased by 1000 Nibi
**How found:** Code review of StateDB.Commit and precompile context caching; noticed evmTxCtx lags cacheCtx and getStateObject reads evmTxCtx, hypothesized precompile state changes get overwritten, then built a localnet PoC to confirm.
**Exploitation:** Attacker converts Nibi to FunToken ERC20, calls sendToBank to mint Nibi to a recipient while the ERC20 balance reduction is overwritten by statedb.commitCtx from stale evmTxCtx, then transfers the still-present ERC20 balance, double-spending.
**Escalation:** Could extend to unlimited FunToken minting, arbitrary contract state corruption via malicious ERC20 callbacks, and factory nonce reuse locking deployments.
**Insight:** When a precompile mutates state via a cached context, check whether the outer StateDB's getStateObject reads the stale tx context rather than the cache; dirty a storage slot after the precompile call to force the stale value to be committed.

## Proof of concept (copied exactly from the original report)

**Localnet setup commands for 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
````

