Fees aren't distributed properly for positions with multiple lenders, causing loss of funds for lenders
Fees owed to lenders are calculated based on each lender's share of the total borrowed amount, so when a borrower increases an existing position with a new loan, the new lender receives a portion of previously accrued fees. This causes older lenders to lose fees, and a borrower could exploit it to steal fees by opening a large loan with a colluding lender.
0xDetermination Follow with zraxx
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-02-leverage-contracts-judging/issues/41
Found by
0xDetermination, zraxx
Summary
Fees distributed are calculated according to a lender's amount lent divided by the total amount lent, which causes more recent lenders to steal fees from older lenders.
Vulnerability Detail
The fees distributed to each lender are determined by the following calculation (https://github.com/sherlock-audit/2024-02-leverage-contracts/blob/main/wagmi-leverage/contracts/LiquidityBorrowingManager.sol#L546-L549):
uint256 feesAmt = FullMath.mulDiv(feesOwed, cache.holdTokenDebt, borrowedAmount); //fees owed multiplied by the individual amount lent, divided by the total amount lent
...
loansFeesInfo[creditor][cache.holdToken] += feesAmt;
harvestedAmt += feesAmt;
The above is from harvest(); repay() calculates the fees similarly. Because borrow() doesn't distribute fees, the following scenario will occur when a borrower increases an existing position:
- Borrower has an existing position with fees not yet collected by the lenders.
- Borrower increases the position with a loan from a new lender.
harvest()orrepay()is called, and the new lender is credited with some of the previous fees earned by the other lenders due to the fees calculation. Other lenders lose fees.
This scenario can naturally occur during the normal functioning of the protocol, or a borrower/attacker with a position with a large amount of uncollected fees can maliciously open a proportionally large loan with an attacker to steal most of the fees.
Also note that ANY UDPATE ISSUE? LOW PRIO
Impact
Loss of funds for lenders, potential for borrowers to steal fees.
Code Snippet
Tool used
Manual Review
Recommendation
A potential fix is to harvest fees in the borrow() function; the scenario above will no longer be possible.
Discussion
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/RealWagmi/wagmi-leverage/commit/84416fcedfcc7eb062917bdc69f919bba9d3c0b7.
fann95
Yes, the problem existed and is associated with the same error as #39. This issue is related to an erroneous scheme for accumulating fees and affected almost all functions in the contract, so the PR turned out to be quite large.
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid: high(1)
nevillehuang
See comments here
0xDetermination
Fix looks good, a new internal function _harvest() has been added and is called when new borrows are added to an existing position.
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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
Likely the fee distribution formula uses the current total borrowed amount without first settling accrued fees, so new lenders are credited with a share of fees earned before they joined.
Impact
Lenders lose funds, and a borrower could steal fees by opening a large loan with a colluding lender; the text does not mention any defence that limited the exploit.
What the attacker needed
Borrower has an existing position with uncollected fees; borrower can add a new loan from another lender
The fix
The issue was fixed by harvesting fees in the borrow function, so new borrows no longer dilute previously accrued fees.
How it was found
- A borrower has an existing position with fees not yet collected by the lenders.
- The borrower increases the position with a loan from a new lender.
- `harvest()` or `repay()` is called, and the new lender is credited with some of the previous fees.
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 protocol, borrowers pay fees that are shared among lenders. This report shows that the way fees are split is flawed: if a borrower takes a new loan from a new lender while old fees are still unpaid, the new lender gets a cut of those old fees. That means earlier lenders get less than they should, and someone could deliberately game the system to take fees from others.
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.