Skip to content
Business logic Audit finding Intermediate

Protection sellers can bypass withdrawal delay mechanism and avoid losing funds when loans are defaulted by creating withdrawal request in each cycle

The withdrawal delay mechanism in a smart contract can be bypassed because the withdrawal request function does not track previous requests, allowing a protection seller to request withdrawal of their full balance in each cycle and then withdraw at the end of the current cycle instead of waiting for the next cycle.

Jeiwan Follow with ctf_sec, 0Kage and 22 others

Originally disclosed on Sherlock

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

Found by

Jeiwan, ctf_sec, 0Kage, HonorLt, monrel, Allarious, Bauer, rvierdiiev, chaduke, mert_eren, XKET, ast3ros, mahdikarimi, jprod15, 0x52, libratus, immeas, KingNFT, peanuts, bin2chen, clems4ever, unforgiven, carrot, csanuragjain, Ruhum

Summary

To prevent protection sellers from withdrawing fund immediately when protected lending pools are defaults, there is withdrawal delay mechanism, but it's possible to bypass it by creating withdraw request in each cycle by doing so user can withdraw in each cycle's open state. there is no penalty for users when they do this or there is no check to avoid this.

Vulnerability Detail

This is _requestWithdrawal() code:

Solidity
  function _requestWithdrawal(uint256 _sTokenAmount) internal {
    uint256 _sTokenBalance = balanceOf(msg.sender);
    if (_sTokenAmount > _sTokenBalance) {
      revert InsufficientSTokenBalance(msg.sender, _sTokenBalance);
    }

    /// Get current cycle index for this pool
    uint256 _currentCycleIndex = poolCycleManager.getCurrentCycleIndex(
      address(this)
    );

    /// Actual withdrawal is allowed in open period of cycle after next cycle
    /// For example: if request is made in at some time in cycle 1,
    /// then withdrawal is allowed in open period of cycle 3
    uint256 _withdrawalCycleIndex = _currentCycleIndex + 2;

    WithdrawalCycleDetail storage withdrawalCycle = withdrawalCycleDetails[
      _withdrawalCycleIndex
    ];

    /// Cache existing requested amount for the cycle for the sender
    uint256 _oldRequestAmount = withdrawalCycle.withdrawalRequests[msg.sender];
    withdrawalCycle.withdrawalRequests[msg.sender] = _sTokenAmount;

    unchecked {
      /// Update total requested withdrawal amount for the cycle considering existing requested amount
      if (_oldRequestAmount > _sTokenAmount) {
        withdrawalCycle.totalSTokenRequested -= (_oldRequestAmount -
          _sTokenAmount);
      } else {
        withdrawalCycle.totalSTokenRequested += (_sTokenAmount -
          _oldRequestAmount);
      }
    }

    emit WithdrawalRequested(msg.sender, _sTokenAmount, _withdrawalCycleIndex);
  }

As you can see it doesn't keep track of user current withdrawal requests and user can request withdrawal for all of his balance in each cycle and by doing so user can set withdrawalCycleDetails[Each Cycle][User] to user's sToken balance. and whenever user wants to withdraw he only need to wait until the end of the current cycle while he should have waited until next cycle end.

Impact

protection sellers can request withdraw in each cycle for their full sToken balance and code would allow them to withdraw in each cycle end time because code doesn't track how much of the balance of users is requested for withdrawals in the past.

Code Snippet

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

Tool used

Manual Review

Recommendation

To avoid this code should keep track of user balance that is not in withdraw delay and user balance that are requested for withdraw. and to prevent users from requesting withdrawing and not doing it protocol should have some penalties for withdrawals, for example the waiting withdraw balance shouldn't get reward in waiting duration.

Discussion

vnadoda

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

clems4ev3r

Fix looks good

The original report as published on Sherlock, by Jeiwan. 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

The withdrawal request function overwrites the previous request amount for the same cycle without tracking cumulative requests across cycles, allowing a user to request their full balance each cycle and withdraw earlier than intended.

Impact

A protection seller can bypass the withdrawal delay and withdraw funds at the end of each cycle, potentially avoiding losses when loans are defaulted.

What the attacker needed

Be a protection seller; have sToken balance

The fix

The code should track the user's balance that is not in withdrawal delay and the balance requested for withdrawal, and consider penalties such as not rewarding the waiting withdrawal balance.

How it was found

  1. Call the withdrawal request function with the full sToken balance in cycle 1.
  2. Wait until the end of cycle 1.
  3. Withdraw the requested amount at the end of cycle 1, which is earlier than the intended cycle 3.
  4. Repeat the process in each subsequent cycle to continuously bypass the delay.

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.

In this smart contract, when you ask to withdraw your money, there is a delay of two cycles to prevent people from pulling out funds right before a loan defaults. However, the code that handles withdrawal requests does not remember your previous requests, so you can ask to withdraw your full balance every cycle. This means you can always withdraw at the end of the current cycle, bypassing the intended delay.

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 Jeiwan

View all

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