Report ID
#31740
Report type
Smart Contract
Has PoC?
Yes
Target
https://etherscan.io/address/0xC1E088fC1323b20BCBee9bd1B9fC9546db5624C5
Impacts
Permanent freezing of funds
Description
The diamondCut function in the provided smart contract lacks adequate validation checks, allowing critical functions to be removed, replaced, or added maliciously. This vulnerability could lead to the contract becoming unusable, funds being permanently frozen, or unauthorized control over the contract.
Vulnerability Details
The diamondCut function allows the owner to add, replace, or remove functions from the contract. However, there are no checks to prevent the removal of critical functions, such as those required for transferring ownership or handling funds. Additionally, there are no validations to ensure that the functions being added or replaced are not malicious. Here is the problematic function:
This function checks for valid facet addresses, ensures critical functions are not removed, and validates the delegate call to _init.
Impact Details
The impact of this vulnerability can be severe, including:
- Permanent Freezing of Funds: If critical functions that manage fund transfers are removed or replaced incorrectly, users may be unable to access or withdraw their funds.
- Ownership Hijacking: Replacing the ownership transfer function with a malicious one can allow an attacker to take over the contract.
- Functionality Disruption: Removing or replacing essential functions can lead to the contract becoming non-functional, disrupting all services dependent on it.
These issues can result in significant financial losses and loss of trust in the contract and associated services.
References
Proof of concept
Scenario: Removal of Critical Functions
Let's demonstrate how the lack of validation in the diamondCut function can lead to the removal of a critical function, such as setContractOwner, which can subsequently freeze the contract's ability to change ownership.
Step-by-Step Proof of Concept
- Setup the Contract:
- Deploy the
DiamondCutFacet contract along with the necessary library and interface contracts.
- Attempt to Remove
setContractOwner: - Use the
diamondCut function to remove the setContractOwner function from the contract.
Deployment Script
First, deploy the necessary contracts.
Proof of Concept Script
This script demonstrates how an attacker can remove the setContractOwner function.
Execution
- Deploy
DiamondCutFacet Contract: - Deploy the
DiamondCutFacet contract to the blockchain.
- Deploy Attack Contract:
- Deploy the
AttackDiamondCut contract to the blockchain.
- Remove
setContractOwner Function: - Call
removeSetContractOwner function from the AttackDiamondCut contract, passing the address of the DiamondCutFacet contract.
Expected Outcome
After executing the proof of concept:
- The
setContractOwner function will be removed from the DiamondCutFacet contract. - The owner will no longer be able to transfer ownership of the contract.
- This demonstrates how the lack of validation in the
diamondCut function can lead to permanent loss of critical functionality, effectively freezing the contract.
Mitigation
To mitigate this risk, ensure that the diamondCut function includes validation checks to prevent the removal or replacement of critical functions. The enhanced contract code provided earlier adds these necessary checks.
References