Corrupted storage after upgrade in the `MarketFactory` contract
The MarketFactory contract upgrade added a new mapping between existing storage slots, causing storage corruption. After the upgrade, the contract would read and write to incorrect slots, breaking the system unpredictably.
Source: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/53
Found by
eeyore
Summary
The old MarketFactory is meant to be upgraded to a new implementation. The problem is that a new extensions mapping was added between currently occupied storage slots. After the upgrade, the newly upgraded smart contract would be reading from storage slots that contain data no longer corresponding to the new storage layout. This would cause the system to break in an unpredictable manner, depending on the number of storage slots added as part of the upgrade.
Vulnerability Detail
As seen in the old storage layout:
IFactory public immutable oracleFactory;
ProtocolParameterStorage private _parameter;
mapping(address => mapping(address => bool)) public operators;
mapping(IOracleProvider => mapping(address => IMarket)) private _markets;
mapping(address => UFixed6) public referralFee;
And the new storage layout:
IFactory public immutable oracleFactory;
IVerifier public immutable verifier;
ProtocolParameterStorage private _parameter;
@> mapping(address => bool) public extensions;
mapping(address => mapping(address => bool)) public operators;
mapping(IOracleProvider => mapping(address => IMarket)) private _markets;
mapping(address => UFixed6) private _referralFees;
mapping(address => mapping(address => bool)) public signers;
The storage will be corrupted after the upgrade because the new extensions mapping was introduced between already populated slots. The extensions, operators, _markets, and _referralFees will read and write to incorrect slots.
Impact
- Corrupted storage of the
MarketFactorycontract. - System would break in an unpredictable manner.
Code Snippet
https://github.com/sherlock-audit/2024-08-perennial-v2-update-3/blob/main/perennial-v2/packages/perennial/contracts/MarketFactory.sol#L11-L35
https://github.com/equilibria-xyz/perennial-v2/blob/main/packages/perennial/contracts/MarketFactory.sol#L10-L25
https://arbiscan.io/address/0x046d6038811c6c14e81d5de5b107d4b7ee9b4cde#code#F1#L1
Tool used
Manual Review
Recommendation
Place the extensions mapping after the _referralFees mapping so that both extensions and signers are added after all the occupied slots, avoiding storage corruption.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/462
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 new `extensions` mapping was inserted before existing storage variables instead of being appended after them, violating storage layout compatibility.
Impact
The storage of the MarketFactory contract would be corrupted, causing the system to break in an unpredictable manner.
What the attacker needed
Contract upgrade
The fix
The `extensions` mapping should be placed after the `_referralFees` mapping so that new variables are added after all occupied slots.
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.
Smart contracts store data in numbered slots. When upgrading a contract, new variables must be added after all existing ones to avoid shifting the positions of old data. Here, a new mapping was inserted in the middle, so the contract would misinterpret old data as new variables, leading to corruption.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.