Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Storage Gap for Upgradeable Contracts #410

Closed
code423n4 opened this issue Oct 9, 2022 · 2 comments
Closed

No Storage Gap for Upgradeable Contracts #410

code423n4 opened this issue Oct 9, 2022 · 2 comments
Labels
bug Something isn't working disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) duplicate This issue or pull request already exists QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-10-blur/blob/main/contracts/BlurExchange.sol#L30

Vulnerability details

Impact

For upgradeable contracts, inheriting contracts may introduce new variables. In order to be able to add new variables to the upgradeable contract without causing storage collisions, a storage gap should be added to the upgradeable acontract.

If no storage gap is added, when the upgradable contract introduces new variables,
it may override the variables in the inheriting contract.

Storage gaps are a convention for reserving storage slots in a base contract, allowing future versions of that contract to use up those slots without affecting the storage layout of child contracts.
To create a storage gap, declare a fixed-size array in the base contract with an initial number of slots.
This can be an array of uint256 so that each element reserves a 32 byte slot. Use the naming convention __gap so that OpenZeppelin Upgrades will recognize the gap:

Classification for a similar problem:
https://code4rena.com/reports/2022-05-alchemix/#m-05-no-storage-gap-for-upgradeable-contract-might-lead-to-storage-slot-collision

contract Base {
    uint256 base1;
    uint256[49] __gap;
}

contract Child is Base {
    uint256 child;
}

Openzeppelin Storage Gaps notification:

Storage Gaps
This makes the storage layouts incompatible, as explained in Writing Upgradeable Contracts. 
The size of the __gap array is calculated so that the amount of storage used by a contract 
always adds up to the same number (in this case 50 storage slots).

Proof of Concept

BlurExchange contract is intended to be upgradeable, including the inherited contracts ReentrancyGuarded, EIP712, OwnableUpgradeable, UUPSUpgradeable

However, neither ReentrancyGuarded nor the EIP712 or OwnableUpgradeable  contract contains storage gaps.

The BlurExchange contract contains storage variables, therefore the base contracts ReentrancyGuarded and EIP712 or OwnableUpgradeable cannot be upgraded to include any additional variables because it would overwrite the variables declared in the child contract BlurExchange. This greatly limits contract upgradeability.

Recommended Mitigation Steps

Consider adding a storage gap at the end of the upgradeable abstract contract

uint256[50] private __gap;
@blur-io-toad
Copy link

blur-io-toad commented Oct 16, 2022

Helpful tweak, but not a vulnerability. I think this should be QA

@blur-io-toad blur-io-toad added disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) labels Oct 16, 2022
@GalloDaSballo
Copy link
Collaborator

We're having discussions about standardizing the severity for this type of findings:
code-423n4/org#55

With the sponsor confirming and resolving, considering that library contract have no gaps.

Will mark the finding QA - Low Severity.

I recommend the sponsor to not add new storage slots to Library Contracts as they are not set to be changed in that way (and most projects never do, see discussion linked).

L

@GalloDaSballo GalloDaSballo added QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Oct 27, 2022
@JeeberC4 JeeberC4 added the duplicate This issue or pull request already exists label Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) duplicate This issue or pull request already exists QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

4 participants