Internal EVM gas dropped on StateDB commit errors in EVM keeper
Description
Cosmos EVM lets native modules (such as x/erc20 during MsgConvertERC20 handling) invoke EVM contracts through CallEVMWithData, which runs the call with a 25,000,000-gas internal cap and charges the consumed EVM gas to the Cosmos transaction gas meter only after ApplyMessage returns successfully. If EVM execution succeeds but the subsequent StateDB.Commit() step fails, ApplyMessage returns an error before the response containing GasUsed is built, so CallEVMWithData returns without calling ConsumeGas and the already-performed EVM work is never charged. An unprivileged user can trigger this path deterministically using the permissionless ERC-20 conversion module and repeat it cheaply, since transaction rollback restores the trigger state. In a reproduced four-validator test, a block of such undercharged transactions stalled block production for over 160 seconds per validator, demonstrating a chain-halt/liveness failure at roughly 100 times less charged gas than the work performed.
Details
Keeper.CallEVMWithData (x/vm/keeper/call_evm.go) constructs an internal EVM message with gasLimit set to config.DefaultGasCap (25,000,000) and calls ApplyMessage, then only on success charges the result with ctx.GasMeter().ConsumeGas(res.GasUsed, "apply evm message"). In ApplyMessageWithConfig (x/vm/keeper/state_transition.go), the EVM runs first, temporaryGasUsed/maxUsedGas are computed, and the response carrying GasUsed is only constructed after a successful stateDB.Commit() (or stateDB.FlushToCacheCtx() in the precompile path). If the commit/flush fails, ApplyMessage returns nil, err, CallEVMWithData returns before ConsumeGas, and the ~25M gas of EVM work already performed is never charged to the Cosmos gas meter.
A deterministic commit error is reachable via the default-enabled x/erc20 module: MsgConvertERC20 invokes the converted token's transfer() through CallEVMWithData. Because ERC-20 registration is permissionless by default, an attacker can register a contract whose transfer() burns nearly the entire 25M internal cap and then sends one wei to a module account, e.g. the x/erc20 module address. StateDB.Commit (x/vm/keeper/statedb.go) rejects balance changes to module accounts with "is not allowed to receive funds", which is a commit-time error rather than an EVM revert. The EVM call succeeds, the commit fails, the gas is dropped, and the transaction fails and rolls back state — restoring the one wei — so the same trigger is reusable from a fresh account sequence.
Because the reference evmd application disables the block gas meter, uses the EVM application mempool (no transaction-count cap), and the consensus limit is byte-based (max_bytes), block admission is governed by encoded transaction size rather than the hidden EVM work. A reproduction on a four-validator v0.7.3 localnet showed 1,600 such 512-byte transactions (declaring 250,000 Cosmos gas each) concentrated in a single block forced every validator to spend roughly 158–163 seconds applying the block, producing a 163-second no-new-block interval versus under 6 seconds for a matched control block; the network recovered once the finite batch cleared.
Patches
The fix caps the internal EVM call's gas limit at the gas remaining on the transaction's Cosmos gas meter in CallEVMWithData, and, in ApplyMessageWithConfig, charges the full maxUsedGas to the Cosmos gas meter on the StateDB.Commit() and FlushToCacheCtx() error paths (without refund, since refunds are only earned when state changes actually commit).
Chain operators must upgrade to v0.7.4 or v0.6.4 (or later).
Workarounds
There is no effective workaround short of upgrading. Restricting permissionless ERC-20 registration (disabling EnableErc20 or PermissionlessRegistration) removes the demonstrated trigger but does not fix the underlying gas-accounting flaw, which any code path reaching a failing post-execution StateDB.Commit() can exercise.
References
- Releases: v0.7.4
Credits
Originally reported through the Cosmos bug bounty program by sl90.
Please reach out to our security email, security@cosmoslabs.io to get further details, or via our bug bounty program immunefi.com/