Open code423n4 opened 1 year ago
asselstine marked the issue as sponsor confirmed
Picodes marked the issue as satisfactory
Nice catch. Since we don't have any control over the YieldVault exchange rate, if it gets manipulated at any time, we should revert any deposits where the amount of withdrawable assets from the YieldVault is lower than the expected amount. Fixed in the following PR: https://github.com/GenerationSoftware/pt-v5-vault/pull/18/files#diff-97c974f5c3c03a0cfcbc52a5b8b9ae2196d535754ff2034e2903de1fec23508aR1011
Lines of code
https://github.com/GenerationSoftware/pt-v5-vault/blob/b1deb5d494c25f885c34c83f014c8a855c5e2749/src/Vault.sol#L1176
Vulnerability details
Impact
Since
_yieldVault
mostly calculatesshares
useround down
when depositing, there is often a1 wei
loss of precision, which can cause thevault
to go intoundercollateralized
mode by mistake.Proof of Concept
When a user deposits an asset, we update
_lastRecordedExchangeRate
, the calculation is done by this method_currentExchangeRate()
The code is as follows:
This method takes
_yieldVault.maxWithdraw(address(this))
as the maximum value to calculate the current exchange rate.If the exchange rate is lower than
_assetUnit
, then it goes intoundercollateralized
mode, where it can only be withdraw, not deposit.So if
_yieldVault
is losing money, it goes intoundercollateralized
.But there is one missing consideration here: As long as
_yieldVault
is not exclusive, there will be precision loss issues, after_yieldVault.deposit()
,maxWithdraw()
will lose precision, because most vaults will dorounds down
shares calculations For example: depositing1000000000
, but it can only withdraw999999999
.This leads to the problem that when the first deposit is made, it is likely to go into
undercollateralized
mode immediately due to the1 wei
loss.The following code demonstrates that when a non-exclusive
_yieldVault
,alice
is first deposited normally, it immediately entersundercollateralized
mode.add to Deposit.t.sol
This small loss of precision should not be treated as a loss, and we can avoid it by adding
1wei
when calculation exchange rate.Tools Used
Recommended Mitigation Steps
_yieldVault.maxWithdraw() + 1
Avoid loss of precision intoundercollateralized
Assessed type
Decimal