Skip to content
Smart contracts Audit finding Intermediate

Malicious Users Can Drain The Assets Of Vault. (Due to not being ERC4626 Complaint)

The vault's withdraw function converts assets to shares using rounding down, so small withdrawals can burn zero shares. An attacker could repeatedly withdraw assets without reducing their share balance, draining the vault.

fs0c Follow with 0xmuxyz, bin2chen and 7 others

Originally disclosed on Code4rena

Submitted by fs0c, also found by 0xmuxyz, bin2chen, ladboy233, Kumpa, nadin, DadeKuma, koxuan, rvierdiiev, and rvi0x

Malicious users can drain the assets of the vault.

Proof of Concept

The withdraw function users convertToShares to convert the assets to the amount of shares. These shares are burned from the users account and the assets are returned to the user.

The function withdraw is shown below:

function withdraw(
        uint256 assets,
        address receiver,
        address owner
    ) public nonReentrant syncFeeCheckpoint returns (uint256 shares) {
        if (receiver == address(0)) revert InvalidReceiver();

        shares = convertToShares(assets);
/// .... [skipped the code]

The function convertToShares is shown below:

function convertToShares(uint256 assets) public view returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return
            supply == 0
                ? assets
                : assets.mulDiv(supply, totalAssets(), Math.Rounding.Down);
    }

It uses Math.Rounding.Down , but it should use Math.Rounding.Up

Assume that the vault with the following state:

  • Total Asset = 1000 WETH
  • Total Supply = 10 shares

Assume that Alice wants to withdraw 99 WETH from the vault. Thus, she calls the Vault.withdraw(99 WETH) function.

The calculation would go like this:

assets = 99
return value = assets * supply / totalAssets()
return value = 99 * 10 / 1000
return value = 0

The value would be rounded round to zero. This will be the amount of shares burned from users account, which is zero.

Hence user can drain the assets from the vault without burning their shares.

Note : A similar issue also exists in mint functionality where Math.Rounding.Down is used and Math.Rounding.Up should be used.

Recommended Mitigation Steps

Use Math.Rounding.Up instead of Math.Rounding.Down.

As per OZ implementation here is the rounding method that should be used:

deposit : convertToShares → Math.Rounding.Down

mint : converttoAssets → Math.Rounding.Up

withdraw : convertToShares → Math.Rounding.Up

redeem : convertToAssets → Math.Rounding.Down

RedVeil (Popcorn) confirmed, but disagreed with severity

LSDan (judge) decreased severity to Medium


The original report as published on Code4rena, by fs0c. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

The withdraw function uses Math.Rounding.Down when converting assets to shares, but it should round up so that at least one share is burned.

Impact

A malicious user could drain the assets from the vault without burning their shares.

What the attacker needed

A vault with a small total supply relative to assets; ability to call withdraw

The fix

The withdraw function should use Math.Rounding.Up instead of Math.Rounding.Down when converting assets to shares.

How it was found

  1. The vault has 1000 WETH in assets and 10 shares in total supply.
  2. Alice calls withdraw for 99 WETH.
  3. The conversion calculates 99 * 10 / 1000, which rounds down to 0 shares.
  4. Zero shares are burned, so Alice receives 99 WETH while keeping her shares.

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.

A vault holds money for many people and tracks each person's share. When you take money out, the vault should burn a matching number of shares. Here the maths rounded the share amount down to zero, so someone could take money out while keeping all their shares, and repeat until the vault is empty.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by fs0c

View all

3 more disclosed reports, best first.

More from Popcorn contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.