# Gas used mismatch in failed contract calls can lead to wrong gas deductions

Nibiru's CallContractWithInput reset the Cosmos tx gas meter to only the failed EVM call's gas, undercounting cumulative gas across bundled EVM messages.

- Payout: Audit contest
- Severity: medium
- Program: Nibiru
- Found by: 0x41
- Disclosed: 2025-02-24
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-11-nibiru-findings/issues/46
- Library page: https://bugbountylibrary.com/r/c4%3A2024-11-nibiru%3AM-03
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:go technique:source-review vuln:accounting-error vuln:dos

**Target:** Cosmos SDK EVM keeper gas accounting (call_contract.go)
**Root cause:** On EVM call failure, the code calls ResetGasMeterAndConsumeGas with only evmResp.GasUsed instead of the cumulative block/transient gas, discarding gas consumed by preceding EVM messages in the same Cosmos tx.
**Steps:**
1. Bundle multiple EVM messages in one Cosmos tx (all evm.MsgEthereumTx via ExtensionOptionsEthereumTx).
2. Let earlier EVM messages consume gas, then make the last EVM message fail.
3. Observe `if evmResp.Failed() { k.ResetGasMeterAndConsumeGas(ctx, evmResp.GasUsed) }` resets the tx gas meter to only the failed msg's gas.
4. Result: block gas meter increased by less gas than actually consumed.
**How found:** Code-reading comparison of the failure path vs success path in call_contract.go: success calls AddToBlockGasUsed then ResetGasMeterAndConsumeGas(blockGasUsed), failure only consumes evmResp.GasUsed, revealing inconsistent gas accounting.
**Insight:** In Cosmos/EVM hybrid chains, audit the gas meter reset semantics: ResetGasMeterAndConsumeGas overwrites the whole tx meter, so any path passing per-message gas instead of cumulative gas silently undercounts and can be exploited to pack more work per tx.

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

**Failed-call gas path in CallContractWithInput**

```
if evmResp.Failed() {
    k.ResetGasMeterAndConsumeGas(ctx, evmResp.GasUsed) // Only consumes failed tx gas
    if strings.Contains(evmResp.VmError, vm.ErrOutOfGas.Error()) {
        err = fmt.Errorf("gas required exceeds allowance (%d)", gasLimit)
        return
    }
    if evmResp.VmError == vm.ErrExecutionReverted.Error() {
        err = fmt.Errorf("VMError: %w", evm.NewRevertError(evmResp.Ret))
        return
    }
    err = fmt.Errorf("VMError: %s", evmResp.VmError)
    return
}
```

**Success path adding block gas used**

```
blockGasUsed, err := k.AddToBlockGasUsed(ctx, evmResp.GasUsed)
// ...
k.ResetGasMeterAndConsumeGas(ctx, blockGasUsed)
```

