Skip to content
Smart contracts Audit finding Advanced

Divergences in the Simulation of the extcodehash EVM Opcode

The zkSync Era's emulation of the extcodehash opcode diverges from EIP-1052 for accounts with zero code and non-zero balance, and for precompile addresses. An attacker could exploit this inconsistency to cause smart contracts that rely on the opcode's standard behaviour to misbehave.

anon Follow with quarkslab, rvierdiiev and 2 others

Originally disclosed on Code4rena

Submitted by anon, also found by quarkslab, rvierdiiev, 0xstochasticparrot, and erebus

The divergences in the emulation of the extcodehash EVM opcode within zkSync Era carry several potential impacts, specially on Developers relying on zkSync Era’s assurance that it emulates the extcodehash opcode as per EIP-1052 might encounter unexpected behavior in their smart contracts.

Proof of Concept

The getCodeHash function within the zkSync Era is designed to emulate the functionality of the extcodehash Ethereum Virtual Machine (EVM) opcode, as laid out in the Ethereum Improvement Proposal 1052 (EIP-1052).

https://github.com/code-423n4/2023-10-zksync/blob/main/code/system-contracts/contracts/AccountCodeStorage.sol#L89

However, it’s important to recognize that this function does not exhibit precisely identical behavior to the EVM’s extcodehash opcode. There are specific discrepancies:

  1. EIP-161 defines an account as “empty” when it satisfies certain criteria: no code, a nonce value of zero, and a balance of zero. According to EIP-1052, the extcodehash of an empty account should evaluate to bytes32(0). In the zkSync Era, the behavior aligns with this definition when codeHash is 0x00, getRawNonce(account) is 0, and isContractConstructing(codeHash) is false. If an account has nonzero balance, then based on the definition of EIP-161, it will not be considered as an empty account anymore. In this case, that account will be considered as an account with no code. So, extcodehash of such account will be keccak256("") in EVM.

The issue is that, zkSync Era returns bytes32(0) regardless of the balance of the account. It only cares about the nonce and the code.

  1. Based on EIP-1052, the extcodehash of an precompile contract is either keccak256("") or bytes32(0). For instance, the extcodehash of address(0x02)—representing the SHA-256 precompile contract in the EVM—should be keccak256("") because it has no code, a nonce of zero, and a nonzero balance.

In contrast, the zkSync Era consistently returns keccak256("") for precompile contracts, regardless of their balances. The zkSync Era’s behavior is based solely on whether the address is lower/equal to CURRENT_MAX_PRECOMPILE_ADDRESS.

These observed inconsistencies could potentially raise concerns for developers who rely on zkSync Era’s assertion that it accurately simulates the extcodehash EVM opcode as dictated by the EIP-1051 specification.

Recommended Mitigation Steps

The following code is recommended to simulate the extcodehash EVM opcode precisely based on EIP-1052.

function getCodeHash(uint256 _input) external view override returns (bytes32) {

        address account = address(uint160(_input));
        if (uint160(account) <= CURRENT_MAX_PRECOMPILE_ADDRESS && account.balance != 0) {
            return EMPTY_STRING_KECCAK;
        } else if (uint160(account) <= CURRENT_MAX_PRECOMPILE_ADDRESS && address(account).balance == 0) {
            return bytes32(0);
        }

        bytes32 codeHash = getRawCodeHash(account);

        if (codeHash == 0x00 && NONCE_HOLDER_SYSTEM_CONTRACT.getRawNonce(account) > 0) {
            codeHash = EMPTY_STRING_KECCAK;
        }
        else if (Utils.isContractConstructing(codeHash)) {
            codeHash = EMPTY_STRING_KECCAK;
        } else if (codeHash == 0x00 && NONCE_HOLDER_SYSTEM_CONTRACT.getRawNonce(account) == 0 && address(account).balance != 0) {
            codeHash = EMPTY_STRING_KECCAK;
        }

        return codeHash;
    }

https://github.com/code-423n4/2023-10-zksync/blob/main/code/system-contracts/contracts/AccountCodeStorage.sol#L89

Assessed type

Context

miladpiri (zkSync) confirmed, but disagreed with severity and commented:

The impact is medium, the probability is low. So, low severity can be fair.

Alex the Entreprenerd (judge) commented:

While I agree with low impact, this seem to be an inconsistency in the implementation of the EVM, which makes the finding notable.

Alex the Entreprenerd (judge) commented:

I would have a different perspective for an ERC or some implementation; however, in this case this is an opcode that behaves differently (although in a edge case). At this time, I believe Medium Severity is most appropriate as the finding demonstrates an inconsistent behaviour of the EVM with the zkSyncVM, as it breaks a coding convention that goes beyond a best-practice.


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

Impact

Smart contracts relying on standard extcodehash behaviour may behave unexpectedly, though the impact is considered medium with low probability.

What the attacker needed

Contract uses extcodehash; account has zero code and non-zero balance

The fix

The getCodeHash function should be modified to return keccak256("") for accounts with non-zero balance and no code, and for precompiles with non-zero balance, as per EIP-1052.

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.

The extcodehash opcode returns a fingerprint of a contract's code. According to the Ethereum standard, an account with no code but some balance should return a specific non-zero fingerprint, while an empty account returns zero. zkSync Era returns zero for any account with no code, ignoring its balance, which can confuse contracts that check this value.

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.