Skip to main content
Menu
Advisories

Non-atomic StateDB commit

GHSA-367M-G444-9MG3Published Updated GitHub

Description

Non-atomic StateDB commit

Impact

StateDB.Commit persists dirty accounts one at a time, in lexicographic address order, writing each directly to the transaction context. If a later write fails, it returns an error but does not roll back the accounts already written, so a "failed" commit can leave part of its state saved. Normally the outer SDK transaction error would discard these partial writes, but the x/erc20 IBC middleware's OnAcknowledgementPacket refund callback catches the reconversion error and returns nil, letting them persist, an attacker can then mint spendable native balance from nothing.

A transfer is two writes (debit sender, credit receiver). Because StateDB.Commit orders writes by address and never undoes earlier ones, an attacker can arrange for the credit to persist while the offsetting debit is skipped.

Exploit path:

  1. Permissionlessly deploy and register a malicious native ERC20.
  2. Send its tokens over ICS20 to an invalid receiver so the transfer fails and refunds.
  3. The ERC20 transfer hook sends native value first to an attacker address, then to the blocked x/erc20 module address, so commit order is: attacker credit → blocked module (error) → token-contract debit/storage
  4. The attacker credit is written first; commit then fails on the blocked module account and never reaches the contract's debit, so the credit stands with no matching debit.
  5. The default ERC20 IBC refund callback catches the reconversion error and returns nil, so the signed MsgAcknowledgement succeeds and BaseApp commits the orphaned credit.

Patches

Commit now writes everything to a scratch copy of the store first, and only publishes it once the whole set has been written successfully:

func (s *StateDB) Commit() error {
	if s.writeCache != nil {
		// fold the remaining dirty set into the precompile cache so the
		// state changes are atomic.
		if err := s.commitWithCtx(s.cacheCtx); err != nil {
			return err
		}
		s.writeCache()
		return nil
	}

	// stage writes here so a late failure leaves s.ctx untouched.
	cacheCtx, writeCache := s.ctx.CacheContext()
	if err := s.commitWithCtx(cacheCtx); err != nil {
		return err
	}
	writeCache()
	return nil
}

The following fix is unrelated makes an overflowing balance fail loudly instead of silently wrapping, matching what SubBalance already did for underflow (x/vm/statedb/state_object.go):

newBalance, overflowed := new(uint256.Int).AddOverflow(s.Balance(), amount)
if overflowed {
	panic(fmt.Sprintf(
		"state balance overflow for %s: have=%s add=%s",
		s.address.Hex(),
		s.Balance().String(),
		amount.String(),
	))
}
return s.SetBalance(newBalance)

Upgrading

Upgrade to one of the patched hotfix tags — v0.6.3 or v0.7.3

Workarounds

There are none.

References

  • Cosmos EVM coordinated vulnerability disclosure policy

Credits

Originally reported through the Cosmos bug bounty program by @bluethroat_labs

Please reach out to our security email, security@cosmoslabs.io to get further details, or via our bug bounty program immunefi.com/bug-bounty/cosmos to report known vulnerabilities.

Affected versions

References

GitHub · cosmos/evm