First-depositor vault inflation attack
A fresh vault with totalSupply == 0 lets the first depositor set the price by donating assets, so the next depositor's shares round down to zero — the fix is virtual shares or a dead minimum stake.
Procedure
- Confirm the share-price formula
assets * totalSupply / totalAssets(OpenZeppelin_convertToShares) and note the truncation to zero whentotalAssetsis large relative to the deposit. - Reproduce the attack in a test:
function testInflationAttack() public {
attacker.deposit(1); // 1 wei -> 1 share
token.mint(address(attacker), 1e18);
attacker.donate(1e18); // totalAssets = 1e18+1, totalSupply = 1
assertEq(vault.previewDeposit(1e18), 0); // victim gets 0 shares
}
- Check for defences: OpenZeppelin ERC-4626 uses virtual
+1shares and+1assets; confirm the vault overrides_decimalsOffset()to a non-zero value (e.g.6). - Or verify a dead-shares bootstrap: mint
10 ** decimalstoaddress(0)in the constructor so the price cannot be set by the first user. - Check
previewDeposit(dust) > 0for all dust after the seed with a fuzz test:
function testFuzz_noZeroShares(uint96 a) public {
a = uint96(bound(a, 1, 1e30));
assertGt(vault.previewDeposit(a), 0);
}
- Confirm the deposit function reverts when
shares == 0rather than silently accepting (require(shares != 0)). - Check
convertToSharesandpreviewDepositrounding agree (Floor both) so integrators cannot exploit a mismatch. - Test with
_decimalsOffsetset to 0 (unprotected) — the fuzz test must fail, proving the test has teeth. - Verify
totalAssets()excludes an externally-donated amount or the donation path is closed (no permissionlessskim). - Run
forge test --match-test "Inflation|noZeroShares" -vvvand read the share counts.
Pitfalls
- A vault that overrides
totalAssets()totoken.balanceOf(address(this))whiletotalSupplyis 0 is directly donatable. - Virtual shares with
_decimalsOffset = 0do nothing — the offset must be > 0 for the attacker to take a loss on the donation. - Seeding with
address(0)shares and a mint that can later be recovered; ensure the seed is unrecoverable. - Fee-on-transfer deposit token:
previewDepositusesamountbut the vault received less, so shares over-mint. - An arbitrarily large donation cost is still profitable if the victim's deposit is larger; the attack scales with the victim, so "expensive" is not "safe".
Verification
forge test --match-test "Inflation|noZeroShares" -vvv
Pass: previewDeposit(1e18) > 0 after the attacker's donation, and the noZeroShares fuzz test reports no counterexample. The unprotected variant fails, proving detection.
Report the share formula, _decimalsOffset, and whether the attack extracts value.