If a MIMOProxy owner destroys their proxy, they cannot deploy another from the same address
If a MIMOProxy owner destroys their proxy via selfdestruct, the registry's check for an existing proxy reverts because it calls owner() on a destroyed contract, preventing deployment of a new proxy from the same address. The owner must use a new EOA address to deploy another proxy.
Originally disclosed on Code4rena
*Submitted by horsefacts*
When deploying a new `MIMOProxy`, the `MIMOProxyRegistry` first checks whether a proxy exists with the same owner for the given address. If an existing proxy is found, the deployment reverts:
[`MIMOProxyRegistry#deployFor`](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxyRegistry.sol#L45-L59)
```
function deployFor(address owner) public override returns (IMIMOProxy proxy) {
IMIMOProxy currentProxy = _currentProxies[owner];
// Do not deploy if the proxy already exists and the owner is the same.
if (address(currentProxy) != address(0) && currentProxy.owner() == owner) {
revert CustomErrors.PROXY_ALREADY_EXISTS(owner);
}
// Deploy the proxy via the factory.
proxy = factory.deployFor(owner);
// Set or override the current proxy for the owner.
_currentProxies[owner] = IMIMOProxy(proxy);
}
}
```
However, if a `MIMOProxy` owner intentionally or accidentally destroys their proxy by `delegatecall`ing a target that calls `selfdestruct`, the address of their destroyed proxy will remain in the `_currentProxies` mapping, but the static call to `currentProxy.owner()` on L49 will revert. The caller will be blocked from deploying a new proxy from the same address that created their original `MIMOProxy.
### Impact
If a user accidentally destroys their MIMOProxy, they must use a new EOA address to deploy another.
### Recommended Mitigation Steps
Check whether the proxy has been destroyed as part of the “proxy already exists” conditions. If the proxy address has a codesize of zero, it has been destroyed:
```
// Do not deploy if the proxy already exists and the owner is the same.
if (address(currentProxy) != address(0) && currentProxy.code.length > 0 && currentProxy.owner() == owner) {
revert CustomErrors.PROXY_ALREADY_EXISTS(owner);
}
```
#### Test cases
We’ll use this `ProxyAttacks` helper contract to manipulate proxy storage. Note that it has the same storage layout as `MIMOProxy`.
```
contract ProxyAttacks {
address public owner;
uint256 public minGasReserve;
mapping(address => mapping(address => mapping(bytes4 => bool))) internal _permissions;
// Selector 0x9cb8a26a
function selfDestruct() external {
selfdestruct(payable(address(0)));
}
}
```
Then deploy the `ProxyAttacks` helper in a test environment and use `MIMOProxy` to `delegatecall` into it:
```
import chai, { expect } from 'chai';
import { solidity } from 'ethereum-waffle';
import { deployments, ethers } from 'hardhat';
import { MIMOProxy, MIMOProxyFactory, MIMOProxyRegistry, ProxyAttacks } from '../../typechain';
chai.use(solidity);
const setup = deployments.createFixture(async () => {
const { deploy } = deployments;
const [owner, attacker] = await ethers.getSigners();
await deploy("MIMOProxy", {
from: owner.address,
args: [],
});
const mimoProxyBase: MIMOProxy = await ethers.getContract("MIMOProxy");
await deploy("MIMOProxyFactory", {
from: owner.address,
args: [mimoProxyBase.address],
});
const mimoProxyFactory: MIMOProxyFactory = await ethers.getContract("MIMOProxyFactory");
await deploy("MIMOProxyRegistry", {
from: owner.address,
args: [mimoProxyFactory.address],
});
const mimoProxyRegistry: MIMOProxyRegistry = await ethers.getContract("MIMOProxyRegistry");
await deploy("ProxyAttacks", {
from: owner.address,
args: [],
});
const proxyAttacks: ProxyAttacks = await ethers.getContract("ProxyAttacks");
return {
owner,
attacker,
mimoProxyBase,
mimoProxyFactory,
mimoProxyRegistry,
proxyAttacks,
};
});
describe("Proxy attack tests", () => {
it("Proxy instance self destruct + recreation", async () => {
const { owner, mimoProxyRegistry, proxyAttacks } = await setup();
await mimoProxyRegistry.deploy();
const currentProxy = await mimoProxyRegistry.getCurrentProxy(owner.address);
const proxy = await ethers.getContractAt("MIMOProxy", currentProxy);
// Delegatecall to selfDestruct on ProxyAttacks contract
await proxy.execute(proxyAttacks.address, "0x9cb8a26a");
// Owner's existing proxy is destroyed
expect(proxy.owner()).to.be.revertedWith("call revert exception");
// Cannot deploy another proxy for this address through the registry
await expect(mimoProxyRegistry.deploy()).to.be.revertedWith("function returned an unexpected amount of data");
});
});
```
**[RayXpub (Mimo) confirmed and commented](https://github.com/code-423n4/2022-08-mimo-findings/issues/162#issuecomment-1210522543):**
> We confirm this issue and intend to implement a fix.
**horsefacts (warden) reviewed mitigation:**
> **Status:** �
Resolved
> **Finding:** A warden identified that if a `MIMOProxy` owner destroys their proxy by calling selfdestruct, they cannot deploy another from the same address.
> **What changed:** The Mimo team added [a check](https://github.com/mimo-capital/2022-08-mimo/blob/5a63c76ed74186d11d3adb361d3b3aacefa7795e/contracts/proxy/MIMOProxyFactory.sol#L50) for the current proxy’s codesize in `MIMOProxyFactory#deploy`. If the proxy has been destroyed it is will be deleted from the `_proxyStates` mapping and a new proxy can be deployed. A [unit test](https://github.com/mimo-capital/2022-08-mimo/blob/5a63c76ed74186d11d3adb361d3b3aacefa7795e/test/01_unit/proxy/MIMOProxyFactory.test.ts#L230) demonstrates this behavior.
> **Why it works:** Since a proxy’s codesize will be zero when it has been destroyed, and cannot be zero otherwise, this check will allow the owner of a destroyed proxy to deploy another.
---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
The registry checks for an existing proxy by calling owner() on the stored address without first verifying that the address still contains code, so a destroyed proxy causes a revert.
What the attacker needed
Owner has a MIMOProxy; owner can delegatecall a selfdestructing contract
How it was found
- Deploy a MIMOProxy through the registry.
- Use the proxy to delegatecall a helper contract that calls selfdestruct.
- The proxy is destroyed and owner() reverts.
- Attempt to deploy a new proxy through the registry for the same owner, which reverts.
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 proxy is a contract that forwards calls to another contract. If the owner makes the proxy destroy itself, the registry still thinks a proxy exists for that owner and tries to ask it who the owner is, but the destroyed contract cannot answer, so the whole deployment fails. The fix is to check whether the proxy still has code before asking for its owner.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.