Skip to content
Business logic Audit finding Advanced

Liquidations are temporarily blocked if user's pending position close amount is greater than the latest position size

A validation check in the protocol's order handling prevents liquidation orders from being created when a user's pending close amount exceeds their latest position size. This can temporarily block liquidations, potentially leading to bad debt and losses for market users.

panprog Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-01-perennial-v2-4-update-judging/issues/33

Found by

panprog

Summary

A new feature in v2.4 is guaranteed position change via Intents: since the price is provided in Intent itself, it doesn't need corresponding epoch oracle price to be valid. This makes it possible to close positions even when position opening in still pending (if all orders are from Intents), as such pending negative (pending position closure) is allowed to be greater than latest position size for intent orders:

Solidity
  if (
      context.pendingLocal.invalidation != 0 &&                              // pending orders are partially invalidatable
      context.pendingLocal.neg().gt(context.latestPositionLocal.magnitude()) // total pending close is greater than latest position
  ) revert IMarket.MarketOverCloseError();

The issue is that liquidation order is always a "normal" order (invalidation == 1), thus this condition is always enforced for liquidations. However, if pending negative is already greater than latest position magnitude, it's impossible to create liquidation order which will satisfy this condition (order can only increase pending negative and can't influence latest position). In such situations liquidations will temporarily be impossible until some pending order becomes commited and the condition can be satisfied.

Root Cause

Incorrect validation for protected orders in case pendingLocal.neg > latestPosition.magnitude(): https://github.com/sherlock-audit/2025-01-perennial-v2-4-update/blob/main/perennial-v2/packages/core/contracts/libs/InvariantLib.sol#L118-L122

Notice, that there is a special handling for the case of crossing zero (when pending order closes long and opens short or vice versa): in such case liquidation order must be empty (since crossing zero is prohibited for non-empty non-intent orders). But there is no such handling for the case of large pending position closure.

Internal pre-conditions

  • User has more pending close than latest position at the last commited oracle price (for example, user fully closes long position, then opens long again, then closes again).

External pre-conditions

None.

Attack Path

  1. User has some position open (for example, long = 10)
  2. User uses Intent to fully close position
  3. Shortly after that (order to close still pending) user uses Intent to open position again (for example, long = 10).
  4. Then immediately user uses Intent again to partially close position of size = 1
  5. Shortly after that (all user orders are still pending) the price sharply drops and user becomes liquidatable.
  6. At this time, user's latest position = 10, pending positive (open) = 10, pending negative (close) = 11.
  7. All liquidations will revert, because regardless of liquidation order size, pending negative will remain greater than latest position (10).
  8. Once pending order to close is settled, liquidation is possible again, but might be too late as user might already accumulate bad debt.

Result: User can not be liquidated in time and is liquidated much later, when his position is already in bad debt, which is a funds loss for all market users or a risk of a bank run as the last to withdraw won't have enough funds in the market to pay out.

Impact

All liquidation attempts revert although the user should be liquidatable, thus liquidation happens later than it should, potentially creating bad debt and loss of funds for all market users.

Mitigation

Similar to crossing zero, include special check when liquidating - and if pending negative is greater than latest position, require liquidation order to be empty.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/567

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

The validation logic for protected orders incorrectly applies a condition that prevents liquidation orders when pending negative exceeds latest position magnitude, without special handling for that case.

What the attacker needed

User has pending close > latest position; price drop makes user liquidatable

The fix

The protocol team fixed this issue by adding a special check for liquidations when pending negative exceeds latest position, requiring the liquidation order to be empty.

How it was found

  1. User has an open position (e.g., long 10).
  2. User uses an Intent to fully close the position.
  3. Shortly after, user uses an Intent to open the position again (e.g., long 10).
  4. Then user uses an Intent to partially close the position by size 1.
  5. Price drops sharply and user becomes liquidatable while all orders are still pending.
  6. Liquidation attempts revert because pending negative (11) remains greater than latest position (10).

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.

In this trading protocol, users can place orders that are not yet finalised (pending). A safety check stops new orders when the total pending close amount is larger than the current position size. But liquidation orders are also subject to this check, so if a user has more pending closes than their position, liquidators cannot submit orders to close out the user's risky position. This delay can cause the protocol to lose money.

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 #4

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.