Wrong formula of getSharesForAmount() can potentially cause fund loss when being used to calculate the shares to be used in withdraw()
The getter functions getAmountForShares() and getSharesForAmount() use totalAssets(), which includes the contract's base token balance, instead of the strategy controller's total value. An attacker using these functions to calculate shares for a withdrawal could receive less base token than expected, causing fund loss.
Submitted by WatchPug
In Collateral, the getter functions getAmountForShares() and getSharesForAmount() is using totalAssets() instead of _strategyController.totalValue(), making the results can be different than the actual shares amount needed to withdraw() a certain amount of _baseToken and the amount of shares expected to get by deposit() a certain amount.
Specifically, totalAssets() includes the extra amount of _baseToken.balanceOf(Collateral).
function getAmountForShares(uint256 _shares)
external
view
override
returns (uint256)
{
if (totalSupply() == 0) {
return _shares;
}
return (_shares * totalAssets()) / totalSupply();
}
function getSharesForAmount(uint256 _amount)
external
view
override
returns (uint256)
{
uint256 _totalAssets = totalAssets();
return
(_totalAssets > 0)
? ((_amount * totalSupply()) / _totalAssets)
: 0;
}
function totalAssets() public view override returns (uint256) {
return
_baseToken.balanceOf(address(this)) +
_strategyController.totalValue();
}
function withdraw(uint256 _amount)
external
override
nonReentrant
returns (uint256)
{
require(_withdrawalsAllowed, "Withdrawals not allowed");
if (_delayedWithdrawalExpiry != 0) {
_processDelayedWithdrawal(msg.sender, _amount);
}
uint256 _owed = (_strategyController.totalValue() * _amount) /
totalSupply();
...
Proof of Concept
Given:
_baseToken.balanceOf(Collateral)== 90_strategyController.totalValue()== 110- totalSupply of shares = 100
totalAssets() returns: 200
getSharesForAmount(100) returns: 50, while withdraw(50) will actual only get: 55.
When Collateral is used by another contract that manages many users’ funds, and if it’s using getSharesForAmount() to calculate the amount of shares needed for a certain amount of underlying tokens to be withdrawn.
This issue can potentially cause fund loss to the user of that contract because it will actually send a lesser amount of _baseToken than expected.
Recommended Mitigation Steps
Consider changing Collateral.totalValue() to:
function totalAssets() public view override returns (uint256) {
return
_strategyController.totalValue();
}
ramenforbreakfast (prePO) confirmed and commented:
Well organized and thorough submission. This is a valid claim.
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 getter functions use totalAssets() (which adds the contract's base token balance) while withdraw() uses only _strategyController.totalValue(), creating a mismatch in share-to-asset conversion.
Impact
A contract using getSharesForAmount() to calculate shares for a withdrawal could cause users to receive less base token than expected, potentially leading to fund loss.
What the attacker needed
Another contract uses getSharesForAmount(); that contract manages many users' funds
The fix
The getter functions should use _strategyController.totalValue() instead of totalAssets(), or totalAssets() should be changed to return only the strategy controller's total value.
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.
This is a smart contract bug where two helper functions calculate how many shares correspond to a given amount of tokens. They use a total that includes tokens sitting idle in the contract, but the actual withdrawal function only counts tokens managed by the strategy. So if another contract relies on these helpers, it may ask for too few shares and the user gets less money than they should.
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.