Gas refunds use block gas instead of transaction gas, leading to incorrect refund amounts
The gas refund calculation used cumulative block gas instead of the transaction's own gas usage, so users received smaller refunds than they should. An attacker could not directly exploit this, but users would lose funds when multiple transactions were included in a block.
0x41 Follow with ABAIKUNANBAEV and Sentryx
Originally disclosed on Code4rena
Submitted by 0x41, also found by ABAIKUNANBAEV and Sentryx
There is a mismatch between how gas fees are deducted and refunded in the EVM implementation:
- In
evmante_gas_consume.go, gas fees are deducted upfront based on each transaction’s individual gas limit. - However, the refund calculation in
msg_server.gouses the cumulative block gas usage to determine refunds for individual transactions.
This mismatch means users will receive incorrect (lower) refunds than they should. The gas refund should be based on the difference between a transaction’s gas limit (what was charged) and its actual gas usage (what was consumed), not the block’s total gas usage.
The impact is that users will lose funds as they receive smaller refunds than they should. This becomes especially problematic when multiple transactions are included in a block, as the cumulative block gas increases with each transaction, reducing refunds for subsequent transactions.
Proof of Concept
The issue stems from two pieces of code:
- Gas fees are deducted in
evmante_gas_consume.gobased on transaction gas limit:
fees, err := keeper.VerifyFee(
txData,
evm.EVMBankDenom,
baseFeeMicronibiPerGas,
ctx.IsCheckTx(),
)
Where VerifyFee returns the fee based on the transaction gas limit:
feeAmtMicronibi := evm.WeiToNative(txData.EffectiveFeeWei(baseFeeWei))
if feeAmtMicronibi.Sign() == 0 {
// zero fee, no need to deduct
return sdk.Coins{{Denom: denom, Amount: sdkmath.ZeroInt()}}, nil
}
return sdk.Coins{{Denom: denom, Amount: sdkmath.NewIntFromBigInt(feeAmtMicronibi)}}, nil
- But refunds in
msg_server.goare calculated using block gas:
blockGasUsed, err := k.AddToBlockGasUsed(ctx, evmResp.GasUsed)
if err != nil {
return nil, errors.Wrap(err, "EthereumTx: error adding transient gas used to block")
}
refundGas := uint64(0)
if evmMsg.Gas() > blockGasUsed {
refundGas = evmMsg.Gas() - blockGasUsed
}
To demonstrate the impact, consider this scenario:
-
Transaction A has gas limit 100,000 and uses 50,000 gas.
-
Transaction B has gas limit 100,000 and uses 40,000 gas.
-
When calculating the refund for transaction B:
- It should receive: 100,000 - 40,000 = 60,000 gas refund.
- But actually receives: 100,000 - (50,000 + 40,000) = 10,000 gas refund.
- The user loses refund for 50,000 gas.
Recommended mitigation steps
The refund calculation should be based on each transaction’s individual gas usage rather than the block gas. Modify the refund logic in msg_server.go:
// Before
blockGasUsed, err := k.AddToBlockGasUsed(ctx, evmResp.GasUsed)
refundGas := uint64(0)
if evmMsg.Gas() > blockGasUsed {
refundGas = evmMsg.Gas() - blockGasUsed
}
// After
refundGas := uint64(0)
if evmMsg.Gas() > evmResp.GasUsed {
refundGas = evmMsg.Gas() - evmResp.GasUsed
}
blockGasUsed, err := k.AddToBlockGasUsed(ctx, evmResp.GasUsed)
This ensures that each transaction’s refund is calculated based on its own gas limit and usage, independent of other transactions in the block.
ABAIKUNANBAEV (warden) commented:
@berndartmueller, I believe this should be of high severity as the issue deals with refunds and therefore, losing of funds. There are several DOS-related issues marked as high but this one with the actual funds losing is not.
berndartmueller (judge) commented:
@ABAIKUNANBAEV, sticking with Medium as this affects individual users, users who batch multiple EVM tx’s within a single Cosmos SDK tx. And batch EVM messages are not that common currently. Thus, I think Medium is justified.
k-yang (Nibiru) disputed and commented:
Please see my comment on Issue 46.
BlockGasUsedwas actually a terrible name for the gas tracker variable. It’s actually the cumulative amount of gas used in the TX when multiple EVM msgs are bundled in it. It gets reset between TXs by the evm ante handler.Since that’s the case, the gas refunded here is correct. The
blockGasUsedvariable is actually the total amount of gas used by the entire TXs, summing up all the individually bundled EVM msgs.
Nibiru mitigated:
PR-2132 - Proper tx gas refund outside of
CallContract.
Status: Mitigation confirmed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The refund logic used the cumulative block gas used instead of the individual transaction's gas used.
Impact
Users could lose funds as they receive smaller refunds than they should, especially when multiple transactions are included in a block.
What the attacker needed
Multiple EVM transactions in a block
The fix
The refund calculation should be based on each transaction's individual gas usage rather than the block gas, and the project mitigated this in PR-2132.
How it was found
- Transaction A has gas limit 100,000 and uses 50,000 gas.
- Transaction B has gas limit 100,000 and uses 40,000 gas.
- When calculating the refund for transaction B, it should receive 60,000 gas refund but actually receives 10,000 gas refund.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you send a transaction, you pay a fee based on the maximum gas you allow. If you use less, you get a refund. Here, the refund was calculated using the total gas used by all transactions in the block, not just yours, so you got less money back.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.