Aquifer is vulnerable to Metamorphic Contract Attack
The Aquifer contract allows deploying Well contracts via create2 with a salt, and if the implementation contract can self-destruct, an attacker can redeploy a different implementation to the same address and then deploy a new Well to the same address with different logic. This could cause users to interact with a Well whose behaviour differs from what they expect.
Submitted by a3yip6
The Aquifer contract supports multiple ways to deploy the Well contracts. More specifically, it supports create and create2 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 different Well implementations in the same address, which is recorded by mapping(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 in Aquifer 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
// the Aquifer contract
function boreWell(
address implementation,
bytes calldata immutableData,
bytes calldata initFunctionCall,
bytes32 salt
) external nonReentrant returns (address well) {
if (immutableData.length > 0) {
if (salt != bytes32(0)) {
well = implementation.cloneDeterministic(immutableData, salt);
} else {
well = implementation.clone(immutableData);
}
} else {
if (salt != bytes32(0)) {
well = implementation.cloneDeterministic(salt);
} else {
well = implementation.clone();
}
}
...
}
// the cloneDeterministic() function
function cloneDeterministic(address implementation, bytes32 salt)
internal
returns (address instance)
{
/// @solidity memory-safe-assembly
assembly {
mstore(0x21, 0x5af43d3d93803e602a57fd5bf3)
mstore(0x14, implementation)
mstore(0x00, 0x602c3d8160093d39f33d3d3d3d363d3d37363d73)
instance := create2(0, 0x0c, 0x35, salt)
// Restore the part of the free memory pointer that has been overwritten.
mstore(0x21, 0)
// If `instance` is zero, revert.
if iszero(instance) {
// Store the function selector of `DeploymentFailed()`.
mstore(0x00, 0x30116425)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
}
}
As shown in the above code, attackers are capable to deploy new Well contracts through cloneDeterministic multiple times with the same input parameter implementation. And the cloneDeterministic function utilizes the following bytecode to deploy a new Well contract: 0x602c3d8160093d39f33d3d3d3d363d3d37363d73 + implementation + 5af43d3d93803e602a57fd5bf3. That is to say, if the address (i.e., implementation) remains the same, then the address of the deployed Well 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 through cloneDeterministic.
Here is how we attack:
- Assuming Bob deploys
Well_Implementation1to address 1. - Bob invoke
Aquifer:boreWellwith address 1 as the parameter to get a newly deployedWell1contract at address 2. - Bob invokes the self-destruct logic in the
Well_Implementation1contract and re-deploy a new contract to address 1 through Metamorphic Contract, namelyWell_Implementation2. - Bob invoke
Aquifer:boreWellwith address 1 again. Since the input ofcreate2remains the same, a new contract is deployed to address 2 with new logic fromWell_Implementation2.
Recommended Mitigation Steps
Remove the cloneDeterministic feature, leaving the clone functionality only.
publiuss (Basin) acknowledged and commented:
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.
It was documented here.
That Well implementations should not be able to self-destruct.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the factory uses create2 with a salt derived only from the implementation address, and it does not prevent the implementation from self-destructing, so the same address can be redeployed with different code.
Impact
The real implementation of the Well contract listed in Aquifer may be inconsistent with the expectation of users, and users may suffer from unexpected loss due to the change of contract logic.
What the attacker needed
Implementation contract can self-destruct; attacker can deploy contracts
The fix
The project documented that Well implementations should not be able to self-destruct, and the report recommends removing the cloneDeterministic feature.
How it was found
- Deploy a Well implementation contract to an address.
- Call Aquifer.boreWell with that implementation address to deploy a Well at a deterministic address.
- Trigger self-destruct on the implementation and redeploy a different implementation to the same address.
- Call Aquifer.boreWell again with the same implementation address, causing a new Well to be deployed at the same deterministic address with the new logic.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract can be deployed at a predictable address using a special opcode called create2. If the original contract at that address can be destroyed (self-destruct), someone can put a different contract at the same address later. This report shows that a factory contract allowed this, so the same address could end up with different code.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.