No Storage Gap for Upgradeable Contracts
The upgradeable contracts SimpleMarket and ExpiringMarket lack a storage gap, so adding new state variables in a future upgrade could overwrite storage slots of child contracts, corrupting variables like stopped or buyEnabled. This could cause unintended and serious consequences for the child contracts.
Submitted by 0x1337, also found by broccolirob
Impact
For upgradeable contracts, there must be storage gap to “allow developers to freely add new state variables in the future without compromising the storage compatibility with existing deployments”. Otherwise it may be very difficult to write new implementation code. Without storage gap, the variable in child contract might be overwritten by the upgraded base contract if new variables are added to the base contract. This could have unintended and very serious consequences to the child contracts.
Refer to the bottom part of this article: https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
Proof of Concept
As an example, the ExpiringMarket contract inherits SimpleMarket, and the SimpleMarket contract does not contain any storage gap. If in a future upgrade, an additional variable is added to the SimpleMarket contract, that new variable will overwrite the storage slot of the stopped variable in the ExpiringMarket contract, causing unintended consequences.
Similarly, the ExpiringMarket does not contain any storage gap either, and the RubiconMarket contract inherits ExpiringMarket. If a new variable is added to the ExpiringMarket contract in an upgrade, that variable will overwrite the buyEnabled variable in ExpiringMarket contract.
Recommended Mitigation Steps
Recommend adding appropriate storage gap at the end of upgradeable contracts such as the below. Please reference OpenZeppelin upgradeable contract templates.
uint256[50] private __gap;
Out of curiosity, is there a reason why the openzeppelin-upgrades package isn’t used?
According to the README, Transparent Upgradeable Proxies is used to make sure “all contracts can be iterated and improved over time.” seems like the
initializablecontract should’ve been inherited and utilized so that you wouldn’t have to worry about adding in storage gaps.
Optimism had a custom compiler that required custom proxies. This led to the package not working otherwise I would have used it.
It seems like always extending the top-level storage contract as a practice should avoid any issues here, right? It is a good issue that highlights I should never try to extend inherited contract’s storage.
Ahhh I see. It depends on future upgrades that will affect the storage layout. Inheriting from Ownable and Pausable for instance shouldnt affect upgradeability much because I dont think their required functionality will drastically change.
But yeah, ensuring there is sufficient gap is to future-proof the contracts. Worst case, do a new deployment.
I just ran into this attempting to add the Reentrancy Gaurd to a contract. To be clear warden should I add this to the end of the existing base contract? For example, appending the uint256[50] gap to the base contract before inheritance?
Thanks and good issue.
I’d love to know the appropriate way to bolt on something like
ReentrancyGuardonto an existing proxy-wrapped contract - is it even possible?My game plan is to bring the
nonReentrantmodifier into the top-level contract to only extend storage. Thank you again warden!
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 upgradeable base contracts did not reserve a storage gap, so future additions to base contracts can collide with child contract storage slots.
Impact
Adding new variables to a base contract in a future upgrade could overwrite child contract variables such as stopped or buyEnabled, causing unintended and serious consequences.
What the attacker needed
Future upgrade adds variables to base contract
The fix
Storage gaps should be added at the end of upgradeable contracts, following OpenZeppelin upgradeable contract templates.
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.
Upgradeable smart contracts keep their data in a fixed layout, like numbered boxes. If you later add a new box in a parent contract, it can take the number of a box already used by a child contract, overwriting its value. This report says the contracts did not reserve empty boxes (a storage gap) to prevent that.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.