#38277 [BC-Insight] Potential Out-of-Range Panic in `UnmarshalJSON()` of `HexOrDecimal256`

Submitted on Dec 29th 2024 at 23:39:47 UTC by @CertiK for Attackathon | Ethereum Protocol

  • Report ID: #38277

  • Report Type: Blockchain/DLT

  • Report severity: Insight

  • Target: https://github.com/ledgerwatch/erigon

  • Impacts:

    • (Specifications) A bug in specifications with no direct impact on client implementations

Description

Brief/Intro

In the erigon-lib/common/math package, the type HexOrDecimal256 marshals big.Int into hex or decimal strings. Due to a mishandling of the slice index in the method UnmarshalJSON(), it would possibly lead to an out-of-range panic if this method is invoked with a certain value.

Vulnerability Details

Affected Codebase: https://github.com/erigontech/erigon/tree/v3.0.0-alpha7

The function UnmarshalJSON() is utilized to parse hex or decimal string into big.Int.

https://github.com/erigontech/erigon/blob/v3.0.0-alpha7/erigon-lib/common/math/big.go#L61

The condition len(input) > 0 && input[0] == ' " ' is intended to ignore the first quote ' " ' if it exists as the first element of the input.

However, this check is not effective. In case that the input only contains the quote ' " ' , then the input length is 1, so the condition is satisfied, which leads to the out-of-range panic when taking the slice input[1:0].

In fact, the check should be len(input) > 1 && input[0] == ' " ' .

Impact Details

Though the type HexOrDecimal256 has been utilized in multiple places of the current codebase, for example,

https://github.com/erigontech/erigon/blob/v3.0.0-alpha7/core/blockchain.go#L71

we are not aware of the potential attack vector and it may not be exploitable at this moment. Due to the potential node crash if it’s triggered implicitly or by future update, it’s recommended to fix it.

References

  • https://github.com/erigontech/erigon/tree/v3.0.0-alpha7

Proof of Concept

Proof of Concept

We provide the following simple test case by setting the input as the quote ' " ' .

The test result shows the out-of-range panic could be triggered in the method UnmarshalJSON() with input ' " ' .

Was this helpful?