Lending pool state transition will be broken when pool is expired in late state
The lending pool state machine checks for the Expired state before Late, so a pool that becomes expired while in a late state never unlocks capital, and a pool that becomes expired without a late transition never locks capital. This can leave capital locked forever or prevent protection buyers from being compensated.
rvierdiiev Follow with Jeiwan
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-carapace-judging/issues/230
Found by
rvierdiiev, Jeiwan
Summary
Lending pool state transition will be broken when pool is expired in late state
Vulnerability Detail
Each lending pool has its state. State is calculated inside ReferenceLendingPools._getLendingPoolStatus function.
https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/core/pool/ReferenceLendingPools.sol#L318-L349
function _getLendingPoolStatus(address _lendingPoolAddress)
internal
view
returns (LendingPoolStatus)
{
if (!_isReferenceLendingPoolAdded(_lendingPoolAddress)) {
return LendingPoolStatus.NotSupported;
}
ILendingProtocolAdapter _adapter = _getLendingProtocolAdapter(
_lendingPoolAddress
);
if (_adapter.isLendingPoolExpired(_lendingPoolAddress)) {
return LendingPoolStatus.Expired;
}
if (
_adapter.isLendingPoolLateWithinGracePeriod(
_lendingPoolAddress,
Constants.LATE_PAYMENT_GRACE_PERIOD_IN_DAYS
)
) {
return LendingPoolStatus.LateWithinGracePeriod;
}
if (_adapter.isLendingPoolLate(_lendingPoolAddress)) {
return LendingPoolStatus.Late;
}
return LendingPoolStatus.Active;
}
Pls, note, that the first state that is checked is expired.
https://github.com/sherlock-audit/2023-02-carapace/blob/main/contracts/adapters/GoldfinchAdapter.sol#L62-L77
function isLendingPoolExpired(address _lendingPoolAddress)
external
view
override
returns (bool)
{
ICreditLine _creditLine = _getCreditLine(_lendingPoolAddress);
uint256 _termEndTimestamp = _creditLine.termEndTime();
/// Repaid logic derived from Goldfinch frontend code:
/// https://github.com/goldfinch-eng/mono/blob/bd9adae6fbd810d1ebb5f7ef22df5bb6f1eaee3b/packages/client2/lib/pools/index.ts#L54
/// when the credit line has zero balance with valid term end, it is considered repaid
return
block.timestamp >= _termEndTimestamp ||
(_termEndTimestamp > 0 && _creditLine.balance() == 0);
}
As you can see, pool is expired if time of credit line has ended or loan is fully paid.
State transition for lending pool is done inside DefaultStateManager._assessState function. This function is responsible to lock capital, when state is late and unlock it when it's changed from late to active again.
Because the first state that is checked is expired there can be few problems.
First problem. Suppose that lending pool is in late state. So capital is locked. There are 2 options now: payment was done, so pool becomes active and capital unlocked, payment was not done then pool has defaulted. But in case when state is late, and lending pool expired or loan is fully repaid(so it's also becomes expired), then capital will not be unlocked as there is no such transition Late -> Expired. The state will be changed to Expired and no more actions will be done. Also in this case it's not possible to detect if lending pool expired because of time or because no payment was done.
Second problem.
Lending pool is in active state. Last payment should be done some time before _creditLine.termEndTime(). Payment was not done, which means that state should be changed to Late and capital should be locked, but state was checked when loan has ended, so it became Expired and again there is no such transition that can detect that capital should be locked in this case. The state will be changed to Expired and no more actions will be done.
Impact
Depending on situation, capital can be locked forever or protection buyers will not be compensated.
Code Snippet
Provided above
Tool used
Manual Review
Recommendation
These are tricky cases, think about transition for lending pool in such cases.
Discussion
vnadoda
@clems4ev3r We are planning to fix this, possibly using recommendation mentioned in a duplicate #251
clems4ev3r
@vnadoda agreed
vnadoda
@clems4ev3r PR for this fix: https://github.com/carapace-finance/credit-default-swaps-contracts/pull/65
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
The state transition logic checks for Expired before Late and has no transition from Late to Expired, so capital locking and unlocking are skipped in certain timing scenarios.
Impact
Capital can be locked forever or protection buyers will not be compensated.
What the attacker needed
A lending pool in late state; the pool expires or is fully repaid; or a pool expires without a late transition
The fix
The state transition logic should be updated to handle pools that expire while late or without a late transition, and the project confirmed a fix was made.
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 tracks whether a lending pool is active, late, or expired. The order in which it checks these states is wrong: it checks expired first, so it can miss a late payment. This means money can stay locked or people owed compensation may not get it.
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.