Vault global shares and assets change will mismatch local shares and assets change during settlement due to incorrect `_withoutSettlementFeeGlobal` formula
The vault's global settlement fee calculation subtracts the full settlement fee for both deposits and redeems, while local calculations subtract only a proportional share. This causes local shares and assets to exceed global ones, leading to underflows that prevent users from redeeming or claiming funds.
Source: https://github.com/sherlock-audit/2024-02-perennial-v2-3-judging/issues/26
Found by
panprog
Summary
Every vault update, which involves change of position in the underlying markets, settlementFee is charged by the Market. Since many users can deposit and redeem during the same oracle version, this settlementFee is shared equally between all users of the same oracle version. However, there is an issue in that settlementFee is charged once both for deposits and redeems, however _withoutSettlementFeeGlobal subtracts settlementFee in full both for deposits and redeems, meaning that for global fee, it's basically subtracted twice (once for deposits, and another time for redeems). But for local fee, it's subtracted proportional to checkpoint.orders, with sum of fee subtracted equal to exactly settlementFee (once). This difference in global and local settlementFee calculations leads to inflated shares and assets added for user deposits (local state) compared to vault overall (global state).
Vulnerability Detail
Here is an easy scenario to demonstrate the issue:
SettlementFee = $10- User1 deposits
$10for oracle versiont = 100 - User2 redeems
10 shares(worth$10) for the same oracle versiont = 100(checkpoint.orders = 2) - Once the oracle version
t = 100settles, we have the following:
4.1. Global deposits = $10, redeems = $10
4.2. Global deposits convert to 0 shares (because _withoutSettlementFeeGlobal(10) applies settlementFee of $10 in full, returning 10-10=0)
4.3. Global redeems convert to 0 assets (because _withoutSettlementFeeGlobal(10) applies settlementFee of $10 in full, returning 10-10=0)
4.4. User1 deposit of $10 converts to 5 shares (because _withoutSettlementFeeLocal(10) applies settlementFee of $5 (because there are 2 orders), returning 10-5=5)
4.5. User2 redeem of 10 shares converts to $5 (for the same reason)
From the example above it can be seen that:
- User1 receives 5 shares, but global vault shares didn't increase. Over time this difference will keep growing potentially leading to a situation where many user redeems will lead to 0 global shares, but many users will still have local shares which they will be unable to redeem due to underflow, thus losing funds.
- User2's assets which he can claim increase by $5, but global claimable assets didn't change, meaning User2 will be unable to claim assets due to underflow when trying to decrease global assets, leading to loss of funds for User2.
The underflow in both cases will happen in Vault._update when trying to update global account:
function update(
Account memory self,
uint256 currentId,
UFixed6 assets,
UFixed6 shares,
UFixed6 deposit,
UFixed6 redemption
) internal pure {
self.current = currentId;
// @audit global account will have less assets and shares than sum of local accounts
(self.assets, self.shares) = (self.assets.sub(assets), self.shares.sub(shares));
(self.deposit, self.redemption) = (self.deposit.add(deposit), self.redemption.add(redemption));
}
Impact
Any time there are both deposits and redeems in the same oracle version, the users receive more (local) shares and assets than overall vault shares and assets increase (global). This mismatch causes:
- Systematic increase of (sum of user shares - global shares), which can lead to bank run since the last users who try to redeem will be unable to do so due to underflow.
- Systematic increase of (sum of user assets - global assets), which will lead to users being unable to claim their redeemed assets due to underflow.
The total difference in local and global shares+assets equals to settlementFee per each oracle version with both deposits and redeems. This can add up to significant amounts (at settlementFee = $1 this can be $100-$1000 per day), meaning it will quickly become visible especially for point 2., because typically global claimable assets are at or near 0 most of the time, since users usually redeem and then immediately claim, thus any difference of global and local assets will quickly lead to users being unable to claim.
Code Snippet
SettlementFee subtracted in _withoutSettlementFeeGlobal
https://github.com/sherlock-audit/2024-02-perennial-v2-3/blob/main/perennial-v2/packages/perennial-vault/contracts/types/Checkpoint.sol#L183-L185
This is subtracted twice: for deposit and for redeem: https://github.com/sherlock-audit/2024-02-perennial-v2-3/blob/main/perennial-v2/packages/perennial-vault/contracts/types/Account.sol#L62-L63
Tool used
Manual Review
Recommendation
Calculate total orders to deposit and total orders to redeem (in addition to total orders overall). Then settlementFee should be multiplied by deposit/orders for toGlobalShares and by redeems/orders for toGlobalAssets. This weightening of settlementFee will make it in-line with local order weights.
Discussion
sherlock-admin4
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/305
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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 global settlement fee is subtracted in full for both deposits and redeems, instead of being weighted by the proportion of deposit and redeem orders.
Impact
Users may be unable to redeem shares or claim assets due to underflow, leading to loss of funds, and the mismatch can cause a bank run as the last users cannot withdraw.
What the attacker needed
Both deposits and redeems in same oracle version
The fix
The protocol team fixed this issue by weighting the settlement fee proportionally to deposit and redeem orders in the global calculation.
How it was found
- Set settlement fee to $10.
- User1 deposits $10 for oracle version t=100.
- User2 redeems 10 shares (worth $10) for the same oracle version t=100.
- After settlement, global deposits and redeems each convert to 0 shares/assets due to full fee subtraction.
- User1's deposit converts to 5 shares and User2's redeem converts to $5 due to proportional local fee.
- The mismatch between local and global shares/assets grows, causing underflows when users try to redeem or claim.
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 vault charges a fee when users deposit or withdraw. The fee is split among all users who act in the same time period. The bug is that the vault's overall accounting subtracts the full fee twice (once for deposits, once for withdrawals), while each user's share is calculated correctly. This makes the sum of user balances larger than the vault's total, so later withdrawals can fail because the vault runs out of recorded funds.
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.