Batches committed during an on going challenge can avoid being challenged
Batches committed during an ongoing challenge are not given extra time to be challenged, so they can be finalized immediately after the challenged batch is resolved, leaving no window for challengers to dispute them. This could allow invalid batches to be finalized, potentially bricking the L2 protocol.
PapaPitufo Follow with sammy
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-morphl2-judging/issues/143
Found by
PapaPitufo, sammy
NOTE : This finding is not the same as Known issue # 10, that is about an invalid batch getting finalized due to challengeState() reverting (OOG error)
Summary
Batches committed during an ongoing challenge can avoid being challenged and pre-maturely finalize if the defender wins
Vulnerability Detail
After a batch is committed, there is a finalization window, in which challengers can challenge the batch's validity, after the period has elapsed, the batch can be finalized.
A batch can be challenged using challengeState() :
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;
}
As you can see, the function loops through all the unfinalized batches, except the batch being challenged and adds a proofWindow to their finalization timestamp. This is to compensate for the amount of time these batches cannot be challenged, which is the duration of the current challenge, i.e, proofWindow (only one batch can be challenged at a time).
However, commitBatch() allows batches to get committed even when a challenge is going on and does not compensate for time spent inside the challenge :
batchDataStore[_batchIndex] = BatchData(
block.timestamp,
block.timestamp + finalizationPeriodSeconds,
_loadL2BlockNumber(batchDataInput.chunks[_chunksLength - 1]),
// Before BLS is implemented, the accuracy of the sequencer set uploaded by rollup cannot be guaranteed.
// Therefore, if the batch is successfully challenged, only the submitter will be punished.
IL1Staking(l1StakingContract).getStakerBitmap(_msgSender()) // => batchSignature.signedSequencersBitmap
);
As you can see, the new batch can be finalized after finalizationPeriodSeconds.
Currently the value of finalizationPeriodSeconds is 86400, i.e, 1 Day and proofWindow is 172800, i.e, 2 Days
Source. This means that a batch committed just after the start of a challenge will be ready to be finalized in just 1 day, before the ongoing challenge even ends.
Now, consider the following scenario :
- A batch is finalized
- A challenger challenges this batch, challenge will end 2 days from the start
- A staker commits a new batch
- After 1 day, this new batch is ready to be finalized, but can't be finalized yet as the parent batch (the challenged batch) needs to be finalized first
- After 1.5 days, the original batch finishes its challenge, the defender wins(by providing a valid ZK proof), and the batch is ready to be finalized
- Right after the original batch is finalized, the new batch is finalized
This leaves no time for a challenger to challenge the new batch, and this can lead to invalid batches getting committed, even if the batch committer (sequencer) doesn't act maliciously.
Since finalizeBatch() is permissionless and only checks whether a batch is in the finalization window, anyone can batch the two finalizeBatch() calls which finalize both the original batch and the invalid batch, right after the challenge ends (by back running proveState()), leaving no time for a challenger to call challengeState()
If a sequencer is malicious, they can easily exploit this to commit invalid batches
Impact
Critical - Can brick the entire Morph L2 protocol
Code Snippet
Tool used
Manual Review
Recommendation
You can make the following change, which correctly compensates for the lost finalization time:
batchDataStore[_batchIndex] = BatchData(
block.timestamp,
- block.timestamp + finalizationPeriodSeconds,
+ block.timestamp + finalizationPeriodSeconds + (inChallenge ? proofWindow - (block.timestamp - challenges[batchChallenged].startTime) : 0),
_loadL2BlockNumber(batchDataInput.chunks[_chunksLength - 1]),
// Before BLS is implemented, the accuracy of the sequencer set uploaded by rollup cannot be guaranteed.
// Therefore, if the batch is successfully challenged, only the submitter will be punished.
IL1Staking(l1StakingContract).getStakerBitmap(_msgSender()) // => batchSignature.signedSequencersBitmap
);
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/morph-l2/morph/pull/577
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
Impact
This could allow invalid batches to be finalized, potentially bricking the entire Morph L2 protocol.
What the attacker needed
A batch is challenged; A new batch is committed during the challenge; The defender wins the challenge
The fix
The protocol team fixed this issue by adjusting the finalization timestamp in commitBatch to account for the remaining challenge time.
How it was found
- A batch is finalized.
- A challenger challenges this batch, starting a challenge that will last 2 days.
- A staker commits a new batch during the challenge.
- After 1 day, the new batch's finalization window elapses, but it cannot be finalized until the parent batch is finalized.
- After 1.5 days, the challenge ends and the defender wins, making the original batch finalizable.
- Right after the original batch is finalized, the new batch is finalized, leaving no time for a challenger to challenge it.
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.
In this system, after a batch of transactions is committed, there is a time window during which someone can challenge its validity. If a challenge is already happening, new batches can still be committed, but they don't get extra time added to their challenge window. So if the challenge ends quickly, the new batch can be finalized before anyone has a chance to challenge it. This means a bad batch could slip through.
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.