In the `revertBatch` function, `inChallenge` is set to `false` incorrectly, causing challenges to continue after the protocol is paused
The revertBatch function incorrectly sets the inChallenge flag to false when reverting batches that are not under challenge, because it checks a challenge's finished status without verifying the challenge exists. This can cause an ongoing challenge to be invalidated, and if the protocol is then paused, the prover may be slashed despite the batch being valid.
0xRajkumar Follow with mstpr-brainbot, p0wd3r and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-morphl2-judging/issues/220
Found by
0xRajkumar, mstpr-brainbot, p0wd3r, ulas
Summary
An unchecked batch reversion will cause challenge invalidation for any committed batch, leading to batch rollback issues for challengers, as the isChallenged flag will reset unexpectedly.
Root Cause
In the revertBatch function, the inChallenge state is set to false even if the batch that was challenged is not part of the reverted batch set. This causes ongoing challenges to be incorrectly invalidated:
function revertBatch(bytes calldata _batchHeader, uint256 _count) external onlyOwner {
.... REDACTED FOR BREVITY ...
if (!challenges[_batchIndex].finished) {
batchChallengeReward[challenges[_batchIndex].challenger] += challenges[_batchIndex].challengeDeposit;
inChallenge = false;
}
.... REDACTED FOR BREVITY ...
In the above code, if (!challenges[_batchIndex].finished) will hold true for challenges that doesn't exist. If there are no challenges for a specific _batchIndex, then challenges[_batchIndex].finished will be false which in turn will make the if condition true.
This will cause inChallenge to be set to false even when there are ongoing challenges. Lets assume the following batches were commit to L1 Rollup::
┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐
│ │ │ │ │ │ │ │
│ Batch 123 ├─────►│ Batch 124 ├─────►│ Batch 125 ├─────►│ Batch 126 ├
│ │ │ │ │ │ │ │
└────────────┘ └────────────┘ └────────────┘ └────────────┘
Challenger calls challengeState on batch 123. This sets isChallenged storage variable to true.
┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐
│ │ │ │ │ │ │ │
│ Batch 123 ├─────►│ Batch 124 ├─────►│ Batch 125 ├─────►│ Batch 126 ├
│ │ │ │ │ │ │ │
└────────────┘ └────────────┘ └────────────┘ └────────────┘
▲
│
challenged
isChallenged = true
While the challenge is ongoing owner calls revertBatch on Batch 125 to revert both Batch 125 and Batch 126.
┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐
│ │ │ │ │ │ │ │
│ Batch 123 ├─────►│ Batch 124 ├─────►│ Batch 125 ├─────►│ Batch 126 ├
│ │ │ │ │ │ │ │
└────────────┘ └────────────┘ └────────────┘ └────────────┘
▲ ▲
│ │
challenged revert batch
isChallenged = true
Due to the bug in the revertBatch function, isChallenged is set to false even though the challenged batch wasn’t in the reverted batches.
┌────────────┐ ┌────────────┐
│ │ │ │
│ Batch 123 ├─────►│ Batch 124 ├
│ │ │ │
└────────────┘ └────────────┘
▲
│
challenged
isChallenged = false
This will lead to issues when the protocol is paused. Due to the following check in the setPause function, the challenge will not be deleted while the protocol is paused:
function setPause(bool _status) external onlyOwner {
.... REDACTED FOR BREVITY ...
// inChallenge is set to false due to the bug in revertBatch
if (inChallenge) {
batchChallengeReward[challenges[batchChallenged].challenger] += challenges[batchChallenged]
.challengeDeposit;
delete challenges[batchChallenged];
inChallenge = false;
}
.... REDACTED FOR BREVITY ...
During the protocol pause, the prover will not be able to verify the proof and if the pause period is larger than the proof window, prover will lose the challenge and gets slashed.
Internal pre-conditions
- Owner calls
revertBatchon batch n, reverting the nth batch. - Challenger monitors the mempool and initiates a challenge on the n-1 batch.
- Due to the bug in
revertBatch, theinChallengeflag is reset tofalse, even though batch n-1 is under challenge. - Owner calls
setPauseand the protocol is paused longer than the challenge window.
External pre-conditions
No response
Attack Path
- The owner calls
revertBatchon batch n, reverting batch n. - A challenger monitors the mempool and calls
challengeBatchon batch n-1. - The
revertBatchfunction incorrectly resets theinChallengeflag to false despite batch n-1 being under challenge. - The protocol is paused, preventing the challenge from being deleted.
- The prover cannot prove the batch in time due to the paused protocol.
- The prover gets slashed, even though the batch is valid.
Impact
If the protocol is paused when there's an ongoing challenge (albeit inChallenge is set to false due to the vulnerability explained above) , the protocol slashes the batch submitter for failing to prove the batch within the challenge window, even though the batch is valid. The challenger may incorrectly receive the challenge reward + slash reward despite no actual issue in the batch.
PoC
No response
Mitigation
Use batchInChallenge function to verify the batch is indeed challenged:
function revertBatch(bytes calldata _batchHeader, uint256 _count) external onlyOwner {
// ... Redacted for brevity ...
while (_count > 0) {
emit RevertBatch(_batchIndex, _batchHash);
committedBatches[_batchIndex] = bytes32(0);
// if challenge exist and not finished yet, return challenge deposit to challenger
if (batchInChallenge(_batchIndex)) {
batchChallengeReward[challenges[_batchIndex].challenger] += challenges[_batchIndex].challengeDeposit;
inChallenge = false;
}
delete challenges[_batchIndex];
// ... Redacted for brevity ...
}
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/morph-l2/morph/pull/561
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 code checks `!challenges[_batchIndex].finished` without first confirming that a challenge exists for that batch, so a non-existent challenge (with `finished` defaulting to false) triggers the reset of `inChallenge`.
Impact
If the protocol is paused during an ongoing challenge, the prover could be slashed for failing to prove a valid batch within the challenge window, and the challenger may incorrectly receive rewards.
What the attacker needed
Owner calls revertBatch on a batch; Challenger challenges a different batch; Protocol is paused longer than challenge window
The fix
The issue was fixed by using the batchInChallenge function to verify that the batch is indeed challenged before resetting inChallenge.
How it was found
- Owner calls revertBatch on batch n, reverting batch n.
- A challenger monitors the mempool and calls challengeBatch on batch n-1.
- The revertBatch function incorrectly resets the inChallenge flag to false despite batch n-1 being under challenge.
- The protocol is paused, preventing the challenge from being deleted.
- The prover cannot prove the batch in time due to the paused protocol.
- The prover gets slashed, even though the batch is valid.
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 smart contract tracks whether any batch is currently being challenged using a true/false flag. When the owner reverts some batches, the code mistakenly turns off that flag even if the challenged batch was not among those reverted. Later, if the protocol is paused, the challenge is not properly cleaned up, and the honest prover can lose their deposit.
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.