The MIPS doesn’t implement ADD , ADDI , and SUB instructions correctly
The MIPS contract's ADD, ADDI and SUB instructions wrap on overflow instead of raising the Integer Overflow exception the MIPS specification requires. The author claims this lets a malicious actor forge invalid claims and challenge valid claims, though the judge found no current way to trigger it.
alexfilippov314 Follow with RadiantLabs and OMEN
Originally disclosed on Code4rena
Submitted by alexfilippov314, also found by RadiantLabs and OMEN
https://github.com/code-423n4/2024-07-optimism/blob/70556044e5e080930f686c4e5acde420104bb2c4/packages/contracts-bedrock/src/cannon/MIPS.sol#L921 https://github.com/code-423n4/2024-07-optimism/blob/70556044e5e080930f686c4e5acde420104bb2c4/packages/contracts-bedrock/src/cannon/MIPS.sol#L929
Vulnerability details
According to the specification, ADD (page A-28), ADDI (page A-29), and SUB (page A-144) instructions should raise an Integer Overflow exception if overflow occurs. The current implementation simply wraps the result in such cases and does not raise any exceptions.
...
function execute(uint32 insn, uint32 rs, uint32 rt, uint32 mem) internal pure returns (uint32 out) {
unchecked {
...
else if (func == 0x20) {
return (rs + rt);
}
...
else if (func == 0x22) {
return (rs - rt);
}
...
}
}
...
This inconsistency leads to a situation where MIPS contract can’t correctly emulate such cases and therefore allows malicious actors to successfully forge invalid claims and challenge valid claims.
Impact
An inconsistent implementation of big-endian 32-bit MIPS32 architecture in the MIPS contract allows malicious actors to successfully forge invalid claims and challenge valid claims.
Recommended Mitigation Steps
Consider raising Integer Overflow exception for ADD, ADDI, and SUB instructions if overflow occurs.
Assessed type
Math
clabby (Optimism) confirmed and commented:
This report is valid, and it is a duplicate of
3.3.4from the Cantina report on the MIPS VM.
@obront -
op-programran means there was no overflow exception happened in runtime, so the issue is not valid.
rileyholterhus (warden) commented:
Without commenting on individual issue validity or how things should be grouped, I would like to point out that the following issues are currently judged as medium-severity:
The above issues all attempt to point out deviations from the MIPS spec that may or may not be reachable in op-program. I think this issue is another example of this, but it is currently high-severity.
obront (judge) decreased severity to Medium and commented:
The original thinking when judging this issue was that it could be presently exploited (whereas the other issues @rileyholterhus mentioned exposed deviations from the spec, but that we don’t currently have a way to exploit).
After some more digging and experimentation, it appears that there isn’t a current way to trigger this issue, as the overflow would revert when op-program is running.
This puts it in the same bucket as the other issues mentioned:
- It is a deviation from spec.
- If we could trigger it, we could get on chain MIPS to provide invalid values.
- This could be exploited in incredibly harmful ways.
- But presently, there is no known way to trigger it (and it shouldn’t be possible, given that op-program ran).
All issues that fit this criteria will be awarded as Medium, so this will be downgraded to align with that.
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 arithmetic instructions are implemented with unchecked wrapping arithmetic instead of detecting signed overflow and raising the exception the specification requires.
Impact
The author claims a malicious actor could forge invalid claims and challenge valid claims, but the judge found no current way to trigger the issue because the overflow would revert when op-program is running, so it was downgraded to Medium.
What the attacker needed
Crafted MIPS program reaching overflow; no known trigger at present
The fix
The instructions should raise an Integer Overflow exception when overflow occurs; the text does not say a fix was applied.
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 is software that pretends to be a processor and runs programs written for it. The MIPS specification says that when adding or subtracting two numbers gives a result too big or too small to store, the processor must signal an error called an overflow exception. This contract instead silently keeps the wrapped-around number, so its behaviour differs from a real MIPS processor.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.