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:
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:
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
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
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
- Predict the timestamp when a pool will switch to Late.
- Use a flash loan to acquire sTokens from a secondary market like Uniswap.
- Call `assessState` to trigger the pool to move to Late, which snapshots the current sToken holders.
- 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.