No Storage Gap for Upgradeable Contract Might Lead to Storage Slot Collision
Several upgradeable contracts lack a storage gap, so if a base contract is later upgraded to add a new state variable, it could overwrite a variable in a child contract. This could cause loss of user funds or make the contract malfunction completely.
Submitted by 0x1337
AlchemicTokenV2Base.sol#L20 CrossChainCanonicalBase.sol#L12 TransmuterV2.sol#L26 CrossChainCanonicalAlchemicTokenV2.sol#L7
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” (quote OpenZeppelin). 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, potentially causing loss of user fund or cause the contract to malfunction completely.
Refer to the bottom part of this article: https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
Proof of Concept
Several contracts are intended to be upgradeable contracts in the code base, including
- AlchemicTokenV2Base
- CrossChainCanonicalBase
- CrossChainCanonicalAlchemicTokenV2
- TransmuterV2
However, none of these contracts contain storage gap. The storage gap is essential for upgradeable contract because “It allows us to freely add new state variables in the future without compromising the storage compatibility with existing deployments”. Refer to the bottom part of this article:
https://docs.openzeppelin.com/contracts/3.x/upgradeable
As an example, both the AlchemicTokenV2Base and the CrossChainCanonicalBase are intended to act as the base contracts in the project. If the contract inheriting the base contract contains additional variable, then the base contract cannot be upgraded to include any additional variable, because it would overwrite the variable declared in its child contract. This greatly limits contract upgradeability.
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;
0xleastwood (judge) commented:
Agree with warden and severity. Storage gaps are essential wherever inheritance is used by an upgradeable contract.
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 upgradeable base contracts do not reserve storage slots with a gap, so future additions to a base contract can collide with variables in inheriting contracts.
Impact
If a base contract is upgraded to add a variable, it could overwrite a child contract's variable, potentially causing loss of user funds or complete malfunction.
What the attacker needed
Contract is upgraded later; base contract gains a new variable
The fix
Add an appropriate storage gap at the end of each upgradeable contract, following OpenZeppelin's 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 numbered storage slots. If a parent contract is later changed to add a new variable, it can take a slot that a child contract already uses, corrupting the child's data. A storage gap is a reserved block of unused slots that prevents this.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.