Skip to content
Access control Write-up Advanced

[web3] Enzyme Finance Missing Privilege Check Bugfix Review. This is a postmortem of a vulnerability… | by Immunefi | Aug, 2022 | Medium

The paymaster contract failed to verify that the forwarder address in a relayed call was trusted, allowing an attacker to supply a malicious forwarder and bypass signature checks. This let the attacker manipulate relay fees and repeatedly drain the paymaster's vault, which funds gasless transactions.

medium.com

Published on medium.com

Immunefi

Immunefi, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

Enzyme Finance is a decentralised asset management platform built on Ethereum. It lets anyone create, manage and invest in custom investment strategies using a variety of assets, including cryptocurrencies and other digital assets, and offers tools for investors to track investments and manage portfolios.

One of the features Enzyme relies on is the Gas Station Network (GSN), which lets gasless clients interact with Ethereum smart contracts without users needing ETH for transaction fees. Enzyme has a set of contracts supporting GSN use: GasRelayPaymasterLib, GasRelayPaymasterFactory and GasRelayRecipientMixin. The factory creates instances of paymasters, the mixin holds shared logic inherited for relayable transactions, and GasRelayPaymasterLib provides the paymaster logic, including the rules for calls that can be relayed. The paymaster is intended to validate that the forwarder is approved both by the paymaster and by the recipient contract in preRelayedCall.

Setup

The GSN is a decentralised network of relayers that lets dApps pay transaction costs instead of individual users. It uses meta-transactions: users sign messages describing a transaction they want executed, and relayers sign the Ethereum transaction, send it to the network and pay the gas. The components involved are:

  • Paymaster contract: maintains an Ethereum balance within RelayHub and can implement business logic deciding whether to approve or deny a meta transaction, for example accepting only whitelisted users or allowing transactions that incorporate token repayment.
  • Trusted forwarder: a simple contract whose main function is verifying user signatures.
  • Relay Hub: the smart contract that facilitates and oversees interactions among users, relay servers, paymaster contracts and recipient contracts.
  • Relay server (relayer): pays gas fees on behalf of users and may later be reimbursed in tokens or other means.

The meta-transaction flow is:

  1. The user sends a signed message to the relay server containing transaction details.
  2. The relay server verifies the transaction and ensures there are sufficient fees to cover costs.
  3. The relay server generates a new transaction that uses the user's signed message, the trusted forwarder's address and the paymaster's address to call the relay hub.
  4. The relay server signs the new transaction and sends it to the Ethereum network, paying the gas fees in advance.
  5. The relay hub calls the trusted forwarder contract with the user's signed message and then calls the recipient contract.
  6. The trusted forwarder validates the user's signature, recovers the user's address and transmits the transaction to the recipient contract.
  7. The recipient contract executes the transaction and updates blockchain state.
  8. The relay hub requests reimbursement from the paymaster for the relay server's gas fees.
  9. The paymaster validates the transaction and sends funds (tokens or ETH) to the relay server to cover gas fees and any additional service fees.
From the original on medium.com

The figure shows this general flow of a meta transaction before the detailed walkthrough above.

Finding it

Within Enzyme's GasRelayPaymasterLib contract, the external function that contained the check for a valid forwarder was overridden. In a typical GSN flow the trusted forwarder performs an important security check by verifying the user's signature when a transaction is relayed. Because the paymaster did not verify the provided forwarder's address, a malicious trusted forwarder could be supplied, the signature verification could be bypassed, and a relay call could be crafted so that the paymaster returns far more in fees than expected, since the from address is believed to match the signature provided.

Exploitation

Step 1: deploy a malicious forwarder

The attacker deploys their own forwarder contract.

Step 2: craft the relayCall parameters

The attacker crafts the parameters of relayCall to exploit the missing validation. The most relevant changes to the relay data are the forwarder, set to the malicious forwarder deployed by the attacker, and the pctRelayFee and baseRelayFee, which manipulate the amount of funds returned to the relayWorker by the paymaster.

Step 3: repeat until the Vault is drained

Enzyme mitigates risk by deploying multiple funds with small amounts of ether (0.5) used to repay relayers, so an attacker would have to submit many exploit transactions to drain the Vault. However, the paymaster automatically tops up funds with assets from the Vault if the paymasterData is set to true. The attacker therefore makes many relayCalls to the RelayHub until the target fund's Vault is drained.

Root cause

The external function in GasRelayPaymasterLib that contained the check for a valid forwarder was overridden, so the paymaster did not verify that the forwarder passed to it was trusted. The design assumes the trusted forwarder verifies user signatures; with that verification bypassed, the from address is taken as matching the signature and the fee parameters (pctRelayFee, baseRelayFee) can be inflated. The small per-fund ether balances (0.5) mean many calls are needed, but the paymaster automatically tops up funds from the Vault when paymasterData is true.

Impact

A critical bug that could have led to the draining of Enzyme's Vault, which funds paymasters and enables Enzyme's contracts to use the Gas Station Network. The fee parameters manipulate the amount returned to the relayWorker, and an attacker would make many calls until the target fund's Vault is drained. The Enzyme team confirmed and fixed the bug quickly and paid rootrescue $400,000 for the find.

Fix

Enzyme introduced a commit to add the required check within GasRelayPaymasterLib, which verifies whether the passed address is a trusted forwarder and reverts otherwise.

From the original on medium.com

Variants to test

  • Look for paymaster or recipient contracts that override the forwarder-validation function and drop the check. A positive result is a relayed call accepted with a forwarder address you control.
  • Test whether pctRelayFee and baseRelayFee in relayCall are bounded. A positive result is a reimbursement far above the real gas cost.
  • Check whether paymasterData can trigger automatic top-up from a Vault. A positive result is the paymaster balance refilling from fund assets after a relayed call.
  • Review other GSN integrations for the same pattern: a trusted forwarder assumed to verify signatures while the paymaster never confirms the forwarder is trusted.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 11 min read.

What happened, in plain words

Enzyme Finance is a service on the Ethereum blockchain that lets people build and run investment portfolios made of cryptocurrencies and other digital assets. Anyone can create a strategy, put money into it, and watch how it performs. To make the experience smoother, Enzyme uses a system called the Gas Station Network (GSN) so that users do not need to hold the special currency (ETH) that normally pays for transactions on Ethereum.

On 28 March 2023, a security researcher who goes by the name rootrescue found a serious flaw in how Enzyme's contracts talked to that gasless system. If someone had exploited it, it could have led to the draining of the money that Enzyme keeps aside to pay for those gasless transactions, a pot the report calls the Vault. The Enzyme team confirmed the problem, fixed it quickly, and paid rootrescue $400,000 for finding it.

This article walks through what the flaw was, how the gasless system works, and how the fix closes the hole. Everything is explained from scratch, so you do not need any blockchain background to follow along.

Background: what Enzyme Finance is

Enzyme Finance is a decentralised asset management platform built on Ethereum. "Decentralised" means there is no single company holding everyone's money; the rules live in software called smart contracts that run on the blockchain. A smart contract is a small program that lives on Ethereum and executes automatically when someone sends it a transaction. You can think of it like a vending machine: you put in the right input, and it gives a predictable output, with no human in the middle.

On Enzyme, anyone can create, manage and invest in custom investment strategies using a variety of assets, including cryptocurrencies and other digital assets. The platform also offers tools that make it easy for investors to track their investments and manage their portfolios.

Background: what the Gas Station Network is

Normally, every action on Ethereum costs a small fee called gas, paid in ETH. That is a problem for newcomers: before you can do anything useful, you need to buy ETH and keep some aside. The Gas Station Network (GSN) solves this by letting dApps (decentralised applications, meaning apps that talk to smart contracts) pay the costs of transactions instead of individual users. This lowers the barrier to entry and improves the user experience, because users can make gasless transactions.

The GSN is a decentralised network of relayers. A relayer is a computer run by someone who is willing to pay the gas fee up front on your behalf, and who gets reimbursed afterwards. The mechanism that makes this possible is called a meta-transaction.

A meta-transaction is a design pattern in which users sign messages containing information about a transaction they would like to execute, but relayers are responsible for signing the Ethereum transaction, sending it to the network, and paying the gas cost. In everyday terms: you write a note saying "please do X for me", sign it so everyone can tell it came from you, and hand it to a courier. The courier pays the postage and delivers it. The blockchain only sees the courier's delivery, but the note inside proves it was your request.

The general flow of the meta transaction works as shown in the following figure. Have a quick look at it before we dive into the nitty-gritty of how it actually works.

From the original on medium.com

The figure illustrates the overall path of a meta transaction, from the user's signed message through the relay server, the relay hub, the trusted forwarder and the paymaster, before the detailed walkthrough below.

The four components

Before the step-by-step flow, here are the pieces involved.

Paymaster contract. A payment management contract, often referred to as a paymaster, maintains an Ethereum balance within RelayHub. It can implement diverse business logic to determine whether to approve or deny a meta transaction. Examples of such logic include accepting transactions exclusively from whitelisted users, and allowing transactions that incorporate token repayment to the payment management contract. Think of the paymaster as a company expense account: it holds the money, and it has rules about which expenses it will cover.

Trusted forwarder. A trusted forwarder contract is remarkably straightforward, serving primarily one function: verifying user signatures. A signature is cryptographic proof that a particular message came from a particular person. The forwarder is the clerk at the counter who checks your ID before passing your request along.

Relay Hub. The RelayHub is a vital smart contract that facilitates and oversees interactions among various components in the system. These components include users, relay servers (also called relayers), paymaster contracts, and recipient contracts. It is the central switchboard that routes everything.

Relay server. A relay server, also known as a relayer, is a vital component in the context of meta-transactions and gasless transactions on the blockchain. Its primary role is to facilitate the process of executing transactions on behalf of users without requiring them to pay for the associated gas fees upfront. Instead, relay servers pay the gas fees and may later receive reimbursement or compensation in the form of tokens or other means, as defined by the system. It is the courier from the earlier comparison.

The meta-transaction flow, step by step

We can now comprehend the flow of meta-transactions as follows:

  1. The user sends a signed message to the relay server containing transaction details. The user is saying: here is what I want done, and here is my proof that I asked for it.
  2. The relay server verifies the transaction and ensures that there are sufficient fees to cover the costs. The courier checks that the job is worth doing before spending money on postage.
  3. The relay server generates a new transaction that uses the user's signed message, the trusted forwarder's address, and the paymaster's address to call the relay hub. The courier wraps your note in an envelope addressed to the switchboard, naming the ID checker and the expense account.
  4. The relay server signs the new transaction and sends it to the Ethereum network, paying the necessary gas fees in advance. The courier pays the postage out of their own pocket.
  5. After receiving the transaction, the relay hub calls the trusted forwarder contract with the user's signed message and then calls the recipient contract. The switchboard first asks the ID checker to look at the note, then passes the request to the actual service.
  6. The trusted forwarder validates the user's signature, recovers the user's address, and transmits the transaction to the recipient contract. "Recovering" the address means working out, from the signature alone, which account must have signed the message.
  7. The transaction is executed and the blockchain state is updated by the recipient contract. The actual work gets done.
  8. Following the completion of the transaction, the relay hub requests reimbursement from the paymaster contract for the relay server's gas fees. The courier asks the expense account to be paid back.
  9. The paymaster contract validates the transaction and sends funds (in tokens or ETH) to the relay server to cover the gas fees and any additional service fees. The expense account pays out.

The vulnerability: a missing privilege check

Enzyme has a set of contracts which support use of the GSN. This consists of GasRelayPaymasterLib, GasRelayPaymasterFactory, and GasRelayRecipientMixin.

  • GasRelayPaymasterFactory helps create instances of paymasters. A factory is a contract whose job is to produce other contracts, like a stamping machine that makes identical parts.
  • GasRelayRecipientMixin has shared logic that is inherited for relayable transactions. "Inherited" means other contracts reuse its code rather than rewriting it.
  • GasRelayPaymasterLib is responsible for providing the logic for paymasters, and importantly, the rules for calls that can be relayed.

The paymaster is intended to validate the forwarder is approved by the paymaster as well as by the recipient contract in preRelayedCall. In plain words, before paying out, the paymaster should check that the forwarder named in the request is one it trusts. preRelayedCall is the hook that runs before a relayed call is accepted.

However, within Enzyme's GasRelayPaymasterLib contract, the external function which contained the check for a valid forwarder was overridden. "Overridden" means a contract replaced the original version of a function with its own version. If the replacement leaves out the check, the check no longer happens.

When a relayed transaction is sent via GSN in a typical flow, the trusted forwarder is being relied on to perform an important security check, verifying the user's signature when a transaction is relayed. Since a malicious trusted forwarder can be provided due to missing verification of the provided forwarder's address in the paymaster, the signature verification can be bypassed, and a relay call can be crafted in such a way that the paymaster returns much more fees than expected, since the from address is believed to be the address which matches the signature provided.

That last sentence is the heart of it. The paymaster trusts whatever forwarder it is handed. An attacker hands it a fake forwarder that always says "yes, this signature is valid". The paymaster then treats the from address as matching the signature provided, and pays out fees on that basis.

How the attack would have worked

An attacker would craft the following parameters of relayCall to exploit the missing validation after deploying a malicious forwarder. The most relevant changes to the relay data are the forwarder, which is set to the malicious forwarder deployed by the attacker, and the pctRelayFee and baseRelayFee, which can be used to manipulate the amount of funds returned to the relayWorker by the paymaster.

Here is what those names mean:

  • relayCall is the function on the RelayHub that a relayer calls to submit a meta-transaction.
  • forwarder is the address of the contract that is supposed to check signatures.
  • pctRelayFee is a percentage-based fee, and baseRelayFee is a flat fee. Together they decide how much the paymaster reimburses.
  • relayWorker is the account that did the relaying and gets paid.

Because the paymaster never confirmed the forwarder was trusted, the attacker could set forwarder to their own contract and use the fee fields to manipulate the amount returned.

Since Enzyme mitigates risk by deploying multiple funds with small amounts of ether (0.5) which are utilized to repay relayers, an attacker would have to submit many exploit transactions to drain the Vault. In other words, each fund keeps only a small float, so one transaction cannot take everything. However, the paymaster automatically tops up funds with assets from the Vault if the paymasterData is set to true. That top-up is the crucial detail: the paymaster automatically tops up funds with assets from the Vault, so an attacker would make many calls until the target fund's Vault is drained.

To exploit this, an attacker would make many relayCalls to the RelayHub until the target fund's Vault is drained.

The fix

To address this issue, Enzyme introduced a commit to add the required check within the GasRelayPaymasterLib, which verifies if the passed address is a trusted forwarder and reverts otherwise. "Reverts" means the transaction is cancelled and any changes it made are undone, so a bad request simply fails instead of paying out.

From the original on medium.com

The image shows the change made in that commit, adding the check inside GasRelayPaymasterLib.

Why this matters

This was a critical bug that could have led to the draining of Enzyme's Vault, which funds paymasters and enables Enzyme's contracts to utilise the Gas Station Network. The lesson generalises: whenever one contract trusts another to perform a security check, the first contract must confirm it is talking to the right counterparty. A check that can be skipped by naming a different contract is not a check at all.

Glossary

  • Blockchain: a shared, append-only record of transactions maintained by many computers, so no single party can rewrite it.
  • Smart contract: a program stored on a blockchain that runs automatically when called.
  • Ethereum: the blockchain network Enzyme is built on.
  • ETH: the currency used on Ethereum, including to pay transaction fees.
  • Gas: the fee paid to have a transaction processed on Ethereum.
  • Gasless transaction: a transaction where someone else (a relayer) pays the gas on the user's behalf.
  • dApp: a decentralised application, an app that interacts with smart contracts.
  • Gas Station Network (GSN): a decentralised network of relayers that lets dApps pay transaction costs instead of users.
  • Meta-transaction: a pattern where a user signs a message describing what they want, and a relayer submits and pays for the actual blockchain transaction.
  • Relayer / relay server: a party that pays gas up front for a user's meta-transaction and is reimbursed later.
  • RelayHub: the central smart contract that coordinates users, relayers, paymasters and recipient contracts.
  • Paymaster: a contract holding a balance in RelayHub that decides whether to approve a meta-transaction and pays the relayer back.
  • Trusted forwarder: a contract whose main job is to verify user signatures before a relayed call proceeds.
  • Recipient contract: the contract that actually executes the requested action.
  • Signature: cryptographic proof that a message was produced by the holder of a particular account's key.
  • Address recovery: deriving which account signed a message from the signature itself.
  • preRelayedCall: the hook that runs before a relayed call is accepted, where the paymaster is meant to validate the forwarder.
  • relayCall: the RelayHub function a relayer calls to submit a meta-transaction.
  • forwarder: the parameter naming which forwarder contract should check the signature.
  • pctRelayFee: a percentage-based fee used to calculate the relayer's reimbursement.
  • baseRelayFee: a flat fee added to the relayer's reimbursement.
  • relayWorker: the account that performs the relaying and receives the reimbursement.
  • paymasterData: data passed to the paymaster; when set to true, it triggers an automatic top-up of the fund from the Vault.
  • Vault: the pool of assets that funds paymasters and enables Enzyme's contracts to use the GSN.
  • Override: replacing an inherited function with a new version in a contract; if the new version drops a check, the check is gone.
  • Revert: cancelling a transaction and undoing its effects, typically because a check failed.
  • Privilege check: a check that confirms the caller or a named party is actually allowed to do what is being asked.
  • Commit: a recorded change to a code repository.
  • Whitelist: an explicit list of parties that are allowed.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

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.