Skip to content
Business logic Audit finding Intermediate

Rebalancing can be blocked when pulling funds from a TrueFi or a Idle vault

The Vault.rebalance function deposits funds into TrueFi or Idle vaults and then attempts to withdraw funds from the same vault in the same transaction. These protocols forbid depositing and withdrawing in the same block, so the withdrawal reverts and blocks rebalancing until the vault has enough tokens to cover reserved funds.

Jeiwan Follow with c7e7eff

Originally disclosed on Sherlock

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

Found by

Jeiwan, c7e7eff

Summary

An attempt to pull funds from a TrueFi or a Idle vault may revert and cause reverting of the Vault.rebalance function. To unlock the function, the team will need to top up the balance of the vault so that there's enough tokens to cover the reserved funds.

Vulnerability Detail

At the end of a rebalancing, the Vault.rebalance function is called to deposit and/or withdraw funds from protocol according to the allocations set by gamers. The function:

  1. calculates vault's current balance;
  2. computes the amounts to deposit or withdraw and withdraws excessive amounts from protocols;
  3. deposits amounts to protocol;
  4. ensures that there's enough tokens to cover the reserved funds and pulls funds from protocols when it's not enough.

Notice the order in which the function interacts with the protocols:

  1. it withdraws excessive funds;
  2. it deposits funds;
  3. it withdraws funds when the vault doesn't have enough funds to cover the reserved funds.

However, TrueFi and Idle vaults don't allow depositing and withdrawing (in this order) in the same block. Thus, if Vault.rebalance deposits funds to a TrueFi or Idle vault and then withdraws funds to cover reserved funds, there will always be a revert and rebalancing of the vault will be blocked.

Impact

A rebalancing of a vault can be blocked indefinitely until the vault has enough funds to cover the reserved funds after funds were distributed to protocols.

Code Snippet

  1. Vault.rebalance distributes funds to protocols after rebalancing:

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

  1. executeDeposits deposits funds to protocols:

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

  1. pullFunds withdraws funds from protocols:

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

  1. TrueFi vaults revert when depositing and withdrawing in the same block:

https://github.com/trusttoken/contracts-pre22/blob/76854d53c5036777286d4392495ef28cd5c5173a/contracts/truefi2/TrueFiPool2.sol#L488

Solidity
function join(uint256 amount) external override joiningNotPaused {
    ...
    latestJoinBlock[tx.origin] = block.number;
    ...
}

function liquidExit(uint256 amount) external sync {
    require(block.number != latestJoinBlock[tx.origin], "TrueFiPool: Cannot join and exit in same block");
    ...
}
  1. Idle vaults revert when depositing and withdrawing in the same block:

https://github.com/Idle-Labs/idle-contracts/blob/develop/contracts/mocks/IdleTokenV3_1NoConst.sol#L630

Solidity
function mintIdleToken(uint256 _amount, bool, address _referral)
  external nonReentrant whenNotPaused
  returns (uint256 mintedTokens) {
  _minterBlock = keccak256(abi.encodePacked(tx.origin, block.number));
  ...
  }

function _redeemIdleToken(uint256 _amount, bool[] memory _skipGovTokenRedeem)
  internal nonReentrant
  returns (uint256 redeemedTokens) {
    _checkMintRedeemSameTx();
    ...
  }

function _checkMintRedeemSameTx() internal view {
  require(keccak256(abi.encodePacked(tx.origin, block.number)) != _minterBlock, "REE");
}

Tool used

Manual Review

Recommendation

Consider improving the logic of the Vault.rebalance function so that funds are never withdrawn from protocols after they have been deposited to protocols.

Discussion

Theezr

Fix: Removed pulling funds after the rebalance

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

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

The Vault.rebalance function deposits funds to protocols before pulling funds to cover reserved amounts, but TrueFi and Idle vaults revert if a deposit and withdrawal occur in the same block.

Impact

Rebalancing of a vault can be blocked indefinitely until the vault has enough funds to cover reserved funds after distribution to protocols.

What the attacker needed

Vault uses TrueFi or Idle protocol; rebalance deposits then needs to pull funds

The fix

The logic of Vault.rebalance was changed so that funds are never withdrawn from protocols after they have been deposited to protocols.

How it was found

  1. Call Vault.rebalance to distribute funds according to allocations.
  2. The function deposits funds to a TrueFi or Idle vault.
  3. Later in the same transaction, it tries to pull funds from the same vault to cover reserved funds.
  4. The protocol reverts because deposit and withdrawal happen in the same block.

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 smart contract tries to move money into and out of savings accounts (vaults) in one go. Some vaults have a rule that you cannot put money in and take it out in the same block (a short time window). Because the contract does both in one transaction, the second action fails and the whole process stops.

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.