Skip to content
Smart contracts Audit finding Intermediate

Frontrun `deposit()` can cause the depositor to lose all the funds

The vault contract's deposit and token transfer functions calculate new shares based on the existing balance, so if an attacker frontruns the first depositor by sending a small amount of LINK tokens directly to the vault, the depositor's shares round down to zero. The depositor and all future depositors lose their funds.

ctf_sec Follow with Lambda, CRYP70 and 8 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-mycelium-judging/tree/main/029-M

Found by

ctf_sec, Lambda, CRYP70, 8olidity, bin2chen, hansfriese, innertia, dipp, rbserver, CodingNameKiki, WATCHPUG

Summary

The attacker can frontrun the first depositor's deposit() transaction and transfer LINK tokens to the Vault contract directly and cause the depositor (and all the future depositors) to lose all the funds.

Vulnerability Detail

In onTokenTransfer(), if there are some existing balance in the Vault contract, say 1 wei of LINK token, supplyBeforeTransfer will be 1, since totalShares == 0, newShares will always be 0.

The same applies for deposit().

Impact

The depositor who got frontrun by the attacker will lose all their funds. And all the future depositors.

Code Snippet

https://github.com/sherlock-audit/2022-10-mycelium/blob/main/mylink-contracts/src/Vault.sol#L252-L276

https://github.com/sherlock-audit/2022-10-mycelium/blob/main/mylink-contracts/src/Vault.sol#L131-L142

https://github.com/sherlock-audit/2022-10-mycelium/blob/main/mylink-contracts/src/Vault.sol#L614-L620

Tool used

Manual Review

Recommendation

It should check if totalShares == 0 to decide whether this is the first mint.

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

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

Likely the share calculation uses the vault's token balance instead of checking whether any shares exist, so a non-zero balance with zero total shares causes new shares to round down to zero.

Impact

The depositor who is frontrun loses all their funds, and all future depositors also lose their funds.

What the attacker needed

Attacker can frontrun the first deposit; vault has no shares yet

The fix

The contract should check whether total shares are zero to determine if this is the first mint.

How it was found

  1. Attacker sends a small amount of LINK tokens directly to the vault contract before the first depositor's deposit transaction.
  2. The first depositor's deposit transaction executes, but because the vault already has a balance and total shares are zero, the calculated new shares are zero.
  3. The depositor receives no shares and loses the deposited funds.

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.

When you put money into a shared vault, you get shares representing your portion. If someone else puts in a tiny amount first, the maths can round your shares down to zero, so you lose your money. This report shows that an attacker can do that by sending a small amount before your deposit.

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 ctf_sec

View all

25 more disclosed reports, best first.

More from Mycelium

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.