Skip to content
Business logic Audit finding Intermediate

MainVault.rebalanceXChain doesn't check that savedTotalUnderlying >= reservedFunds

The MainVault.rebalanceXChain function does not verify that the vault retains enough funds to cover reservedFunds after sending funds cross-chain. If the vault's total underlying decreases between allocation and rebalance, the vault may send all its funds, leaving nothing to cover reserved withdrawals.

rvierdiiev Follow

Originally disclosed on Sherlock

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

Found by

rvierdiiev

Summary

MainVault.rebalanceXChain doesn't check that savedTotalUnderlying >= reservedAmount. Because of that, shortage can occur, if vault will lose some underlying during cross chain calls and reservedFundswill not be present in the vault.

Vulnerability Detail

reservedFunds is the amount that is reserved to be withdrawn by users. It's increased by totalWithdrawalRequests amount every cycle, when setXChainAllocation is called.

setXChainAllocation call is initiated by xController. This call provides vault with information about funds. In case if vault should send funds to the xController, then SendingFundsXChain state is set, aslo amount to send is stored.

After that someone should call MainVault.rebalanceXChain in order to send that amount to the xController. https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/MainVault.sol#L307-L326

Solidity
  function rebalanceXChain(uint256 _slippage, uint256 _relayerFee) external payable {
    require(state == State.SendingFundsXChain, stateError);


    if (amountToSendXChain > getVaultBalance()) pullFunds(amountToSendXChain);
    if (amountToSendXChain > getVaultBalance()) amountToSendXChain = getVaultBalance();


    vaultCurrency.safeIncreaseAllowance(xProvider, amountToSendXChain);
    IXProvider(xProvider).xTransferToController{value: msg.value}(
      vaultNumber,
      amountToSendXChain,
      address(vaultCurrency),
      _slippage,
      _relayerFee
    );


    emit RebalanceXChain(vaultNumber, amountToSendXChain, address(vaultCurrency));


    amountToSendXChain = 0;
    settleReservedFunds();
  }

As you can see, function just pulls needed funds from providers if needed and sends them to xController. It doesn't check that after that amount that is held by vault is enough to cover reservedFunds. Because of that next situation can occur.

1.Suppose that vault has 1000 tokens as underlying amount. 2.reservedFunds is 200. 3.xController calculated that vault should send 800 tokens to xController(vault allocations is 0) and 200 should be still in the vault in order to cover reservedFunds. 4.when vault is going to send 800 tokens(between setXChainAllocation and rebalanceXChain call), then loss happens and totalUnderlying becomes 800, so currently vault has only 800 tokens in total. 5.vault sends this 800 tokens to xController and has 0 to cover reservedFunds, but actually he should leave this 200 tokens in the vault in this case.

Solidity
    if (amountToSendXChain > getVaultBalance()) pullFunds(amountToSendXChain);
    if (amountToSendXChain > getVaultBalance()) amountToSendXChain = getVaultBalance();

I think that this is incorrect approach for withdrawing of funds as there is a risk that smth will happen with underlying amount in the providers, so it will be not enough to cover reservedFunds and calculations will be broken, users will not be able to withdraw. Same approach is done in rebalance function, which pulls reservedFunds after depositing to all providers. I guess that correct approach is not to touch reservedFunds amount. In case if you need to send amount to xController, then you need to withdraw it directly from provider. Of course if you have getVaultBalance that is bigger than reservedFunds + amountToSendXChain, then you can send them directly, without pulling.

Impact

Reserved funds protection can be broken

Code Snippet

Provided above

Tool used

Manual Review

Recommendation

You need to check that after you send funds to xController it's enough funds to cover reservedFunds.

The original report as published on Sherlock, by rvierdiiev. 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 rebalanceXChain function lacks a check that the vault's balance after sending funds still covers reservedFunds, and it may pull funds from providers without preserving the reserved amount.

Impact

Reserved funds protection can be broken, potentially preventing users from withdrawing their reserved funds.

What the attacker needed

Vault has reservedFunds > 0; loss occurs between allocation and rebalance

The fix

The function should check that after sending funds to xController, the vault still has enough funds to cover reservedFunds, and should not touch the reserved amount.

How it was found

  1. Vault has 1000 tokens and reservedFunds is 200.
  2. xController calculates vault should send 800 tokens to xController, leaving 200 for reservedFunds.
  3. Between setXChainAllocation and rebalanceXChain, a loss occurs and totalUnderlying becomes 800.
  4. rebalanceXChain sends all 800 tokens to xController, leaving 0 to cover reservedFunds.

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 users' money and keeps some aside for withdrawals (reserved funds). When moving money to another chain, it should ensure enough stays behind. This report says the vault might send too much, leaving nothing for withdrawals if its total value drops unexpectedly.

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 rvierdiiev

View all

72 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.