Skip to content
Business logic Audit finding Advanced

Usage of **revert** in case of low gas in `L1CrossDomainMessenger` can result in loss of fund

The L1CrossDomainMessenger uses a revert opcode when the gas check fails during relayMessage, which prevents the failed message flag from being set. If the gas check fails after the withdrawal is finalized in OptimismPortal, the user's funds are lost because the withdrawal cannot be replayed.

HE1M Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-03-optimism-judging/issues/27

Found by

HE1M

Summary

I am reporting this issue separate from my other report Causing users lose fund if bridging long message from L2 to L1 due to uncontrolled out-of-gas error, as I think they provide different attack surface and vulnerability.

In the previous report, it is explained that if the forwarded gas passes the gas condition in OptimismPortal, but goes out of gas in L1CrossDomainMessenger, it will result in loss of fund since baseGas does not consider the effect of memory expansion.

But, in this report, I am going to explain that due to usage of revert opcode instead of return, users can lose fund if for any reason the gas left is not meeting the condition in L1CrossDomainMessenger.

In other words, there is a check for the amount of gas provided in callWithMinGas. This check is invoked twice: One in OptimismPortal.finalizeWithdrawalTransaction and one in L1CrossDomainMessenger.relayMessage. If the first check is passed, and the second check is not passed, the users' withdrawal transactions are considered as finalized, but not considered as failed message. So, they can not replay their withdrawal transactions.

Vulnerability Detail

Suppose Alice (an honest user) intends to bridge a message from L2 to L1 by calling sendMessage: https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L247

The amount of required gas is calculated in the function baseGas: https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L258 https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L423

Suppose some time is passed, and an EIP is proposed so that the gas consumption of some opcodes are changed. During this time, still Alice's withdrawal transaction is not executed on L1 yet.

Bob (the attacker), after proving the Alice's withdrawal transaction and passing the challenge period, calls finalizeWithdrawalTransaction to finalize Alice's withdrawal transaction. https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/L1/OptimismPortal.sol#L315

Bob provides the the required gas calculated by baseGas function on L2. This amount of gas passes the check in OptimimPortal. So, the mapping finalizedWithdrawals is set to true. https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/L1/OptimismPortal.sol#L397 https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/libraries/SafeCall.sol#L64 https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/L1/OptimismPortal.sol#L383

And the left gas will be forwarded to L1CrossDomainMessenger calling relayMessage. https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L291

Suppose for any reason (like what assumed above: an EIP was proposed and it changed some opcodes gas), the gas consumption in relayMessage is more than expected, so that it does not pass gas condition: https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L361 https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/libraries/SafeCall.sol#L64

Since, it does not pass the gas condition, it will revert in Line 82.

But, the revert here (in L1CrossDomainMessenger.relayMessage) is not correct. Because, the whole transaction of relayMessage will be reverted so it will not set the flag failedMessages[versionedHash] as true. https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L368

Since, the withdrawal transaction is set as finalized in OptimismPortal but not set as failed in L1CrossDomainMessenger, it can not be replayed, and Alice loses her fund.

Impact

Causing users lose fund.

Code Snippet

Tool used

Manual Review

Recommendation

I would say that the callWithMinGas should return false instead of revert when called during L1CrossDomainMessenger.relayMessage if the condition of required gas is not met. The following modification in L1CrossDomainMessenger.relayMessage is recommended.

Solidity
function relayMessage(
        uint256 _nonce,
        address _sender,
        address _target,
        uint256 _value,
        uint256 _minGasLimit,
        bytes calldata _message
    ) external payable {
        // ...
        //bool success = SafeCall.callWithMinGas(_target, _minGasLimit, _value, _message);
        bool success;
        bytes memory _calldata = _message;
        if (gasleft() >= ((_minGasLimit + 200) * 64) / 63) {
            assembly {
                success := call(
                    gas(), // gas
                    _target, // recipient
                    _value, // ether value
                    add(_calldata, 32), // inloc
                    mload(_calldata), // inlen
                    0x00, // outloc
                    0x00 // outlen
                )
            }
        }
        // ...
    }

Discussion

maurelian

This one is tricky. The recommendation is a good one, and we agree that it is better to avoid reverting on this code path, however it is also speculative as it depends on some future gas schedule changes and lacks a PoC.

GalloDaSballo

Per the rules:

I don't think the pre-conditions"have a reasonable chance of becoming true in the future", as this would require:

  • Computing incorrect limit
  • Waiting for Hardfork
  • Hardfork does change CALL / base level costs (risk of every transfer contract being bricked)
  • User is oblivious and doesn't fix
  • Relaying (and getting griefed)

GalloDaSballo

Recommend: Closing as invalid

HE1M

Escalate for 10 USDC

Using revert in L1CrossDomainMessenger clearly breaks the replayability guarantee of the project. In the report it is mentioned that:

due to usage of revert opcode instead of return, users can lose fund if for any reason the gas left is not meeting the condition in L1CrossDomainMessenger

I agree with your comment regarding Future issues, but the EIP proposed assumption, is just an example for better understanding.

In other words, the project tried to estimate the gas consumption accurately enough when crossing the message from L2 to L1, so that correct amount of gas is forwarded to the target. If, in the middle something happens, there is replayability mechanism to guarantee that the users are able to retry their withdrawal. But, due to this wrong usage of revert all those efforts can be broken, and users' fund can be lost.

Please also note that providing a scenario to reach to the revert opcode in L1CrossDomainMessenger.relayMessage should be considered as a separate bug, because there were lots of effort to calculate the correct gas estimation. So, reaching to this line of code means that something is broken in the middle.

For example, due to math miscalculation of the gas (as reported in this contest like: #40 #5 , ...) the revert is reachable and can result to loss of fund. But, if revert was not used, the message would be considered as failed, and the tx would be able to be replayed. So, other bugs would have less impact.

All in all, this wrong usage of revert is a bug besides other bugs (math miscalculations) that lead to reaching to this line of code.

For example, in the following function sampleFunction, there are lots of processing in Part A to ensure that x is bigger than 10. If x is lower than 10, Part B is reachable and will be executed.

Solidity
function sampleFunction() {
        // Part A: all the processing to ensure x is bigger than 10
        if(x < 10){
            // Part B: some other processing
        }
        // remaining of the code
    }

In this sample code, there are two ways to find bug:

  1. Finding a bug in Part A that leads to x lower than 10
  2. Finding a bug in Part B

You can replace, the gasLeft() with x, 10 with ((_minGasLimit + 200) * 64) / 63), Part A with all the math calculations for the gas estimation, and Part B with the lines.

In this report, I found a bug in Part B which should be considered separate from the bug related to Part A.

sherlock-admin

Escalate for 10 USDC

Using revert in L1CrossDomainMessenger clearly breaks the replayability guarantee of the project. In the report it is mentioned that:

due to usage of revert opcode instead of return, users can lose fund if for any reason the gas left is not meeting the condition in L1CrossDomainMessenger

I agree with your comment regarding Future issues, but the EIP proposed assumption, is just an example for better understanding.

In other words, the project tried to estimate the gas consumption accurately enough when crossing the message from L2 to L1, so that correct amount of gas is forwarded to the target. If, in the middle something happens, there is replayability mechanism to guarantee that the users are able to retry their withdrawal. But, due to this wrong usage of revert all those efforts can be broken, and users' fund can be lost.

Please also note that providing a scenario to reach to the revert opcode in L1CrossDomainMessenger.relayMessage should be considered as a separate bug, because there were lots of effort to calculate the correct gas estimation. So, reaching to this line of code means that something is broken in the middle.

For example, due to math miscalculation of the gas (as reported in this contest like: #40 #5 , ...) the revert is reachable and can result to loss of fund. But, if revert was not used, the message would be considered as failed, and the tx would be able to be replayed. So, other bugs would have less impact.

All in all, this wrong usage of revert is a bug besides other bugs (math miscalculations) that lead to reaching to this line of code.

For example, in the following function sampleFunction, there are lots of processing in Part A to ensure that x is bigger than 10. If x is lower than 10, Part B is reachable and will be executed.

Solidity
function sampleFunction() {
        // Part A: all the processing to ensure x is bigger than 10
        if(x < 10){
            // Part B: some other processing
        }
        // remaining of the code
    }

In this sample code, there are two ways to find bug:

  1. Finding a bug in Part A that leads to x lower than 10
  2. Finding a bug in Part B

You can replace, the gasLeft() with x, 10 with ((_minGasLimit + 200) * 64) / 63), Part A with all the math calculations for the gas estimation, and Part B with the lines.

In this report, I found a bug in Part B which should be considered separate from the bug related to Part A.

You've created a valid escalation for 10 USDC!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

hrishibhat

Escalation accepted

After further discussions with the Lead judge and the protocol this issue was considered to be a weak version of #40, hence considering this issue a valid solo medium.

sherlock-admin

Escalation accepted

After further discussions with the Lead judge and the protocol this issue was considered to be a weak version of #40, hence considering this issue a valid solo medium.

This issue's escalations have been accepted!

Contestants' payouts and scores will be updated according to the changes made on this issue.

The original report as published on Sherlock, by HE1M. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 use of a revert opcode instead of returning false in the gas check within relayMessage, which undoes the entire transaction and prevents the failed message flag from being set.

Impact

Users could lose their funds if the gas check fails during relayMessage, as the withdrawal is finalized but not marked as failed, preventing replay.

What the attacker needed

Victim bridges a message from L2 to L1; attacker finalizes the withdrawal; gas check fails in relayMessage

The fix

The callWithMinGas function should return false instead of reverting when called during relayMessage if the gas condition is not met.

How it was found

  1. Alice bridges a message from L2 to L1 by calling sendMessage.
  2. Bob finalizes Alice's withdrawal transaction by calling finalizeWithdrawalTransaction with the required gas, which passes the gas check in OptimismPortal and sets finalizedWithdrawals to true.
  3. The remaining gas is forwarded to L1CrossDomainMessenger.relayMessage, but the gas check fails due to increased gas consumption.
  4. relayMessage reverts, so the failedMessages flag is not set to true.
  5. Alice's withdrawal is finalized but not marked as failed, so she cannot replay it and loses her funds.

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 move funds between blockchains, a message is sent to the other side. If something goes wrong during processing, the system should mark the message as failed so you can try again. Here, a coding mistake causes the whole process to be undone without marking it as failed, so your funds get stuck.

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 HE1M

View all

14 more disclosed reports, best first.

More from Optimism Update

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.