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.
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.
- Taking the
underlyingBorrowTokendoes not affect thecollateralRatio. - 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.
openingLimitBorrowCRis 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
$1000and gives$1500as collateral; his collateral ratio is now1.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.
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.
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.