Skip to content
Smart contracts Write-up Advanced

[web3] Informational bug in GYSR found by MiloTruck

The staking contract calculates shares based on its token balance, which can be inflated by direct transfers, causing precision loss in unstake. An attacker can exploit this to withdraw more tokens than they should, stealing funds from other stakers.

github.com

Published on github.com

History

The author, on github.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely, the contract uses its token balance to calculate shares, which can be manipulated by direct transfers, and integer division rounds down, allowing an attacker to burn fewer shares than the token amount withdrawn.

Impact

An attacker could steal staked tokens from other users when total shares are low or zero, and the likelihood is increased because users can create new pools with zero shares.

What the attacker needed

Attacker can stake a minimal amount; Attacker can directly transfer tokens to contract; Other users stake later; Total shares are low or zero

The fix

The contract should use a storage variable to track staked tokens instead of relying on the token balance.

How it was found

  1. Attacker stakes a minimal amount (1 wei) when total shares are zero, receiving 1e6 shares.
  2. Attacker directly transfers a large amount of tokens to the contract, inflating the token balance.
  3. Other users stake a significant amount, receiving shares based on the inflated balance.
  4. Attacker repeatedly calls unstake with an amount that rounds down to burning only 1 share, but withdraws a large token amount.
  5. Attacker profits, causing loss of staked funds for other users.

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 write-up has not been written yet. The Original tab links to the source.

In a staking contract, users deposit tokens and receive shares representing their portion of the pool. The contract determines share value by dividing its token balance by total shares. An attacker can manipulate this by directly sending tokens to the contract, making each share worth more, then exploiting rounding errors when withdrawing to take more tokens than they are entitled to.

What is Smart contracts?

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

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.