Closed sherlock-admin closed 1 year ago
5 comment(s) were left on this issue during the judging contest.
141345 commented:
l
n33k commented:
invalid
0xyPhilic commented:
invalid because if totalShares is 0 this means also totalAssets is 0
darkart commented:
Invalid because its breaking Sherlock rules on annonymous judging !!!
panprog commented:
invalid because this is a view function not used anywhere which is invalid according to sherlock rules
YakuzaKiawe
medium
Incorrect calculation in
convertToAssets
YakuzaKiawe
Medium
Incorrect calculation in
convertToAssets
Summary
The calculaton for
convertToAssets
can get a wrong valueVulnerability Detail
The
convertToAssets
function in theperennial-v2\packages\perennial-vault\contracts\Vault.sol
contract has an incorrect calculation. This can lead to users having wrong shares in their account. TheconvertToAssets
function converts a given amount of shares to assets. The calculation is as follows:The problem is with the
_totalShares.isZero()
check. This check should be_totalAssets.isZero()
.If
_totalShares
is zero, then the user has no shares. In this case, the correct calculation is to return the number of shares that were passed in. However, the current calculation returns the total assets in the contract, which is wrong.If
_totalAssets
is zero, then the contract has no assets. In this case, the correct calculation is to return zero. However, the current calculation returns the number of shares that were passed in, which is also wrong.The correct calculation should be as follows:
This ensures that the correct amount of assets is returned, regardless of whether the total shares or total assets in the contract are zero.
Proof of Concept
Here is an example of how the incorrect calculation can lead to users having wrong shares in their account:
convertToAssets
function with 25 shares.This means that the user would have 25 shares in their account, but they would only have 12.5 assets.
Impact
The incorrect calculation in
convertToAssets
could lead to users having wrong shares in their account. This could result in users losing money or being unable to withdraw their assets.Code Snippet
https://github.com/sherlock-audit/2023-07-perennial/blob/main/perennial-v2/packages/perennial-vault/contracts/Vault.sol#L119-L126
Tool used
Manual Review
Recommendation
Correct the calculation by doing this:
This will ensure that the correct amount of assets is returned, regardless of whether the total shares or total assets in the contract are zero.