Invalid oracle version can cause the `maker` position to exceed `makerLimit`, temporarily or permanently bricking the Market contract
When an oracle version is invalid, pending positions are invalidated and accumulated without applying position limit checks, which can push the global maker position above the maker limit. This causes all market updates to revert, temporarily or permanently bricking the contract and preventing users from withdrawing funds.
Source: https://github.com/sherlock-audit/2023-10-perennial-judging/issues/24
Found by
panprog
Summary
When invalid oracle version happens, positions pending at the oracle version are invalidated with the following pending positions increasing or decreasing in size. When this happens, all position limit checks are not applied (and can't be cancelled/modified), but they are still verified for the final positions in _invariant. This means that many checks are bypassed during such event. There is a protection against underflow due to this problem by enforcing the calculated closable value to be 0 or higher. However, exactly the same problem can happen with overflow and there is no protection against it.
Vulnerability Detail
For example:
- Latest global maker = maker limit = 1000
- Pending global maker = 500 [t=100]
- Pending global maker = 1000 [t=200]
If oracle version at t = 100 is invalid, then pending global maker = 1500 (at t = 200). However, due to this check in _invariant:
if (context.currentPosition.global.maker.gt(context.riskParameter.makerLimit))
revert MarketMakerOverLimitError();
all Market updates will revert except update to reduce maker position by 500+, which might not be even possible in 1 update depending on maker distribution between users. For example, if 5 users have maker = 300 (1500 total), then no single user can update to reduce maker by 500. This will temporarily brick Market (all updates will revert) until coordinator increases maker limit. If the limit is already close to max possible (2^62-1), then the contract will be bricked permanently (all updates will revert regardless of maker limit, because global maker will exceed 2^62-1 in calculations and will revert when trying to store it).
The same issue can also cause the other problems, such as:
- Bypassing the market utilization limit if long/short is increased above maker
- User unexpectedly becomes liquidatable with too high position (for example: position 500 -> pending 0 -> pending 500 - will make current = 1000 if middle oracle version is invalid)
Impact
If current maker is close to maker limit, and some user(s) reduce their maker then immediately increase back, and the oracle version is invalid, maker will be above the maker limit and the Market will be temporarily bricked until coordinator increases the maker limit. Even though it's temporary, it still bricked for some time and coordinator is forced to increase maker limit, breaking the intended market config. Furthermore, once the maker limit is increased, there is no guarantee that the users will reduce it so that the limit can be reduced back.
Also, for some low-price tokens, the maker limit can be close to max possible value (2^62-1 is about 4*1e18 or Fixed6(4*1e12)). If the token price is about $0.00001, this means such maker limit allows $4*1e7 or $40M. So, if low-value token with $40M maker limit is used, this issue will lead to maker overflow 2^62-1 and bricking the Market permanently, with all users being unable to withdraw their funds, losing everything.
While this situation is not very likely, it's well possible. For example, if the maker is close to limit, any maker reducing the position will have some other user immediately take up the freed up maker space, so things like global maker change of: 1000->900->1000 are easily possible and any invalid oracle version will likely cause the maker overflowing the limit.
Code Snippet
_processPositionGlobal invalidates the pending position if oracle version is invalid:
https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial/contracts/Market.sol#L486
This is done by changing the invalidation accumulated values of the position. When each position is loaded, it's also adjusted by applying the difference between accumulated invalidation of the latest and the loaded position:
https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial/contracts/Market.sol#L223-L229
Such invalidation change will update the current position ignoring any position limits. If global maker is increased above maker limit, then any Market.update call will revert at the following line:
https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial/contracts/Market.sol#L586-L587
Alternatively, if the new position size (maker, long or short) is above 2^62-1, the Market.update will permanently revert at the following lines when trying to store new position value:
https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial/contracts/types/Position.sol#L533-L535
Tool used
Manual Review
Recommendation
The same issue for underflow is already resolved by using closable and enforcing such pending positions that no invalid oracle can cause the position to be less than 0. This issue can be resolved in the same way, by introducing some opeanable value (calculated similar to closable, but in reverse - when position is increased, it's increased, when position is decreased, it doesn't change) and enforcing different limits, such that settled position + openable:
- can not exceed the max maker
- can not break utilization
- for local position - calculate maxMagnitude amount from
settled + local openableinstead of absolute pending position values for margined/maintained calculations.
Discussion
kbrizzle
Invariant bricking issue resolved via: https://github.com/equilibria-xyz/perennial-v2/pull/155.
Margin using the incorrect maximum pending position resolved by: https://github.com/equilibria-xyz/perennial-v2/pull/168.
We chose to not fix the incorrect maximum makerLimit issue due to the complexity involved in implementing the above pending open calculation on the global pending positions compared to its relatively low severity since the error on the limit is capped. We will make a note of this property for parameter tuning, especially for markets with expected invalid versions.
panprog
Mitigation Review:
Bricking - Fixed. Only orders increasing maker will now revert when maker is over the limit.
Exceeding maker limit - Still possible (acknowledged). It's possible to exceed maker limit by at most 2x and is unlikely to happen (needs invalid oracle version and specific pattern of maker changes).
MLON33
Fix: https://github.com/equilibria-xyz/perennial-v2/pull/168
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 invalidation of pending positions on an invalid oracle version bypasses position limit checks, allowing the global maker position to exceed the maker limit and causing subsequent updates to revert.
Impact
The Market could be temporarily bricked until the coordinator increases the maker limit, and if the limit is near the maximum value, the contract may be permanently bricked, preventing users from withdrawing funds.
What the attacker needed
Maker position close to limit; invalid oracle version; users reduce then increase maker
The fix
The invariant bricking issue was fixed by only reverting orders that increase maker when over the limit, but the incorrect maximum maker limit issue was acknowledged and not fixed.
How it was found
- Set global maker close to the maker limit.
- Have users reduce and then increase their maker positions, creating pending positions.
- An oracle version becomes invalid, causing pending positions to be invalidated and accumulated.
- The global maker position exceeds the maker limit.
- All Market.update calls revert due to the limit check, bricking the market.
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 tracks how much 'maker' position users have. If the price oracle fails for a moment, the contract recalculates positions without checking limits, so the total can exceed the allowed maximum. Once over the limit, the contract refuses all updates, so users cannot withdraw or change their positions.
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.