GASLIMIT opcode returns transaction gas limit instead of block gas limit resulting in incompatibility with the EVM
Originally disclosed on Code4rena
Submitted by berndartmueller
The EVM GASLIMIT opcode (https://www.ethervm.io/#45) is supposed to return the block gas limit.
However, when building the BlockContext in buildBlockContext(), GasLimit is set to the current transaction’s remaining gas, which is calculated as sdkCtx.GasMeter().Limit() - sdkCtx.GasMeter().GasConsumedToLimit(). This results in incompatibility with the EVM and potential issues with Solidity contracts.
For comparison, Evmos correctly uses the block gas meter.
Proof of Concept
minievm:x/evm/keeper/context.go#L70
GasLimit is determined by calling computeGasLimit().
58: func (k Keeper) buildBlockContext(ctx context.Context, defaultBlockCtx vm.BlockContext, evm *vm.EVM, fee types.Fee) (vm.BlockContext, error) {
59: sdkCtx := sdk.UnwrapSDKContext(ctx)
60: baseFee, err := k.baseFee(ctx, fee)
61: if err != nil {
62: return vm.BlockContext{}, err
63: }
64:
65: return vm.BlockContext{
66: BlockNumber: defaultBlockCtx.BlockNumber,
67: Time: defaultBlockCtx.Time,
68: Random: defaultBlockCtx.Random,
69: BaseFee: baseFee,
70:❌ GasLimit: k.computeGasLimit(sdkCtx),
computeGasLimit() returns the transaction’s remaining gas, calculated via the gas meter. For simulations, it even returns a fixed value of k.config.ContractSimulationGasLimit (i.e., DefaultContractSimulationGasLimit = uint64(3_000_000)).
35: func (k Keeper) computeGasLimit(sdkCtx sdk.Context) uint64 {
36: gasLimit := sdkCtx.GasMeter().Limit() - sdkCtx.GasMeter().GasConsumedToLimit()
37: if sdkCtx.ExecMode() == sdk.ExecModeSimulate {
38: gasLimit = k.config.ContractSimulationGasLimit
39: }
40:
41: return gasLimit
42: }
The GASLIMIT EVM opcode uses this GasLimit value to return the block gas limit:
core/vm/instructions.go#L501-L504
func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit))
return nil, nil
}
Recommended mitigation steps
Consider setting GasLimit to the block gas limit instead of the transaction’s gas.
leojin (Initia) confirmed and commented:
We fixed in this PR.
Useds block gas limit in block context.
Status: Mitigation confirmed. Full details in reports from 0xAlix2, berndartmueller and Franfran.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity misconfiguration bug in Initia Cosmos. Code4rena made it public on 10 April 2025. Found in the 2025-02-initia-cosmos audit contest (finding M-04).
About Misconfiguration
Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is Misconfiguration?
Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.