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
MIPSVM currently does not check for half-word alignment within theLHandLHUinstruction implementations. Every piece of memory is guaranteed to be 4-byte aligned via the0xFFFFFFFCmask prior to thereadMemcall, but the ISA specification explicitly states a requirement for 2-byte alignment for these instructions.
@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.
@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.
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.
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.