getSharesForAmount returns wrong value when totalAssets == 0
The getSharesForAmount function returns zero when totalAssets is zero, but the deposit function mints shares equal to the deposit amount when totalSupply is zero. Contracts or frontends using this function to estimate deposits will get an incorrect value.
Submitted by cmichel
The getSharesForAmount function returns 0 if totalAssets == 0.
However, if totalSupply == 0, the actual shares that are minted in a deposit are _amount even if totalAssets == 0.
Contracts / frontends that use this function to estimate their deposit when totalSupply == 0 will return a wrong value.
Recommended Mitigation Steps
function getSharesForAmount(uint256 _amount)
external
view
override
returns (uint256)
{
+ // to match the code in `deposit`
+ if (totalSupply() == 0) return _amount;
uint256 _totalAssets = totalAssets();
return
(_totalAssets > 0)
? ((_amount * totalSupply()) / _totalAssets)
: 0; // @audit this should be _amount according to `deposit`
}
ramenforbreakfast (prePO) confirmed and commented:
Valid claim, I believe this is a bug in our code, still need to verify.
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
What the attacker needed
TotalSupply == 0; totalAssets == 0
The fix
The function should be modified to return the deposit amount when totalSupply is zero, matching the deposit logic.
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 a helper function that estimates how many shares you get for a deposit gives the wrong answer in a special case. If the total supply of shares is zero, the actual deposit gives you shares equal to your deposit amount, but the estimate says zero. Anyone building a tool that relies on this estimate will show incorrect numbers.
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.