Skip to content
Race condition Audit finding Intermediate

Sandwich attack to accruePremiumAndExpireProtections()

The function accruePremiumAndExpireProtections() increases the pool's underlying token balance without minting new shares, raising the exchange rate. An attacker can deposit just before this call and withdraw just after to capture a disproportionate share of the premium.

libratus Follow with jkoppel, immeas and 3 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-carapace-judging/issues/26

Found by

libratus, jkoppel, immeas, 0Kage, chaduke, monrel

Summary

accruePremiumAndExpireProtections() will increase totalSTokenUnderlying, and thus increase the exchange rate of the ProtectionPool. A malicious user can launch a sandwich attack and profit. This violates the Fair Distribution principle of the protocol: https://www.carapace.finance/WhitePaper#premium-pricing

Vulnerability Detail

Let's show how a malicious user, Bob, can launch a sandwich attack to accruePremiumAndExpireProtections() and profit.

  1. Suppose there are 1,000,000 underlying tokens for the ProtectionPool, and totalSupply = 1,000,000, therefore the exchange rate is 1/1 share. Suppose Bob has 100,000 shares.

  2. Suppose accruePremiumAndExpireProtections() is going to be called and add 100,000 to totalSTokenUnderlying at L346.

https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/pool/ProtectionPool.sol#L279-L354

  1. Bob front-runs accruePremiumAndExpireProtections() and calls deposit() to deposit 100,000 underlying tokens into the contract. The check for ProtectionPoolPhase will pass for an open phase. As a result, there are 1,100,000 underlying tokens, and 1,100,000 shares, the exchange rate is still 1/1 share. Bob now has 200,000 shares.
JavaScript
 function deposit(uint256 _underlyingAmount, address _receiver)
    external
    override
    whenNotPaused
    nonReentrant
  {
    _deposit(_underlyingAmount, _receiver);
  }

  function _deposit(uint256 _underlyingAmount, address _receiver) internal {
    /// Verify that the pool is not in OpenToBuyers phase
    if (poolInfo.currentPhase == ProtectionPoolPhase.OpenToBuyers) {
      revert ProtectionPoolInOpenToBuyersPhase();
    }

    uint256 _sTokenShares = convertToSToken(_underlyingAmount);
    totalSTokenUnderlying += _underlyingAmount;
    _safeMint(_receiver, _sTokenShares);
    poolInfo.underlyingToken.safeTransferFrom(
      msg.sender,
      address(this),
      _underlyingAmount
    );

    /// Verify leverage ratio only when total capital/sTokenUnderlying is higher than minimum capital requirement
    if (_hasMinRequiredCapital()) {
      /// calculate pool's current leverage ratio considering the new deposit
      uint256 _leverageRatio = calculateLeverageRatio();

      if (_leverageRatio > poolInfo.params.leverageRatioCeiling) {
        revert ProtectionPoolLeverageRatioTooHigh(_leverageRatio);
      }
    }

    emit ProtectionSold(_receiver, _underlyingAmount);
 }
  1. Now accruePremiumAndExpireProtections()gets called and 100,000 is added to totalSTokenUnderlying at L346. As a result, we have 1,200,000 underlying tokens with 1,100,000 shares. The exchange rate becomes 12/11 share.

  2. Bob calls the withdraw() function (assume he made a request two cycles back, he could do that since he had 100,000 underlying tokens in the pool) to withdraw 100,000 shares and he will get 100,000*12/11 = 109,090 underlying tokens. So he has a profit of 9,090 underlying tokens by the sandwich attack.

Impact

A malicious user can launch a sandwich attack to accruePremiumAndExpireProtections()and profit.

Code Snippet

See above

Tool used

VScode

Manual Review

Recommendation

  • Create a new contract as a temporary place to store the accrued premium, and then deliver it to the ProtectionPool over a period of time (delivery period) with some premiumPerSecond to lower the incentive of a quick profit by sandwich attack.
  • Restrict the maximum deposit amount for each cycle.
  • Restrict the maximum withdraw amount for each cycle.

Discussion

vnadoda

@clems4ev3r this is a duplicate of #294 and #204

vnadoda

@clems4ev3r can you verify this is a duplicate? @hrishibhat can we close this?

clems4ev3r

@vnadoda agreed, this is a duplicate of #294 and #204

vnadoda

@hrishibhat please close this.

vnadoda

@clems4ev3r PR for this fix: https://github.com/carapace-finance/credit-default-swaps-contracts/pull/57

clems4ev3r

Fix looks good

The original report as published on Sherlock, by libratus. 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 premium accrual increases totalSTokenUnderlying without minting new shares, so the exchange rate jumps instantly and can be captured by depositing just before the accrual.

Impact

A malicious user could sandwich the premium accrual and profit, taking value from other depositors.

What the attacker needed

Attacker holds pool shares; attacker can front-run the accrual call; attacker has a pending withdrawal request

The fix

The fix was to stream the accrued premium into the pool over time via a separate contract, and to cap deposits and withdrawals per cycle, reducing the incentive for a quick sandwich.

How it was found

  1. Note the pool has 1,000,000 underlying tokens and 1,000,000 shares, with Bob holding 100,000 shares.
  2. Bob front-runs the accruePremiumAndExpireProtections() call by depositing 100,000 underlying tokens, receiving 100,000 new shares.
  3. The accruePremiumAndExpireProtections() call adds 100,000 to totalSTokenUnderlying, raising the exchange rate to 12/11.
  4. Bob withdraws 100,000 shares and receives about 109,090 underlying tokens, making a profit of about 9,090 tokens.

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 protection pool works like a shared savings account: people deposit tokens and receive shares representing their portion. When the pool earns premium, the total tokens increase but the number of shares stays the same, so each share becomes worth more. The attacker deposits right before the premium is added and withdraws right after, so their new shares also benefit from the premium even though they only held them for a moment.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by libratus

View all

4 more disclosed reports, best first.

More from Carapace

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.