Skip to content
Business logic Audit finding Intermediate

Missing validation of snapshotId makes it possible for the investor to claim unlocked capitals from the same snapshot multiple times

The contract fails to check that the snapshot ID returned when calculating claimable unlocked capital is non-zero before updating the seller's last claimed snapshot ID. This allows a seller to reset their last claimed snapshot ID to zero and repeatedly claim the same unlocked capital, potentially draining funds.

Koolex Follow with bin2chen

Originally disclosed on Sherlock

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

Found by

Koolex, bin2chen

Summary

Missing validation of snapshotId makes it possible for the investor/seller to claim unlocked capitals from the same snapshot multiple times

Vulnerability Detail

Description

The seller can call ProtectionPool.claimUnlockedCapital function to claim unlocked capitals. It then calls defaultStateManager.calculateAndClaimUnlockedCapital to calculate the claimable amount, then it transfers the amount to the seller if it is greater than zero. defaultStateManager.calculateAndClaimUnlockedCapital function works as follows:

  1. Iterates over all lending pools.
  2. For each lending pool calculates the claimable amount for the seller.
  3. Updates the last claimed snapshot id for the seller for the given lending pool so it becomes uncalimable next time.

However, before updating the last claimed snapshot id, it doesn't check if the returned snapshot Id (from _calculateClaimableAmount function) is zero. This means if it happens that the returned value is zero, the last claimed snapshot id will be reset to its initial value (zero) and the seller can claim again as if s/he never did.

PoC

Given: A pool protection with one lending pool for simplicity Seller's lastClaimedSnapshotId = 0

Imagine the following sequence of events:

  • Event: Lending pool transition from Active to Late
    • A snapshot taken
    • LockedCapitals[0].snapshotId = 1
    • LockedCapitals[0].locked = true
  • Event: Lending pool transition from Late to Active
    • LockedCapitals[0].snapshotId = 1
    • LockedCapitals[0].locked = false
  • Event: Seller claims => receives his/her share of the unlocked capital
    • LastClaimedSnapshotId = 1
  • Event: Seller claims => doesn't receive anything
    • LastClaimedSnapshotId = 0 (this is the issue as it is reset to zero)
  • Now the seller can claim again from the same snapshot
  • Event: Seller claims => receives his/her share of the unlocked capital
    • LastClaimedSnapshotId = 1

This can be repeated till all funds/capitals are drained.

This happens when all snapshots were claimed before, then the function _calculateClaimableAmount will return_latestClaimedSnapshotId as zero.

Impact

  • An investor/seller could possibly claim unlocked capitals from the same snapshot multiple times which is unfair.
  • An attacker could join the pool as seller, later drains the funds from the protection pool whenever any lending pool goes into Late then Active state again.

Code Snippet

  • ProtectionPool.claimUnlockedCapital
Shell
    /// Investors can claim their total share of released/unlocked capital across all lending pools
    uint256 _claimableAmount = defaultStateManager
      .calculateAndClaimUnlockedCapital(msg.sender);

    if (_claimableAmount > 0) {
      console.log(
        "Total sToken underlying: %s, claimableAmount: %s",
        totalSTokenUnderlying,
        _claimableAmount
      );
      /// transfer the share of unlocked capital to the receiver
      poolInfo.underlyingToken.safeTransfer(_receiver, _claimableAmount);
    }

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

  • defaultStateManager.calculateAndClaimUnlockedCapital
Shell
 /// Calculate the claimable amount across all the locked capital instances for a given protection pool
      (
        uint256 _unlockedCapitalPerLendingPool,
        uint256 _snapshotId
      ) = _calculateClaimableAmount(poolState, _lendingPool, _seller);
      _claimedUnlockedCapital += _unlockedCapitalPerLendingPool;

      /// update the last claimed snapshot id for the seller for the given lending pool,
      /// so that the next time the seller claims, the calculation starts from the last claimed snapshot id
      poolState.lastClaimedSnapshotIds[_lendingPool][_seller] = _snapshotId;

https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/DefaultStateManager.sol#L186-L195

  • defaultStateManager._calculateClaimableAmount
Shell
      /// Verify that the seller does not claim the same snapshot twice
      if (!lockedCapital.locked && _snapshotId > _lastClaimedSnapshotId) {
        ERC20SnapshotUpgradeable _poolSToken = ERC20SnapshotUpgradeable(
          address(poolState.protectionPool)
        );

        console.log(
          "balance of seller: %s, total supply: %s at snapshot: %s",
          _poolSToken.balanceOfAt(_seller, _snapshotId),
          _poolSToken.totalSupplyAt(_snapshotId),
          _snapshotId
        );

        /// The claimable amount for the given seller is proportional to the seller's share of the total supply at the snapshot
        /// claimable amount = (seller's snapshot balance / total supply at snapshot) * locked capital amount
        _claimableUnlockedCapital =
          (_poolSToken.balanceOfAt(_seller, _snapshotId) *
            lockedCapital.amount) /
          _poolSToken.totalSupplyAt(_snapshotId);

        /// Update the last claimed snapshot id for the seller
        _latestClaimedSnapshotId = _snapshotId;

        console.log(
          "Claimable amount for seller %s is %s",
          _seller,
          _claimableUnlockedCapital
        );
      }

https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/DefaultStateManager.sol#L487-L515

Tool used

Manual Review

Recommendation

Only update the last snapshot id of the seller if it is greater than zero.

Example:

Shell
if(_snapshotId > 0){
      poolState.lastClaimedSnapshotIds[_lendingPool][_seller] = _snapshotId;
	 }

Discussion

vnadoda

@clems4ev3r this isn't a valid concern/issue. The function DefaultStateManager. _calculateClaimableAmount always starts with the seller's last claimed snapshot id and not with 0. So in the scenario/PoC mentioned, when the seller tries to claim a second time, returned _latestClaimedSnapshotId will be 1 and not 0.

See: https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/DefaultStateManager.sol#L466

clems4ev3r

@vnadoda surprisingly this seems valid.

After a user has claimed for a snapshot, latest snapshot claimed is correctly set here: https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/DefaultStateManager.sol#L195

If the user calls the function again, they will be able to claim zero funds since the condition here is always false: https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/DefaultStateManager.sol#L488

Unfortunately that means the variable _latestClaimedSnapshotId is not set here: https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/DefaultStateManager.sol#L508

and so _calculateClaimableAmount returns a _latestClaimedSnapshotId == 0 resetting latest claimed snapshot and user can claim again

vnadoda

@clems4ev3r ah, I see it now. var at the beginning is _lastClaimedSnapshotId is different than return var _latestClaimedSnapshotId. I will add this to the fix list

vnadoda

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

clems4ev3r

Fix looks good

The original report as published on Sherlock, by Koolex. 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 function that updates the last claimed snapshot ID does not verify that the new snapshot ID is greater than zero, so a zero value (returned when no new claimable amount exists) overwrites the previous non-zero ID.

Impact

An attacker could join the pool as a seller and drain funds whenever a lending pool goes into Late then Active state again.

What the attacker needed

Attacker must be a seller in the protection pool; A lending pool must transition to Late and back to Active

The fix

The fix was to only update the last claimed snapshot ID if it is greater than zero.

How it was found

  1. A lending pool transitions from Active to Late, creating a snapshot with a non-zero ID and marking locked capital as locked.
  2. The lending pool transitions back from Late to Active, unlocking the capital.
  3. The seller claims unlocked capital, receiving their share and the last claimed snapshot ID is set to the snapshot ID.
  4. The seller claims again; no new amount is claimable, but the last claimed snapshot ID is reset to zero.
  5. The seller can now claim again from the same snapshot, receiving the same share repeatedly.

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.

When a seller claims their share of unlocked capital, the system records which snapshot they last claimed from. If they claim again and there is nothing new, the system mistakenly resets that record to zero. This means they can claim the same money over and over.

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 Koolex

View all

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

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.