Skip to content
Business logic Audit finding Advanced

TransactionValidator checks intrinsic costs against wrong value

The transaction validator checked the intrinsic gas cost against the total gas limit, which already includes operator overhead, instead of the remaining gas after overhead. An attacker could submit L1-to-L2 transactions that cover only the larger of overhead or intrinsic costs, forcing the operator to process them without full compensation.

minhtrng Follow with Koolex, BARW and 2 others

Originally disclosed on Code4rena

Submitted by minhtrng, also found by Koolex, BARW, rvierdiiev, and anon

An incorrect check allows an L1->L2 transaction to be sent that does not cover the sum of overhead and intrinsic costs for the operator.

Proof of Concept

The gasLimit required for a transaction consists of (see docs):

{totalGasLimit} = {overhead + actualGasLimit} = {overhead + (intrinisicCosts + executionCosts)}

The function TransactionValidator.getMinimalPriorityTransactionGasLimit calculates the intrinsic costs that will be incurred for the processing of a transaction on L2. The calculated value is checked in TransactionValidator.validateL1ToL2Transaction like this:

require(
    getMinimalPriorityTransactionGasLimit(
        _encoded.length,
        _transaction.factoryDeps.length,
        _transaction.gasPerPubdataByteLimit
    ) <= _transaction.gasLimit,
    "up"
);

The issue is that _transaction.gasLimit is the total gasLimit including the overhead for the operator. The overhead costs are already checked before that in TransactionValidator.getTransactionBodyGasLimit

function getTransactionBodyGasLimit(
    uint256 _totalGasLimit,
    uint256 _gasPricePerPubdata,
    uint256 _encodingLength
) internal pure returns (uint256 txBodyGasLimit) {
    uint256 overhead = getOverheadForTransaction(_totalGasLimit, _gasPricePerPubdata, _encodingLength);

    require(_totalGasLimit >= overhead, "my"); // provided gas limit doesn't cover transaction overhead
    unchecked {
        txBodyGasLimit = _totalGasLimit - overhead;
    }
}

The value returned by this function is the actualGasLimit (see formula above) that will be available for the processing of the transaction and which should be used to check if the intrinsic costs are covered.

In the current implementation the totalGasLimit is checked twice if it’s greater than overhead and intrinsic costs (separately, not the sum). The bootloader does things correctly by subtracting the overhead from the totalGasLimit first and then checking if the rest covers the intrinsic costs (also called “preparation” costs):

function processL1Tx(...){
    ...
    //gasLimitForTx is total - overhead (and some other intrinsic costs)
    let gasLimitForTx, reservedGas := getGasLimitForTx(...)
    ...
    canonicalL1TxHash, gasUsedOnPreparation := l1TxPreparation(txDataOffset)
    ...
}   if gt(gasLimitForTx, gasUsedOnPreparation) {
        ...
        potentialRefund, success := getExecuteL1TxAndGetRefund(txDataOffset, sub(gasLimitForTx, gasUsedOnPreparation))

That means users will not be able to execute transactions for cheap. However, since the L1->L2 transaction have to be processed (due to the way the priority queue works), it would be possible to grief the operator by submitting transactions that only cover the following:

{max(overhead, intrinsicCosts)}

Those transactions would not have enough gas to be executed on L2, but the overhead and intrinsic costs would still be incurred.

Recommended Mitigation Steps

In TransactionValidator.validateL1ToL2Transaction change the check like this:

uint256 l2GasForTxBody = getTransactionBodyGasLimit(...)
...
require(
    getMinimalPriorityTransactionGasLimit(
        _encoded.length,
        _transaction.factoryDeps.length,
        _transaction.gasPerPubdataByteLimit
    ) <= l2GasForTxBody, // <---
    "up"
);

l2GasForTxBody is what remains after subtracting the overhead from the totalGasLimit.

miladpiri (zkSync) confirmed and commented via duplicate issue #975:

While the describe is large & complex, it is about wrong validation of transactions have enough gas.

Explanation: getMinimalPriorityTransactionGasLimit calculates how much gas the priority tx will minimally consume (intrinsic costs + some other costs like hashing it, etc). However, _transaction.gasLimit besides those costs also contains the overhead (for batch verification, etc) costs. Basically this means that if getMinimalPriorityTransactionGasLimit = _transaction.gasLimit, then the _transaction.gasLimit, once it pays for the overhead, may pay little-to-nothing for the processing of this transaction (it will be marked as failed, but the operator will have to bear the costs anyway as he must process, hash L1 transactions etc).

Alex the Entreprenerd (judge) commented via duplicate issue #975:

From the Zksync side, their job is not to ensure that the tx will not run OOG, as that cannot be guaranteed. Their job is to ensure that the “cost of processing” is lower than the “cost paid”.

I believe the check meets those requirements, in that, zkSync is defending itself against spam. This however, seems to create scenarios where a normal user could have a tx validated as functioning which would end up reverting on L2.


The original report as published on Code4rena, by minhtrng. 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

The validation compared the intrinsic gas cost against the total gas limit instead of the gas limit remaining after subtracting the overhead.

Impact

An attacker could grief the operator by submitting cheap L1-to-L2 transactions that lack enough gas to execute on L2, though the operator still incurs overhead and intrinsic costs; the judge noted this may also cause normal user transactions to revert on L2.

What the attacker needed

Ability to submit L1-to-L2 transactions

The fix

The check in `validateL1ToL2Transaction` should compare the minimal priority transaction gas limit against the gas remaining after subtracting overhead, not the total gas limit.

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.

When you send a transaction, you pay for two things: a fixed overhead for the operator and the actual work on the second layer. The code compared the work cost against the total you paid, not against what is left after the overhead is taken out. This meant someone could pay just enough to cover one of the two costs, and the operator would still have to process the transaction.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by minhtrng

View all

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

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.