The First-Depositor Attack: How Share Inflation Drains ERC-4626 Vaults
Security Wiz team4 min read
A vault is a shared pot on a blockchain. People put money in and get shares back, the way investors in a fund do. ERC-4626 is the common standard for building one. If you're building a vault, there's a classic flaw to know about. It's called the first-depositor attack, or share inflation, and it lets the first person into a brand-new vault walk away with the next person's deposit. It falls under SC07, arithmetic errors, in the OWASP Smart Contract Top 10.
How vault shares work
When you deposit, the vault gives you shares in proportion to your slice of the pot. Put in 10% of the money and you get 10% of the shares. In code:
shares = assets * totalShares / totalAssets;
When the vault is empty, the first depositor usually gets one share for each unit deposited. After that, the formula keeps the price of a share consistent.
The catch is that blockchains don't do decimals. Every division is rounded down to a whole number. If your deposit is small compared with what's already in the pot, the answer can round all the way down to zero shares.
The attack
- The attacker is the first depositor into an empty vault. They deposit the smallest amount possible and receive 1 share.
- The attacker then donates a large sum by sending tokens straight to the vault's address, skipping the deposit function. The vault now holds a lot of money, but only 1 share exists, so that one share is worth all of it.
- A normal user deposits. Say the attacker donated 10,000 tokens and the user deposits 5,000. The user is owed half a share, and half a share rounds down to zero.
- The user's 5,000 tokens are in the vault, and they hold no shares for them. The attacker redeems the only share and takes everything, including the user's deposit.
Nothing in the code "broke." Every line did what it was written to do. The loss comes from three things together: rounding, a vault that starts empty, and a way to add money to the pot without creating shares.
Why it's a Medium for us, and a Critical elsewhere
This flaw shows why a single severity label can mislead. Our scoring system, OVSS, records each judgment about a flaw in a short code and turns the code into a score. For this one:
OVSS:2.0/F:L/C:N/A:N/D:N/M:L/R:N/S:U/PR:N/AC:M/PC:R/UI:N → 4.2 Medium
In plain terms: deposited money is stolen (F:L), but from individual depositors and not from the vault as a whole (M:L). The attacker needs real money of their own, tied up across several transactions (AC:M). And the attack only works in a rare situation, an empty vault at launch (PC:R).
A scale that looks only at the damage, such as the Immunefi classification many bug bounty programs use, would call the same flaw Critical, because user funds are stolen directly. Both are right about different things, which is why our reports show both.
The fixes
Virtual shares and assets. The most common defense is to have the vault do its math as if a small amount of money and a small number of shares were already in it. The starting price can then never be distorted, and the attacker would have to donate far more than they could ever take back. OpenZeppelin's widely used ERC-4626 code includes this, and you can adjust the size of the cushion.
Dead shares. At launch, create a small number of shares and send them to an address nobody controls, so the total number of shares can never be tiny.
Internal bookkeeping. Have the vault keep its own record of what has been deposited and withdrawn, and not just read its token balance. Tokens sent straight to the vault then can't move the share price.
Reject deposits that would earn zero shares. Cancel the deposit if the depositor would get nothing for it:
require(shares > 0, "zero shares");
A minimum first deposit. Require a meaningful first deposit, ideally made by the team at launch.
Use more than one of these. The direction of rounding matters too. Always round in the vault's favor: slightly fewer shares on the way in, slightly less paid out on the way out. Any rounding error then stays with the vault and never becomes a profit for the person transacting.
How we test for it
We write the attack as a test anyone can run: an attacker deposits first, donates, then a victim deposits, and we check that the victim gets a fair number of shares. We also have software try thousands of deposits, from tiny to huge, checking that none can earn zero shares and that nobody can withdraw more than they put in. On the Fork tier we repeat the scenario on a fork, a private copy of the live blockchain, against the real token the vault will hold, because a token that charges a fee on transfer or adjusts balances on its own changes the math.
If you're building a vault, have these paths tested before launch. Request an audit, or read about how fork testing works.
Shipping something onchain?
Send us your repo and we’ll return a quote within 24 hours.