A batch can be unintentionally be challenged during L1 reorg leading to loss of funds
During an L1 reorg, the batch stored at a given batchIndex can change, so a challenger who calls challengeState for that index may end up challenging a correct batch instead of the incorrect one they intended. This causes the challenger to lose their challenge stake.
HaxSecurity Follow with PASCAL, PapaPitufo and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-morphl2-judging/issues/146
Found by
HaxSecurity, PASCAL, PapaPitufo, sammy
Summary
A batch can be unintentionally be challenged during L1 reorg leading to loss of funds
Root Cause
The incorrect batch can be challenged during L1 reorg leading to loss of funds. Firstly the README states:
But if there is any issue about Ethereum L1 re-org leading to financial loss, that issue is valid.
In the challengeBatch function, the batch that is challenged is referenced by the batchIndex.
/// @dev challengeState challenges a batch by submitting a deposit.
function challengeState(uint64 batchIndex) external payable onlyChallenger nonReqRevert whenNotPaused {
require(!inChallenge, "already in challenge");
require(lastFinalizedBatchIndex < batchIndex, "batch already finalized");
require(committedBatches[batchIndex] != 0, "batch not exist");
require(challenges[batchIndex].challenger == address(0), "batch already challenged");
// check challenge window
require(batchInsideChallengeWindow(batchIndex), "cannot challenge batch outside the challenge window");
// check challenge amount
require(msg.value >= IL1Staking(l1StakingContract).challengeDeposit(), "insufficient value");
batchChallenged = batchIndex;
challenges[batchIndex] = BatchChallenge(batchIndex, _msgSender(), msg.value, block.timestamp, false, false);
emit ChallengeState(batchIndex, _msgSender(), msg.value);
for (uint256 i = lastFinalizedBatchIndex + 1; i <= lastCommittedBatchIndex; i++) {
if (i != batchIndex) {
batchDataStore[i].finalizeTimestamp += proofWindow;
}
}
inChallenge = true;
}
However, this poses a problem because a reorg can cause the batch present in the committedBatches[batchIndex] to change and the challenger unintentionally challenging the incorrect batch, losing the challenge and their ETH.
For instance, consider the scenario where the sequencers upload two different batches at the same batchIndex, a correct batch and an incorrect batch. The initial transaction ordering is:
- Transaction to upload a incorrect batch at
batchIndex = x - Transaction to upload a correct batch (it will revert, as it is already occupied) at
batchIndex = x - Challenger calls
challengeStateatbatchIndex = x.
An L1 reorg occurs, resulting in the new transaction ordering
- Transaction to upload a correct batch (it will revert, as it is already occupied) at
batchIndex = x - Transaction to upload a incorrect batch at
batchIndex = x - Challenger calls
challengeStateatbatchIndex = x.
Due to the L1 reorg, the challenger will now be challenging a correct batch and will proceed to lose their challenge stake as it can be proven by the sequencer.
This issue is really similar to the Optimism reorg finding: https://github.com/sherlock-audit/2024-02-optimism-2024-judging/issues/201, where the incorrect state root can also be challenged leading to loss of bonds.
Internal pre-conditions
- L1 reorg
External pre-conditions
n/a
Attack Path
n/a
Impact
A batch can be unintentionally challenged leading to loss of funds for the challenger
PoC
No response
Mitigation
In the challengeState function, allow loading the batchHeader and verifying that the committedBatches[_batchIndex] is equal to the _batchHash as is done in the other functions such as revertBatch
/// @dev challengeState challenges a batch by submitting a deposit.
- function challengeState(uint64 batchIndex) external payable onlyChallenger nonReqRevert whenNotPaused {
+ function challengeState(bytes calldata _batchHeader) external payable onlyChallenger nonReqRevert whenNotPaused {
require(!inChallenge, "already in challenge");
+ (uint256 memPtr, bytes32 _batchHash) = _loadBatchHeader(_batchHeader);
+ // check batch hash
+ uint256 _batchIndex = BatchHeaderCodecV0.getBatchIndex(memPtr);
+ require(committedBatches[_batchIndex] == _batchHash, "incorrect batch hash");
require(lastFinalizedBatchIndex < batchIndex, "batch already finalized");
require(committedBatches[batchIndex] != 0, "batch not exist");
require(challenges[batchIndex].challenger == address(0), "batch already challenged");
// check challenge window
require(batchInsideChallengeWindow(batchIndex), "cannot challenge batch outside the challenge window");
// check challenge amount
require(msg.value >= IL1Staking(l1StakingContract).challengeDeposit(), "insufficient value");
batchChallenged = batchIndex;
challenges[batchIndex] = BatchChallenge(batchIndex, _msgSender(), msg.value, block.timestamp, false, false);
emit ChallengeState(batchIndex, _msgSender(), msg.value);
for (uint256 i = lastFinalizedBatchIndex + 1; i <= lastCommittedBatchIndex; i++) {
if (i != batchIndex) {
batchDataStore[i].finalizeTimestamp += proofWindow;
}
}
inChallenge = true;
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/morph-l2/morph/pull/558
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 challengeState function identifies the batch only by its index, without verifying that the batch hash at that index matches the intended batch, so a reorg can swap the batch at that index.
Impact
A challenger could lose their challenge stake if an L1 reorg causes them to challenge a correct batch.
What the attacker needed
L1 reorg occurs; challenger calls challengeState
The fix
The challengeState function should accept the batch header and verify that the committed batch hash at the index matches the provided header, as done in other functions.
How it was found
- A sequencer uploads an incorrect batch at a given batchIndex.
- A challenger calls challengeState for that batchIndex.
- An L1 reorg changes the order so a correct batch ends up at that batchIndex.
- The challenger's challenge now targets the correct batch, and they lose their stake.
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 challenger is someone who disputes a batch of transactions they believe is wrong by putting up a deposit. If the blockchain reorders recent transactions (a reorg), the batch at a particular position can change, so the challenger might accidentally dispute a correct batch and lose their deposit.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.