Skip to content
Business logic Audit finding Advanced

forceUnsponsor() may open a window for attackers to manipulate the _totalShares and freeze users’ funds at a certain deposit amount

The forceUnsponsor function can transfer all underlying tokens to a sponsor even when other depositors exist, leaving the vault with a tiny balance. An attacker can then deposit a small amount to receive an extremely large number of shares, which can later freeze user funds when deposit or withdraw amounts reach a certain value.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/code-423n4/2022-01-sandclock/blob/a90ad3824955327597be00bb0bd183a9c228a4fb/sandclock/contracts/Vault.sol#L390-L401

if (_force && sponsorAmount > totalUnderlying()) {
    sponsorToTransfer = totalUnderlying();
} else if (!_force) {
    require(
        sponsorToTransfer <= totalUnderlying(),
        "Vault: not enough funds to unsponsor"
    );
}

totalSponsored -= sponsorAmount;

underlying.safeTransfer(_to, sponsorToTransfer);

When sponsorAmount > totalUnderlying(), the contract will transfer totalUnderlying() to sponsorToTransfer, even if there are other depositors and totalShares > 0.

After that, and before others despoiting into the Vault, the Attacker can send 1 wei underlying token, then cal deposit() with 0.1 * 1e18 , since newShares = (_amount * _totalShares) / _totalUnderlyingMinusSponsored and _totalUnderlyingMinusSponsored is 1, with a tiny amount of underlying token, newShares will become extremly large.

As we stated in issue #166, when the value of totalShares is manipulated precisely, the attacker can plant a bomb, and the contract will not work when the deposit/withdraw amount reaches a certain value, freezing the user’s funds.

However, this issue is not caused by lack of reentrancy protection, therefore it cant be solved by the same solution in issue #166.

Recommendation

Consider adding a minimum balance reserve (eg. 1e18 Wei) that cannot be withdrawn by anyone in any case. It can be transferred in alongside with the deployment by the deployer.

This should make it safe or at least make it extremely hard or expensive for the attacker to initiate such an attack.

naps62 (Sandclock) confirmed and commented:

@gabrielpoca @ryuheimat is this new?

ryuheimat (Sandclock) commented:

it’s new

gabrielpoca (Sandclock) commented:

yap, it’s interesting. The sponsor really is an issue

The original report as published on Code4rena, by WatchPug. 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

Impact

The attacker can manipulate totalShares to a precise value, causing the contract to fail when deposit or withdraw amounts reach a certain value, thereby freezing users' funds.

What the attacker needed

Attacker can call forceUnsponsor; vault has other depositors; attacker can send 1 wei and call deposit

The fix

The recommendation is to add a minimum balance reserve (e.g., 1e18 Wei) that cannot be withdrawn by anyone, transferred in by the deployer at deployment.

How it was found

  1. Call forceUnsponsor with a sponsorAmount greater than totalUnderlying, causing the contract to transfer all underlying tokens to the sponsor.
  2. Send 1 wei of the underlying token to the vault.
  3. Call deposit() with 0.1 * 1e18, which due to the tiny _totalUnderlyingMinusSponsored (1) results in an extremely large number of new 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 from many people and gives them shares in return. A function meant to let a sponsor leave can accidentally empty the vault, and then an attacker can exploit the near-zero balance to get a huge number of shares, which later breaks the vault for everyone.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Sandclock contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.