code-423n4 / 2024-02-wise-lending-findings

11 stars 8 forks source link

Withdrawing uncollateralized deposits is possible even though the position is in liquidation mode #260

Open c4-bot-1 opened 5 months ago

c4-bot-1 commented 5 months ago

Lines of code

https://github.com/code-423n4/2024-02-wise-lending/blob/main/contracts/WiseSecurity/WiseSecurity.sol#L263-L265 https://github.com/code-423n4/2024-02-wise-lending/blob/main/contracts/WiseSecurity/WiseSecurity.sol#L125-L128

Vulnerability details

Impact

Users can withdraw uncollateralized deposits even though their position is liquidable, as opposed to the README, if the position is in liquidation mode, users should use their uncollateralized deposits to avoid liquidation instead of removing them

Proof of Concept

When withdrawing deposits from public pools, at the end of the tx is executed the WiseLending._healthStateCheck() function, which depending on the value of the powerFarmCheck will determine if the position's collateral is enough to cover the borrows.

When withdrawing an uncollateralized deposit, the WiseCore._coreWithdrawToken() function calls the WiseSecurity.checksWithdraw() function to determine the value of the powerFarmCheck, if the pool from where the tokens are being withdrawn is uncollateralized, the powerFarmCheck will be set to true, which will cause that the WiseLending._healthStateCheck() function uses the bare value of the full collateral to determine if the collateral can cover the existing borrows.

For example, a user requests a withdrawal of an uncollateralized deposit in a position with the below values:

WiseSecurity.sol


function checksWithdraw(
...
)
...
{
...
if (_isUncollateralized(_nftId, _poolToken) == true) {
    return true;
}

...

}

function _getState( uint256 _nftId, bool _powerFarm ) internal view returns (bool) { ...

//@audit-info => If `powerFarmCheck` is true, overalCollateral will be computed using the value of the `bareCollateral`
//@audit-info => If `powerFarmCheck` is false, overalCollateral will be computed using the value of the `weightedCollateral`

uint256 overallCollateral = _powerFarm == true
    ? overallETHCollateralsBare(_nftId)
    : overallETHCollateralsWeighted(_nftId);

//@audit-info => If 95% of the overalCollateral > borrowAmount, withdrawal will be allowed!
return overallCollateral
    * BORROW_PERCENTAGE_CAP
    / PRECISION_FACTOR_E18
    < borrowAmount;

}


  - Now, when using the same values but for a liquidation, we have that the `weightedCollateral` is not enough to cover the borros, thus, the position is liquidable.

> WiseSecurity.sol

function checksLiquidation( ... ) external view { ...

//@audit-info => When doing liquidations, the value of the `weightedCollateral` is used to determine if the position is liquidable or not!
canLiquidate(
    borrowETHTotal,
    weightedCollateralETH
);

...

}


## Tools Used
Manual Audit

## Recommended Mitigation Steps
If the protocol wants to enforce that users use their uncollateralized deposits to avoid liquidations when the positions are liquidable, don't set to `true` the `powerFarmCheck` when doing withdrawals for uncollateralized deposits. Allow the [`WiseLending._healthStateCheck() function`](https://github.com/code-423n4/2024-02-wise-lending/blob/main/contracts/WiseLending.sol#L77-L90) to validate if the position is indeed in liquidation mode by using the `weightedCollateral` instead of the `bareCollateral` value.

> WiseSecurity.sol

function checksWithdraw( .. ) .. { ...

Assessed type

Context

c4-pre-sort commented 5 months ago

GalloDaSballo marked the issue as sufficient quality report

GalloDaSballo commented 5 months ago

Would have been better to have a POC

c4-pre-sort commented 5 months ago

GalloDaSballo marked the issue as primary issue

vonMangoldt commented 5 months ago

Thats no issue since the additional check is reflecting a higher %. We will keep it like that

c4-judge commented 5 months ago

trust1995 marked the issue as satisfactory

c4-judge commented 5 months ago

trust1995 marked the issue as selected for report

thebrittfactor commented 3 months ago

For transparency and per conversation with the sponsors, see here for the Wise Lending team's mitigation.