The following flow of events (one among many) causes a revert:
Alice calls depositExactAmountETH() to deposit 1 ether. This executes successfully, as expected.
Bob calls depositExactAmountETH() to deposit 1.5 ether (or 0.5 ether or 1 ether or 2 ether or any other value). This reverts unexpectedly.
In case Bob was attempting to make this deposit to rescue his soon-to-become or already bad debt and to avoid liquidation, this revert will delay his attempt which could well be enough for him to be liquidated by any liquidator, causing loss of funds for Bob. Here’s a concrete example with numbers:
Bob calls depositExactAmountETH() to deposit 1 ether. This executes successfully, as expected.
Bob calls borrowExactAmountETH() to borrow 0.7 ether. This executes successfully, as expected.
Bob can see that price is soon going to spiral downward and cause a bad debt. He plans to deposit some additional collateral to safeguard himself. He calls depositExactAmountETH() again to deposit 0.5 ether. This reverts unexpectedly.
Prices go down and he is liquidated.
Scenario 2:
A similar revert occurs when the following flow of events occur:
Alice calls depositExactAmountETH() to deposit 10 ether. This executes successfully, as expected.
Bob calls withdrawExactAmountETH() to withdraw 10 ether (or 10 ether - 1 or 10 ether - 1000 or 9.5 ether or 9.1 ether). This reverts unexpectedly.
Bob is not able to withdraw his entire deposit. If he leaves behind 1 ether and withdraws only 9 ether, then he does not face a revert.
In both of the above cases, eventually the revert is caused by the validation failure on L234-L237 due to the check inside _validateParameter():
The _getSharePrice() functions uses this lendingPoolData[_poolToken].totalDepositShares variable in the denominator on L185-187 and in many cases, returns an increased value (in this case it evaluates to 1000000000000000001) which is captured in the variable lendSharePriceAfter inside _compareSharePrices().
Circling back to our first step, this causes the validation to fail on L234-L237 inside _compareSharePrices() since the lendSharePriceAfter is now greater than currentSharePriceMax i.e. 1000000000000000001 > 1000000000000000000. Hence, the transaction reverts.
The reduction by 1 inside _calculateShares() is done by the protocol in its own favour to safeguard itself. The lendingPoolData[_poolToken].pseudoTotalPool; however, is never modified. This mismatch eventually reaches a significant divergence, and is the root cause of these reverts.
See:
The last comment inside the Proof of Concept-2 (Withdraw scenario) section later in the report.
Option1 inside the Recommended Mitigation Steps section later in the report.
Add the following tests inside contracts/WisenLendingShutdown.t.sol and run via forge test --fork-url mainnet -vvvv --mt test_t0x1c_DepositsRevert to see the tests fail.
function test_t0x1c_DepositsRevert_Simple()
public
{
uint256 nftId;
nftId = POSITION_NFTS_INSTANCE.mintPosition();
LENDING_INSTANCE.depositExactAmountETH{value: 1 ether}(nftId); // @audit-info : If you want to make the test pass, change this to `2 ether`
address bob = makeAddr("Bob");
vm.deal(bob, 10 ether); // give some ETH to Bob
vm.startPrank(bob);
uint256 nftId_bob = POSITION_NFTS_INSTANCE.mintPosition();
LENDING_INSTANCE.depositExactAmountETH{value: 1.5 ether}(nftId_bob); // @audit : REVERTS incorrectly (reverts for numerous values like `0.5 ether`, `1 ether`, `2 ether`, etc.)
}
function test_t0x1c_DepositsRevert_With_Borrow()
public
{
address bob = makeAddr("Bob");
vm.deal(bob, 10 ether); // give some ETH to Bob
vm.startPrank(bob);
uint256 nftId = POSITION_NFTS_INSTANCE.mintPosition();
LENDING_INSTANCE.depositExactAmountETH{value: 1 ether}(nftId); // @audit-info : If you want to make the test pass, change this to `2 ether`
LENDING_INSTANCE.borrowExactAmountETH(nftId, 0.7 ether);
LENDING_INSTANCE.depositExactAmountETH{value: 0.5 ether}(nftId); // @audit : REVERTS incorrectly; Bob can't deposit additional collateral to save himself
}
If you want to check with values which make the test pass, change the following line in both the tests and run again:
- LENDING_INSTANCE.depositExactAmountETH{value: 1 ether}(nftId); // @audit-info : If you want to make the test pass, change this to `2 ether`
+ LENDING_INSTANCE.depositExactAmountETH{value: 2 ether}(nftId); // @audit-info : If you want to make the test pass, change this to `2 ether`
There are numerous combinations which will cause such a “revert” scenario to occur. Just to provide another example:
Four initial deposits are made in either Style1 or Style2:
- Style1:
- Alice makes 4 deposits of `2.5 ether` each. Total deposits made by Alice = 4 /ast 2.5 ether = 10 ether.
- Style2:
- Alice makes a deposit of `2.5 ether`
- Bob makes a deposit of `2.5 ether`
- Carol makes a deposit of `2.5 ether`
- Dan makes a deposit of `2.5 ether`. Total deposits made by 4 users = 4 /ast 2.5 ether = 10 ether.
Now, Emily tries to make a deposit of 2.5 ether. This reverts.
Withdraw scenario:
Add the following test inside contracts/WisenLendingShutdown.t.sol and run via forge test --fork-url mainnet -vvvv --mt test_t0x1c_WithdrawRevert to see the test fail.
function test_t0x1c_WithdrawRevert()
public
{
address bob = makeAddr("Bob");
vm.deal(bob, 100 ether); // give some ETH to Bob
vm.startPrank(bob);
uint256 nftId = POSITION_NFTS_INSTANCE.mintPosition();
LENDING_INSTANCE.depositExactAmountETH{value: 10 ether}(nftId);
LENDING_INSTANCE.withdrawExactAmountETH(nftId, 9.1 ether); // @audit : Reverts incorrectly for all values greater than `9 ether`.
}
If you want to check with values which make the test pass, change the following line of the test case like shown below and run again:
- LENDING_INSTANCE.withdrawExactAmountETH(nftId, 9.1 ether); // @audit : Reverts incorrectly for all values greater than `9 ether`.
+ LENDING_INSTANCE.withdrawExactAmountETH(nftId, 9 ether); // @audit : Reverts incorrectly for all values greater than `9 ether`.
This failure happened because the moment lendingPoolData[_poolToken].pseudoTotalPool and lendingPoolData[_poolToken].totalDepositShares go below 1 ether, their divergence is significant enough to result in lendSharePrice being calculated as greater than 1000000000000000000 or 1 ether:
Which in this case, evaluates to 1000000000000000001. This brings us back to our root cause of failure. Due to the divergence, lendSharePrice of 1000000000000000001 has become greater than currentSharePriceMax of 1000000000000000000 and fails the validation on L234-L237 inside _compareSharePrices().
High likelihood, as it’s possible for a huge number of value combinations, as shown above.
If user is trying to save his collateral, this is High severity. Otherwise he can try later with modified values making it a Medium severity.
Tools Used
Foundry
Recommended Mitigation Steps
Since the reduction by 1 inside _calculateShares() is being done to round-down in favour of the protocol, removing that without a deeper analysis could prove to be risky as it may open up other attack vectors. Still, two points come to mind which can be explored:
Option1: Reducing lendingPoolData[_poolToken].pseudoTotalPool too would keep it in sync with lendingPoolData[_poolToken].totalDepositShares and will avoid the current issue.
Option2: Not reducing it by 1 seems to solve the immediate problem at hand (needs further impact analysis):
If you remove -1 then it opens other attacks, so it is not justified suggestion. To qualify this for a finding I will let @vonMangoldt give his opinion for these details.
@Trust - Could you please expand on your reasoning behind this? As I mentioned in the report, the frequency/likelihood at which this happens currently is very high which I supported by various random examples.
To strengthen my case, here are additional example flows of events which cause a revert for Bob when he tries to save his collateral by depositing additional amount. I have even added more actors so that it can mimic a real world scenario even more closely than before.
I am also supplementing Scenario 1 and Scenario 2 (below) of the table with coded PoCs (very similar to the one I already provided in my report), just in case it helps to run it and see the scenario in action. Due to these reasons, I believe the vulnerability should qualify as a High. Requesting you to re-assess.
There is a huge number of combinations which would cause a revert, but considering the vast space of uint256 that number is actually small. Eventually, the issue is called by a rounding which is off by one, and is easily fixed in a repeat transaction. High would be an overstatement.
The original report as published on Code4rena, by t0x1c. 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
What is known
A medium-severity business logic bug in Wise Lending. Code4rena made it public on 3 May 2024. Found in the 2024-02-wise-lending audit contest (finding M-17), also found by 1 other warden.
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.