The PreimageOracle contract allows anyone to load preimage data for any address, including future precompile addresses, and mark it as valid. An attacker could pre-populate data for a precompile that does not yet exist, so that when that precompile is later added and accelerated, the stored data would be accepted as valid, potentially allowing invalid proofs to be validated.
Inside PreimageOracle.sol, anyone can call the loadPrecompilePreimagePart() to load data into the contract. The problem is that anyone is able to specify the address _precompile parameter. This opens up the door for malicious users to pre-populate data and use it to validate wrong data.
Proof of Concept
PreimageOracle.readPreimage() is used to read already validated preimage_data, this happens in the MIPS.sol implementation and the mips.go implementation:
Looking at readPreimage, we can notice this check that happens that ensures the field is set to true:
function readPreimage(bytes32 _key, uint256 _offset) external view returns (bytes32 dat_, uint256 datLen_) {
-> require(preimagePartOk[_key][_offset], "pre-image must exist");
// Calculate the length of the pre-image data
// Add 8 for the length-prefix part
datLen_ = 32;
uint256 length = preimageLengths[_key];
if (_offset + 32 >= length + 8) {
datLen_ = length + 8 - _offset;
}
// Retrieve the pre-image data
dat_ = preimageParts[_key][_offset];
}
Now, a malicious user can use the address of the next precompile to be added as a parameter, specify the data he wants to be pass and it will set the flag to true, enabling the ability to read the preimage with invalid data.
Put this test in PreimageOracle.t.sol:
function test_poc() public {
bytes memory input = hex"deadbeef1337";
uint256 offset = 0x00;
// Newly added precompile
address precompile = address(bytes20(uint160(0x0b)));
bytes32 key = precompilePreimageKey(precompile, input);
oracle.loadPrecompilePreimagePart(offset, precompile, input);
// try reading, it should succeed
oracle.readPreimage(key, offset);
}
This means that the calls from cannon and mips.sol can all succeed, leading to malicious users crafting data such that they can validate invalid proofs because the storage can be pre-populated and when doing a read on the precompileimage, it will be true and succeed.
The precompile address is used in the hash to create the key and as such makes keys unique for different addresses. Since the program will use the actual precompile addresses here, any custom non-precompile addresses will create different keys and the precompile data will not be affected.
@obront - the Sponsors comment and your comment are 100% correct.
However, our report is not about the issue of keys of different addresses. As stated in the report, loadPrecompilePreimagePart() is made to be able to handle newly added precompiles. Note that this has been confirmed by the Sponsor here during the Judging phase:
We do not check the precompile address during loadPrecompilePreimagePart to allow the op-program to use request newly added precompiles on L1 without upgrading the PreimageOracle.
Our report describes the fact that a malicious user can set:
preimagePartOk[_key][_offset] = true
Before a new precompile gets added and leverage this to make invalid reads during step correct due to the fact that this check in readPreimage() will result in true:
function readPreimage(bytes32 _key, uint256 _offset) external view returns (bytes32 dat_, uint256 datLen_) {
-> require(preimagePartOk[_key][_offset], "pre-image must exist");
For example, 0xb precompile is not out yet, however, it will be added. A malicious user can already make calls using that address and load into it. When 0xb precompile is released, it will be the same address as the one used by the malicious user so the preimagePartOk[_key][_offset] will be the same, which a malicious user can leverage.
Thanks for the clarification - I’d agree that is a valid finding specifically around populating data for a precompile that doesn’t yet exist and that is later added and needs to be accelerated. cc: @obront
The original report as published on Code4rena, by bronze_pickaxe. 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the contract does not restrict which addresses can be used when loading preimage data, allowing data to be stored for future precompile addresses before they are officially added.
Impact
An attacker could pre-populate data for a future precompile, and if that precompile is later added and accelerated, invalid proofs might be validated, though this requires the precompile to be added and used by the op-program.
What the attacker needed
Attacker can call loadPrecompilePreimagePart; future precompile is added and accelerated
The fix
The contract should restrict loading preimage data to only known precompile addresses or otherwise prevent pre-population for future precompiles.
How it was found
Call loadPrecompilePreimagePart with a future precompile address and arbitrary data.
The call sets the preimagePartOk flag to true for the corresponding key and offset.
Later, when the precompile is added, readPreimage for that key and offset succeeds and returns the stored data.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
This is about a smart contract that stores data used to verify computations. Anyone can add data for any address, even addresses that are not yet active. If a future address becomes a special built-in (a precompile), the data added earlier could be treated as correct, which might let someone cheat the verification.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.