Skip to content
Business logic Audit finding Intermediate

Vault max redeem calculations limit redeem amount to the smallest position size in underlying markets which can lead to very small max redeem amount even with huge TVL vault

The vault's maximum redeem amount was limited by the smallest open position across underlying markets, so a tiny position in one market could cap redemptions at a very small amount even for a large vault. An attacker could not directly exploit this, but users with large deposits would need many transactions to redeem, losing funds to gas or being unable to redeem at all.

panprog Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-10-perennial-judging/issues/29

Found by

panprog

Summary

When redeeming from the vault, maximum amount allowed to be redeemed is limited by current opened position in each underlying market (the smallest opened position adjusted for weight). However, if any one market has its maker close to maker limit, the vault will open very small position, limited by maker limit. But now all redeems will be limited by this very small position for no reason: when almost any amount is redeemed, the vault will attempt to increase (not decrease) position in such market, so there is no sense in limiting redeem amount to the smallest position.

This issue can create huge problems for users with large deposits. For example, if the user has deposited $10M to the vault, but due to one of the underlying markets the max redeem amount is only $1, user will need to do 10M transactions to redeem his full amount (which will not make sense due to gas).

Vulnerability Detail

Vault's maxRedeem is calculated for each market as:

Solidity
UFixed6 collateral = marketContext.currentPosition.maker
    .sub(marketContext.currentPosition.net().min(marketContext.currentPosition.maker))  // available maker
    .min(marketContext.closable.mul(StrategyLib.LEVERAGE_BUFFER))                       // available closable
    .muldiv(marketContext.latestPrice.abs(), registration.leverage)                     // available collateral
    .muldiv(totalWeight, registration.weight);                                          // collateral in market

redemptionAssets = redemptionAssets.min(collateral);

closable is limited by the vault's settled and current positions in the market. As can be seen from the calculation, redeem amount is limited by vault's position in the market. However, if the position is far from target due to different market limitations, this doesn't make much sense. For example, if vault has $2M deposts and there are 2 underlying markets, each with weight 1, and:

  1. In Market1 vault position is worth $1 (target position = $1M)
  2. In Market2 vault position is worth $1M (target position = $1M)

The maxRedeem will be limited to $1, even though redeeming any amount up to $999999 will only make the vault attempt to increase position in Market1 rather than decrease.

There is also an opposite situation possible, when current position is higher than target position (due to LEVERAGE_BUFFER). This will make maxredeem too high. For example, similar example to previous, but:

  1. In Market1 vault position is worth $1.2M (target position = $1M)
  2. In Market2 vault position is worth $1.2M (target position = $1M)

The maxRedeem will be limited to $1.44M (due to LEVERAGE_BUFFER), without even comparing the current collateral (which is just $1M per market), based only on position size.

Impact

When vault's position is small in any underlying market due to maker limit, the max redeem amount in the vault will be very small, which will force users with large deposits to use a lot of transactions to redeem it (they'll lose funds to gas) or it might even be next to impossible to do at all (if, for example, user has a deposit of $10M and max redeem = $1), in such case the redeems are basically broken and not possible to do.

Code Snippet

maxRedeem calculation: https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial-vault/contracts/lib/StrategyLib.sol#L94-L98

Tool used

Manual Review

Recommendation

Consider calculating max redeem by comparing target position vs current position and then target collateral vs current collateral instead of using only current position for calculations. This might be somewhat complex, because it will require to re-calculate allocation amounts to compare target vs current position. Possibly max redeem should not be limited as a separate check, but rather as part of the allocate() calculations (reverting if the actual leverage is too high in the end)

Discussion

panprog

Mitigation Review:

Not fully fixed. There is still possibility to put the vault at max leverage. This happens when leverage is smaller than target leverage (for example, maker has accumulated some profit). The scenario is as following:

  1. Vault leverage in market = 2
  2. Deposit 40 (assets = 40, position opened = 80)
  3. After some time maker has accumulated a profit of 10, so vault has assets = 50, position opened = 80
  4. Say, market has net position opened = 20, meaning min position = 80 - (80 - 20) = 20
  5. MaxRedeem returns MAX, meaning user is allowed to withdraw all assets, but position will be reduced to 20 instead of 0, meaning vault will be at incorrect leverage.

This happens because of these lines:

Solidity
(UFixed6 minPosition, ) = _positionLimit(marketContext);
UFixed6 availableClosable = targets[marketId].position.unsafeSub(minPosition);

if (availableClosable.gte(marketContext.currentAccountPosition.maker)) continue;        // entire position can be closed, don't limit in cases of price deviation
  • minPosition = 20 (market maker = 80, net = 20, closable = 80)
  • target.position = 100 (assets = 50)
  • availableClosable = target - min = 80
  • currentAccountPosition.maker = 80

The if statement is true (80 >= 80) thus user is allowed to redeem all assets. However, minPosition will still be limited to 20.

Possible fix

The main logic appears to be correct, the problem is with the if statement - the currentAccountPosition doesn't provide any info about collateral, thus it's not correct to allow unlimited withdrawal based on this value - when there are more assets than current position suggests (due to decreased leverage), the limit from availableClosable should still apply (because it's possible to redeem more assets than assets derived from current position and leverage).

One possiblity is just to remove that if statement, it will then return incorrect (higher than vault assets) values sometimes, but it will not cause any problems anyway, because it will revert with underflow when trying to subtract more than total shares.

Another possibility is to use available market's vault collateral adjusted for leverage instead of current position, but I can't quickly determine if that'll be enough or not, as it will require additional research/time.

kbrizzle

So we do need this if statement in general - without it, slight price deviations version-to-version can cause the amount that is calculated to slightly undershoot. This isn't really a problem if the position can't be fully closed, but when it can, this prevents the user from being able to fully withdraw.

I was able to find a really clean solution to this though, essentially directly checking if we can fully close now that we have that information: https://github.com/equilibria-xyz/perennial-v2/pull/191.

panprog

Great solution! This is fixed now.

MLON33

Fix: https://github.com/equilibria-xyz/perennial-v2/pull/173

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 max redeem calculation uses the current position size in each market as a hard limit without comparing it to the target position, so a small position caused by market limits unnecessarily caps redemptions.

Impact

Users with large deposits could face very small maximum redeem amounts, forcing many transactions and gas losses, or making redemption practically impossible; the text does not describe an attacker gaining funds.

What the attacker needed

Vault has deposits in multiple markets; one market has a very small position due to maker limit

The fix

The issue was fixed by changing the max redeem logic to compare target and current positions and collateral, with a follow-up fix to correctly handle cases where the position can be fully closed.

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 pools money and invests it in several markets. When you withdraw, the code checks how much it can safely take out by looking at the smallest investment it holds in any one market. If one market only holds a tiny amount, the vault wrongly thinks you can only withdraw a tiny amount, even if you deposited a lot. This is a design flaw that makes withdrawals impractical, not a way for an attacker to steal 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.

More by panprog

View all

66 more disclosed reports, best first.

More from Perennial V2 Update #1

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.