Skip to content
Smart contracts Audit finding Advanced

Reentrancy issue in yVault.deposit

The deposit function in yVault caches the balance before calling token.transferFrom, allowing a reentrant token to manipulate share minting. An attacker could split deposits and reenter to mint excess shares, then withdraw for a profit and repeat until the vault is drained.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

yVault.sol#L144-L145

In deposit, the balance is cached and then a token.transferFrom is triggered which can lead to exploits if the token is a token that gives control to the sender, like ERC777 tokens.

Proof of Concept

Initial state: balance() = 1000, shares supply = 1000. Depositing 1000 amount should mint 1000 supply, but one can split the 1000 amounts into two 500 deposits and use re-entrancy to profit.

  • Outer deposit(500): balanceBefore = 1000. Control is given to attacker …
  • Inner deposit(500): balanceBefore = 1000. shares = (_amount * supply) / balanceBefore = 500 * 1000 / 1000 = 500 shares are minted …
  • Outer deposit(500) continues with the mint: shares = (_amount * supply) / balanceBefore = 500 * 1500 / 1000 = 750 are minted.
  • Withdrawing the 500 + 750 = 1250 shares via withdraw(1250), the attacker receives backingTokens = (balance() * _shares) / supply = 2000 * 1250 / 2250 = 1111.111111111. The attacker makes a profit of 1111 - 1000 = 111 tokens.
  • They repeat the attack until the vault is drained.

Recommended Mitigation Steps

The safeTransferFrom should be the last call in deposit.

spaghettieth (JPEG’d) confirmed

spaghettieth (JPEG’d) resolved and commented:

Fixed in jpegd/core#19.


The original report as published on Code4rena, by cmichel. 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 deposit function caches the balance before the external token transfer, so a reentrant call can use the stale balance to mint excess shares.

Impact

An attacker could repeatedly exploit the reentrancy to mint excess shares and withdraw more tokens than deposited, potentially draining the vault.

What the attacker needed

The vault accepts a reentrant token like ERC777; Attacker has tokens to deposit

The fix

The safeTransferFrom should be the last call in deposit, so the balance is updated before any external call.

How it was found

  1. Attacker calls deposit(500) on the vault, which caches balanceBefore = 1000 and then triggers token.transferFrom, giving control to the attacker.
  2. During the transfer, attacker reenters and calls deposit(500) again; this inner call sees balanceBefore = 1000 and mints 500 shares.
  3. The outer deposit continues and mints 750 shares using the stale balanceBefore = 1000 and updated supply = 1500.
  4. Attacker withdraws the total 1250 shares and receives 1111 tokens, making a profit of 111 tokens.
  5. Attacker repeats the attack until the vault is drained.

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 lets people deposit tokens and get shares representing their portion. The deposit function reads the vault's balance, then transfers tokens in, but if the token lets the sender run code during the transfer, they can call deposit again before the first one finishes. This lets them trick the share calculation into giving them more shares than they should, which they can withdraw for profit.

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 cmichel

View all

196 more disclosed reports, best first.

More from JPEG'd 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.