Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

0xDetermination - Fees aren't distributed properly for positions with multiple lenders, causing loss of funds for lenders #41

Open
sherlock-admin opened this issue Feb 27, 2024 · 6 comments
Labels
Has Duplicates A valid issue with 1+ other issues describing the same vulnerability High A valid High severity issue Reward A payout will be made for this issue Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed

Comments

@sherlock-admin
Copy link
Contributor

sherlock-admin commented Feb 27, 2024

0xDetermination

high

Fees aren't distributed properly for positions with multiple lenders, causing loss of funds for lenders

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:

  1. Borrower has an existing position with fees not yet collected by the lenders.
  2. Borrower increases the position with a loan from a new lender.
  3. harvest() or repay() 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

https://github.com/sherlock-audit/2024-02-leverage-contracts/blob/main/wagmi-leverage/contracts/LiquidityBorrowingManager.sol#L546-L549

Tool used

Manual Review

Recommendation

A potential fix is to harvest fees in the borrow() function; the scenario above will no longer be possible.

@sherlock-admin
Copy link
Contributor Author

The protocol team fixed this issue in PR/commit RealWagmi/wagmi-leverage@84416fc.

@sherlock-admin2 sherlock-admin2 added Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed labels Feb 28, 2024
@fann95
Copy link

fann95 commented Feb 29, 2024

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
Copy link

1 comment(s) were left on this issue during the judging contest.

takarez commented:

valid: high(1)

@nevillehuang
Copy link
Collaborator

See comments here

@sherlock-admin sherlock-admin changed the title Huge Teal Octopus - Fees aren't distributed properly for positions with multiple lenders, causing loss of funds for lenders 0xDetermination - Fees aren't distributed properly for positions with multiple lenders, causing loss of funds for lenders Mar 7, 2024
@sherlock-admin sherlock-admin added the Reward A payout will be made for this issue label Mar 7, 2024
@0xDetermination
Copy link
Collaborator

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
Copy link

The Lead Senior Watson signed off on the fix.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has Duplicates A valid issue with 1+ other issues describing the same vulnerability High A valid High severity issue Reward A payout will be made for this issue Sponsor Confirmed The sponsor acknowledged this issue is valid Will Fix The sponsor confirmed this issue will be fixed
Projects
None yet
Development

No branches or pull requests

6 participants