Precompile call limit bypass via uint8 counter wraparound in x/vm StateDB
Description
Cosmos EVM limits the number of stateful precompile calls per transaction to 20, because each precompile call allocates a cached context. Both the counter and the limit constant were declared as uint8, and the counter was incremented before the limit check. Rejected calls therefore continued to advance the counter, and the 256th call wrapped it to zero, reopening 21 accepted precompile-call slots per 256 attempts. Reverted calls did not restore the counter, so the pattern could be repeated many times within a single transaction.
A single small transaction can combine this wraparound with low-cost malformed ICS20 calls (which advance the counter at near-zero required gas) to reopen arbitrarily many call slots, and each reopened slot can trigger a fresh 25,000,000-gas internal EVM execution whose cost is not charged to the outer transaction when the call fails during a post-execution state flush. In a four-validator test network, one such 917-byte transaction left all validators unable to finalize the block for a measured 125 seconds, and restarting the nodes did not restore liveness because each validator replayed the committed block through the application. This is a chain-halt / liveness failure reachable by any funded account where the ICS20 static precompile and permissionless ERC-20 registration are enabled.
Details
The precompile call limiter tracks stateful precompile calls per transaction with a counter and limit that are both uint8 (types.MaxPrecompileCalls uint8 = 20, StateDB.precompileCallsCounter uint8). StateDB.AddPrecompileFn appended its journal entry and incremented the counter before comparing against the limit, so calls 21 and onward were rejected but the counter kept advancing through 255. The 256th call wrapped 255 + 1 to zero and was accepted, and each additional 256 attempts opened another 21 accepted call slots. Because the precompile journal's Revert restores the multistore, events, and processed-event count but not the counter, reverted calls consumed no permanent state, and the composition could be repeated within one transaction.
Two behaviors made the reopened slots costly. The ICS20 precompile returns zero required gas for input shorter than four bytes, yet such calls still enter native-action setup and reach AddPrecompileFn, so they can advance the blocked counter at minimal cost. And a call involving a permissionlessly registered native ERC-20 executes the token's code under a fresh 25,000,000-gas internal EVM cap before the transfer keeper rejects a syntactically valid but nonexistent IBC v2 client; when a post-execution flush error then propagated, the already-measured internal gas was discarded before being charged to the outer transaction. Each accepted call could therefore execute roughly 24.9 million internal EVM gas while contributing almost nothing to the outer transaction's gas consumption. Because the work is deterministic consensus execution of a committed transaction, restarting validators does not restore liveness: each node replays the same block through the application.
Patches
The fix widens MaxPrecompileCalls and the counter to uint64 and moves the limit check before the increment, so rejected calls can no longer advance or wrap the counter back into the accepted range. The journal revert deliberately does not restore the counter, so reverted precompile calls still consume their quota.
Chain operators must upgrade to v0.6.4 or v0.7.4 (or later).
Workarounds
Disabling the ICS20 static precompile or permissionless ERC-20 registration removes the described route. Enforcing a finite consensus block-gas limit and per-transaction gas-wanted ceiling limits the blast radius but does not fix the counter wraparound. There is no complete workaround short of upgrading.
References
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/