CrossDomainMessenger does not successfully guarantee replayability, can lose user funds
The CrossDomainMessenger's replayability guarantee can be broken because the gas forwarded to the target contract may leave insufficient gas for the messenger to mark the transaction as failed, while the portal has already marked it as finalised. This can permanently brick a user's withdrawal, causing loss of funds.
Source: https://github.com/sherlock-audit/2023-03-optimism-judging/issues/96
Found by
obront
Summary
While SafeCall.callWithMinGas successfully ensures that the called function will not revert, it does not ensure any remaining buffer for continued execution on the calling contract.
As a result, there are situations where OptimismPortal can be called with an amount of gas such that the remaining gas after calling L1CrossDomainMessenger is sufficient to finalize the transaction, but such that the remaining gas after L1CrossDomainMessenger makes its call to target is insufficient to mark the transaction as successful or failed.
In any of these valid scenarios, users who withdraw using the L1CrossDomainMessenger (expecting replayability) will have their withdrawals bricked, permanently losing their funds.
Vulnerability Detail
When a user performs a withdrawal with the L1CrossDomainMessenger, they include a gasLimit value, which specifies the amount of gas that is needed for the function to execute on L1.
This value is translated into two separate values:
-
The
OptimismPortalsends at leastbaseGas(_message, _minGasLimit) = 64/63 * _minGasLimit + 16 * data.length + 200_000toL1CrossDomainMessenger, which accounts for the additional overhead used by the Cross Domain Messenger. -
The
L1CrossDomainMessengersends at least_minGasLimitto the target contract.
The core of this vulnerability is in the fact that, if:
OptimismPortalretains sufficient gas after its call to complete the transaction, andL1CrossDomainMessengerruns out of gas after its transaction is complete (even if the tx succeeded)
...then the result will be that the transaction is marked as finalized in the Portal (disallowing it from being called again), while the Cross Domain Messenger transaction will revert, causing the target transaction to revert and not setting it in failedMessages (disallowing it from being replayed). The result is that the transaction will be permanently stuck.
Calcuations
Let's run through the math to see how this might unfold. We will put aside the additional gas allocated for calldata length, because this amount is used up in the call and doesn't materially impact the calculations.
When the OptimismPortal calls the L1CrossDomainMessenger, it is enforced that the gas sent will be greater than or equal to _minGasLimit * 64/63 + 200_000.
This ensures that the remaining gas for the OptimismPortal to continue execution after the call is at least _minGasLimit / 64 + 3125. Even assuming that _minGasLimit == 0, this is sufficient for OptimismPortal to complete execution, so we can safely say that any time OptimismPortal.finalizeWithdrawalTransaction() is called with sufficient gas to pass the SafeCall.callWithMinGas() check, it will complete execution.
Moving over to L1CrossDomainMessenger, our call begins with at least _minGasLimit * 64/63 + 200_000 gas. By the time we get to the external call, we have remaining gas of at least _minGasLimit * 64/63 + 158_998. This leaves us with the following guarantees:
- Gas available for the external call will be at least 63/64ths of that, which equals
_minGasLimit + 156_513. - Gas available for continued execution after the call will be at least 1/64th of that, which equals
_minGasLimit * 1/63 + 3125.
The additional gas required to mark the transaction as failedMessages[versionedHash] = true and complete the rest of the execution is 23,823.
Therefore, in any situation where the external call uses all the available gas will revert if _minGasLimit * 1/63 + 3125 < 23_823, which simplifies to _minGasLimit < 1_303_974. In other words, in most cases.
However, it should be unusual for the external call to use all the available gas. In most cases, it should only use _minGasLimit, which would leave 156_513 available to resolve this issue.
So, let's look at some examples of times when this may not be the case.
At Risk Scenarios
There are several valid scenarios where users might encounter this issue, and have their replayable transactions stuck:
User Sends Too Little Gas
The expectation when using the Cross Domain Messenger is that all transactions will be replayable. Even if the _minGasLimit is set incorrectly, there will always be the opportunity to correct this by replaying it yourself with a higher gas limit. In fact, it is a core tenet of the Cross Domain Messengers that they include replay protection for failed transactions.
However, if a user sets a gas limit that is too low for a transaction, this issue may result.
The consequence is that, while users think that Cross Domain Messenger transactions are replayable and gas limits don't need to be set precisely, they can in fact lose their entire withdrawal if they set their gas limit too low, even when using the "safe" Standard Bridge or Cross Domain Messenger.
Target Contract Uses More Than Minimum Gas
The checks involved in this process ensure that sufficient gas is being sent to a contract, but there is no requirement that that is all the gas a contract uses.
_minGasLimit should be set sufficiently high for the contract to not revert, but that doesn't mean that _minGasLimit represents the total amount of gas the contract uses.
As a silly example, let's look at a modified version of the gas() function in your Burn.sol contract:
function gas(uint256 _amountToLeave) internal view {
uint256 i = 0;
while (gasleft() > _amountToLeave) {
++i;
}
}
This function runs until it leaves a specified amount of gas, and then returns. While the amount of gas sent to this contract could comfortably exceed the _minGasLimit, it would not be safe to assume that the amount leftover afterwards would equal startingGas - _minGasLimit.
While this is a contrived example, but the point is that there are many situations where it is not safe to assume that the minimum amount of gas a function needs will be greater than the amount it ends up using, if it is provided with extra gas.
In these cases, the assumption that our leftover gas after the function runs will be greater than the required 1/64th does not hold, and the withdrawal can be bricked.
Impact
In certain valid scenarios where users decide to use the "safe" Cross Domain Messenger or Standard Bridge with the expectation of replayability, their withdrawals from L2 to L1 can be bricked and permanently lost.
Code Snippet
Tool used
Manual Review
Recommendation
L1CrossDomainMessenger should only send _minGasLimit along with its call to the target (rather than gas()) to ensure it has sufficient leftover gas to ensure replayability.
Discussion
hrishibhat
Sponsor comment: Tentatively marking this as medium for several reasons. First, this issue can only be encountered through user misconfiguration. If a sufficient minimum gas limit is supplied to the withdrawal transaction, it won't occur. However, it is a valid issue and it does break the CrossDomainMessenger's replayability guarantee.
GalloDaSballo
Agree with Medium, because reliant on the specific tx type as well as the user mistake
Agree with maintaining Medium because the code's goal is to ensure replayability even if the tx run OOG, expectation which the POC shows can be broken
GalloDaSballo
Made #5 primary
zobront
Escalate for 10 USDC
This was dup'd with #5, but they appear to be different issues.
#5 focuses on the risks of long message data increasing gas costs. This is more of an obscure edge case (and has a live escalation about the validity of their hash function assumptions, which I don't have an opinion on.)
However, this is a separate issue, which focuses on the risks around gas usage in the target contract and how it can break the replayability guarantee.
sherlock-admin
Escalate for 10 USDC
This was dup'd with #5, but they appear to be different issues.
#5 focuses on the risks of long message data increasing gas costs. This is more of an obscure edge case (and has a live escalation about the validity of their hash function assumptions, which I don't have an opinion on.)
However, this is a separate issue, which focuses on the risks around gas usage in the target contract and how it can break the replayability guarantee.
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
Considering this issue a separate valid medium
sherlock-admin
Escalation accepted
Considering this issue a separate valid medium
This issue's escalations have been accepted!
Contestants' payouts and scores will be updated according to the changes made on this issue.
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 L1CrossDomainMessenger forwards all remaining gas to the target contract, which can consume the gas needed to record a failed message, breaking replayability.
Impact
In certain valid scenarios, withdrawals from L2 to L1 can be bricked and permanently lost, though this requires user misconfiguration or a target contract that uses more than the minimum gas.
What the attacker needed
User sets a low gas limit; or target contract uses more than minimum 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.
When you send funds from one blockchain to another, a 'bridge' contract handles the transfer. This bridge promises that if something goes wrong, you can retry the transfer later. The bug is that in some cases the bridge runs out of gas at the wrong moment, so it cannot record the failure, and the transfer gets stuck forever.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.