All migrated withdrarwals that require more than 135,175 gas may be bricked
Migrated withdrawals are encoded with an inner gas limit of zero and an outer gas limit of calldata cost plus 200,000, assuming the CrossDomainMessenger can always be replayed. For withdrawals that consume more than 135,175 gas, insufficient gas can cause the CrossDomainMessenger call to revert while the Portal still finalises the withdrawal, permanently bricking the funds.
Migrated withdrawals are given an "outer" (Portal) gas limit of calldata cost + 200,000, and an "inner" (CrossDomainMessenger) gas limit of 0. The assumption is that the CrossDomainMessenger is replayable, so there is no need to specify a correct gas limit.
This is an incorect assumption. For any withdrawals that require more than 135,175 gas, insufficient gas can be sent such that CrossDomainMessenger's external call reverts and the remaining 1/64th of the gas sent is not enough for replayability to be encoded in the Cross Domain Messenger.
However, the remaining 1/64th of gas in the Portal is sufficient to have the transaction finalize, so that the Portal will not process the withdrawal again.
Vulnerability Detail
When old withdrawals are migrated to Bedrock, they are encoded as calls to L1CrossDomainMessenger.relayMessage() as follows:
Go
func MigrateWithdrawal(withdrawal *LegacyWithdrawal, l1CrossDomainMessenger *common.Address) (*Withdrawal, error) {
// Attempt to parse the value
value, err := withdrawal.Value()
if err != nil {
return nil, fmt.Errorf("cannot migrate withdrawal: %w", err)
}
abi, err := bindings.L1CrossDomainMessengerMetaData.GetAbi()
if err != nil {
return nil, err
}
// Migrated withdrawals are specified as version 0. Both the
// L2ToL1MessagePasser and the CrossDomainMessenger use the same
// versioning scheme. Both should be set to version 0
versionedNonce := EncodeVersionedNonce(withdrawal.XDomainNonce, new(big.Int))
// Encode the call to `relayMessage` on the `CrossDomainMessenger`.
// The minGasLimit can safely be 0 here.
data, err := abi.Pack(
"relayMessage",
versionedNonce,
withdrawal.XDomainSender,
withdrawal.XDomainTarget,
value,
new(big.Int), // <= THIS IS THE INNER GAS LIMIT BEING SET TO ZERO
[]byte(withdrawal.XDomainData),
)
if err != nil {
return nil, fmt.Errorf("cannot abi encode relayMessage: %w", err)
}
gasLimit := MigrateWithdrawalGasLimit(data)
w := NewWithdrawal(
versionedNonce,
&predeploys.L2CrossDomainMessengerAddr,
l1CrossDomainMessenger,
value,
new(big.Int).SetUint64(gasLimit), // <= THIS IS THE OUTER GAS LIMIT BEING SET
data,
)
return w, nil
}
As we can see, the relayMessage() call uses a gasLimit of zero (see comments above), while the outer gas limit is calculated by the MigrateWithdrawalGasLimit() function:
Go
func MigrateWithdrawalGasLimit(data []byte) uint64 {
// Compute the cost of the calldata
dataCost := uint64(0)
for _, b := range data {
if b == 0 {
dataCost += params.TxDataZeroGas
} else {
dataCost += params.TxDataNonZeroGasEIP2028
}
}
// Set the outer gas limit. This cannot be zero
gasLimit := dataCost + 200_000
// Cap the gas limit to be 25 million to prevent creating withdrawals
// that go over the block gas limit.
if gasLimit > 25_000_000 {
gasLimit = 25_000_000
}
return gasLimit
}
This calculates the outer gas limit value by adding the calldata cost to 200,000.
Let's move over to the scenario in which these values are used to see why they can cause a problem.
When a transaction is proven, we can call OptimismPortal.finalizeWithdrawalTransaction() to execute the transaction. In the case of migrated withdrawals, this executes the following flow:
OptimismPortal calls to L1CrossDomainMessenger with a gas limit of 200,000 + calldata
This guarantees remaining gas for continued execution after the call of (200_000 + calldata) * 64/63 * 1/64 > 3174
XDM uses 41,002 gas before making the call, leaving 158,998 remaining for the call
The SafeCall.callWithMinGas() succeeds, since the inner gas limit is set to 0
If the call uses up all of the avaialble gas (succeeding or reverting), we are left with 158,998 * 1/64 = 2,484 for the remaining execution
The remaining execution includes multiple SSTOREs which totals 23,823 gas, resulting in an OutOfGas revert
In fact, if the call uses any amount greater than 135,175, we will have less than 23,823 gas remaining and will revert
As a result, none of the updates to L1CrossDomainMessenger occur, and the transaction is not marked in failedMessages for replayability
However, the remaining 3174 gas is sufficient to complete the transction on the OptimismPortal, which sets finalizedWithdrawals[hash] = true and locks the withdrawals from ever being made again
Impact
Any migrated withdrawal that uses more than 135,175 gas will be bricked if insufficient gas is sent. This could be done by a malicious attacker bricking thousands of pending withdrawals or, more likely, could happen to users who accidentally executed their withdrawal with too little gas and ended up losing it permanently.
There doesn't seem to be an easy fix for this, except to adjust the migration process so that migrated withdrawals are directly saved as failedMessages on the L1CrossDomainMessenger (and marked as finalizedWithdrawals on the OptimismPortal), rather than needing to be reproven through the normal flow.
Discussion
maurelian
Valid but we believe it to be a medium. There definitely exist edge cases of transactions where this is an issue but the majority of transactions it is not an issue.
Based on the following call trace for a finalization of a withdrawal transaction + the address mapping, we believe that this issue is unable to impact transactions transferring ERC20 tokens through the bridge.
I believe there are some cases in which the above txs, which have corresponding events, will require more than 135k gas meaning they are subject to the attack
While the issue is creatively accurate with the specified gas values, it still requires certain conditions to be feasbile (e.g. only withdrarwals require more than 135,175 gas).
Causes a loss of funds but requires certain external conditions or specific states
Lastly, all the respect to the good efforts put in behind this finding.
sherlock-admin
Escalate for 10 USDC.
While the issue is creatively accurate with the specified gas values, it still requires certain conditions to be feasbile (e.g. only withdrarwals require more than 135,175 gas).
Causes a loss of funds but requires certain external conditions or specific states
Lastly, all the respect to the good efforts put in behind this finding.
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 rejected
Lead Judge comment:
Maintain High Severity because while the condition is necessary it is not up to the user to decide whether that requirement was met but rather the requirement is imposed by the script
This is a valid flaw in the system due to an incorrect assumption.
sherlock-admin
Escalation rejected
Lead Judge comment:
Maintain High Severity because while the condition is necessary it is not up to the user to decide whether that requirement was met but rather the requirement is imposed by the script
This is a valid flaw in the system due to an incorrect assumption.
This issue's escalations have been rejected!
Watsons who escalated this issue will have their escalation amount deducted from their next payout.
The original report as published on Sherlock, by obront. 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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the migration sets the inner gas limit to zero and the outer gas limit too low, so the CrossDomainMessenger can run out of gas before it records the message for replay, while the Portal still finalises the withdrawal.
What the attacker needed
A migrated withdrawal; withdrawal uses more than 135,175 gas; insufficient gas sent on finalisation
The fix
The migration process should be adjusted so that migrated withdrawals are directly saved as failedMessages on the L1CrossDomainMessenger and marked as finalizedWithdrawals on the OptimismPortal, rather than being reproven through the normal flow.
How it was found
A migrated withdrawal is encoded as a call to relayMessage with an inner gas limit of zero and an outer gas limit of calldata cost plus 200,000.
The user calls finalizeWithdrawalTransaction on the OptimismPortal with insufficient gas.
The Portal calls the L1CrossDomainMessenger, which uses 41,002 gas before making the external call.
The external call consumes more than 135,175 gas, leaving less than 23,823 gas for the remaining execution.
The remaining execution runs out of gas during SSTOREs and reverts, so the message is not recorded for replay.
The Portal still has enough gas to complete and marks the withdrawal as finalised, locking the 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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When old withdrawals are moved to a new system, they are wrapped in a call that is given a fixed amount of gas. If the withdrawal needs more gas than that, the inner call fails but the outer transaction still succeeds, marking the withdrawal as finalised. The money is then stuck forever because the system thinks it has already been paid out.
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.