Skip to content
Business logic Audit finding Intermediate

secondary markets are problematic with how `lockCapital` works

The `assessState` function can be called by anyone to trigger a pool's transition to the Late state, which snapshots the current sToken holders. An attacker can use a flash loan to acquire sTokens just before this snapshot, then claim a share of any later unlocked capital, stealing from legitimate holders.

charlesjhongc Follow with immeas

Originally disclosed on Sherlock

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

Found by

charlesjhongc, immeas

Summary

Seeing that a pool is about to lock, an attacker can use a flash loan from a secondary market like uniswap to claim the share of a potential unlock of capital later.

Vulnerability Detail

The timestamp a pool switches to Late can be predicted and an attacker can use this to call assessState which is callable by anyone. This will trigger the pool to move from Active/LateWithinGracePeriod to Late calling lockCapital on the ProtectionPool:

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

Solidity
File: ProtectionPool.sol

365:    /// step 1: Capture protection pool's current investors by creating a snapshot of the token balance by using ERC20Snapshot in SToken
366:    _snapshotId = _snapshot();

This records who is holding sTokens at this point in time. If the borrower makes a payment and the pool turns back to Active, later the locked funds will be available to claim for the sToken holders at that snapshot:

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

Solidity
File: DefaultStateManager.sol

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

From docs:

If sellers wish to redeem their capital and interest before the lockup period, they might be able to find a buyer of their sToken in a secondary market like Uniswap. Traders in the exchanges can long/short sTokens based on their opinion about the risk exposure associated with sTokens. Since an sToken is a fungible ERC20 token, it is fairly easy to bootstrap the secondary markets for protection sellers.

If there is a uniswap (or similar) pool for this sToken, an attacker could potentially, using a flash loan, trigger the switch to Late and since they will be the ones holding the sTokens at the point of locking they will be the ones that can claim the funds at a potential unlock.

Impact

An attacker can, using a flash loan from a secondary market like uniswap, steal a LPs possible share of unlocked tokens. Only paying taking the risk of the flash loan fee.

Code Snippet

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

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

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

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

Tool used

Manual Review

Recommendation

I recommend you make assessState only callable by a trusted user. This would remove the attack vector, since you must hold the tokens over a transaction. It would still be possible to use the withdraw bug, but if that is fixed this would remove the possibility to "flash-lock".

Discussion

vnadoda

@clems4ev3r PR to fix this issue: https://github.com/carapace-finance/credit-default-swaps-contracts/pull/69

clems4ev3r

Fix looks good

The original report as published on Sherlock, by charlesjhongc. 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 `assessState` function is publicly callable, allowing an attacker to control the timing of the snapshot that determines who can claim unlocked capital.

Impact

An attacker could steal a legitimate liquidity provider's share of unlocked tokens, paying only the flash loan fee.

What the attacker needed

A secondary market for the sToken; flash loan availability; attacker can call assessState

The fix

The `assessState` function should be restricted to a trusted caller, so that an attacker cannot control when the snapshot is taken.

How it was found

  1. Predict the timestamp when a pool will switch to Late.
  2. Use a flash loan to acquire sTokens from a secondary market like Uniswap.
  3. Call `assessState` to trigger the pool to move to Late, which snapshots the current sToken holders.
  4. Later, if the pool turns back to Active and funds are unlocked, claim the unlocked capital as a snapshot holder.

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 holds money from sellers. When the pool is about to lock funds, it records who currently holds the pool's tokens. An attacker can borrow a large amount of those tokens for a very short time, trigger the lock, and appear as a holder. Later, if the funds are unlocked, the attacker can claim a share that should have gone to real holders.

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