RIPEMD160 precompile crashes with a Cairo exception for some input lengths #120
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
downgraded by judge
Judge downgraded the risk level of this issue
edited-by-warden
M-01
primary issue
Highest quality submission among a set of duplicates
🤖_48_group
AI based duplicate group recommendation
selected for report
This submission will be included/highlighted in the audit report
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/kkrt-labs/kakarot/blob/7411a5520e8a00be6f5243a50c160e66ad285563/src/kakarot/precompiles/ripemd160.cairo#L32-L83
Vulnerability details
Calling the RIPEMD160 precompile with certain input lengths results in a Cairo exception. As a result, some L2 smart contracts that use this precompile cannot be executed.
The bug seems to occur in line 477 of the precompile. The relevant code:
The lower part of the code is reached for some input lengths. Note that
x
is redefined with the line:However,
start
still points to the first element of the original dictx
initialized at the start of the function. Consequently, squashing the dict withdefault_dict_finalize(start, x, 0)
will fail because start and x point to different segments.Proof of Concept
The issue can be verified by running the following Solidity contract as an e2e-test.:
While calling
hashInputLength20()
returns the correct result,hashInputLength32()
will result in a failed test:In the ordinary EVM, the same function returns the result:
Additionally, the following Cairo test reproduces the issue:
This will crash with the exception:
Recommended Mitigation Steps
Set the pointer
start
to the correct value. It is also very important to finalize the previously initializeddict x
as the prover can cheat otherwise.Double-check the invocation path of the precompile exec_precompile
exec_precompile()
, and add end-to-end tests for all precompiles to make sure that they work as expected when called from L2.Assessed type
Error
The text was updated successfully, but these errors were encountered: