Strategy Migration May Leave Tokens in the Old Strategy Impacting Share Calculations
When migrating between strategies, if the old strategy cannot withdraw the full amount, tokens remain stuck there. This causes totalValue() to be undervalued, so new depositors receive too many shares, and later withdrawals can overpay them, diluting existing users.
Originally disclosed on Code4rena
Submitted by kirk-baird
If a strategy does not have sufficient funds to withdraw() for the full amount then it is possible that tokens will be left in this yield contract during migrate().
It is common for withdrawal from a strategy to withdraw less than a user’s balance. The reason is that these yield protocols may lend the deposited funds to borrowers, if there is less funds in the pool than the withdrawal amount the withdrawal may succeed but only transfer the funds available rather than the full withdrawal amount.
The impact of tokens remaining in the old strategy is that when we call StrategyController.totalValue() this will only account for the tokens deposited in the new strategy and not those stuck in the previous strategy. Therefore totalValue() is undervalued.
Thus, when a user calls Collateral.deposit() the share calculations _shares = (_amountToDeposit * totalSupply()) / (_valueBefore); will be over stated (note: uint256 _valueBefore = _strategyController.totalValue();). Hence, the user will receive more shares than they should.
The old tokens may be recovered by calling migrate() back to the old strategy. If this is done then totalValue() will now include the tokens previously stuck. The recent depositer may now withdraw and will be owed (_strategyController.totalValue() * _amount) / totalSupply(). Since totalValue() is now includes the previously stuck tokens _owed will be overstated and the user will receive more collateral than they should.
The remaining users who had deposited before migrate() will lose tokens proportional to their share of the totalSupply().
Proof of Concept
SingleStrategyController.sol#L51-L72
function migrate(IStrategy _newStrategy)
external
override
onlyOwner
nonReentrant
{
uint256 _oldStrategyBalance;
IStrategy _oldStrategy = _strategy;
_strategy = _newStrategy;
_baseToken.approve(address(_newStrategy), type(uint256).max);
if (address(_oldStrategy) != address(0)) {
_baseToken.approve(address(_oldStrategy), 0);
_oldStrategyBalance = _oldStrategy.totalValue();
_oldStrategy.withdraw(address(this), _oldStrategyBalance);
_newStrategy.deposit(_baseToken.balanceOf(address(this)));
}
emit StrategyMigrated(
address(_oldStrategy),
address(_newStrategy),
_oldStrategyBalance
);
}
Recommended Mitigation Steps
The recommendation is to ensure that require(_oldStrategy.totalValue() == 0) after calling _oldStrategy.withdraw(). This ensures that no funds are left in the strategy. Consider the code example below.
function migrate(IStrategy _newStrategy)
external
override
onlyOwner
nonReentrant
{
uint256 _oldStrategyBalance;
IStrategy _oldStrategy = _strategy;
_strategy = _newStrategy;
_baseToken.approve(address(_newStrategy), type(uint256).max);
if (address(_oldStrategy) != address(0)) {
_baseToken.approve(address(_oldStrategy), 0);
_oldStrategyBalance = _oldStrategy.totalValue();
_oldStrategy.withdraw(address(this), _oldStrategyBalance);
require(_oldStrategy.totalValue() == 0)
_newStrategy.deposit(_baseToken.balanceOf(address(this)));
}
emit StrategyMigrated(
address(_oldStrategy),
address(_newStrategy),
_oldStrategyBalance
);
}
ramenforbreakfast (prePO) confirmed and commented:
This is a valid claim, although it is an edge case. I will maintain the severity of this issue as is. Should consider removing a fixed migration procedure altogether as this issue demonstrates one of the many problems that can occur.
Agree with sponsor.
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 migrate function does not verify that the old strategy's totalValue is zero after withdrawal, allowing leftover tokens to be excluded from totalValue calculations.
Impact
A depositor could receive more shares than deserved and later withdraw more than owed, causing existing users to lose tokens proportionally; the sponsor noted this is an edge case.
What the attacker needed
Strategy has insufficient funds to cover full withdrawal; owner calls migrate()
How it was found
- Call migrate() to move funds from old strategy to new strategy.
- Old strategy withdraws less than its totalValue, leaving tokens stuck.
- totalValue() now only counts new strategy, undervaluing total assets.
- A user calls deposit(), receiving more shares than they should due to undervaluation.
- Call migrate() back to old strategy to recover stuck tokens.
- totalValue() now includes stuck tokens, so the recent depositor can withdraw more than owed, diluting others.
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 strategy is like a savings account that lends out money. When moving to a new strategy, the old one might not have all the money back yet, so some stays behind. The system then thinks there is less money than there really is, which messes up how many shares new depositors get.
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.