`Controller`'s core function of Rebalance will not rebalance when rebalance is needed in some cases, breaking core functionality
The rebalance function incorrectly skips rebalancing when a market's collateral is zero, even if the target collateral is non-zero. This can prevent users from opening positions in new markets, potentially causing financial loss.
Source: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/20
Found by
panprog
Summary
One of the main Controller's functions is rebalanceGroup, which rebalances collateral between several markets in a pre-set ratio. The issue is that the rebalance is not done if the market collateral is 0, even if the target collateral is not 0. This happens, because the RebalanceLib.checkMarket incorrectly returns canRebalance = false in such case:
if (marketCollateral.eq(Fixed6Lib.ZERO)) return (false, targetCollateral);
This leads to core functionality not working in certain circumstances, for example when user adds a new market without any collateral and the rebalance threshold is high enough so that the other markets do not trigger a rebalance. This might, in turn, lead to a loss of funds for the user since he won't be able to open the position in this new market, as his collateral will remain 0 after rebalance, and will lose funds due to missed opportunity or due to lack of hedge the market expected to provide to the user.
Root Cause
RebalanceLib.checkMarket returns canRebalance = false when market's current collateral is 0, regardless of what target is:
https://github.com/sherlock-audit/2024-08-perennial-v2-update-3/blob/main/perennial-v2/packages/perennial-account/contracts/libs/RebalanceLib.sol#L27
Internal pre-conditions
- User's
collateral = 0in a market which is added toControllerrebalance config. - Rebalance config's
thresholdis high enough not to trigger rebalance in the other markets
External pre-conditions None
Attack Path
Happens by itself when user calls Controller.rebalanceGroup - the collateral is not rebalanced and the new market remains without collateral.
Example:
- User had rebalance config for market1 (0.5) and market2 (0.5),
threshould = 0.2 (20%) - User had market1 collateral = 50, market2 collateral = 50
- User changes rebalance config to market1 (0.4), market2 (0.4), market3 (0.2).
Controller.rebalanceGroupreverts, becausecanRebalance = falseeven though the market3 should be rebalanced to have some funds.
Note, that if the market3 has even 1 wei of collateral in the example, rebalanceGroup will do the re-balancing, meaning that the collateral of 0 should also trigger re-balancing.
Impact
- Core
Controllerfunctionality is broken (doesn't rebalance when it should) - As a result user can lose funds as he won't be able to open positions in the new market where he expected to have collateral after rebalance. For example, if the market was intended to hedge some other user position, inability to open position will expose the user to a market price risk he didn't expect to take and will lose substantial funds due to this.
PoC Not needed
Mitigation
When market collateral is 0, return false only if targetCollateral == 0, otherwise return true:
if (marketCollateral.eq(Fixed6Lib.ZERO)) return (!targetCollateral.eq(Fixed6Lib.ZERO), targetCollateral);
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/447
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 check in RebalanceLib.checkMarket returns false for canRebalance whenever the market's current collateral is zero, without considering the target collateral.
Impact
Core Controller functionality is broken, and users may lose funds because they cannot open positions in new markets, potentially missing hedges and exposing them to unexpected market risk.
What the attacker needed
User's collateral is 0 in a market added to rebalance config; Rebalance threshold is high enough to not trigger rebalance…
The fix
The protocol team fixed this issue by changing the check to return true when target collateral is non-zero, even if current collateral is zero.
How it was found
- User has a rebalance config for market1 and market2 with equal weights and a threshold.
- User changes the config to include market3 with a non-zero weight.
- User calls Controller.rebalanceGroup.
- The function reverts because market3 has zero collateral, so canRebalance is false.
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 smart contract's rebalance function is supposed to move funds between markets to maintain a set ratio. A bug causes it to skip a market if that market currently has no funds, even when it should receive funds. This means users might not get the funds they need in a new market, missing opportunities or hedges.
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.