本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2024/3/29

The Possibility of Inflation Attacks - A Deep Dive into ERC4626 Vaults

Pre-Credits:

Recently while scrolling X(formerly Twitter), I came across this tweet from @bytes032 saying these amazing lines:

We've decided to put our thoughts on the whole drama around ERC4626 and it's love for Inflation attacks, hoping that you can make a bank after reading this blog.

Understanding The Basics

We don't want you to go back and forth to learn about Vaults, ERC4626, etc... Let us take you on a ride to understand this drama well.

First, What are vaults?

Simply putting it out, it is a smart contract where users deposit their tokens (like ETH, BNB, USDC, USDT, and whatever) into the smart contract and receive an appropriate proportion of shares back to them representing their contribution to the system.

Most importantly you've to understand the fact that it's not only one way(depositing the tokens) process but also you can put your shares back into the vault to get back the original tokens that you were deposited + "the interest you've earned meanwhile."

Now the question comes, how am I getting the interest for just putting out my tokens into the vault?

To answer this, we need to understand what's going on behind the scenes. And let's take it step-by-step:

source: own drawings

Before that let's understand the meaning of LP Tokens: 

LP Tokens: These are also just vault tokens that you'll receive upon depositing equal worth of two tokens. For example, On Uniswap you can deposit an equal amount of USDT & ETH into the USDT/ETH Pool and you receive an equal proportion of LP Tokens representing your contribution.

1) The user deposits these LP tokens into the Vault contract.

2) The Vault contract mints some Vault Tokens (shares) and sends them back to the user.

At this stage, the Vault is responsible for all the funds. But the vault doesn't want to take the risk. So,

3) Vault sends all these LP tokens to a new smart contract called "Strategy".

A brief idea of Strategy: Take it as your uncle who knows everything and can give you $5 for every $2 you give him. You don't know what he's doing with your money, but somehow you're getting some profits in the end.

[This person/contract is solely responsible for all the money ]

In our case, Strategy is a good person who lets us view the internal mechanism.

4) At this point, Strategy deposits all these LP tokens into the "MASTERCHEF" contract (which is kind of another Strategy contract).

5) After a while, when the "MASTERCHEF" contract comes with a profit, we claim those reward tokens to our "Strategy" contract.

6) Now, to get more profits, we take these reward tokens and swap/convert them to the original tokens(USDT & ETH).

7) And we'll deposit these tokens again back into the "Uniswap" contract.

8) At this point, Uniswap mints these LP Tokens and sends them back to the Strategy contract directly and we continue the same process from Step 4).

Main Drama

We've gone through all these basic things and have you ever got a doubt on how these Vaults are calculating these shares?

Before that, To clear: ERC4626 is a tokenized vault standard that uses ERC20 tokens to represent ownership of another asset.

So you heard that? It's just an ERC20 extension with some little extra features. And main feature is: Calculating the Shares.

The formula for calculating the amount of shares while depositing:

shares = amount(deposited Amount) * totalSupply / balanceOf(address(this))

Let's decode each value:

  • Shares: these will be amount of shares that we'll be receiving upon depositing our funds into the Vault.

  • AssetAmount: represents our deposited amount of that specific asset (Can be any token).

  • totalSupply: As said ERC4626 is just an extension of ERC20, it stores the total vault-shares tokens ever created. This variable is just a representation of s.

  • balanceOf(address(this)): This represents the balance of the vault of that specific asset that the user deposited. If the user deposited BNB, this shows the amount of BNB which were held by the vault.

As we all know, Solidity doesn't support precision like 44.7346 and this leads to many rounding issues.

In reality, one can change the value of the denominator causing some rounding errors. But why only the denominator?

Simple, one cannot change the value of the existing total supply variable as it counts the number of shares ever minted, which means to change the value, we need to simultaneously provide more amount into the vault. Technically, increasing both the values in the numerator doesn't help for the inflation attack.

Wait, what is an Inflation Attack?

Before that, you need to understand the meaning of "Inflation" which reads as:

" The rate of increase in prices over a given period of time "

Simply putting it out, It's the ability to change the share value resulting in investors losing some or all of their funds to either the pool or a hacker.

Let's understand these in scenarios:

1) First Depositer Bug

Look into this code below which calculates the amount of shares as per ERC4626 standards.

Let's say, a fresh vault was deployed and some legitimate user deposits 20,000 tokens into the Vault.

The hacker monitors the mempool and sees this as profitable(we'll learn why) and deposits 1 token into the vault, actually front-running the legitimate user.

Stage 1)

At this stage, as per the logic, if the total supply is 0, we get the amount of shares equal to our deposited amount.

As the hacker deposited 1 token, he received 1 share. At this point, the values are:

  • TotalSupply = 1

  •  _Pool = 1

Just right before the legitimate user transaction, the hacker sends "20_000" tokens + 2 wei.

Now the values will be:

  • TotalSupply = 1

  • Pool = 20000 + 2 wei + 1

Stage 2)

Now Hacker back-runs the legitimate user transaction making him to gain no shares. Let's look into this:

When a user deposits 20_000 tokens, the value of the shares will be calculated as follows -

shares = 20000 * 1 / 20000 + 2 wei + 1

As the denominator value is slightly more than 20_000, the original value will be 0.99xxxxx. But as there are no decimals in Solidity, the precision goes back to 0, thus minting 0 shares to the legitimate user.

At this point, the values will be:

  • TotalSupply = 1

  • Pool = 40_000 + 2 wei + 1

Stage 3)

The Hacker sends back his 1 share (representing 100% of the vault) and gains a net profit of 20_000 as he technically steals the user's money.

Remediation Possible? 

Everyone generally thinks that this attack is possible only on the first deposit and doesn't work from the second time.

That's not the complete story, it just behaves differently. Instead of giving profits to the hacker, it turns up eating the user's deposits acting as a "Griefing Attack - where you get nothing in return but the user will be losing their funds."

But which hacker ends up spending more money just to make other people suffer? - Will it be you?

So according to the experts, the remediations are as follows:

  • Using Slippage protection: before interacting with the Vault, the web app should measure the number of shares it gonna receive during a deposit and revert if it does not receive the quantity expected within a certain slippage tolerance.

  • Using OpenZeppelin's ERC4626 extension: this extension fixes all the underlying issues by calculating the shares as the vault behaves as if it has much liquidity causing hackers to deposit more funds into the vault, resulting in a griefing attack.

Though some might say, that minting "Dead-Shares" is a good mechanism, we never felt it as superior to these remediations as given above.

So, simply speaking out, There's no complete remediation to fix this issue. All we can do is, make the attack unprofitable for the hacker.

Hoping that you learned something from this blog.  See you again next time in another awesome blog. Good Luck & WAGMI :)


Author: Gowtham Naidu Ponnana

Security Research at TECHFUND

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More