Skip to content
Crypto Audit finding Advanced

decode_legacy_tx allows validation of signatures with chain_id that are larger than felt, and overflows

The legacy transaction decoder did not limit the length of the chain_id field, so a chain_id longer than 31 bytes overflowed the felt conversion. This allowed signatures with oversized chain_id values to pass validation.

Emmanuel Follow with 20centclub

Originally disclosed on Code4rena

Submitted by Emmanuel, also found by 20centclub

In decode_legacy_tx, data_len for chain_id is not constrained to be <31, which allows validation of signatures with chain_id that are larger than felt, and overflows.

Proof of Concept

chain_id is pulled from items\[6].data via the bytes_to_felt helper function.

Looking at bytes_to_felt function, we will observe that the return value will overflow if len>31, because current would have been shifted 2^(8\*32) times, which overflows the max felt value of 2^252:

        func bytes_to_felt(len: felt, ptr: felt*) -> felt {
            if (len == 0) {
                return 0;
            }
            tempvar current = 0;

            // len, ptr, ?, ?, current
            // ?, ? are intermediate steps created by the compiler to unfold the
            // complex expression.
            loop:
            let len = [ap - 5];
            let ptr = cast([ap - 4], felt*);
            let current = [ap - 1];

            tempvar len = len - 1;
            tempvar ptr = ptr + 1;
            tempvar current = current * 256 + [ptr - 1];//@audit-info overflow if len>31

            static_assert len == [ap - 5];
            static_assert ptr == [ap - 4];
            static_assert current == [ap - 1];
            jmp loop if len != 0;

            return current;
        }

Since items\[6].data_len is not constrained to be <=31, absurd values of chain_id(containing >31 data_len) can be passed, which would overflow, and pass the signature validation.

Recommended Mitigation Steps

Assert that items\[6].data_len <= 31:

    assertassert_nn(31 - items[6].data_len);

Assessed type

Under/Overflow

ClementWalter (Kakarot) confirmed and commented:

Severity: Medium

Kakarot mitigated:

This PR decodes legacy chain id overflow. Assert the chain id does not overflow for legacy post eip 155 tx.

Status: Mitigation confirmed.


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

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

Likely the decoder never checked that the chain_id field length was at most 31 bytes before converting it to a felt, so the conversion overflowed.

Impact

Signatures carrying an oversized chain_id could be validated when they should not be, though the project rated this Medium severity.

What the attacker needed

Craft a legacy transaction; chain_id field longer than 31 bytes

The fix

The decoder was fixed to assert that the legacy chain_id does not overflow.

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.

A felt is a fixed-size number used by this blockchain, and it can only hold so much. When the code read a chain_id longer than that size, the number wrapped around instead of failing, so a signature that should have been rejected was accepted.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More by Emmanuel

View all

5 more disclosed reports, best first.

More from Kakarot

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.