Skip to content
Business logic Audit finding Advanced

BalancedVault.sol: loss of funds + global settlement flywheel / user settlement flywheels getting out of sync

A vault contract processes deposits and redemptions using two separate settlement mechanisms that can become out of sync when a user's pending deposit or redemption is pushed forward by an additional small deposit. This can cause a user to receive fewer shares than entitled, leading to a loss of funds and violating invariants about total supply and unclaimed assets.

0xGoodess Follow with cergyk and roguereddwarf

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-05-perennial-judging/issues/45

Found by

0xGoodess, cergyk, roguereddwarf

Summary

When an epoch has become "stale", the BalancedVault will treat any new deposits and redemptions in this epoch as "pending". This means they won't get processed by the global settlement flywheel in the next epoch but one epoch later than that.

Due to the fact that anyone can push a pending deposit or redemption of a user further ahead by making an arbitrarily small deposit in the "intermediate epoch" (i.e. the epoch between when the user creates the pending deposit / redemption and the epoch when it is scheduled to be processed by the global settlement flywheel), the user can experience a DOS.

Worse than that, by pushing the pending deposit / pending redemption further ahead, the global settlement flywheel and the user settlement flywheel get out of sync.

Also users can experience a loss of funds.

Vulnerability Detail

Let's assume we are currently in epoch 1 and it is stale. User1 calls the deposit function and we set _pendingEpochs[user1] = context.epoch + 1 = 2 https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L160-L164

We move one epoch ahead and are in epoch 2 now. By looking at the global settlement flywheel we see that _deposit = _pendingDeposit and _redemption = _pendingRedemption which means that the deposit we made will be processed in the global settlement flywheel in the next epoch (epoch 3). https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L378-L403

By looking into the user settlement flywheel we see that the if (accountContext.epoch > _pendingEpochs[account]) condition to process the pending deposit is not fulfilled yet (2 !> 2). It will be fulfilled in epoch 3. https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L413-L422

So far so good. The global settlement flywheel and the user settlement flywheel are in sync and will process the pending deposit in epoch 3.

Now here's the issue. A malicious user2 or user1 unknowingly (depending on the specific scenario) calls deposit for user1 again in the current epoch 2 once it has become stale (it's possible to deposit an arbitrarily small amount). By doing so we set _pendingEpochs[user1] = context.epoch + 1 = 3, thereby pushing the processing of the deposit in the user settlement flywheel one epoch ahead.

It's important to understand that the initial deposit will still be processed in epoch 3 in the global settlement flywheel, it's just being pushed ahead in the user settlement flywheel.

Thereby the global settlement flywheel and user settlement flywheel are out of sync now.

The total supply will be increased by an amount calculated based on the epoch 3 context: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L379

The user balance will be increased by an amount calculated based on the epoch 4 context: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L407

An example for a loss of funds that can occur as a result of this issue is when the PnL from epoch 3 to epoch 4 is positive. Thereby the user1 will get less shares than he is entitled to.

Similarly it is possible to push pending redemptions ahead, thereby the _totalUnclaimed amount would be increased by an amount that is different from the amount that _unclaimed[account] is increased by.

Coming back to the case with the pending deposit, I wrote a test that you can add to BalancedVaultMulti.test.ts:

JavaScript
it('pending deposit pushed by 1 epoch causing shares difference', async () => {
      const smallDeposit = utils.parseEther('1000')
      const smallestDeposit = utils.parseEther('0.000001')

      await updateOracleEth() // epoch now stale
      // make a pending deposit
      await vault.connect(user).deposit(smallDeposit, user.address)
      await updateOracleBtc()
      await vault.sync()

      await updateOracleEth() // epoch now stale
      /* 
      user2 deposits for user1, thereby pushing the pending deposit ahead and causing the 
      global settlement flywheel and user settlement flywheel to get out of sync
      */
      await vault.connect(user2).deposit(smallestDeposit, user.address)
      await updateOracleBtc()
      await vault.sync()

      await updateOracle()
      // pending deposit for user1 is now processed in the user settlement flywheel
      await vault.syncAccount(user.address)

      const totalSupply = await vault.totalSupply()
      const balanceUser1 = await vault.balanceOf(user.address)
      const balanceUser2 = await vault.balanceOf(user2.address)

      /*
      totalSupply is bigger than the amount of shares of both users together
      this is because user1 loses out on some shares that he is entitled to
      -> loss of funds
      */
      console.log(totalSupply);
      console.log(balanceUser1.add(balanceUser2));

})

The impact that is generated by having one pending deposit that is off by one epoch is small. However over time this would evolve into a chaotic situation, where the state of the Vault is significantly corrupted.

Impact

The biggest impact comes from the global settlement flywheel and user settlement flywheel getting out of sync. As shown above, this can lead to a direct loss of funds for the user (e.g. the amount of shares he gets for a deposit are calculated with the wrong context).

Apart from the direct impact for a single user, there is a subtler impact which can be more severe in the long term. Important invariants are violated:

  • Sum of user balances is equal to the total supply
  • Sum of unclaimed user assets is equal to total unclaimed assets

Thereby the impact is not limited to a single user but affects the calculations for all users.

Less important but still noteworthy is that users that deposit into the Vault are partially exposed to PnL in the underlying products. The Vault does not employ a fully delta-neutral strategy. Therefore by experiencing a larger delay until the pending deposit / redemption is processed, users incur the risk of negative PnL.

Code Snippet

https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L156-L175

https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L184-L205

https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial-vaults/contracts/balanced/BalancedVault.sol#L375-L424

Tool used

Manual Review

Recommendation

My recommendation is to implement a queue for pending deposits / pending redemptions of a user. Pending deposits / redemptions can then be processed independently (without new pending deposits / redemptions affecting when existing ones are processed).

Possibly there is a simpler solution which might involve restricting the ability to make deposits to the user himself and only allowing one pending deposit / redemption to exist at a time.

The solution to implement depends on how flexible the sponsor wants the deposit / redemption functionality to be.

Discussion

KenzoAgada

Also note duplicate issue #74 which mentions how a user can redeem more assets than he's entitled to.

The original report as published on Sherlock, by 0xGoodess. 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 vault updates a user's pending epoch on every deposit, so an additional deposit in an intermediate epoch pushes the processing of earlier pending deposits forward in the user settlement flywheel while the global settlement flywheel still processes them at the original time.

Impact

Users can lose funds due to receiving fewer shares than entitled, and the vault's invariants (sum of user balances equals total supply, sum of unclaimed assets equals total unclaimed) are violated, affecting all users over time.

What the attacker needed

Vault in a stale epoch; attacker can make a deposit for another user

The fix

The recommendation is to implement a queue for pending deposits and redemptions so they can be processed independently, or restrict deposits to the user themselves and allow only one pending deposit or redemption at a time.

How it was found

  1. In a stale epoch, user1 makes a deposit, setting their pending epoch to the next epoch.
  2. The vault moves to the next epoch, and the global settlement flywheel schedules the deposit for processing in the following epoch.
  3. In that next epoch, which is also stale, user2 makes a tiny deposit for user1, updating user1's pending epoch to one epoch later.
  4. The global settlement flywheel processes the original deposit in the scheduled epoch, but the user settlement flywheel processes it one epoch later.
  5. The user receives shares calculated with a later epoch's context, potentially getting fewer shares than entitled.

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.

This is about a smart contract vault where users deposit funds and later redeem shares. The vault uses two separate accounting systems (called 'flywheels') to track when deposits and redemptions are processed. If someone makes a tiny extra deposit for another user, it can delay that user's original deposit in one system but not the other, causing them to get fewer shares than they should.

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 0xGoodess

View all

13 more disclosed reports, best first.

More from Perennial V2 Update #1

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.