Skip to content
Smart contracts Audit finding Intermediate

Wrong implementation of EIP712MetaTransaction

The EIP712MetaTransaction utility contract used the initializer modifier on its initializer function, which is intended for concrete contracts, causing a revert when a derived contract's initializer calls it. This prevents proper initialisation of the meta-transaction functionality in inherited contracts.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

  1. EIP712MetaTransaction is a utils contract that intended to be inherited by concrete (actual) contracts, therefore. it’s initializer function should not use the initializer modifier, instead, it should use onlyInitializing modifier. See the implementation of openzeppelin EIP712Upgradeable initializer function.

EIP712MetaTransaction.sol#L102-L114

    /// @notice initialize method for EIP712Upgradeable
    /// @dev called once after initial deployment and every upgrade.
    /// @param _name the user readable name of the signing domain for EIP712
    /// @param _version the current major version of the signing domain for EIP712
    function initializeEIP712(string memory _name, string memory _version)
        public
        initializer
    {
        name = _name;
        version = _version;

        __EIP712_init(_name, _version);
    }

Otherwise, when the concrete contract’s initializer function (with a initializer modifier) is calling EIP712MetaTransaction’s initializer function, it will be mistok as reentered and so that it will be reverted (unless in the context of a constructor, e.g. Using @openzeppelin/hardhat-upgrades deployProxy() to initialize).

https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.5.1/contracts/proxy/utils/Initializable.sol#L50-L53

    /**
     * @dev Modifier to protect an initializer function from being invoked twice.
     */
    modifier initializer() {
        // If the contract is initializing we ignore whether _initialized is set in order to support multiple
        // inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
        // contract may have been reentered.
        require(_initializing ? _isConstructor() : !_initialized, "Initializable: contract is already initialized");

        bool isTopLevelCall = !_initializing;
        if (isTopLevelCall) {
            _initializing = true;
            _initialized = true;
        }

        _;

        if (isTopLevelCall) {
            _initializing = false;
        }
    }

See also: https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/releases/tag/v4.4.1

  1. initializer can only be called once, it can not be “called once after every upgrade”.

EIP712MetaTransaction.sol#L102-L114

    /// @notice initialize method for EIP712Upgradeable
    /// @dev called once after initial deployment and every upgrade.
    /// @param _name the user readable name of the signing domain for EIP712
    /// @param _version the current major version of the signing domain for EIP712
    function initializeEIP712(string memory _name, string memory _version)
        public
        initializer
    {
        name = _name;
        version = _version;

        __EIP712_init(_name, _version);
    }
  1. A utils contract that is not expected to be deployed as a standalone contract should be declared as abstract. It’s initializer function should be internal.

See the implementation of openzeppelin EIP712Upgradeable.

https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.5.1/contracts/utils/cryptography/draft-EIP712Upgradeable.sol#L28

abstract contract EIP712Upgradeable is Initializable {
    // ...
}

Recommended Mitigation Steps

Change to:

abstract contract EIP712MetaTransaction is EIP712Upgradeable {
    // ...
}
    /// @notice initialize method for EIP712Upgradeable
    /// @dev called once after initial deployment.
    /// @param _name the user readable name of the signing domain for EIP712
    /// @param _version the current major version of the signing domain for EIP712
    function __EIP712MetaTransaction_init(string memory _name, string memory _version)
        internal
        onlyInitializing
    {
        name = _name;
        version = _version;

        __EIP712_init(_name, _version);
    }

0xca11 (Rolla) confirmed, resolved, and commented:

Resolved in RollaProject/quant-protocol@25112fa, but upgradeability was later removed as per RollaProject/quant-protocol#90.


The original report as published on Code4rena, by WatchPug. 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

Likely the utility contract used the initializer modifier instead of onlyInitializing, and was not declared abstract, so its initializer could not be safely called from a derived contract's initializer.

Impact

The meta-transaction initialisation would revert when called from a derived contract's initializer, preventing the contract from being properly set up unless initialised via a constructor or proxy deployment tool.

What the attacker needed

Contract inherits EIP712MetaTransaction; initializer called outside constructor

The fix

The contract should be made abstract and its initializer changed to internal with the onlyInitializing modifier, as confirmed resolved by the project.

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.

Smart contracts can inherit code from other contracts. An initializer function sets up the contract once. The bug is that the utility contract's initializer was marked in a way that only works for the final contract, so when a real contract tried to use it, the setup failed and the contract could not be initialised properly.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Rolla contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.