Skip to content
Smart contracts Audit finding Advanced

Discrepancy in ECRECOVER Precompile when Using Delegatecall

In zkSync Era, calling the ECRECOVER precompile through delegatecall returns a zero value instead of the recovered address, unlike standard EVM behaviour where call, staticcall and delegatecall all agree. A contract relying on this path could accept an invalid signature or misidentify a signer, which may compromise data integrity or funds.

anon Follow with anon and zkrunner

Originally disclosed on Code4rena

Submitted by anon, also found by anon and zkrunner

The discrepancy in delegatecall behavior with the ECRECOVER precompile contract in zkSync Era can have significant impact leading to incorrect signature validation, potentially compromising data integrity and user funds.

Proof of Concept

In the context of zkSync Era, there exists a noticeable inconsistency in how the ECRECOVER precompile contract (located at address 0x01) behaves when accessed via delegatecall. This behavior differs from the standard Ethereum Virtual Machine (EVM) behavior, where the outcomes remain uniform across call, staticcall, and delegatecall.

https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/contracts/precompiles/Ecrecover.yul#L7

In zkSync Era, when the ECRECOVER precompile contract is invoked using delegatecall, it diverges from the usual behavior by delegating the call to the contract itself and executing its code within the caller’s context. This results in a returned value that does not align with the anticipated outcome of a precompileCall. Instead, it yields bytes32(0).

To illustrate, in the following example, when executing the provided code in the EVM, the returned bytes32 value consistently appears as 0x000000000000000000000000759389e8e5c1aa1f511e9ea98b6caedd262bff0b for all three scenarios: ecrecoverStaticcall, ecrecoverCall, and ecrecoverDelegatecall. However, in the zkSync Era, while ecrecoverStaticcall and ecrecoverCall maintain the same results as in the EVM, ecrecoverDelegatecall produces an incorrect outcome:

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.20;

contract PoC {
    bytes32 h = keccak256("");
    uint8 v = 27;
    bytes32 r = bytes32(uint256(1));
    bytes32 s = bytes32(uint256(2));

    function ecrecoverStaticcall() public returns (bytes32) {
        bytes memory data = abi.encode(h, v, r, s);
        assembly {
            pop(staticcall(gas(), 0x01, add(data, 0x20), mload(data), 0, 0x20))
            return(0, 0x20)
        }
    }

    function ecrecoverCall() public returns (bytes32) {
        bytes memory data = abi.encode(h, v, r, s);
        assembly {
            pop(call(gas(), 0x01, 0x00, add(data, 0x20), mload(data), 0, 0x20))
            return(0, 0x20)
        }
    }

    function ecrecoverDelegatecall() public returns (bytes32) {
        bytes memory data = abi.encode(h, v, r, s);
        assembly {
            pop(
                delegatecall(gas(), 0x01, add(data, 0x20), mload(data), 0, 0x20)
            )
            return(0, 0x20)
        }
    }
}

This discrepancy is critical in its impact because it introduces a divergence from the expected EVM response. While the likelihood of encountering this issue is not high, as precompile contracts are typically invoked through staticcall rather than delegatecall.

Recommended Mitigation Steps

The following revised code is recommended:

function delegateCall(
        uint256 _gas,
        address _address,
        bytes calldata _data
    ) internal returns (bytes memory returnData) {
        bool success;
        if(_address == address(0x01){
            success = rawStaticCall(_gas, _address, _data);
        } else {
            success = rawDelegateCall(_gas, _address, _data);
        }
        returnData = _verifyCallResult(success);
    }

https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/contracts/libraries/EfficientCall.sol#L88

Assessed type

Context

miladpiri (zkSync) confirmed and commented:

It has high impact (we do not know in which context it will be used, so its wrong result can have critical impact), but low probability. So, medium is fair.

Alex the Entreprenerd (judge) commented:

The Warden has shown an inconsistency in the behaviour of ecrecover when using delegatecall. Because the goal of the zkSync EVM is to be the EVM compatible, Medium Severity seems appropriate.

Alex the Entreprenerd (judge) commented:

I have verified my statement through testing via the zksync-foundry code repo as well as reviewing both the Precompile as well as the Rust code.

Note: to view the provided image, please see the original comment here.

Note: for full discussion, see here.


The original report as published on Code4rena, by anon. 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 delegatecall path in zkSync Era's EfficientCall library forwards the call to the precompile address as a normal delegatecall rather than routing it through the precompile call mechanism, so the precompile logic never runs and a zero word is returned.

Impact

Contracts on zkSync Era that validate signatures through delegatecall could get a wrong result, potentially compromising data integrity and user funds, though the text notes the likelihood is low because precompiles are usually called with staticcall.

What the attacker needed

Contract uses delegatecall to ECRECOVER; running on zkSync Era

The fix

The delegateCall helper should detect the ECRECOVER precompile address and route that call through a raw static call instead of a raw delegate call.

How it was found

  1. Deploy a contract that calls address 0x01 with the same signature data via staticcall, call and delegatecall.
  2. Run each function on standard EVM and note all three return the same recovered address.
  3. Run the same functions on zkSync Era and observe that only the delegatecall variant returns a zero value.

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 precompile is a built-in mini-contract that lives at a fixed address; ECRECOVER at address 1 works out who signed a message. Contracts can call another contract in different ways, and delegatecall is one of them. On zkSync Era this particular call style gives back an empty result instead of the signer, so any contract that trusts it could be fooled about who signed.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by anon

View all

10 more disclosed reports, best first.

More from zkSync Era

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.