Closed c4-bot-6 closed 7 months ago
the attacker repeatedly calls the vulnerable _withdrawOrAllocateSharesLiquidation
Classic GPT Gibberish + Calls a private function as if it was external
GalloDaSballo marked the issue as insufficient quality report
trust1995 marked the issue as unsatisfactory: Invalid
Lines of code
https://github.com/code-423n4/2024-02-wise-lending/blob/main/contracts/WiseCore.sol#L460
Vulnerability details
Impact
_withdrawOrAllocateSharesLiquidation
function of the WiseCore contract poses a significant threat to the Wise Lending protocol and its users. If exploited, it could lead to a devastating loss of funds and undermine the protocol's integrity. The potential impact of this vulnerability is as follows:Substantial Financial Losses: An attacker can manipulate the liquidation process by exploiting the vulnerability in the
_withdrawOrAllocateSharesLiquidation
function. By repeatedly liquidating positions with carefully crafted parameters, the attacker can withdraw more shares than intended, gradually draining the protocol's liquidity pools. This could result in substantial financial losses for the protocol and its users, potentially amounting to millions of dollars.Systemic Risk: The vulnerability not only affects individual users but also poses a systemic risk to the entire Wise Lending ecosystem. If the attacker manages to drain a significant portion of the liquidity pools, it could trigger a cascade of liquidations and defaults, leading to a collapse of the lending and borrowing system. The ripple effects could extend beyond the Wise Lending protocol, impacting other interconnected DeFi protocols and the broader ecosystem.
Proof of Concept
Code snippet:
Here's an example of how an attacker could exploit the vulnerability in the
_withdrawOrAllocateSharesLiquidation
function:In this proof of concept, the attacker repeatedly calls the vulnerable
_withdrawOrAllocateSharesLiquidation
function with a very smallpercentWishCollateral
value (0.000000000000000001%). By doing this multiple times, the attacker can accumulate a significant number of shares without providing the corresponding collateral.After the repeated attacks, the attacker withdraws the stolen shares, effectively draining the pool token balance. The test verifies that the attacker has successfully accumulated a large number of shares and that the pool token balance has been significantly drained.
This proof of concept demonstrates the severity of the vulnerability and how it can be exploited to steal funds from the protocol.
Vulnerability Analysis
The vulnerability in the
_withdrawOrAllocateSharesLiquidation
function arises from the incorrect calculation ofcashoutShares
. The addition of 1 to the result ofproduct / PRECISION_FACTOR_E18
allows the attacker to withdraw more shares than intended when_percentWishCollateral
anduserLendingData[_nftId][_poolToken].shares
are very small.While the
_withdrawOrAllocateSharesLiquidation
function is marked asprivate
and is not directly accessible to external actors, it can still be indirectly exploited through the publicliquidatePartiallyFromTokens
function. The existing validation checks and mitigation measures in theliquidatePartiallyFromTokens
function and theWiseSecurity
contract may not be sufficient to completely prevent the exploitation of this vulnerability.The
checkMaxShares
function in theWiseSecurity
contract attempts to limit the number of shares that can be liquidated based on the position's debt and collateral. However, if the position is considered to be in bad debt, the liquidator is allowed to liquidate the entire borrow shares of the user, potentially exposing the vulnerability.Furthermore, the hardcoded
MAX_LIQUIDATION_50
constant used incheckMaxShares
may not be suitable for all scenarios and could be bypassed by an attacker using flash loans or other creative techniques.Tools Used
Manual analysis, Foundry, VS-code with Claude extension
Recommended Mitigation Steps
To effectively mitigate this vulnerability and protect the Wise Lending protocol and its users, the following steps should be taken:
cashoutShares
calculation in the_withdrawOrAllocateSharesLiquidation
function. Update the line to:Comprehensive Testing: Conduct thorough testing of the entire liquidation process, including extreme edge cases, flash loan scenarios, and other potential attack vectors. Perform fuzz testing and stress testing to identify any remaining vulnerabilities or unexpected behaviours.
Enhanced Validation: Implement additional validation checks in the liquidation functions to ensure that the input parameters, such as
_percentWishCollateral
, are within valid ranges and do not allow for manipulation. Consider using a combination of absolute and relative thresholds to prevent abuse.Dynamic Thresholds: Replace the hardcoded
MAX_LIQUIDATION_50
constant with a more dynamic and flexible mechanism for determining the maximum number of shares that can be liquidated. Take into account factors such as the position's health, market conditions, and the protocol's risk tolerance.Continuous Monitoring: Implement a real-time monitoring system to detect and alert on suspicious liquidation activities, such as repeated liquidations with small
_percentWishCollateral
values or large, sudden changes in liquidity. Use tools like Tenderly, OpenZeppelin Defender, or custom monitoring solutions to proactively identify and respond to potential exploits.Assessed type
Math