Skip to content
Business logic Audit finding Intermediate

withdraw() users may can’t withdraw underlyingBorrowToken properly

The withdraw function incorrectly applies a collateral ratio check to borrowed tokens, which do not affect the collateral ratio. This can prevent users from withdrawing their borrowed tokens if a slight price fluctuation causes the check to fail, even though they are far from liquidation.

bin2chen Follow with 0xAlix2, 3n0ch and 8 others

Originally disclosed on Code4rena

Submitted by bin2chen, also found by 0xAlix2, 3n0ch, samuraii77, ayden, inzinko, zarkk01, elhaj, Bob, alix40, and ether_sky

We can withdraw underlyingCollateralToken and underlyingBorrowToken by withdraw():

    function withdraw(WithdrawParams calldata params) external payable override(ISize) whenNotPaused {
        state.validateWithdraw(params);
        state.executeWithdraw(params);
@>      state.validateUserIsNotBelowOpeningLimitBorrowCR(msg.sender);
    }

    function executeWithdraw(State storage state, WithdrawParams calldata params) public {
        uint256 amount;
        if (params.token == address(state.data.underlyingBorrowToken)) {
            amount = Math.min(params.amount, state.data.borrowAToken.balanceOf(msg.sender));
            if (amount > 0) {
@>              state.withdrawUnderlyingTokenFromVariablePool(msg.sender, params.to, amount);
            }
        } else {
            amount = Math.min(params.amount, state.data.collateralToken.balanceOf(msg.sender));
            if (amount > 0) {
                state.withdrawUnderlyingCollateralToken(msg.sender, params.to, amount);
            }
        }

        emit Events.Withdraw(params.token, params.to, amount);
    }

From the code above we know that whether we take underlyingCollateralToken or underlyingBorrowToken will check validateUserIsNotBelowOpeningLimitBorrowCR() ==> collateralRatio() > openingLimitBorrowCR.

This makes sense for taking underlyingCollateralToken, but not for taking underlyingBorrowToken.

  1. Taking the underlyingBorrowToken does not affect the collateralRatio.
  2. The user has already borrowed the funds (with interest accrued and collateralized), it is the user’s asset, and should be able to be withdrawn at will, even if it may be liquidated.
  3. openingLimitBorrowCR is still far from being liquidated, and should not restrict the user from withdrawing the borrowed token.

Impact

If the token is already borrowed, just stored in Size and not yet taken, but due to a slight price fluctuation, validateUserIsNotBelowOpeningLimitBorrowCR() fails but is still far from being liquidated, the user may not be able to take the borrowed token, resulting in the possibility that the user may not be able to withdraw the borrowed funds.

Recommended Mitigation

    function withdraw(WithdrawParams calldata params) external payable override(ISize) whenNotPaused {
        state.validateWithdraw(params);
        state.executeWithdraw(params);
+       if (params.token != address(state.data.underlyingBorrowToken)  {
+             state.validateUserIsNotBelowOpeningLimitBorrowCR(msg.sender);
+       }
    }

Assessed type

Context

aviggiano (Size) confirmed and commented:

This is a valid bug and the report is good.

Fixed in https://github.com/SizeCredit/size-solidity/pull/124.

samuraii77 (warden) commented:

Hi, this issue should be of high severity. The likelihood is very high, imagine a user borrows $1000 and gives $1500 as collateral; his collateral ratio is now 1.5e18. If the price of ETH drops by just 1 cent (or even less), his borrowed funds will be locked. The impact is also high as well.

hansfriese (judge) commented:

I still believe Medium is appropriate for the following reasons:

  • The user can withdraw the borrowed funds at the time of borrowing using a multicall.
  • The user can still use the borrowed funds to repay their loan.
  • There is no fund loss; the funds are simply locked for a while.

The original report as published on Code4rena, by bin2chen. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 withdraw function applies the collateral ratio check to both collateral and borrowed token withdrawals, but it should only apply to collateral withdrawals.

Impact

Users may be unable to withdraw their borrowed tokens if a slight price fluctuation causes the collateral ratio check to fail, even though they are far from liquidation.

What the attacker needed

User has borrowed tokens; slight price fluctuation

The fix

The check should only be applied when withdrawing collateral tokens, not borrowed tokens.

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 a lending system, users deposit collateral and can borrow other tokens. The system checks that the collateral is sufficient relative to the debt. The bug is that this check is also applied when withdrawing borrowed tokens, which is unnecessary and can lock funds.

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 bin2chen

View all

139 more disclosed reports, best first.

More from Size

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.