Skip to content
Business logic Audit finding Intermediate

`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.

panprog Follow

Originally disclosed on Sherlock

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:

Solidity
    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

  1. User's collateral = 0 in a market which is added to Controller rebalance config.
  2. Rebalance config's threshold is 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:

  1. User had rebalance config for market1 (0.5) and market2 (0.5), threshould = 0.2 (20%)
  2. User had market1 collateral = 50, market2 collateral = 50
  3. User changes rebalance config to market1 (0.4), market2 (0.4), market3 (0.2).
  4. Controller.rebalanceGroup reverts, because canRebalance = false even 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

  1. Core Controller functionality is broken (doesn't rebalance when it should)
  2. 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:

Solidity
    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

The original report as published on Sherlock, by panprog. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. User has a rebalance config for market1 and market2 with equal weights and a threshold.
  2. User changes the config to include market3 with a non-zero weight.
  3. User calls Controller.rebalanceGroup.
  4. 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.

More by panprog

View all

66 more disclosed reports, best first.

More from Perennial V2 Update #3

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.