Legacy withdrawals can be relayed twice, causing double spending of bridged assets
During the Bedrock migration, the storage of predeployed contracts is wiped, so the L2CrossDomainMessenger's record of already-relayed legacy messages becomes empty. An attacker could therefore relay the same withdrawal message twice, once as a legacy message before migration and again as a Bedrock message during migration, withdrawing bridged ETH or ERC20 tokens twice.
Source: https://github.com/sherlock-audit/2023-03-optimism-judging/issues/87
Found by
Jeiwan
Summary
L2CrossDomainMessenger.relayMessage checks that legacy messages have not been relayed by reading from the successfulMessages state variable, however the contract's storage will wiped during the migration to Bedrock and successfulMessages will be empty after the deployment of the contract. The check will always pass, even if a legacy message have already been relayed using its v0 hash. As a result, random withdrawal messages, as well as messages from malicious actors, can be relayed multiple times during the migration: first, as legacy v0 messages (before the migration); then, as Bedrock v1 messages (during the migration).
Vulnerability Detail
L2CrossDomainMessenger inherits from CrossDomainMessenger, which inherits from CrossDomainMessengerLegacySpacer0, CrossDomainMessengerLegacySpacer1, assuming that the contract will be deployed at an address with existing state–the two spacer contracts are needed to "skip" the slots occupied by previous implementations of the contract.
During the migration, legacy (i.e. pre-Bedrock) withdrawal messages will be converted to Bedrock messages–they're expected to call the relayMessage function of L2CrossDomainMessenger. The L2CrossDomainMessenger.relayMessage function checks that the relayed legacy message haven't been relayed already:
// If the message is version 0, then it's a migrated legacy withdrawal. We therefore need
// to check that the legacy version of the message has not already been relayed.
if (version == 0) {
bytes32 oldHash = Hashing.hashCrossDomainMessageV0(_target, _sender, _message, _nonce);
require(
successfulMessages[oldHash] == false,
"CrossDomainMessenger: legacy withdrawal already relayed"
);
}
It reads a V0 message hash from the successfulMessages state variable, assuming that the content of the variable is preserved during the migration. However, the state and storage of all predeployed contracts is wiped during the migration:
// We need to wipe the storage of every predeployed contract EXCEPT for the GovernanceToken,
// WETH9, the DeployerWhitelist, the LegacyMessagePasser, and LegacyERC20ETH. We have verified
// that none of the legacy storage (other than the aforementioned contracts) is accessible and
// therefore can be safely removed from the database. Storage must be wiped before anything
// else or the ERC-1967 proxy storage slots will be removed.
if err := WipePredeployStorage(db); err != nil {
return nil, fmt.Errorf("cannot wipe storage: %w", err)
}
Also notice that withdrawals are migrated after predeploys were wiped and deployed–predeploys will have empty storage at the time withdrawals are migrated.
Moreover, if we check the code at the L2CrossDomainMessenger address of the current version of Optimism, we'll see that the contract's storage layout is different from the layout of the CrossDomainMessengerLegacySpacer0 and CrossDomainMessengerLegacySpacer1 contracts: there are no gaps and other spacer slots; successfulMessages is the second slot of the contract. Thus, even if there were no wiping, the successfulMessages mapping of the new L2CrossDomainMessenger contract would still be empty.
Impact
Withdrawal messages can be relayed twice: once right before and once during the migration. ETH and ERC20 tokens can be withdrawn twice, which is basically double spending of bridged assets.
Code Snippet
L2CrossDomainMessengerisCrossDomainMessenger:
CrossDomainMessengerinherits fromCrossDomainMessengerLegacySpacer0andCrossDomainMessengerLegacySpacer1to preserve the storage layout:
CrossDomainMessenger.relayMessagereads fromsuccessfulMessagesto ensure that legacy withdrawals haven't been relayed already:
- All predeploys are wiped during the migration, thus
L2CrossDomainMessenger.successfulMessageswill not contain the hashes of legacy withdrawals:
Tool used
Manual Review
Recommendation
- Consider cleaning up the storage layout of
L1CrossDomainMessenger,L2CrossDomainMessengerand other proxied contracts. - In the PreCheckWithdrawals function, consider reading withdrawal hashes from the
successfulMessagesmapping of the oldL2CrossDomainMessengercontract and checking if the values are set. Successful withdrawals should be skipped at this point to filter out legacy withdrawals that have already been relayed. - Consider removing the check from the
relayMessagefunction, since the check will be useless due to the empty state of the contract.
Discussion
hrishibhat
Sponsor comment:
This report is valid. The storage layout of the new CrossDomainMessenger contract is different from the old CrossDomainMessenger, which allows for replaying old cross domain messages- this would be catastrophic for the network.
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 migration wipes the storage of predeployed contracts, and the new contract's storage layout does not match the old one, so the successfulMessages mapping that guards against replays is empty.
Impact
ETH and ERC20 tokens could be withdrawn twice, effectively double spending bridged assets, which the sponsor described as potentially catastrophic for the network.
What the attacker needed
Occurs during the Bedrock migration window; attacker can submit withdrawal messages
The fix
The report recommends reading withdrawal hashes from the old contract's successfulMessages mapping during the pre-check and skipping already-relayed withdrawals, cleaning up the storage layout, and removing the now-useless check in relayMessage.
How it was found
- Note that legacy withdrawals are converted to Bedrock messages that call relayMessage on L2CrossDomainMessenger.
- Observe that relayMessage checks successfulMessages for a version 0 hash to reject already-relayed legacy messages.
- Observe that the migration wipes the storage of all predeployed contracts before withdrawals are migrated.
- Observe that the new contract's storage layout differs from the legacy spacer layout, so successfulMessages is empty regardless.
- Relay a legacy withdrawal before migration, then relay the same withdrawal again as a Bedrock message during migration.
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.
A bridge lets you move tokens between two blockchains. To stop someone claiming the same withdrawal twice, the bridge contract keeps a list of messages it has already processed. This report found that the migration to a new version of the bridge erased that list, so an old withdrawal could be processed a second time and the tokens paid out twice.
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.