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.
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:
- calculates vault's current balance;
- computes the amounts to deposit or withdraw and withdraws excessive amounts from protocols;
- deposits amounts to protocol;
- 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:
- it withdraws excessive funds;
- it deposits funds;
- 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
Vault.rebalancedistributes funds to protocols after rebalancing:
executeDepositsdeposits funds to protocols:
pullFundswithdraws funds from protocols:
- TrueFi vaults revert when depositing and withdrawing in the same block:
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");
...
}
- Idle vaults revert when depositing and withdrawing in the same block:
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
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
- Call Vault.rebalance to distribute funds according to allocations.
- The function deposits funds to a TrueFi or Idle vault.
- Later in the same transaction, it tries to pull funds from the same vault to cover reserved funds.
- 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.