Skip to content
Business logic Audit finding Intermediate

Rebalancing can be indefinitely blocked due to ever-increasing `totalWithdrawalRequests`, causing locking of funds in vaults

The XChainController contract never resets its totalWithdrawalRequests counter between rebalancing periods, so the value accumulates over time. Once it exceeds a vault's total underlying balance, the subtraction in pushVaultAmounts underflows and reverts, blocking rebalancing indefinitely and locking funds because withdrawals only execute after a successful rebalance.

Jeiwan Follow with SPYBOY and evan

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/311

Found by

Jeiwan, SPYBOY, evan

Summary

Rebalancing can get stuck indefinitely at the pushVaultAmounts step due to an error in the accounting of totalWithdrawalRequests. As a result, funds will be locked in vaults since requested withdrawals are only executed after a next successful rebalance.

Vulnerability Detail

Funds deposited to underlying protocols can only be withdrawn from vaults after a next successful rebalance:

  1. a depositor has to make a withdrawal request first, which is tracked in the current rebalance period;
  2. requested funds can be withdrawn in the next rebalance period.

Thus, it's critical that rebalancing doesn't get stuck during one of its stages.

During rebalancing, vaults report their balances to XChainController via the pushTotalUnderlyingToController function: the functions sends the current unlocked (i.e. excluding reserved funds) underlying token balance of the vault and the total amount of withdrawn requests in the current period. The latter amount is stored in the totalWithdrawalRequests storage variable:

  1. the variable is increased when a new withdrawal request is made;
  2. and it's set to 0 after the vault has been rebalanced–it's value is added to the reserved funds.

The logic of totalWithdrawalRequests is that it tracks only the requested withdrawal amounts in the current period–this amount becomes reserved during rebalancing and is added to reservedFunds after the vault has been rebalanced.

When XChainController receives underlying balances and withdrawal requests from vaults, it tracks them internally. The amounts then used to calculate how much tokens a vault needs to send or receive after a rebalancing: the total withdrawal amount is subtracted from vault's underlying balance so that it's excluded from the amounts that will be sent to the protocols and so that it could then be added to the reserved funds of the vault.

However, totalWithdrawalRequests in XChainController is not reset between rebalancings: when a new rebalancing starts, XChainController receives allocations from the Game and calls resetVaultUnderlying, which resets the underlying balances receive from vaults in the previous rebalancing. resetVaultUnderlying doesn't set totalWithdrawalRequests to 0:

Solidity
function resetVaultUnderlying(uint256 _vaultNumber) internal {
  vaults[_vaultNumber].totalUnderlying = 0;
  vaultStage[_vaultNumber].underlyingReceived = 0;
  vaults[_vaultNumber].totalSupply = 0;
}

This cause the value of totalWithdrawalRequests to accumulate over time. At some point, the total historical amount of all withdrawal requests (which totalWithdrawalRequests actually tracks) will be greater than the underlying balance of a vault, and this line will revert due to an underflow in the subtraction:

Solidity
uint256 totalUnderlying = getTotalUnderlyingVault(_vaultNumber) - totalWithdrawalRequests;

Impact

Due to accumulation of withdrawal request amounts in the totalWithdrawalRequests variable, XChainController.pushVaultAmounts can be blocked indefinitely after the value of totalWithdrawalRequests has grown bigger than the value of totalUnderlying of a vault. Since withdrawals from vaults are delayed and enable in a next rebalancing period, depositors may not be able to withdraw their funds from vaults, due to a block rebalancing.

While XChainController implements a bunch of functions restricted to the guardian that allow the guardian to push a rebalancing through, neither of these functions resets the value of totalWithdrawalRequests. If totalWithdrawalRequests becomes bigger than totalUnderlying, the guardian won't be able to fix the state of XChainController and push the rebalancing through.

Code Snippet

  1. When a rebalancing starts, previously reported underlying balances are reset:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/XChainController.sol#L212

  1. However, totalWithdrawalRequests is never reset:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/XChainController.sol#L180-L182

  1. When XChainController calculates new amounts of underlying tokens, it subtracts totalWithdrawalRequests from totalUnderlying to reserve requested amounts in the current period:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/XChainController.sol#L303

  1. totalWithdrawalRequests is always increased when a vault reports its balances:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/XChainController.sol#L287

Tool used

Manual Review

Recommendation

In XChainController.resetVaultUnderlying, consider setting vaults[_vaultNumber].totalWithdrawalRequests to 0. totalWithdrawalRequests, like its MainVault.totalWithdrawalRequests counterpart, tracks withdrawal requests only in the current period and should be reset to 0 between rebalancings.

Discussion

sjoerdsommen

totalWithdrawalRequests in the xChainController is never reset.

sjoerdsommen

duplicate with #239

Theezr

Fix: https://github.com/derbyfinance/derby-yield-optimiser/pull/200

The original report as published on Sherlock, by Jeiwan. 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 resetVaultUnderlying function resets other per-period accounting fields but omits totalWithdrawalRequests, so the counter accumulates across rebalancing periods instead of tracking only the current period.

Impact

Rebalancing can be blocked indefinitely once the accumulated totalWithdrawalRequests exceeds a vault's totalUnderlying, and since withdrawals only execute after a successful rebalance, depositors may be unable to withdraw their funds; the guardian functions also cannot reset the value to push the rebalance through.

What the attacker needed

A depositor makes withdrawal requests; enough rebalancing periods pass for the counter to exceed…

The fix

The recommendation is to set vaults[_vaultNumber].totalWithdrawalRequests to 0 inside XChainController.resetVaultUnderlying so it tracks only the current period; a fix pull request is linked in the discussion.

How it was found

  1. A depositor makes a withdrawal request, which increases the vault's totalWithdrawalRequests.
  2. During rebalancing the vault reports its balance and withdrawal requests to XChainController, which adds the amount to its own totalWithdrawalRequests.
  3. When a new rebalancing starts, resetVaultUnderlying resets underlying balances but leaves totalWithdrawalRequests unchanged.
  4. Over successive rebalancings the accumulated totalWithdrawalRequests grows until it exceeds the vault's totalUnderlying.
  5. pushVaultAmounts then subtracts totalWithdrawalRequests from totalUnderlying, underflows and reverts, blocking rebalancing.

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 money and later ask to take it out. Withdrawals are only paid after a periodic 'rebalance' step, and that step subtracts the total amount people asked to withdraw from the vault's balance. The contract keeps adding to that withdrawal total but never clears it, so eventually the total is larger than the balance, the subtraction fails, and the rebalance can never finish, leaving people unable to get their money out.

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 Jeiwan

View all

69 more disclosed reports, best first.

More from Derby

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.