Skip to content
Business logic Audit finding Advanced

Missing address check for instructions LH and LHU

The MIPS virtual machine implementation in Optimism's contracts does not check that the address for the LH and LHU instructions is naturally aligned (2-byte aligned), as required by the MIPS specification. An attacker could potentially exploit this divergence from the spec, though no adverse effects have been observed to date.

zraxx Follow with RadiantLabs

Originally disclosed on Code4rena

Submitted by zraxx, also found by RadiantLabs

https://github.com/code-423n4/2024-07-optimism/blob/main/packages/contracts-bedrock/src/cannon/MIPS.sol#L990-L993 https://github.com/code-423n4/2024-07-optimism/blob/main/packages/contracts-bedrock/src/cannon/MIPS.sol#L1008-L1011

Impact

Instruction processing cannot detect problems in time.

Details

According to this, page 99, Section Restrictions, the address must be naturally aligned. If the least-significant bit of the address is non-zero, an Address Error exception occurs. However, in the contract, the address is not checked.

Tools Used

Vscode

Recommended Mitigation Steps

Check the address and when the least-significant bit of the address is non-zero, revert.

clabby (Optimism) confirmed and commented:

This report is valid. The MIPS VM currently does not check for half-word alignment within the LH and LHU instruction implementations. Every piece of memory is guaranteed to be 4-byte aligned via the 0xFFFFFFFC mask prior to the readMem call, but the ISA specification explicitly states a requirement for 2-byte alignment for these instructions.

obront (judge) commented:

@clabby - If I’m understanding correctly, you’re saying that there is no possible risk to this in Optimism’s context, but that it is confirmed that it’s not following the MIPS spec? If that’s the case, I will plan to downgrade to low/QA.

clabby (Optimism) commented:

@obront, I can confirm that we’ve seen no adverse effects from the out-of-spec implementation of these instructions to date. Though the surface is too large to make a blanket statement on there being no possible risk. We do intend to fix this and align with the MIPS specification.

obront (judge) commented:

Because this is a confirmed divergence from the spec and we don’t have a firm guarantee that it won’t be reached in the program, I will be considering it a valid Medium.


The original report as published on Code4rena, by zraxx. 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

The contract does not verify that the address is 2-byte aligned before executing the LH and LHU instructions, contrary to the MIPS specification.

Impact

The out-of-spec behaviour could potentially be reached by a program, but no adverse effects have been observed to date; the judge considered it a valid Medium because there is no firm guarantee it won't be reached.

What the attacker needed

None

The fix

The team intends to fix this by checking the address and reverting when the least-significant bit is non-zero, aligning with the MIPS specification.

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 virtual machine (VM) is software that mimics a computer. The MIPS VM is supposed to follow a rulebook (the MIPS specification) that says certain memory-reading instructions must only use addresses that are multiples of two. This report found that the VM skips that check, so it might behave differently from a real MIPS machine.

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 zraxx

View all

1 more disclosed report, 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.