Loss of funds for the sender when L1->L2 TX fails in the bootloader on L2
When an L1-to-L2 transaction fails on zkSync, the bootloader burns all remaining execution gas instead of refunding the unused portion to the sender. An attacker or unlucky user could lose a significant amount of gas fees, especially for computationally intensive transactions with large gas limits.
Koolex Follow with Audittens and rvierdiiev
Originally disclosed on Code4rena
Submitted by Koolex, also found by Audittens and rvierdiiev
Sending L1->L2 TX
In zksync, requestL2Transaction can be used to send a L1->L2 TX.
function requestL2Transaction(
address _contractL2,
uint256 _l2Value,
bytes calldata _calldata,
uint256 _l2GasLimit,
uint256 _l2GasPerPubdataByteLimit,
bytes[] calldata _factoryDeps,
address _refundRecipient
) external payable nonReentrant senderCanCallFunction(s.allowList) returns (bytes32 canonicalTxHash) {
Mailbox.requestL2Transaction:L236-L245
An important param is _l2TxGasLimit, which is used to process the TX on L2. The L2 gas limit should include both the overhead for processing the batch and the L2 gas needed to process the transaction itself (i.e. the actual l2GasLimit that will be used for the transaction).
So, _l2TxGasLimit should have an amount that’s enough to cover:
- Transaction overhead.
- The transaction itself (body execution).
To ensure this, there is a validation enforced on requestL2Transaction before pushing the TX to the priorityQueue.
L2CanonicalTransaction memory transaction = _serializeL2Transaction(_priorityOpParams, _calldata, _factoryDeps);
bytes memory transactionEncoding = abi.encode(transaction);
TransactionValidator.validateL1ToL2Transaction(transaction, transactionEncoding, s.priorityTxMaxGasLimit);
Mailbox._writePriorityOp:L361-L368
This is to make sure the transaction at least get a chance to be executed.
Bootloader L1->L2 TX execuation flow
When the bootloader receives the L1->L2 TX, it does the following:
- Calculates the L2 gas limit for the transaction’s body, i.e. without intrinsic costs and overhead.
let gasLimitForTx, reservedGas := getGasLimitForTx(
innerTxDataOffset,
transactionIndex,
gasPerPubdata,
L1_TX_INTRINSIC_L2_GAS(),
L1_TX_INTRINSIC_PUBDATA()
)
gasLimitForTx - is the gas limit for the transaction’s body. reservedGas - is the amount of gas that is beyond the operator’s trust limit to refund it back later.
Note: the operator’s trust limit is guaranteed to be at least MAX_GAS_PER_TRANSACTION which is at the moment 80000000 (check SystemConfig.json).
- Ensure that the deposited eth covers
txInternalCostandvalue.
let gasPrice := getMaxFeePerGas(innerTxDataOffset)
let txInternalCost := safeMul(gasPrice, gasLimit, "poa")
let value := getValue(innerTxDataOffset)
if lt(getReserved0(innerTxDataOffset), safeAdd(value, txInternalCost, "ol")) {
assertionError("deposited eth too low")
}
- Checks if gasLimitForTx is greater than
gasUsedOnPreparationand then callsgetExecuteL1TxAndGetRefundto attempt to execute the TX. This function returns apotentialRefundif there is any.
if gt(gasLimitForTx, gasUsedOnPreparation) {
let potentialRefund := 0
potentialRefund, success := getExecuteL1TxAndGetRefund(txDataOffset, sub(gasLimitForTx, gasUsedOnPreparation))
For example, let’s say the sender has 10_000_000 gas limit (after deducting the overhead ..etc.), and the TX execution consumed 3_000_000, then the potentialRefund is supposed to be 10_000_000-3_000_000 = 7_000_000. This will be returned to refundRecipient. However, if the actual TX execution fails, then potentialRefund will always be zero. Therefore, no refund for the sender at all. In other words, let’s say that the TX execution consumed only 500_000 only till it reverted (for whatever reason). so, the potentialRefund should be 9_500_000 which is not the case since it will be always zero on failure.
Note: obviously this is not applicable if L1->L2 transactions were set to be free. Check the comments here.
Reason & Explanation
This issue occurs due to the fact that near call opcode is used to execute the TX (to avoid 63/64 rule), and when the TX execution fails, near call panic is utilised to avoid reverting the bootloader and to revert minting ether to the user.
let gasBeforeExecution := gas()
success := ZKSYNC_NEAR_CALL_executeL1Tx(
callAbi,
txDataOffset
)
// If the success is zero, we will revert in order
// to revert the minting of ether to the user
if iszero(success) {
nearCallPanic()
}
Check zkSync specific opcodes: Generally accessible,
near_call. It is basically a “framed” jump to some location of the code of your contract. The difference between thenear_calland ordinary jump are:
- It is possible to provide an
ergsLimitfor it. Note, that unlike“far_call”s(i.e. calls between contracts) the 63/64 rule does not apply to them.- If the near call frame panics, all state changes made by it are reversed. Please note, that the memory changes will not be reverted.
Please note that the only way to revert only the near_call frame (and not the parent) is to trigger out of gas error or invalid opcode.
Check Simulations via our compiler: Simulating near_call (in Yul only)
Important note: the compiler behaves in a way that if there is a revert in the bootloader, the
ZKSYNC_CATCH_NEAR_CALLis not called and the parent frame is reverted as well. The only way to revert only thenear_callframe is to trigger VM’s panic (it can be triggered with either invalid opcode or out of gas error).
In ZKSYNC_NEAR_CALL_executeL1Tx, nearCallPanic() is called in case of TX failure. If we check nearCallPanic() function, we find that it exhausts all the gas of the current frame so that out of gas error is triggered.
/// @dev Used to panic from the nearCall without reverting the parent frame.
/// If you use `revert(...)`, the error will bubble up from the near call and
/// make the bootloader to revert as well. This method allows to exit the nearCall only.
function nearCallPanic() {
// Here we exhaust all the gas of the current frame.
// This will cause the execution to panic.
// Note, that it will cause only the inner call to panic.
precompileCall(gas())
}
Because of this, no matter how much gas was spent on the TX itself, if it fails, all the unused remaining gas will be burned.
According to the docs zkSync: Batch overhead & limited resources of the batch the refund should be provided at the end of the transaction.
Note, that before the transaction is executed, the system can not know how many of the limited system resources the transaction will actually take, so we need to charge for the worst case and provide the refund at the end of the transaction
On the surface, this might not look like a critical issue since the lost funds are relatively small. However, this may be true for normal or small transactions unlike computationally intensive tasks which may require big upfront payment. The lost funds will be not negligible.
Please refer to How baseFee works on zkSync
This does not actually matter a lot for normal transactions, since most of the costs will still go on pubdata for them. However, it may matter for computationally intensive tasks, meaning that for them a big upfront payment will be required, with the refund at the end of the transaction for all the overspent gas.
Please note that while there is MAX_TRANSACTION_GAS_LIMIT for the gasLimit, it may go way beyond the MAX_TRANSACTION_GAS_LIMIT (since the contracts can be 10s of kilobytes in size). This is called Trusted gas limit which is provided by the operator.
Please check Trusted gas limit
the operator may provide the trusted gas limit, i.e. the limit which exceeds
MAX_TRANSACTION_GAS_LIMITassuming that the operator knows what he is doing (e.g. he is sure that the excess gas will be spent on the pubdata).
From this, we conclude that the upper limit for the loss is the cost of the gaslimit provided by the user which could possibly be a big amount of payment to cover complex tasks (assuming the operator provided a trusted gas limit bigger or equal to gaslimit provided by the user).
It’s worth mentioning, that this breaks the trust assumptions that the users have, since the users assume that they will always get a refund if it wasn’t consumed by their requested TX.
For the reasons explained above, I’ve set the severity to high.
Proof of Concept
We have one test file. This file demonstrates two cases:
test_actual_gas_spent_on_success()- When the TX succeed, the potential refund holds the actual remaining gas to be refunded.test_no_gas_refund_on_failure- When the TX fails, the potential refund is zero.
Important notes:
- While the test is not executed via zkEVM, the only specific opcode for this PoC is, the near call panic. For this reason, it is simulated.
- The code is written in solidity for simplicity. However, for this PoC, it is good enough to demonstrate and prove the issue.
To run the test file:
forge test --via-ir -vv
You should get the following output:
[PASS] test_actual_gas_spent_on_success() (gas: 47121)
Logs:
Nearcall callAbi: 100000000
gasSpentOnExecution: 31533
success: true
potentialRefund: 99968467
[PASS] test_no_gas_refund_on_failure() (gas: 101606767)
Logs:
Nearcall callAbi: 100000000
gasSpentOnExecution: 101591221
success: false
potentialRefund: 0
Test result: ok. 2 passed; 0 failed; finished in 604.85ms
Test File
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// PoC => No refund for gas on `L1->L2` tx failure, it always burns the gas even if not used
import {Test} from "forge-std/Test.sol";
import "forge-std/console.sol";
import {DSTest} from "ds-test/test.sol";
uint256 constant OVERHEAD_TX = 100_000; // assume overhead as 100000
uint256 constant GAS_PREP = 2000; // assume preparation value
contract ExternalContract {
uint256 varState;
function doSomething(uint256 num) external {
varState = 1;
// revert if num is zero to cause nearCallPanic later
if (num == 0) {
revert("something wrong happened");
}
}
}
interface IExternalContract {
function doSomething(uint256 num) external;
}
interface IBooloaderMock {
function ZKSYNC_NEAR_CALL_SIMULATION_executeL1Tx(
uint256 callAbi,
bytes memory txCalldataEncoded
) external;
}
contract BooloaderMock {
ExternalContract externalContract;
constructor() {
externalContract = new ExternalContract();
}
/// @dev The overhead in gas that will be used when checking whether the context has enough gas, i.e.
/// when checking for X gas, the context should have at least X+CHECK_ENOUGH_GAS_OVERHEAD() gas.
function CHECK_ENOUGH_GAS_OVERHEAD() internal pure returns (uint256 ret) {
ret = 1000000;
}
function checkEnoughGas(uint256 gasToProvide) internal view {
// Using margin of CHECK_ENOUGH_GAS_OVERHEAD gas to make sure that the operation will indeed
// have enough gas
// CHECK_ENOUGH_GAS_OVERHEAD => 1_000_000
if (gasleft() < (gasToProvide + CHECK_ENOUGH_GAS_OVERHEAD())) {
revert("No enough gas");
}
}
function notifyExecutionResult(bool success) internal {}
function nearCallPanic() internal pure {
// Here we exhaust all the gas of the current frame.
// This will cause the execution to panic.
// Note, that it will cause only the inner call to panic.
uint256 x = 0;
while (true) {
x += 1;
}
}
// simulation of near call
function ZKSYNC_NEAR_CALL_SIMULATION_executeL1Tx(
uint256 callAbi,
bytes memory txCalldataEncoded
) public {
(bool success, ) = address(externalContract).call{gas: callAbi}(
txCalldataEncoded
);
if (!success) {
// nearCall panic
nearCallPanic();
}
}
function getExecuteL1TxAndGetRefund(
uint256 gasForExecution,
bytes memory txCalldataExternalContract
) internal returns (uint256 potentialRefund, bool success) {
uint256 callAbi = gasForExecution;
checkEnoughGas(gasForExecution);
uint256 gasBeforeExecution = gasleft();
bytes memory txCalldataEncoded = abi.encodeCall(
IBooloaderMock.ZKSYNC_NEAR_CALL_SIMULATION_executeL1Tx,
(callAbi, txCalldataExternalContract)
);
console.log("Nearcall callAbi: %d", callAbi);
// pass 64/63 to simulate nearCall that doesn't follow this 63/64 rule
uint256 fullGas = (callAbi * 64) / 63;
(success, ) = address(this).call{gas: fullGas}(txCalldataEncoded);
notifyExecutionResult(success);
uint256 gasSpentOnExecution = gasBeforeExecution - gasleft();
console.log("gasSpentOnExecution: %d", gasSpentOnExecution);
if (gasSpentOnExecution <= gasForExecution) {
potentialRefund = gasForExecution - gasSpentOnExecution;
}
}
function processL1Tx(
uint256 l2ValueProvidedByUser,
uint256 gasLimitProvidedByUser,
bytes memory txCalldataExternalContract
) external payable returns (uint256 potentialRefund, bool success) {
uint256 overheadTX = OVERHEAD_TX; // assume overhead for simplicity
uint256 gasLimitForTx = gasLimitProvidedByUser - overheadTX;
uint256 gasUsedOnPreparation = GAS_PREP; // assume preparation value simplicity
uint256 gasLimit = gasLimitProvidedByUser;
uint256 gasPrice = 13e9;
uint256 txInternalCost = gasPrice * gasLimit;
require(
msg.value >= l2ValueProvidedByUser + txInternalCost,
"deposited eth too low"
);
require(gasLimitForTx > gasUsedOnPreparation, "Tx didn't continue");
(potentialRefund, success) = getExecuteL1TxAndGetRefund(
(gasLimitForTx - gasUsedOnPreparation),
txCalldataExternalContract
);
}
}
contract BootloaderMockTest is DSTest, Test {
BooloaderMock bootloaderMock;
function setUp() public {
bootloaderMock = new BooloaderMock();
vm.deal(address(this),100 ether);
}
function test_no_gas_refund_on_failure() public {
uint256 gasLimitByUser = 100_000_000 + OVERHEAD_TX + GAS_PREP;
uint256 l2Value = 0;
bytes memory txCalldataExternalContract = abi.encodeCall(
IExternalContract.doSomething,
(0) // value 0 cause the call to fail
);
(uint256 potentialRefund, bool success) = bootloaderMock.processL1Tx{
value: 10 ether
}(l2Value, gasLimitByUser, txCalldataExternalContract);
console.log("success: ", success);
console.log("potentialRefund: %d", potentialRefund);
}
function test_actual_gas_spent_on_success() public {
uint256 gasLimitByUser = 100_000_000 + OVERHEAD_TX + GAS_PREP;
uint256 l2Value = 0;
bytes memory txCalldataExternalContract = abi.encodeCall(
IExternalContract.doSomething,
(1) // value 1 makes the call successful
);
(uint256 potentialRefund, bool success) = bootloaderMock.processL1Tx{
value: 10 ether
}(l2Value, gasLimitByUser, txCalldataExternalContract);
console.log("success: ", success);
console.log("potentialRefund: %d", potentialRefund);
}
}
Recommended Mitigation Steps
One suggestion is to use invalid opcode instead of burning gas.
miladpiri (zkSync) confirmed, but disagreed with severity and commented:
It is Low. The user does indeed lose all the gas if success is false, as we invoke the
nearCallPanicthat burns all the gas in the near call (which is basically all the execution gas).
Alex the Entreprenerd (judge) decreased severity to QA and commented:
I believe the design decision makes sense, a
L1->L2must consume all gas as otherwise computing the refunds would be ripe for exploitation.
koolexcrypto (warden) commented:
@Alex the Entreprenerd - I kindly ask you to re-evaluate the issue above considering the following:
EVM non-equivalence
If you check the differences from Ethereum at zkSync docs, EVM REVERT instruction is not listed. Meaning that, REVERT behaviour in zkSync Era VM should behave exactly the same as in EVM. Here is a simple summary of how the EIP-140 defines the REVERT instruction:
The REVERT instruction provides a way to stop execution and revert state changes, without consuming all provided gas and with the ability to return a reason.
The REVERT instruction behaviour is also confirmed in the ZkSync Era Virtual Machine primer document which says:
Revert: a recoverable error happened. Unspent gas is returned to the caller, which will execute the exception handler. The instruction is revert.
However, it is not the case as demonstrated in the issue above. This is due to the fact that when a TX (requested from L1) fails due to a REVERT opcode in the contract code of the callee, all the remaining gas (from the actual execution gas) will be always consumed. This behaviour is similar to the deprecated/removed opcode
throwfrom EVM.EVM Behaviour Discrepancy
Assume we have:
- User X - who is the caller.
- Contract C - on L2 which is the callee and has Function F.
- Function F - which consists of 1000 lines of code. However at line 4, there is a revert.
Let’s check two scenarios:
- User X calls Function F by sending an L2 TX directly, at line 4, the execution finishes due to the revert and all unspent gas is returned to the caller.
- User X calls the same function Function F by sending an L2 TX indirectly (i.e. requested from L1), at line 4, the execution stops due to the revert and all unspent gas is consumed anyway.
In the first scenario, REVERT behaves exactly as expected. In the second scenario, REVERT behaves as if it was the deprecated/removed opcode
throw. Please note that we are talking here about the actual execution gas.From this, we can see how the Bootloader breaks the zkSync Era VM consistency
zkSync Era’s fee model
Some distinctive feature of zkSync Era’s fee model is the abundance of refunds for unused limited system resources and overpaid computation. Please check High-level: conclusion which says:
The other distinctive feature of the fee model used on zkSync is the abundance of refunds, i.e.:
- For unused limited system resources.
- For overpaid computation.
Also, In the Fee mechanism docs which says:
zkSync Era’s fee model is similar to Ethereum’s where gas is charged for computational cost, cost of publishing data on-chain and storage effects.
The above issue shows how the implementation is not aligned with this design since it charges for opcodes that were never executed (i.e. opcodes that happen to be after a REVERT). Therefore, no resources have been used for it.
In the light of this basis, the unspent gas should be returned to the user especially when the user can not influence the relevant logic in the bootloader by any mean. In other words, we have 4 actors:
- Caller
- Callee
- Operator
- zkSync Era VM
When the caller sends an L2 TX (from L1), It seems that the caller can not provide any input that could prevent the issue from occuring.
Lastly, it breaks the trust assumption that the users (or protocols) have, since they assume that they will receive back their unspent gas from the actual execution gas since it wasn’t consumed by the TX.
Alex the Entreprenerd (judge) increased severity to Medium and commented:
I went back and forth on this finding a bit because:
- It’s impossible to determine what a reverted transaction should have cost.
- Meaning that it’s impossible for zkSync to calculate the refund based on the fair value of the expected tx cost.
- However, it is fair to expect that just like a normal revert, the cost paid would be capped at the gas spent and not at the gas set as limit.
Due to this, the finding can be interpreted as saying that:
L1->L2txs that revert will consume all gas. With this being inconsistent for the EVM, and potentially risky for end users, I agree with Medium SeverityThe gas used is known and a refund could be computed, which is, in my opinion, inconsistent with other behaviours of the system. I understand the Sponsor may elect for a nofix, and that this would be a warning to developers/end-users/integrators.
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 bootloader uses a near-call panic (exhausting all gas) to revert the transaction execution without reverting the whole bootloader, which also prevents any refund of unused gas.
Impact
Senders of failed L1-to-L2 transactions lose all unused execution gas, which could be a large amount for computationally intensive transactions with high gas limits.
What the attacker needed
Ability to send L1->L2 transactions; transaction execution fails on L2
The fix
The report suggests using an invalid opcode instead of burning gas, but the text does not state that a fix was applied.
How it was found
- Send an L1-to-L2 transaction with a gas limit that covers overhead and execution.
- The bootloader calculates the gas limit for the transaction body and checks the deposited ETH.
- The bootloader attempts to execute the transaction via a near call.
- If the transaction execution fails, the bootloader calls nearCallPanic, which exhausts all gas in the near call frame.
- As a result, the potential refund is zero and the sender loses all unused gas.
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, you can send a transaction from Ethereum (L1) to zkSync (L2). You pay upfront for a maximum amount of gas. If the transaction fails on L2, the system should return the gas you did not use. Instead, it burns all the remaining gas, so you lose money even though the transaction did not run to completion.
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.