-
Notifications
You must be signed in to change notification settings - Fork 0
solidity: Simulated upgrade attack fix #228
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | ||
|
|
||
| /** | ||
| * @title Universal Proposeable Upgradeable Proxy Standard (UPUPS) | ||
| * @notice Extends UUPSUpgradeable with a two-step upgrade process that prevents | ||
| * silent simulated upgrades that could extract confidential contract state. | ||
| * The upgrader must first call `proposeUpgrade` to announce the new | ||
| * implementation, and then call regular `upgradeToAndCall` in a later block. | ||
| * | ||
| * #### Example | ||
| * | ||
| * ```solidity | ||
| * contract MyContract is UPUPSUpgradeable, OwnableUpgradeable { | ||
| * function initialize(address _owner) public initializer { | ||
| * __Ownable_init(_owner); | ||
| * } | ||
| * | ||
| * // Gate proposeUpgrade with appropriate modifier. | ||
| * function _authorizeProposeUpgrade() internal onlyOwner { } | ||
| * | ||
| * // Gate UUPSUpgradeable.authorizeUpgrade with additional acceptProposedUpgrade() modifier. | ||
| * function _authorizeUpgrade(address newImpl) internal onlyOwner acceptProposedUpgrade(newImpl) { } | ||
| * } | ||
| * ``` | ||
| */ | ||
| abstract contract UPUPSUpgradeable is UUPSUpgradeable { | ||
| /// @custom:storage-location erc7201:oasisprotocol.storage.UPUPSUpgradeable | ||
| struct UPUPSUpgradeableStorage { | ||
| address _newImplementation; | ||
| bytes32 _newImplementationHash; | ||
| uint256 _minBlockNumber; | ||
| } | ||
|
|
||
| // keccak256(abi.encode(uint256(keccak256("oasisprotocol.storage.UPUPSUpgradeable")) - 1)) & ~bytes32(uint256(0xff)) | ||
| bytes32 private constant UPUPSUpgradeableStorageLocation = | ||
| 0x2ba9668233a4827367587178d890a758d4583dfd9e5c9ef8b58defb278a6ad00; | ||
|
|
||
| function _getUPUPSUpgradeableStorage() | ||
| private | ||
| pure | ||
| returns (UPUPSUpgradeableStorage storage $) | ||
| { | ||
| assembly { | ||
| $.slot := UPUPSUpgradeableStorageLocation | ||
| } | ||
| } | ||
|
|
||
| error ImplementationDoesNotMatch(); | ||
| error ImplementationHashDoesNotMatch(); | ||
| error MinBlockNumberNotReached(); | ||
| error MinBlockNumberInPast(); | ||
| error NewImplementationNotAContract(); | ||
|
|
||
| event UpgradeProposed( | ||
| address indexed newImplementation, | ||
| bytes32 indexed newImplementationHash, | ||
| uint256 indexed minBlockNumber | ||
| ); | ||
|
|
||
| event UpgradeAccepted( | ||
| address indexed newImplementation, | ||
| bytes32 indexed newImplementationHash, | ||
| uint256 indexed minBlockNumber | ||
| ); | ||
|
|
||
| /** | ||
| * @notice Initializes the contract. | ||
| */ | ||
| function __UPUPSUpgradeable_init() | ||
| internal | ||
| onlyInitializing | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @notice Checks if the proposed upgrade is valid and clears the proposed | ||
| * upgrade bits. | ||
| */ | ||
| modifier acceptProposedUpgrade(address newImplementation) { | ||
| _acceptProposeUpgrade(newImplementation); | ||
|
|
||
| _; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Function that should revert when `msg.sender` is not authorized | ||
| * to propose the upgrade. Use {proposedUpgradeImplementation} and | ||
| * {proposedUpgradeMinBlockNumber} to fetch current proposal details. Called | ||
| * by {proposeUpgrade}. | ||
| * | ||
| * Normally, this function will use an xref:access.adoc[access control] | ||
| * modifier such as {Ownable-onlyOwner}. | ||
| * | ||
| * ```solidity | ||
| * function _authorizeProposeUpgrade() internal onlyOwner {} | ||
| * ``` | ||
| */ | ||
| function _authorizeProposeUpgrade() internal virtual; | ||
|
|
||
| /** | ||
| * @notice Returns the new implementation address of the proposed upgrade. | ||
| */ | ||
| function proposedUpgradeImplementation() public view virtual returns (address) { | ||
| UPUPSUpgradeableStorage storage $ = _getUPUPSUpgradeableStorage(); | ||
| return $._newImplementation; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns the hash of the implementation runtime code of the proposed upgrade. | ||
| */ | ||
| function proposedUpgradeImplementationHash() public view virtual returns (bytes32) { | ||
| UPUPSUpgradeableStorage storage $ = _getUPUPSUpgradeableStorage(); | ||
| return $._newImplementationHash; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns the proposed upgrade minimum block number. | ||
| */ | ||
| function proposedUpgradeMinBlockNumber() public view virtual returns (uint256) { | ||
| UPUPSUpgradeableStorage storage $ = _getUPUPSUpgradeableStorage(); | ||
| return $._minBlockNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Reverts unless the implementations matches and the current block | ||
| * number is at least the minBlockNumber. | ||
| */ | ||
| function _acceptProposeUpgrade(address newImplementation) internal virtual { | ||
| if (newImplementation != proposedUpgradeImplementation()) { | ||
| revert ImplementationDoesNotMatch(); | ||
| } | ||
| if (newImplementation.codehash != proposedUpgradeImplementationHash()) { | ||
| revert ImplementationHashDoesNotMatch(); | ||
| } | ||
| if (block.number < proposedUpgradeMinBlockNumber()) { | ||
| revert MinBlockNumberNotReached(); | ||
| } | ||
| emit UpgradeAccepted(newImplementation, newImplementation.codehash, proposedUpgradeMinBlockNumber()); | ||
|
|
||
| UPUPSUpgradeableStorage storage $ = _getUPUPSUpgradeableStorage(); | ||
| $._newImplementation = address(0); | ||
| $._minBlockNumber = 0; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Propose the upgrade to the new implementation address after the | ||
| * given block number. If minBlockNumber is zero, take the number of the | ||
| * next block. | ||
| * @dev minBlockNumber is intentionally left to the caller's discretion: | ||
| * the function guards against the simulated upgrade attack, but a longer | ||
| * window may be provided for the new contract implementation review. | ||
| */ | ||
| function proposeUpgrade(address newImplementation, uint256 minBlockNumber) public virtual { | ||
|
matevz marked this conversation as resolved.
|
||
| if (newImplementation.code.length == 0) { | ||
| revert NewImplementationNotAContract(); | ||
| } | ||
| if (minBlockNumber == 0) { | ||
| minBlockNumber = block.number+1; | ||
| } | ||
| if (minBlockNumber <= block.number) { | ||
| revert MinBlockNumberInPast(); | ||
| } | ||
|
|
||
| UPUPSUpgradeableStorage storage $ = _getUPUPSUpgradeableStorage(); | ||
| $._newImplementation = newImplementation; | ||
| $._newImplementationHash = newImplementation.codehash; | ||
| $._minBlockNumber = minBlockNumber; | ||
|
|
||
| _authorizeProposeUpgrade(); | ||
|
|
||
| emit UpgradeProposed(newImplementation, newImplementation.codehash, minBlockNumber); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.