Submitted by muellerberndt, also found by superpozycja and RadiantLabs
The function felt_to_bytes_little() in bytes.cairo converts a felt into an array of bytes.
The prover can cheat by returning a near arbitrary string that does not correspond to the input felt, whereby, the spoofed output bytes and `bytes_len, bytes must fulfill some specific conditions (but, if carefully crafted, can contain almost arbitrary sequences of bytes).
This issue affects the function felt_to_bytes_little() as well as other functions that depend on it:
Those functions are used throughout the code, notably in get_create_address() and get_create2_address(), which an attacker could exploit to deploy L2 smart contracts from a spoofed sender address (e.g., to steal funds from wallets that use account abstraction).
First, note the following loop:
We observe that:
To pass the bounds check, we need a code offset that contains the value 0. Fortunately, from a malicious provers perspective, there are many zero-value locations in the code segment. When we dump the memory segment with a hint we find multiple zeroes:
When creating the proof of concept I had some problems calculating the offsets reliably, but I got it to work with a few brute-force tries. The following test worked reliably for me:
Running the test shows the provers manipulated output vs. the expected output:
A skilled attacker who invests enough time can almost arbitrary output strings, as long as they make sure that value reaches zero at some point and pow256_table + bytes_len] == 0.
P.S., whether the same offset works may depend on the Cairo compiler version. I used the following test to find a valid offset:
The easiest fix is to do range checks on value to prevent underflows.
Math
ClementWalter (Kakarot) confirmed and commented:
Kakarot mitigated:
Status: Mitigation confirmed.
