When the SHA256 built-in function is called with a bytes32 input, we use the same scratch space to save the input and return the output. If a chain does not implement the SHA256 precompile (which is a requirement for many ZK rollups), this address will be an EOA, so the call will silently fail and we'll return the input value from memory.
The SHA256 built-in function is a wrapper around the precompiled contract at address(0x02). In the event that it is called with a bytes32 argument, we perform the following logic:
Place the input argument at the 0 memory slot.
Call the precompile with an input of memory slots 0-31.
Assert that the call succeeded.
Ask the precompile to return the hashed value to memory slots 0-31.
mload the value from memory slots 0-31 to return the hashed value.
We can see this logic implemented here:
In the event that the staticcall to address(0x02) succeeds (ie passes the assert) but returns no data, the input data will remain at memory slot 0 and will be returned from the function call.
In the event that a chain does not implement the SHA256 precompile, this is exactly what would happen. Because calls to EOAs always return 1
(success), such a call will pass the assert, but will return no calldata. The input data will then be returned with no error, leading to major vulnerabilities in any contract that uses this function.
Note that not implementing the SHA256 precompile is a common requirement for ZK rollups. Both ZKsync and Scroll do not implement the precompile at present. Fortunately, both currently have errors that will stop this vulnerability from being exploited, but future rollups that simply skip implementing the precompile will be vulnerable.
Rollups that do not implement the SHA256 precompile will lead to the SHA256 built-in function returning the input (rather than no data) for all bytes32 inputs.
Manual Review
Because there is a risk of the call succeeding with no return value, return the data to FREE_VAR_SPACE2
to ensure that 0
is returned in the case of no data being returned.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.