Skip to content
Access control Audit finding Advanced

Nonce ordering of EOA can be updated to “arbitrary” through an L1 tx

The default account on zkSync Era does not allow calling updateNonceOrdering, but an L1 priority transaction bypasses this check because the bootloader sets the isSystem flag for any call to the ContractDeployer. An attacker can permanently freeze their own account by changing its nonce ordering to arbitrary, after which no new transactions can be initiated.

xuwinnie Follow

Originally disclosed on Code4rena

Submitted by xuwinnie

Nonce order of an EOA should always be sequential, otherwise the transaction cannot be validated. The default account does not allow user to call function updateNonceOrdering of ContractDeployer. However, the restriction can be bypassed if user call updateNonceOrdering through an L1 priority transaction. As a result, the user’s account will be permanently frozen.

Proof of Concept

function _validateTransaction(
    bytes32 _suggestedSignedHash,
    Transaction calldata _transaction
) internal returns (bytes4 magic) {
    // Note, that nonce holder can only be called with "isSystem" flag.
    SystemContractsCaller.systemCallWithPropagatedRevert(
        uint32(gasleft()),
        address(NONCE_HOLDER_SYSTEM_CONTRACT),
        0,
        abi.encodeCall(INonceHolder.incrementMinNonceIfEquals, (_transaction.nonce))
    );

function incrementMinNonceIfEquals(uint256 _expectedNonce) external onlySystemCall {
    uint256 addressAsKey = uint256(uint160(msg.sender));
    uint256 oldRawNonce = rawNonces[addressAsKey];

    (, uint256 oldMinNonce) = _splitRawNonce(oldRawNonce);
    require(oldMinNonce == _expectedNonce, "Incorrect nonce");

    unchecked {
        rawNonces[addressAsKey] = oldRawNonce + 1;
    }
}

incrementMinNonceIfEquals is used in _validateTransaction. It always uses a sequential nonce.

        // Checks whether the nonce `nonce` have been already used for
        // account `from`. Reverts if the nonce has not been used properly.
        function ensureNonceUsage(from, nonce, shouldNonceBeUsed) {
            // INonceHolder.validateNonceUsage selector
            mstore(0, {{RIGHT_PADDED_VALIDATE_NONCE_USAGE_SELECTOR}})
            mstore(4, from)
            mstore(36, nonce)
            mstore(68, shouldNonceBeUsed)

            let success := call(
                gas(),
                NONCE_HOLDER_ADDR(),
                0,
                0,
                100,
                0,
                0
            )

            if iszero(success) {
                revertWithReason(
                    ACCOUNT_TX_VALIDATION_ERR_CODE(),
                    1
                )
            }
        }

function validateNonceUsage(address _address, uint256 _key, bool _shouldBeUsed) external view {
    bool isUsed = isNonceUsed(_address, _key);

    if (isUsed && !_shouldBeUsed) {
        revert("Reusing the same nonce twice");
    } else if (!isUsed && _shouldBeUsed) {
        revert("The nonce was not set as used");
    }
}

function isNonceUsed(address _address, uint256 _nonce) public view returns (bool) {
    uint256 addressAsKey = uint256(uint160(_address));
    return (_nonce < getMinNonce(_address) || nonceValues[addressAsKey][_nonce] > 0);
}

The bootloader calls validateNonceUsage too, which considers the arbitrary ordering.

We need to prevent default account from changing nonce ordering type; otherwise, these two methods will conflict. If user’s current minNonce is 13, nonce of new tx also needs to be 13, but if some value is already stored under 14, isNonceUsed will return true. Which means the user will no longer be able to initiate new transactions.

function _execute(Transaction calldata _transaction) internal {
    address to = address(uint160(_transaction.to));
    uint128 value = Utils.safeCastToU128(_transaction.value);
    bytes calldata data = _transaction.data;
    uint32 gas = Utils.safeCastToU32(gasleft());

    // Note, that the deployment method from the deployer contract can only be called with a "systemCall" flag.
    bool isSystemCall;
    if (to == address(DEPLOYER_SYSTEM_CONTRACT) && data.length >= 4) {
        bytes4 selector = bytes4(data[:4]);
        // Check that called function is the deployment method,
        // the others deployer method is not supposed to be called from the default account.
        isSystemCall =
            selector == DEPLOYER_SYSTEM_CONTRACT.create.selector ||
            selector == DEPLOYER_SYSTEM_CONTRACT.create2.selector ||
            selector == DEPLOYER_SYSTEM_CONTRACT.createAccount.selector ||
            selector == DEPLOYER_SYSTEM_CONTRACT.create2Account.selector;
    }

Flag isSystemCall is only set true when calling deployment method of ContractDeployer, so if user tries to call function updateNonceOrdering of ContractDeployer, isSystemCall will be false and the call will fail.

        function msgValueSimulatorMimicCall(to, from, value, dataPtr) -> success {
            // Only calls to the deployer system contract are allowed to be system
            let isSystem := eq(to, CONTRACT_DEPLOYER_ADDR())

            success := mimicCallOnlyResult(
                MSG_VALUE_SIMULATOR_ADDR(),
                from,
                dataPtr,
                0,
                1,
                value,
                to,
                isSystem
            )
        }

However, if the transaction is initiated on L1, selector is not checked. Flag isSystem is set true as long as the target is ContractDeployer. The transaction to updateNonceOrdering will succeed.

function updateNonceOrdering(AccountNonceOrdering _nonceOrdering) external onlySystemCall {
    AccountInfo memory currentInfo = accountInfo[msg.sender];

    require(
        _nonceOrdering == AccountNonceOrdering.Arbitrary &&
            currentInfo.nonceOrdering == AccountNonceOrdering.Sequential,
        "It is only possible to change from sequential to arbitrary ordering"
    );

    currentInfo.nonceOrdering = _nonceOrdering;
    _storeAccountInfo(msg.sender, currentInfo);

    emit AccountNonceOrderingUpdated(msg.sender, _nonceOrdering);
}

Once updated, the change is permanent. The account can no longer initiate any new transactions.

Recommended Mitigation Steps

Don’t allow EOA to call updateNonceOrdering. Do the check on L1 side.

Assessed type

Context

miladpiri (zkSync) confirmed, but disagreed with severity

Alex the Entreprenerd (judge) commented:

A dispute regarding usage of future code was made, which is rational. Personally, I believe that being able to access the isSystem flag could be precursor to higher impact findings and that for this reason the finding warrants medium severity.

I understand that as of now, the system is safe; however, this broken invariant could be a root cause for drastic issues in the future if zkSync Era functionality is further expanded.

Note: For full discussion, see here.


The original report as published on Code4rena, by xuwinnie. 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 L1 transaction path does not check the function selector before setting the isSystem flag, allowing any call to the ContractDeployer to be treated as a system call.

Impact

The user's account is permanently frozen and can no longer initiate new transactions; the report does not claim impact beyond the user's own account.

What the attacker needed

Ability to send an L1 priority transaction

The fix

The check should be done on the L1 side to prevent EOAs from calling updateNonceOrdering.

How it was found

  1. Send an L1 priority transaction that calls updateNonceOrdering on the ContractDeployer with the Arbitrary argument.
  2. The bootloader sets isSystem to true because the target is the ContractDeployer, so the call succeeds.
  3. The account's nonce ordering becomes Arbitrary permanently.
  4. Future transactions fail because the nonce validation logic conflicts with the arbitrary ordering.

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.

On zkSync Era, each account has a nonce (a counter) that must increase by one for every transaction. The system contract that manages nonces can be told to allow out-of-order nonces, but only through a special system call. The default account blocks this, but a transaction sent from Ethereum (L1) skips the block, letting the user switch to out-of-order nonces. Once switched, the account's nonce checks conflict and the account can no longer send any transactions.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by xuwinnie

View all

12 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.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.