Skip to content
DoS Audit finding Intermediate

Too many active protections can cause the ProtectionPool reach the block gas limit

The ProtectionPool contract loops over an unbounded array of protections in two functions, which can cause transactions to exceed the block gas limit and revert. An attacker could stuff the array with many small protections, making critical functions like lockCapital and _accruePremiumAndExpireProtections unusable, potentially denying coverage to buyers.

ctf_sec Follow with Koolex, Tricko and 20 others

Originally disclosed on Sherlock

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

Found by

ctf_sec, Koolex, Tricko, 0Kage, Bauer, rvierdiiev, chaduke, ast3ros, 0x52, libratus, minhtrng, MalfurionWhitehat, modern_Alchemist_00, yixxas, jkoppel, __141345__, SPYBOY, KingNFT, peanuts, bin2chen, clems4ever, unforgiven, Ruhum

Summary

There are two instances where the ProtectionPool contract loops over an unbounded array. These can cause the transaction to succeed the block gas limit causing the transaction to revert, see https://swcregistry.io/docs/SWC-128.

Vulnerability Detail

Both in lockCapital() and in _accruePremiumAndExpireProtections() the contract loops over the unbounded array of protections. If there are too many protections the transaction will revert because it reached the block gas limit.

Impact

Both the lockCapital() and _accruePremiumAndExpireProtections() functions are critical components of the contract. Them not being accessible renders the contract useless. Protection buyers won't be covered in case of the underlying pool defaulting because the deposited tokens can't be locked up

Code Snippet

lockCapital(): https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/pool/ProtectionPool.sol#L378-L411

SOL
    /// Iterate all active protections and calculate total locked amount for this lending pool
    /// 1. calculate remaining principal amount for each loan protection in the lending pool.
    /// 2. for each loan protection, lockedAmt = min(protectionAmt, remainingPrincipal)
    /// 3. total locked amount = sum of lockedAmt for all loan protections
    uint256 _length = activeProtectionIndexes.length();
    for (uint256 i; i < _length; ) {
      /// Get protection info from the storage
      uint256 _protectionIndex = activeProtectionIndexes.at(i);
      ProtectionInfo storage protectionInfo = protectionInfos[_protectionIndex];

      /// Calculate remaining principal amount for a loan protection in the lending pool
      uint256 _remainingPrincipal = poolInfo
        .referenceLendingPools
        .calculateRemainingPrincipal(
          _lendingPoolAddress,
          protectionInfo.buyer,
          protectionInfo.purchaseParams.nftLpTokenId
        );

      /// Locked amount is minimum of protection amount and remaining principal
      uint256 _protectionAmount = protectionInfo
        .purchaseParams
        .protectionAmount;
      uint256 _lockedAmountPerProtection = _protectionAmount <
        _remainingPrincipal
        ? _protectionAmount
        : _remainingPrincipal;

      _lockedAmount += _lockedAmountPerProtection;

      unchecked {
        ++i;
      }
    }

_accruePremiumAndExpireProtections(): https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/pool/ProtectionPool.sol#L957-L1021

SOL
  /**
   * @dev Accrue premium for all active protections and mark expired protections for the specified lending pool.
   * Premium is only accrued when the lending pool has a new payment.
   * @return _accruedPremiumForLendingPool the total premium accrued for the lending pool
   * @return _totalProtectionRemoved the total protection removed because of expired protections
   */
  function _accruePremiumAndExpireProtections(
    LendingPoolDetail storage lendingPoolDetail,
    uint256 _lastPremiumAccrualTimestamp,
    uint256 _latestPaymentTimestamp
  )
    internal
    returns (
      uint256 _accruedPremiumForLendingPool,
      uint256 _totalProtectionRemoved
    )
  {
    /// Get all active protection indexes for the lending pool
    uint256[] memory _protectionIndexes = lendingPoolDetail
      .activeProtectionIndexes
      .values();

    /// Iterate through all active protection indexes for the lending pool
    uint256 _length = _protectionIndexes.length;
    for (uint256 j; j < _length; ) {
      uint256 _protectionIndex = _protectionIndexes[j];
      ProtectionInfo storage protectionInfo = protectionInfos[_protectionIndex];

      /// Verify & accrue premium for the protection and
      /// if the protection is expired, then mark it as expired
      (
        uint256 _accruedPremiumInUnderlying,
        bool _expired
      ) = ProtectionPoolHelper.verifyAndAccruePremium(
          poolInfo,
          protectionInfo,
          _lastPremiumAccrualTimestamp,
          _latestPaymentTimestamp
        );
      _accruedPremiumForLendingPool += _accruedPremiumInUnderlying;

      if (_expired) {
        /// Add removed protection amount to the total protection removed
        _totalProtectionRemoved += protectionInfo
          .purchaseParams
          .protectionAmount;

        ProtectionPoolHelper.expireProtection(
          protectionBuyerAccounts,
          protectionInfo,
          lendingPoolDetail,
          _protectionIndex
        );
        emit ProtectionExpired(
          protectionInfo.buyer,
          protectionInfo.purchaseParams.lendingPoolAddress,
          protectionInfo.purchaseParams.protectionAmount
        );
      }

      unchecked {
        ++j;
      }
    }
  }

Tool used

Manual Review

Recommendation

You either have to limit the number of protections so that it is impossible that you surpass the block gas limit. Or, you change the logic so that you're never forced to loop over all the existing protections.

Discussion

vnadoda

@clems4ev3r We are aware of this concern re: accruePremiumAndExpireProtections and hence we had put mitigation in place. If there are too many protections to iterate over, we can call the function accruePremiumAndExpireProtections in batch using _lendingPools param. See https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/pool/ProtectionPool.sol#L279

The function lockCapital deals with one lending pool at a time, so practically it will not have too many protections to iterate over.

clems4ev3r

@vnadoda activeProtectionIndexes is unbounded even for one lendingPool. Any malicious user can stuff the array by taking small protections and cause the DOS. This sounds like a valid issue

vnadoda

@clems4ev3r @taisukemino let's discuss possible remedies. can we set up conf call?

vnadoda

@clems4ev3r As discussed on the call, the following 2 fixes should reduce lockCapital attack vector significantly:

  1. Don't allow buyers to buy protection for the same NFT/lending position multiple times
  2. Introduce min protection amount param

@taisukemino let's discuss this internally.

vnadoda

@hrishibhat we are planning to address this

vnadoda

@hrishibhat @clems4ev3r Min protection amount change is NOT needed because a buyer can only buy one protection per lending position. Let me know if you have any questions/concerns.

jacksanford1

No code change planned, labeling as Acknowledged by protocol team

The original report as published on Sherlock, by ctf_sec. 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 contract iterates over an unbounded array of protections without limiting its size, so a large number of entries can exceed the block gas limit.

Impact

The critical functions lockCapital and _accruePremiumAndExpireProtections could become inaccessible, rendering the contract useless and preventing protection buyers from being covered if the underlying pool defaults.

What the attacker needed

Attacker can buy protections; many protections in the array

The fix

The protocol team acknowledged the issue but planned no code change, stating that a buyer can only buy one protection per lending position, which they believe reduces the attack vector.

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 has functions that go through a list of items one by one. If the list gets too long, the transaction runs out of gas and fails. An attacker could add many small items to the list, making those functions always fail, so the contract cannot work properly.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by ctf_sec

View all

25 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.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.