Skip to content
Business logic Audit finding Advanced

Withdrawal caps can be bypassed by opening positions against the SpotHedgeBaseMaker

The CircuitBreaker's withdrawal limit does not account for base tokens deposited into the SpotHedgeBaseMaker until they are swapped into quote tokens and deposited into the vault. An attacker can open large short positions to trigger this swap, inflating the vault's TVL and thereby increasing the amount they can withdraw beyond the intended percentage limit.

IllIllI Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-02-perpetual-judging/issues/117

The protocol has acknowledged this issue.

Found by

IllIllI

Summary

Deposits/withdrawals of base tokens to the SpotHedgeBaseMaker aren't accounted for in the CircuitBreaker's accounting, so the tokens can be used by attackers to increase the amount withdrawable past the high water mark percentage limits.

Vulnerability Detail

The SpotHedgeBaseMaker allows LPs to deposit base tokens in exchange for shares. The CircuitBreaker doesn't include base tokens in its accounting, until they're converted to quote tokens and added to the vault, which happens when someone opens a short base position, and the SpotHedgeBaseMaker needs to hedge its corresponding long base position, by swapping base tokens for the quote token. The CircuitBreaker keeps track of net deposits/withdrawals of the quote token using a high water mark system, in which the high water mark isn't updated until the sync interval has passed. As long as the net outflow between sync intervals doesn't pass the threshold, withdrawals are allowed.

Impact

Assume there is some sort of exploit, where the attacker is able to artificially inflate their 'fund' amount (e.g. one of my other submissions), and are looking to withdraw their ill-gotten gains. Normally, the amount they'd be able to withdraw would be limited to X% of the TVL of collateral tokens in the vault. By converting some of their 'fund' to margin, and opening huge short base positions against the SpotHedgeBaseMaker, they can cause the SpotHedgeBaseMaker to swap its contract balance of base tokens into collateral tokens, and deposit them into its vault account, increasing the TVL to TVL+Y. Since the time-based threshold will still be the old TVL, they're now able to withdraw TVL.old * X% + Y, rather than just TVL.old * X%. Depending on the price band limits set for swaps, the attacker can use a flash loan to temporarily skew the base/quote UniswapV3 exchange rate, such that the swap nets a much larger number of quote tokens than would normally be available to trade for. This means that if the 'fund' amount that the attacker has control over is larger than the actual number of collateral tokens in the vault, the attacker can potentially withdraw 100% of the TVL.

Code Snippet

Quote tokens acquired via the swap are deposited into the vault, which passes them through the CircuitBreaker:

Solidity
// File: src/maker/SpotHedgeBaseMaker.sol : SpotHedgeBaseMaker.fillOrder()   #1

415                } else {
416                    quoteTokenAcquired = _formatPerpToSpotQuoteDecimals(amount);
417 @>                 uint256 oppositeAmountXSpotDecimals = _uniswapV3ExactOutput(
418                        UniswapV3ExactOutputParams({
419                            tokenIn: address(_getSpotHedgeBaseMakerStorage().baseToken),
420                            tokenOut: address(_getSpotHedgeBaseMakerStorage().quoteToken),
421                            path: path,
422                            recipient: maker,
423                            amountOut: quoteTokenAcquired,
424                            amountInMaximum: _getSpotHedgeBaseMakerStorage().baseToken.balanceOf(maker)
425                        })
426                    );
427                    oppositeAmount = _formatSpotToPerpBaseDecimals(oppositeAmountXSpotDecimals);
428                    // Currently we don't utilize fillOrderCallback for B2Q swaps,
429                    // but we still populate the arguments anyways.
430                    fillOrderCallbackData.amountXSpotDecimals = quoteTokenAcquired;
431                    fillOrderCallbackData.oppositeAmountXSpotDecimals = oppositeAmountXSpotDecimals;
432                }
433    
434                // Deposit the acquired quote tokens to Vault.
435:@>             _deposit(vault, _marketId, quoteTokenAcquired);

https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/maker/SpotHedgeBaseMaker.sol#L405-L442

The CircuitBreaker only tracks net liqInPeriod changes changes during the withdrawal period, and only triggers the CircuitBreaker based on the TVL older than the withdrawal period:

Solidity
// File: src/circuitBreaker/LibLimiter.sol : LibLimiter.status()   #2

119            int256 currentLiq = limiter.liqTotal;
...
126 @>         int256 futureLiq = currentLiq + limiter.liqInPeriod;
127            // NOTE: uint256 to int256 conversion here is safe
128            int256 minLiq = (currentLiq * int256(limiter.minLiqRetainedBps)) / int256(BPS_DENOMINATOR);
129    
130 @>         return futureLiq < minLiq ? LimitStatus.Triggered : LimitStatus.Ok;
131:       }

https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/circuitBreaker/LibLimiter.sol#L119-L131

Tool used

Manual Review

Recommendation

Calculate the quote-token value of each base token during LP deposit()/withdraw(), and add those amounts as CircuitBreaker flows during deposit()/withdraw(), then also invert those flows prior to depositing into/withdrawing from the vault

Discussion

sherlock-admin3

1 comment(s) were left on this issue during the judging contest.

santipu_ commented:

Medium/Low - Attacker would have to open a big short position against SpotHedgeBaseMaker during some time, exposing itself to losses due to price changes

tailingchen

Valid but won't fix.

Although circuit breaker can be bypassed, it still depends on the liquidity of SpotHedgeBaseMaker and the price band. The team prefers not to fix it in the early stages.

If the team want to fix it in the future, we have discussed several possible solutions. These solutions all have some pros and cons. The team is still evaluating possible options.

  1. Separately calculate inflows and outflows for the same period. Only previous TVL is taken.
  2. Do not include whitelisted maker’s deposit into TVL calculations.
  3. Let SHBM check the current rate limit status of CircuitBreaker before depositing collateral into the vault. If the remaining balance that can be withdrawn is too small, it means that the current vault risk is too high and there is a risk that it cannot be withdrawn. Therefore, SHBM can refuse this position opening.

The original report as published on Sherlock, by IllIllI. 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 CircuitBreaker only tracks quote-token flows and ignores base-token deposits/withdrawals in the SpotHedgeBaseMaker, so the TVL used for withdrawal limits can be inflated by converting base tokens to quote tokens via a swap.

Impact

An attacker could bypass the withdrawal cap and potentially withdraw up to 100% of the TVL, but this depends on the liquidity of the SpotHedgeBaseMaker and the price band, and the attacker would be exposed to losses from price changes during the short position.

What the attacker needed

Attacker has inflated 'fund' amount; attacker can open large short positions; sufficient liquidity in SpotHedgeBaseMaker; price band allows favourable swap

The fix

The team acknowledged the issue but decided not to fix it in the early stages; a possible fix would be to calculate the quote-token value of base tokens during LP deposit/withdraw and include those amounts in the CircuitBreaker flows.

How it was found

  1. Attacker artificially inflates their 'fund' amount (e.g., via another exploit).
  2. Attacker converts some of their 'fund' to margin and opens huge short base positions against the SpotHedgeBaseMaker.
  3. This causes the SpotHedgeBaseMaker to swap its base tokens for quote tokens and deposit them into the vault, increasing TVL.
  4. The withdrawal limit is still based on the old TVL, so the attacker can withdraw more than the intended percentage.

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.

The protocol limits how much can be withdrawn in a period based on the total value locked (TVL). The SpotHedgeBaseMaker holds base tokens that are not counted in that TVL until they are swapped for quote tokens. An attacker can force that swap by opening a large short position, which temporarily raises the TVL and lets them withdraw more than the limit allows.

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 IllIllI

View all

119 more disclosed reports, best first.

More from Perpetual

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.