Causing users lose fund if bridging long message from L2 to L1 due to uncontrolled out-of-gas error
The gas estimation for finalizing withdrawals on L1 does not account for the quadratic memory expansion cost of hashing long messages, so a relayer can provide just enough gas to pass the first check but cause the message relay to run out of gas and revert. This leaves the withdrawal marked as finalized but not failed, preventing replay and causing loss of funds.
Source: https://github.com/sherlock-audit/2023-03-optimism-judging/issues/5
Found by
HE1M
Summary
If the amount of gas provided during finalizing withdrawal transactions passes the check in callWithMinGas, it is not guaranteed that the relaying message transaction does not go out of gas.
This can happen if the bridged message from L2 to L1 is long enough to increase the gas consumption significantly so that the predicted baseGas is not accurate enough.
Vulnerability Detail
During finalizing withdrawal transaction in OptimismPortal.sol, before calling _tx.target, it is checked if enough gas is provided gasleft() >= ((_minGas + 200) * 64) / 63, otherwise it will be reverted.
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#L82
So far so good.
Suppose, enough gas is provided, so that check is passed during finalizing withdrawal transaction, and finalizedWithdrawals[withdrawalHash] will be set to true for this withdrawal hash.
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/L1/OptimismPortal.sol#L383
If the _tx.target is L1CrossDomainMessenger, then the function L1CrossDomainMessenger.relayMessage will be called.
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L291
It will again check there is enough gas to call the next target (like bridge or any other receiver address) during relaying the message. https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L361
Here, it is not guaranteed to pass gasleft() >= ((_minGas + 200) * 64) / 63. If it is not passed, it will revert. In other words, it is not guaranteed that the transaction does not go out of gas during relaying the message.
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/libraries/SafeCall.sol#L82
Then 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 function relayMessage is reverted, the low-level call in OptimismPortal will set success to false. Since, this return value is not handled (because of the design decisions), the transaction OptimismPortal.finalizeWithdrawalTransaction is executed successfully.
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/L1/OptimismPortal.sol#L397
As a result, while the transaction OptimismPortal.finalizeWithdrawalTransaction sets the flag finalizedWithdrawals[withdrawalHash] as true, the flags failedMessages[versionedHash] and successfulMessages[versionedHash] are false. So, the users can not replay their message, and his fund is lost.
The question is that is there any possibility that L1CrossDomainMessenger reverts due to OOG, even though the required gas is calculated in L2 in the function baseGas?
Suppose, G is the gas provided to call OptimismPortal.finalizeWithdrawalTransaction. From line 319 to line 396, let's say some gas is consumed. I call it, K1. So, the gasLeft() when line 397 is called is equal to: G - K1
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/L1/OptimismPortal.sol#L319-L396
Suppose enough gas is provided to pass the check in OptimismPortal: G - K1 >= ((_minGas + 200) * 64) / 63
So, it is necessary to have: G >= ((_minGas + 200) * 64) / 63 + K1
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
Please note that _minGas here is equal to the base gas calculated in L2: _minGasLimit * (1016/1000) + messageLength * 16 + 200_000 in which, _minGasLimit is the amount of gas set by the user to be forwarded to the final receiver on L1.
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L423-L435
So, by replacing _minGas with _minGasLimit * (1016/1000) + messageLength * 16 + 200_000, we have:
G >= ((_minGasLimit * (1016/1000) + messageLength * 16 + 200_000 + 200) * 64) / 63 + K1
So, the amount of gas available to L1CrossDomainMessenger will be: (G - K1 - 51)*(63/64)
Please note this number is based on the estimation of gas consumption explained in the comment:
// Because EIP-150 ensures that, a maximum of 63/64ths of the remaining gas in the call // frame may be passed to a subcontext, we need to ensure that the gas will not be // truncated to hold this function's invariant: "If a call is performed by //
callWithMinGas, it must receive at least the specified minimum gas limit." In // addition, exactly 51 gas is consumed between the belowGASopcode and theCALL// opcode, so it is factored in with some extra room for error.
In the function L1CrossDomainMessenger.relayMessage, some gas will be consumed from line 299 to line 360. For simplicity, I call this amount of gas K2 + HashingGas, i.e. the consumed gas is separated for later explanation. In other words, the sum of consumed gas from line 299 to 303 and the consumed gas from line 326 to 360, is called K2, and the consumed gas from line 304 to line 325 is called HashingGas.
- ConsumedGas(L299 to L303 + L326 to L360) =
K2https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L299-L303 https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L326-L360 - ConsumedGas(L304 to L325) =
HashingGas
So, the gasLeft() in line 361 will be: (G - K1 - 51)*(63/64) - K2 - HashingGas
To pass the condition gasleft() >= ((_minGas + 200) * 64) / 63 in L1CrossDomainMessenger, it is necessary to have:
(G - K1 - 51)*(63/64) - K2 - HashingGas >= ((_minGas + 200) * 64) / 63
Please note that, _minGas here is equal to _minGasLimit which is the amount of gas set by the user to be forwarded to the final receiver on L1.
So, after simplification:
G >= [((_minGasLimit + 200) * 64) / 63 + K2 + HashingGas] *(64/63) + 51 + K1
All in all:
- To pass the gas check in
OptimismPortal:G >= ((_minGasLimit * (1016/1000) + messageLength * 16 + 200_000 + 200) * 64) / 63 + K1 - To pass the gas check in
L1CrossDomainMessenger:G >= [((_minGasLimit + 200) * 64) / 63 + K2 + HashingGas] *(64/63) + 51 + K1
If, G is between these two numbers (bigger than the first one, and smaller than the second one), it will pass the check in OptimismPortal, but it will revert in L1CrossDomainMessenger, as a result it is possible to attack.
Since, K1 and K2 are almost equal to 50_000, after simplification:
G >= (_minGasLimit * (1016/1000) + messageLength * 16 ) * (64 / 63) + 253_378G >= (_minGasLimit * (64 / 63) + HashingGas) *(64/63) + 101_051
So it is necessary to satisfy the following condition to be able to attack (in that case it is possible that the attacker provides gas amount between the higher and lower bound to execute the attack):
(_minGasLimit * (1016/1000) + messageLength * 16 ) * (64 / 63) + 253_378 < (_minGasLimit * (64 / 63) + HashingGas) *(64/63) + 101_051After simplification, we have:messageLength < (HashingGas - 150_000) / 16`
Please note that the HashingGas is a function of messageLength. In other words, the consumed gas from Line 304 to 325 is a function of messageLength, the longer length the higher gas consumption, but the relation is not linear, it is exponential.**
Please consider that if the version is equal to zero, the hashing is done twice (one in hashCrossDomainMessageV0, and one in hashCrossDomainMessageV1):
https://github.com/sherlock-audit/2023-03-optimism/blob/main/optimism/packages/contracts-bedrock/contracts/universal/CrossDomainMessenger.sol#L307-L324
So, for version zero, the condition can be relaxed to:
messageLength < (HashingGas * 2 - 150_000) / 16
The calculation shows that if the messageLength is equal to 1 mb for version 0, the gas consumed during hashing will be around 23.5M gas (this satisfies the condition above). While, if the messageLength is equal to 512 kb for version 0, the gas consumed during hashing will be around 7.3M gas (this does not satisfy the condition above marginally).
A short summary of calculation is:
messageLength= 128 kb, HashingGas for v1= 508_000, HahingGas for v0= 1_017_287, attack not possible
messageLength= 256 kb, HashingGas for v1= 1_290_584, HahingGas for v0= 2_581_168, attack not possible
messageLength= 512 kb, HashingGas for v1= 3_679_097, HahingGas for v0= 7_358_194, attack not possible
messageLength= 684 kb, HashingGas for v1= 5_901_416, HahingGas for v0= 11_802_831, attack possible
messageLength= 1024 kb, HashingGas for v1= 11_754_659, HahingGas for v0= 23_509_318, attack possible
Which can be calculated approximately by:
function checkGasV1(bytes calldata _message)
public
view
returns (uint256, uint256)
{
uint256 gas1 = gasleft();
bytes32 versionedHash = Hashing.hashCrossDomainMessageV1(
0,
address(this),
address(this),
0,
0,
_message
);
uint256 gas2 = gasleft();
return (_message.length, (gas1 - gas2));
}
function checkGasV0(bytes calldata _message)
public
view
returns (
uint256,
uint256,
uint256
)
{
uint256 gas1 = gasleft();
bytes32 versionedHash1 = Hashing.hashCrossDomainMessageV0(
address(this),
address(this),
_message,
0
);
uint256 gas2 = gasleft();
uint256 gas3 = gasleft();
bytes32 versionedHash2 = Hashing.hashCrossDomainMessageV1(
0,
address(this),
address(this),
0,
0,
_message
);
uint256 gas4 = gasleft();
return (_message.length, (gas1 - gas2), (gas3 - gas4));
}
It means that if for example the messageLength is equal to 684 kb (mostly non-zero, only 42 kb zero), and the message is version 0, and for example the _minGasLimit is equal to 21000, an attacker can exploit the user's withdrawal transaction by providing a gas meeting the following condition:
(_minGasLimit * (1016/1000) + 684 * 1024 * 16 ) * (64 / 63) + 253_378 < G < (_minGasLimit * (64 / 63) + 11_802_831) *(64/63) + 101_051
After, replacing the numbers, the provided gas by the attacker should be: 11_659_592 < G < 12_112_900
So, by providing almost 12M gas, it will pass the check in OptimismPortal, but it will revert in L1CrossDomainMessenger due to OOG, as a result the user's transaction will not be allowed to be replayed.
Please note that if there is a long time between request of withdrawal transaction on L2 and finalizing withdrawal transaction on L1, it is possible that the gas price is low enough on L1, so economically reasonable for the attacker to execute it.
In Summary:
When calculating the baseGas on L2, only the minGasLimit and message.length are considered, and a hardcoded overhead is also added. While, the hashing mechanism (due to memory expansion) is exponentially related to the length of the message. It means that, the amount of gas usage during relaying the message can be increased to the level that is higher than calculated value in baseGas. So, if the length of the message is long enough (to increase the gas significantly due to memory expansion), it provides an attack surface so that the attacker provides the amount of gas that only pass the condition in OptimismPortal, but goes out of gas in L1CrossDomainMessenger.
Impact
Users will lose fund because it is set as finalized, but not set as failed. So, they can not replay it.
Code Snippet
Tool used
Manual Review
Recommendation
If all the gas is consumed before reaching to L361, the vulnerability is available.
So, it is recommended to include memory expansion effect when calculating baseGas.
Discussion
hrishibhat
Sponsor comment: This is similar to issue #96 whereby a withdrawal with a gas limit configured to be too low can be bricked if the call to the XDM silently fails due to OOG.
GalloDaSballo
Making this primary
ydspa
Escalate for 10 USDC.
This finding should be invalid.
It's description for HashingGas is inaccurate , the gas cost for keccak256 hashing is only 6 gas per 32 bytes, which is linear. The primary issue in this case, as illustrated in https://github.com/sherlock-audit/2023-03-optimism-judging/issues/52, is that the current implementation is missing to consider the dynamic gas cost of memory usage, which is quadratic. Both of the two parts are not exponential as described in this finding. As opposed to https://github.com/sherlock-audit/2023-03-optimism-judging/issues/52, It's clear that this finding didn't provide full aspects and accurate analysis of the issue.
sherlock-admin
Escalate for 10 USDC.
This finding should be invalid.
It's description for
HashingGasis inaccurate , the gas cost for keccak256 hashing is only6 gas per 32 bytes, which is linear. The primary issue in this case, as illustrated in https://github.com/sherlock-audit/2023-03-optimism-judging/issues/52, is that the current implementation is missing to consider the dynamic gas cost of memory usage, which is quadratic. Both of the two parts are notexponentialas described in this finding. As opposed to https://github.com/sherlock-audit/2023-03-optimism-judging/issues/52, It's clear that this finding didn't provide full aspects and accurate analysis of the issue.
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.
HE1M
Escalate for 10 USDC
Some points:
- This report is not duplicate of #96.
- This report is not dependent on the user's mistake (or misconfiguration). It shows that due to wrong calculation of
baseGasit does break the CrossDomainMessenger's replayability guarantee in case the user's message is long enough. - The term
HashingGasis not including only thekecca256. As it is explained in the report:
the consumed gas from line 304 to line 325 is called HashingGas
- In the report, the working code is provided to show how the gas consumption is dependent to the length of the message.
sherlock-admin
Escalate for 10 USDC
Some points:
- This report is not duplicate of #96.
- This report is not dependent on the user's mistake (or misconfiguration). It shows that due to wrong calculation of
baseGasit does break the CrossDomainMessenger's replayability guarantee in case the user's message is long enough.- The term
HashingGasis not including only thekecca256. As it is explained in the report:the consumed gas from line 304 to line 325 is called HashingGas
- In the report, the working code is provided to show how the gas consumption is dependent to the length of the message.
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
Accepting the 2nd escalation, Considering this issue a separate valid medium issue based on the escalation comments
sherlock-admin
Escalation accepted
Accepting the 2nd escalation, Considering this issue a separate valid medium issue based on the escalation comments
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 baseGas calculation on L2 only considers message length linearly and ignores the quadratic memory expansion cost during hashing on L1, leading to an underestimate of the gas needed for long messages.
Impact
Users could lose funds because the withdrawal is marked as finalized but not failed, preventing replay.
What the attacker needed
Long message (e.g., >684 kb); low minGasLimit; attacker provides gas between bounds
The fix
The baseGas calculation should include the memory expansion effect when hashing long messages.
How it was found
- A user initiates a withdrawal on L2 with a long message (e.g., 684 kb) and a low minGasLimit.
- The user's withdrawal is finalized on L1 by calling finalizeWithdrawalTransaction with a gas amount that passes the gas check in OptimismPortal but is insufficient for the subsequent relayMessage call.
- The relayMessage call runs out of gas and reverts, so the failedMessages flag is not set.
- The finalizeWithdrawalTransaction succeeds, marking the withdrawal as finalized, but the message cannot be replayed, resulting in loss of 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 withdraw funds from a rollup to Ethereum, a relayer submits a transaction that calls a contract to release your funds. The contract checks that enough gas (the fee for computation) is provided, but it underestimates the gas needed for long messages because hashing them uses extra memory. An attacker can supply just enough gas to pass the first check but not enough for the full process, causing the relay to fail and your withdrawal to be stuck as finalized but not replayable.
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.