Open code423n4 opened 1 year ago
141345 marked the issue as low quality report
141345 marked the issue as primary issue
141345 marked the issue as high quality report
publiuss marked the issue as sponsor acknowledged
This issue is only an issue if an implementation address contains a way to self-destruct itself. No implementation address should be considered valid if it contains a way to self-destruct. This should be probably documented in all documentation.
Agree that this should be documented. It is pertinent to those auditing Wells.
alcueca marked the issue as satisfactory
alcueca marked the issue as selected for report
It was documented here: https://github.com/BeanstalkFarms/Basin/blob/91233a22005986aa7c9f3b0c67393842cd8a8e4d/src/interfaces/IWell.sol#L19
That Well implementations should not be able to self-destruct.
Lines of code
https://github.com/code-423n4/2023-07-basin/blob/9403cf973e95ef7219622dbbe2a08396af90b64c/src/Aquifer.sol#L40-L52
Vulnerability details
The Aquifer contract supports multiple ways to deploy the
Well
contracts. More specifically, it supportscreate
andcreate2
at the same time. However, such a feature is vulnerable to the Metamorphic Contract Attack. That is to say, attackers are capable to deploy two differentWell
implementations in the same address, which is recorded bymapping(address => address) wellImplementations;
.Although the Aquifer contract is claimed to be permissionless, it should not break the immutability. Thus, we consider it a medium-risk bug.
Impact
The real implementation of the
Well
contract listed inAquifer
may be inconsistent with the expectation of users. Even worse, users may suffer from unexpected loss due to the change of contract logic.Proof of Concept
As shown in the above code, attackers are capable to deploy new
Well
contracts throughcloneDeterministic
multiple times with the same input parameterimplementation
. And thecloneDeterministic
function utilizes the following bytecode to deploy a newWell
contract:0x602c3d8160093d39f33d3d3d3d363d3d37363d73 + implementation + 5af43d3d93803e602a57fd5bf3
. That is to say, if the address (i.e.,implementation
) remains the same, then the address of the deployedWell
contract also remains the same.Normally, EVM would revert if anyone re-deploy a contract to the same address. However, if the
implementation
contract contains self-destruct logic, then attackers can re-deploy a new contract with different bytecode to the same address throughcloneDeterministic
.Here is how we attack:
Well_Implementation1
to address 1.Aquifer:boreWell
with address 1 as the parameter to get a newly deployedWell1
contract at address 2.Well_Implementation1
contract and re-deploy a new contract to address 1 through Metamorphic Contract, namelyWell_Implementation2
.Aquifer:boreWell
with address 1 again. Since the input ofcreate2
remains the same, a new contract is deployed to address 2 with new logic fromWell_Implementation2
.Recommended Mitigation Steps
Remove the
cloneDeterministic
feature, leaving theclone
functionality only.Assessed type
Other