Skip to content
Business logic Audit finding Advanced

Invalid DISPUTED_L2_BLOCK_NUMBER is passed to VM

The smart contract passes a disputed L2 block number to the VM that is always starting block + trace index + 1, ignoring the actual claimed block number. This allows an attacker to challenge a valid claim for a block earlier than the safe head by extending the trace to a later safe block, causing the honest party's defence to fail.

xuwinnie Follow

Originally disclosed on Code4rena

Submitted by xuwinnie

The span of the game tree at split depth is far larger than the length between the starting block and the claimed block. When starting block + trace index + 1 > claimed block, honest party should continue to commit to the root of the claimed block. However, the DISPUTED_L2_BLOCK_NUMBER passed to the VM is always starting block + trace index + 1, which means the op-program (at inter-block perspective) will not stop until it reaches the l2 safe head (corresponding to parenthash), and if the claimed block is earlier than the safe head, it can be challenged and will be considered invalid.

Proof of Concept

Since op-program is out of scope of this audit, this report will not spend too much time in proving block earlier than safe head can be challenged, instead it will only show the inconsistency within the smart contract part.

For simplicity, we assume the span of the game tree at split depth is 8 and starting block is 0. At block level, we use defend a ->p b to refer to commit a valid VM trace with a as starting output, b as disputed output, p as disputed block number and VALID as the final state, similarly attack a ->p b refers to commit a valid VM trace with … and INVALID or PANIC as the final state. We use Bi to refer to the valid L2 root at block i.

Suppose Alice made a valid root claim B2 for block 2, Bob made a valid root claim B3 for block 3 and they claim at the same L1 block (so the stored l1Head will be the identical). Ideally, both of them should be able to defend their claim. We already know that Bob can defend B2 ->3 B3 in his own game. What if Bob tries to attack Alice in her game?

(Recall Alice’s view of valid state is 12222222 and Bob’s is 12333333).

  1. Bob attacks by claiming B3 (trace index 3).
  2. Alice attacks by claiming B2 (trace index 1).
  3. Bob defends by claiming B3 (trace index 2).
  4. Alice attacks B2 ->3 B3 (disputed block = starting block + trace index + 1 = 3).
        /// @inheritdoc IFaultDisputeGame
        function addLocalData(uint256 _ident, uint256 _execLeafIdx, uint256 _partOffset) external {
            // INVARIANT: Local data can only be added if the game is currently in progress.
            if (status != GameStatus.IN_PROGRESS) revert GameNotInProgress();

            (Claim starting, Position startingPos, Claim disputed, Position disputedPos) =
                _findStartingAndDisputedOutputs(_execLeafIdx);
            Hash uuid = _computeLocalContext(starting, startingPos, disputed, disputedPos);

            IPreimageOracle oracle = VM.oracle();
            if (_ident == LocalPreimageKey.L1_HEAD_HASH) {
                // Load the L1 head hash
                oracle.loadLocalData(_ident, uuid.raw(), l1Head().raw(), 32, _partOffset);
            } else if (_ident == LocalPreimageKey.STARTING_OUTPUT_ROOT) {
                // Load the starting proposal's output root.
                oracle.loadLocalData(_ident, uuid.raw(), starting.raw(), 32, _partOffset);
            } else if (_ident == LocalPreimageKey.DISPUTED_OUTPUT_ROOT) {
                // Load the disputed proposal's output root
                oracle.loadLocalData(_ident, uuid.raw(), disputed.raw(), 32, _partOffset);
            } else if (_ident == LocalPreimageKey.DISPUTED_L2_BLOCK_NUMBER) {
                // Load the disputed proposal's L2 block number as a big-endian uint64 in the
                // high order 8 bytes of the word.

                // block number.
                uint256 l2Number = startingOutputRoot.l2BlockNumber + disputedPos.traceIndex(SPLIT_DEPTH) + 1;

                oracle.loadLocalData(_ident, uuid.raw(), bytes32(l2Number << 0xC0), 8, _partOffset);
            } else if (_ident == LocalPreimageKey.CHAIN_ID) {
                // Load the chain ID as a big-endian uint64 in the high order 8 bytes of the word.
                oracle.loadLocalData(_ident, uuid.raw(), bytes32(L2_CHAIN_ID << 0xC0), 8, _partOffset);
            } else {
                revert InvalidLocalIdent();
            }
        }

However, all VM inputs above are identical for B2 ->3 B3 in these two cases. Since VM step is deterministic, B2 ->3 B3 cannot be defended in one game while attacked in another, which shows the contradiction. The problem is that claimed block number is not passed to the VM, so the VM cannot differentiate the context between the two games.

Recommended Mitigation Steps

    uint256 l2Number = min(startingOutputRoot.l2BlockNumber + disputedPos.traceIndex(SPLIT_DEPTH) + 1, l2BlockNumber());

Note

    func (d *Driver) ValidateClaim(l2ClaimBlockNum uint64, claimedOutputRoot eth.Bytes32) error {
    	l2Head := d.SafeHead()
    	outputRoot, err := d.l2OutputRoot(min(l2ClaimBlockNum, l2Head.Number))
    	if err != nil {
    		return fmt.Errorf("calculate L2 output root: %w", err)
    	}
    	d.logger.Info("Validating claim", "head", l2Head, "output", outputRoot, "claim", claimedOutputRoot)
    	if claimedOutputRoot != outputRoot {
    		return fmt.Errorf("%w: claim: %v actual: %v", ErrClaimNotValid, claimedOutputRoot, outputRoot)
    	}
    	return nil
    }

Here l2ClaimBlockNum is just DISPUTED_L2_BLOCK_NUMBER, so DISPUTED_L2_BLOCK_NUMBER clearly should be capped at claimed l2 block number; otherwise, the inter-block op-program execution will never stop until it reaches safe head, which means all claims earlier than safe head is invalid in op-program’s perspective.

Assessed type

Context

ajsutton (Optimism) confirmed and commented:

This is valid and an excellent analysis. I’ve seen a number of claims that the DISPUTED_L2_BLOCK_NUMBER needs to be capped but before this all of them have provided an example where trace extension still results in the correct game resolution. This is the only case I’ve seen that identifies the actual case that I believe will give the wrong result. It does require a few more details to make the attack actually work though.

Specifically, if the correct output root is proposed for block 13, the attacker could counter using a trace that includes valid blocks up to block 14. The value the attacker uses for block 14 must be the honest value and block 14 must be safe at the game’s L1 head. In the honest actor’s trace, the output for block 13 is extended to the end of the game so the only difference is the attacker includes one extra block in the trace they use. At the split depth both actors would agree on block 13 and disagree on block 14 and then execute cannon/op-program to decide if the transition from block 13 to 14 is valid or not. Since block 14 is a valid safe block, op-program will be able to continue the derivation past block 13 and resolve that the claimed block 14 is valid even though it’s past the proposed output root.

In addition, you have to manipulate the game so that either the dishonest actor posts the first claim in the bottom half or that the honest actor is required to post the first claim of the bottom half such that the post state is an honest claim. Otherwise the restrictions on the required status code for the root of the bottom game and not being able to defend the root of the bottom claim prevent the attack.


The original report as published on Code4rena, by xuwinnie. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 contract fails to cap the disputed L2 block number at the claimed block number, so the VM is given a block number that can be beyond the claim.

Impact

An attacker could cause a valid claim for a block earlier than the safe head to be considered invalid, potentially leading to incorrect game resolution.

What the attacker needed

Attacker can post claims in the dispute game; honest party's claim is for a block earlier than safe head; attacker can include a later safe block in their trace

The fix

The disputed L2 block number should be capped at the claimed L2 block number.

How it was found

  1. Bob attacks Alice's game by claiming B3 at trace index 3.
  2. Alice responds by claiming B2 at trace index 1.
  3. Bob defends by claiming B3 at trace index 2.
  4. Alice attacks with disputed block number equal to starting block + trace index + 1, which is 3.

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 a dispute game, two parties argue about the correct state of a blockchain at a certain block. The contract tells a verification program which block to check, but it always uses a number based on the position in the game tree, not the block the claim is actually about. This means an attacker can make the program check a later block that is known to be valid, so the honest party's correct claim gets rejected.

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.

More by xuwinnie

View all

12 more disclosed reports, best first.

More from Optimism Superchain

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.