Corruptible Upgradability Pattern
Several parent contracts inherited by the Boosted3TokenAuraVault and MetaStable2TokenAuraVault upgradeable vaults lack storage gap slots. Adding new storage variables to those parents in a future upgrade could overwrite the child contract's storage layout, breaking the vault and trapping assets.
xiaoming90 Follow with supernova
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-09-notional-judging/issues/64
Found by
xiaoming90, supernova
Summary
Storage of Boosted3TokenAuraVault and MetaStable2TokenAuraVault vaults might be corrupted during an upgrade.
Vulnerability Detail
Following are the inheritance of the Boosted3TokenAuraVault and MetaStable2TokenAuraVault vaults.
Note: The contracts highlighted in Orange mean that there are no gap slots defined. The contracts highlighted in Green mean that gap slots have been defined
Inheritance of the MetaStable2TokenAuraVault vault
graph BT;
classDef nogap fill:#f96;
classDef hasgap fill:#99cc00;
MetaStable2TokenAuraVault-->MetaStable2TokenVaultMixin:::nogap
MetaStable2TokenVaultMixin:::nogap-->TwoTokenPoolMixin:::nogap
MetaStable2TokenVaultMixin:::nogap-->BalancerOracleMixin:::nogap
TwoTokenPoolMixin:::nogap-->PoolMixin:::nogap
PoolMixin:::nogap-->AuraStakingMixin:::nogap
PoolMixin:::nogap-->BalancerStrategyBase;
BalancerStrategyBase:::hasgap-->BaseStrategyVault:::hasgap
BalancerStrategyBase:::hasgap-->UUPSUpgradeable
Inheritance of the Boosted3TokenAuraVault vault
graph BT;
classDef nogap fill:#f96;
classDef hasgap fill:#99cc00;
Boosted3TokenAuraVault-->Boosted3TokenPoolMixin:::nogap
Boosted3TokenPoolMixin:::nogap-->PoolMixin:::nogap
PoolMixin:::nogap-->BalancerStrategyBase
PoolMixin:::nogap-->AuraStakingMixin:::nogap
BalancerStrategyBase:::hasgap-->BaseStrategyVault:::hasgap
BalancerStrategyBase:::hasgap-->UUPSUpgradeable
The Boosted3TokenAuraVault and MetaStable2TokenAuraVault vaults are meant to be upgradeable. However, it inherits contracts that are not upgrade-safe.
The gap storage has been implemented on the BaseStrategyVault and BalancerStrategyBase contracts inherited by the Boosted3TokenAuraVault and MetaStable2TokenAuraVault vaults.
abstract contract BaseStrategyVault is Initializable, IStrategyVault {
using TokenUtils for IERC20;
using TradeHandler for Trade;
..SNIP..
// Storage gap for future potential upgrades
uint256[45] private __gap;
}
abstract contract BalancerStrategyBase is BaseStrategyVault, UUPSUpgradeable {
/** Immutables */
uint32 internal immutable SETTLEMENT_PERIOD_IN_SECONDS;
..SNIP..
// Storage gap for future potential upgrades
uint256[100] private __gap;
}
However, no gap storage is implemented on the Boosted3TokenPoolMixin, MetaStable2TokenVaultMixin, TwoTokenPoolMixin, PoolMixin, AuraStakingMixin and BalancerOracleMixin contracts inherited by the Boosted3TokenAuraVault and MetaStable2TokenAuraVault vaults.
Thus, adding new storage variables to any of these inherited contracts can potentially overwrite the beginning of the storage layout of the child contract. causing critical misbehaviors in the system.
Impact
Storage of Boosted3TokenAuraVault and MetaStable2TokenAuraVault vaults might be corrupted during upgrading, thus causing the vaults to be broken and assets to be stuck.
Code Snippet
https://github.com/sherlock-audit/2022-09-notional/blob/main/leveraged-vaults/contracts/vaults/BaseStrategyVault.sol#L14 https://github.com/sherlock-audit/2022-09-notional/blob/main/leveraged-vaults/contracts/vaults/balancer/BalancerStrategyBase.sol#L9
Tool used
Manual Review
Recommendation
Consider defining an appropriate storage gap in each upgradeable parent contract at the end of all the storage variable definitions as follows:
uint256[50] __gap; // gap to reserve storage in the contract for future variable additions
Reference
A similar issue was found in the past audit report:
- https://blog.openzeppelin.com/notional-audit/ - [M02] Adding new variables to multi-level inherited upgradeable contracts may break storage layout
Discussion
jeffywu
@weitianjie2000
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 parent mixin contracts (Boosted3TokenPoolMixin, MetaStable2TokenVaultMixin, TwoTokenPoolMixin, PoolMixin, AuraStakingMixin and BalancerOracleMixin) define no trailing storage gap, so future variable additions would shift the child contract's storage layout.
Impact
Storage of the two vaults might be corrupted during an upgrade, which could break the vaults and leave assets stuck; this is a latent risk that only materialises if a future upgrade adds variables to the affected parents.
What the attacker needed
A future upgrade adds variables to a parent; vaults are upgradeable proxies
The fix
An appropriate storage gap should be defined at the end of the storage variables in each upgradeable parent contract.
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, and an upgrade must never shift what lives in each slot. If a parent contract gains a new variable without reserved empty space (a gap), the child's existing data gets pushed along and read as the wrong thing. Here the vaults inherit parents that reserve no such space, so a later upgrade could scramble their stored values.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.