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.
Submitted by WatchPug
EIP712MetaTransactionis a utils contract that intended to be inherited by concrete (actual) contracts, therefore. it’s initializer function should not use theinitializermodifier, instead, it should useonlyInitializingmodifier. See the implementation of openzeppelinEIP712Upgradeableinitializer 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).
/**
* @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
initializercan 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);
}
- A utils contract that is not expected to be deployed as a standalone contract should be declared as
abstract. It’sinitializerfunction should beinternal.
See the implementation of openzeppelin EIP712Upgradeable.
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.
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.