Skip to content
Smart contracts Audit finding Advanced

Overflow due to lack of checks leading to incorrect price calculation

The IntrinsicGas function in the Celo optimism code adds access-list gas costs to the running total without checking for uint64 overflow, unlike every other addition in the same function. An attacker could craft a transaction with a very large access list so the gas value wraps around, producing an incorrect gas limit and causing the transaction flow to fail.

okmxuse Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#37206 [SC-Medium] Overflow due to lack of checks leading to incorrect price calculation

Submitted on Nov 28th 2024 at 20:08:33 UTC by @okmxuse for Audit Comp | Celo

Description

Inside the check-derivation/main.go function, the getRandomSignedTransaction function is invoked (note that this function traces all the way back to checkConsolidation which is then called in the main.go function ).

getRandomSignedTransaction calls IntrinsicGas at three places. We will focus on the one that includes the accessList, which is case types.AccessListTxType:

Go
=> case types.AccessListTxType:
	accessList := types.AccessList{types.AccessTuple{
		Address:     randomAddress,
		StorageKeys: []common.Hash{common.HexToHash("0x1234")},
	}}
	gasLimit, err := core.IntrinsicGas(data, accessList, false, true, true, false, nil, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get intrinsicGas: %w", err)
	}

Next, let's look at the IntrinsicGas function:

Go
func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool, feeCurrency *common.Address, feeIntrinsicGas common.IntrinsicGasCosts) (uint64, error) {
	// Set the starting gas for the raw transaction
	var gas uint64
	if isContractCreation && isHomestead {
		gas = params.TxGasContractCreation
	} else {
		gas = params.TxGas
	}
	dataLen := uint64(len(data))
	// Bump the required gas by the amount of transactional data
	if dataLen > 0 {
		// Zero and non-zero bytes are priced differently
		var nz uint64
		for _, byt := range data {
			if byt != 0 {
				nz++
			}
		}
		// Ensure we do not exceed uint64 limits for all data combinations
		nonZeroGas := params.TxDataNonZeroGasFrontier
		if isEIP2028 {
			nonZeroGas = params.TxDataNonZeroGasEIP2028
		}
=>		if (math.MaxUint64-gas)/nonZeroGas < nz {
			return 0, ErrGasUintOverflow
		}
		gas += nz * nonZeroGas

		z := dataLen - nz
=>		if (math.MaxUint64-gas)/params.TxDataZeroGas < z {
			return 0, ErrGasUintOverflow
		}
		gas += z * params.TxDataZeroGas

		if isContractCreation && isEIP3860 {
			lenWords := toWordSize(dataLen)
=>			if (math.MaxUint64-gas)/params.InitCodeWordGas < lenWords {
				return 0, ErrGasUintOverflow
			}
			gas += lenWords * params.InitCodeWordGas
		}
	}

	if feeCurrency != nil {
		intrinsicGasForFeeCurrency, ok := common.CurrencyIntrinsicGasCost(feeIntrinsicGas, feeCurrency)
		if !ok {
			return 0, fmt.Errorf("%w: %x", exchange.ErrUnregisteredFeeCurrency, feeCurrency)
		}
=>		if (math.MaxUint64 - gas) < intrinsicGasForFeeCurrency {
			return 0, ErrGasUintOverflow
		}
		gas += intrinsicGasForFeeCurrency
	}

	if accessList != nil {
=>		gas += uint64(len(accessList)) * params.TxAccessListAddressGas
=>		gas += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas
	}
	return gas, nil
}

Looking at the arrowed code-lines everywhere in the function, when gas is incremented, overflow checks are performed using conditions like:

Go
if (math.MaxUint64 - gas)/value < multiplier {
	return 0, ErrGasUintOverflow
}

This ensures that the gas does not become bigger than uint64.

However, no overflow check is performed in the final block involving the accessList, specifically at:

Go
	if accessList != nil {
=>		gas += uint64(len(accessList)) * params.TxAccessListAddressGas
=>		gas += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas
	}

As you can see it simply adds the result to gas without any overflow check.

Impact

This bug can lead to silent overflows, causing:

  • Incorrect computation of gas limits for transactions which will cause the complete flow to fail.

Recommendation

Add an overflow check for the gas additions within the accessList block:

Go
	if accessList != nil {
		addressGas := uint64(len(accessList)) * params.TxAccessListAddressGas
		if (math.MaxUint64 - gas) < addressGas {
			return 0, ErrGasUintOverflow
		}
		gas += addressGas

		storageKeyGas := uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas
		if (math.MaxUint64 - gas) < storageKeyGas {
			return 0, ErrGasUintOverflow
		}
		gas += storageKeyGas
	}

This ensures that both address and storage key gas costs are properly validated, preventing silent overflows and ensuring accurate gas calculations.

Proof of Concept

POC

First I modified the main IntrinsicGasfunction slightly to ensure that an overflow is guaranteed:

Diff
+	if accessList != nil {
+		// set the gas to uint64 - 2400 to guarantee it to trigger (note this is just for testing simplicity)
+		gas := uint64(18446744073709551615)
+		gas += uint64(len(accessList)) * params.TxAccessListAddressGas
+		gas += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas
+ // return the gas to log in the test file 
	}
+	return gas, nil
+ }

then create a main_test.go file and past the following:

JavaScript
package main_test

import (
	"math/rand"
	"testing"

	"github.com/ethereum-optimism/optimism/op-service/testutils"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
)

func Test_Overflow(t *testing.T) {
	rng := rand.New(rand.NewSource(1337))
	data := testutils.RandomData(rng, 10)

	accessList := make(types.AccessList, 5) 
	for i := range accessList {
		accessList[i] = types.AccessTuple{
			Address:     randomAddress(rng),
			StorageKeys: []common.Hash{common.HexToHash("0x1234")},
		}
	}

	// Call IntrinsicGas with the near-overflow AccessList
	gas, err := core.IntrinsicGas(data, accessList, false, true, true, false, nil, nil)

	// Log both the gas value and the error to understand the result
	t.Logf("Returned gas: %d", gas) // Correctly print the gas value
	t.Logf("Caught expected error: %v", err)
}

// Helper function to generate random Ethereum addresses
func randomAddress(rng *rand.Rand) common.Address {
	var addr [20]byte
	rng.Read(addr[:])
	return common.Address(addr)
}
  • run go test -v -run Test_Overflow
  • Logs:
=== RUN   Test_Overflow
    main_test.go:30: Returned gas: 42660
    main_test.go:31: Caught expected error: <nil>
--- PASS: Test_Overflow (0.00s)
PASS

As we can see the gas has wrapped and now returns 42660.

The original report as published on Immunefi, by okmxuse. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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 access-list gas additions in IntrinsicGas are performed without the overflow guard used for every other addition in the function.

Impact

Silent overflow could produce incorrect gas limits for transactions, causing the complete flow to fail; the proof of concept relies on a modified function, so real-world exploitability is not demonstrated.

What the attacker needed

Crafted transaction with a very large access list

The fix

Overflow checks should be added around both access-list gas additions, returning ErrGasUintOverflow when the sum would exceed uint64.

How it was found

  1. Modify IntrinsicGas so the gas value starts near the maximum uint64 to force a wrap.
  2. Write a test that builds an access list with several entries and calls IntrinsicGas.
  3. Run the test and observe that no error is returned and the gas value has wrapped to a small number.

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.

Gas is a number that measures how much work a transaction costs, and it is stored in a fixed-size counter that wraps back to a small number if it gets too big. Most of this function carefully checks before adding to that counter, but the last part that adds the cost of an access list (a list of addresses and storage slots a transaction plans to touch) skips the check. So a huge list can make the counter wrap around and report a wrong, tiny cost.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by okmxuse

View all

9 more disclosed reports, best first.

More from Celo

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.