Malicious User Could Burn The Assets After A Successful Migration
Originally disclosed on Code4rena
Submitted by xiaoming90, also found by 0x52, cccz, codexploder, hyh, kenzo, Lambda, oyc_109, and zzzitron
Background
The following describes the migration process for a vault.
- Assume that Alice is the proposer.
- Alice calls
Migration.proposeto propose a set of modules and plugins to migrate a vault to - Other contributors could join a migration proposal by contributing ether and fractional tokens by calling
Migration.join. - Alice calls
Migration.committo kick off the buyout process for a migration after the proposal period (7 days) - If the buyout is successful, Alice calls the
Migration.settleVaultto settle a migration. Within this function, a new vault with new set permissions and plugins will be deployed. - Alice calls the
Migration.settleFractionsto mint the fractional tokens for a new vault. - Contributors who earlier joined the migration proposal could call the
Migration.migrateFractionsto migrate their fractional tokens from the old vault to the new vault. - Finally, Alice will call
Migration.migrateVaultERC20,Migration.migrateVaultERC721, and/orMigration.migrateVaultERC1155to transfer the ERC20, ERC721 (NFT), and/or ERC1155 tokens from the old vault to the new vault.
Vulnerability Details
It was observed that after a successful vault migration, an attacker could Migration.migrateVaultERC20, Migration.migrateVaultERC721, and/or Migration.migrateVaultERC1155 with an invalid _proposalId parameter, causing the assets within the vault to be burned.
Proof of Concept
The PoC for
Migration.migrateVaultERC20,Migration.migrateVaultERC721, and/orMigration.migrateVaultERC1155is the same. Thus, only the PoC forMigration.migrateVaultERC721is shown below, and the PoC formigrateVaultERC20andmigrateVaultERC1155are omitted for brevity.
Assume that the following:
-
vault Aholds only one (1) APE ERC721 NFT -
Alice proposes to migrate
vault Ato a new vault, and the buyout is successful. -
Alice proceeds to call
Migration.settleVaultto settle a migration, followed byMigration.settleFractionsto mint the fractional tokens for a new vault. -
An attacker calls
Migration.migrateVaultERC721(vault A, invalid_proposal_id, ape_nft_address, ape_nft_tokenId, erc721TransferProof)with an invalid proposal ID (proposal ID that does not exist).- Within the
Migration.migrateVaultERC721function, thenewVault = migrationInfo[_vault][_proposalId].newVaultwill evaluate to zero. This is because the_proposalIdis a non-existent index in themigrationInfoarray, so it will point to an address space that has not been initialised yet. Thus, the valuezerowill be returned, andnewVaultwill be set toaddress(0).
- Within the
-
Next, the
Migration.migrateVaultERC721function will attempt to transfer the ERC721 NFT from the old vault (_vault) to the new vault (newVault) by callingIBuyout(buyout).withdrawERC721. SincenewVaultis set toaddress(0), this will cause the ERC721 NFT to be sent toaddress(0), which effectively burns the NFT.
/// @notice Migrates an ERC-721 token to the new vault after a successful migration
/// @param _vault Address of the vault
/// @param _proposalId ID of the proposal
/// @param _token Address of the ERC-721 token
/// @param _tokenId ID of the token
/// @param _erc721TransferProof Merkle proof for transferring an ERC-721 token
function migrateVaultERC721(
address _vault,
uint256 _proposalId,
address _token,
uint256 _tokenId,
bytes32[] calldata _erc721TransferProof
) external {
address newVault = migrationInfo[_vault][_proposalId].newVault;
// Withdraws an ERC-721 token from the old vault and transfers to the new vault
IBuyout(buyout).withdrawERC721(
_vault,
_token,
newVault,
_tokenId,
_erc721TransferProof
);
}
Additional Note #1 - About Buyout.withdrawERC721
When a user proposes a migration, the user will kick off the buyout process after the proposal period. The Migration module will initiate the buyout on behalf of the user. Thus, the proposer of this buyout, in this case, would be the Migration module. Whenever Buyout.withdrawERC721 function is called, it will verify that msg.sender is equal to the proposer to ensure that only the proposer who is the auction winner can migrate the assets from old vault to new vault.
In this example, the attacker has access to Migration.migrateVaultERC20, Migration.migrateVaultERC721, and/or Migration.migrateVaultERC1155 functions that effectively instruct the Migration module to perform the withdrawal. In this case, it will pass the if (msg.sender != proposer) revert NotWinner(); validation within the Buyout.withdrawERC721 because the msg.sender is the Migration contract who initiates the buyout at the start.
function IBuyout(buyout).withdrawERC721(
address _vault,
address _token,
address _to,
uint256 _tokenId,
bytes32[] calldata _erc721TransferProof
) external {
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, address proposer, State current, , , ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller is not the auction winner
if (msg.sender != proposer) revert NotWinner();
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ITransfer.ERC721TransferFrom,
(_token, _vault, _to, _tokenId)
);
// Executes transfer of ERC721 token to caller
..SNIP..
}
Additional Note #2 - Can we send NFT to address(0)?
Yes, it is possible to send NFT to address(0).
If the ERC721 NFT contract uses Openzeppelin’s ERC721 contract or Solmate’s ERC721 contract, then the NFT cannot be sent to address(0) because the contracts have implemented validation check to ensure that the to address is not address(0).
However, not all the ERC721 NFT contracts use Openzeppelin or Solmate ERC721 implementation. Therefore, there will be a large number of custom implementations that allow NFT to be transferred to address(0).
The same theory applies to ERC20 and ERC1155 implementations.
Impact
Loss of assets for the users as the assets that they own can be burned by an attacker after a successful migration.
Recommended Mitigation Steps
It is recommended to implement additional validation to ensure that the _proposalId submitted is valid.
Consider checking if newVault points to a valid vault address before transferring the assets from old vault to new vault.
function migrateVaultERC721(
address _vault,
uint256 _proposalId,
address _token,
uint256 _tokenId,
bytes32[] calldata _erc721TransferProof
) external {
address newVault = migrationInfo[_vault][_proposalId].newVault;
+ if (newVault == address(0)) reverts VaultDoesNotExistOrInvalid;
// Withdraws an ERC-721 token from the old vault and transfers to the new vault
IBuyout(buyout).withdrawERC721(
_vault,
_token,
newVault,
_tokenId,
_erc721TransferProof
);
}
In the above implementation, if anyone attempts to submit an invalid _proposalId, the newVault will be set to address(0). The newly implemented validation will detect the abnormal behavior and revert the transaction.
For defense-in-depth, perform additional validation to ensure that the _to address is not address(0) within the Buyout.withdrawERC721 function.
function withdrawERC721(
address _vault,
address _token,
address _to,
uint256 _tokenId,
bytes32[] calldata _erc721TransferProof
) external {
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
+ if (_to == 0) revert ToAddressIsZero();
// Reverts if auction state is not successful
(, address proposer, State current, , , ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller is not the auction winner
if (msg.sender != proposer) revert NotWinner();
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ITransfer.ERC721TransferFrom,
(_token, _vault, _to, _tokenId)
);
// Executes transfer of ERC721 token to caller
IVault(payable(_vault)).execute(transfer, data, _erc721TransferProof);
}
The same validation checks should be implemented on migrateVaultERC20, migrateVaultERC1155, withdrawERC20 and withdrawERC1155
stevennevins (Fractional) confirmed
HardlyDifficult (judge) commented:
migrateVaultERC20could transfer assets to address(0). ERC721 and 1155 standards require revert when to is address(0), but this is not required by the ERC20 standard. This could be triggered by calling migrate with an invalidproposalId. Agree this is a High risk issue.Selecting this submission as the primary report for clearly outlining the potential high risk scenario here.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A high-severity access control bug in Fractional v2 contest. Code4rena made it public on 27 September 2022. Found in the 2022-07-fractional audit contest (finding H-09).
About Access control
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.