Skip to content
Business logic Audit finding Advanced

Users pay higher fee than intended

The protocol mints too many deposit shares to the reserve when accruing interest, because the reserve share is calculated by dividing by the pre-interest deposit amount instead of the post-interest amount. This causes the reserve fee to be slightly higher than intended, and lenders pay the extra fee.

T1MOH Follow

Originally disclosed on Code4rena

Submitted by T1MOH

Protocol mints incorrect depositAmount and depositShare to protocol. Such that reserveFee is higher than defined. Suppose following scenario:

  1. Tranche 2 has 20% APR, has 5_000 borrowed
  2. Tranche 1 has 10% APR, has 10_000 borrowed
  3. ReserveFee is 10%
  4. It means that reserveFee that must be paid after 1 year is 5_000 * 20% * 10% + 10_000 * 10% * 10% = 200. However current implementation will calculate 208, which will be shown PoC.

Lender pays this extra fee.

Proof of Concept

Here is gist with all tests https://gist.github.com/T1MOH593/34729b5333fe43eb58cf8b4948ef137f

First part: Show that issue exists. It is shown in custom_test1().

Second part: Explaining the issue.

Let’s set initial values, for example:

Tranche 2 has 20% APR, 5_000 borrowed, 20_000 deposited; Tranche 1 has 10% APR, 10_000 borrowed, 20_000 depositeed. And we use 1 year as time difference to ease calculations.

Tranche struct has 2 types of variables, let’s focus regarding deposit: totalDepositAmount and totalDepositShare. Initially without accrued interest they equal, 1 : 1, in above scenario will be 20_000 in both tranches. As the time passes, interest accrues. It means that totalDepositAmount increases. If reserveFee is 0%, that’s it - for example 1000 of interests accrued, totalDepositAmount is increased by 1000.

However in case 10% of interest must be paid to reserve. It means that extra shares will be minted increasing totalDepositShare AND totalDepositAmount should be also increased because otherwise user’s balance will decrease. Let’s take delta of totalDepositAmount as X and totalDepositShare as Y. And calculate these values for above scenario.

  • Total interest for Tranche2 is 5_000 * 20% = 1_000. For Tranche1 is 10_000 * 10% = 1_000
  • Interest for Tranche 2 stays in this tranche. Interest for Tranche 1 is divided between tranche1 and tranche2 based on deposit amounts, such that tranche1 receives 1000 * 20_000 / 40_000 = 500, tranche2 receives 500 too. Finally tranche2 has 1500 of interest, tranche1 has 500 of interest
  • Reserve fee must be paid. 1500 * 10% = 150 in tranche2, and 500 * 10% = 50 in tranche1
  • We need to mint such amount of share Y to Reserve and increase totalDepositAmount by X so that Reserve balance is 150 AND User’s balance is 20_000 + 1500 * 90% = 21350 in Tranche2. So let’s create system of equations.
(20_000 / (20_000 + Y)) * (20_000 + X) = 21_350 is balance of User
Y / (20_000 + Y) * (20_000 + X) = 150 is balance of Reserve

Solving this we get X = 1500, Y = 140.51. I.e. 140.51 of shares must be minted to Reserve, and totalDepositAmount must be increased by 1500 (amount of accrued interest) in Tranche2. Now let’s calculate accordingly for Tranche1:

(20_000 / (20_000 + Y)) * (20_000 + X) = 20_450 is balance of User
Y / (20_000 + Y) * (20_000 + X) = 50 is balance of Reserve

X = 500, Y = 48.89.

However current implementation mints 100 shares to Reserve and increases totalDepositAmount by 1450 in Tranche 2; Also mints 100 shares to reserve and increases totalDepositAmount by 550 in Tranche1 as shown in test custom_test2

This is the core issue: Code always mints amount of shares equal to reserveInterest to Reserve (100 in this example) and calculates sub-optimal amount of depositAmount to increase.

Tools Used

Foundry

Recommended Mitigation Steps

Fix is not obvious at all, it’s the most difficult part of report to be honest. Algorithm should be completely refactored, should be implemented following accounting:

  • totalDepositAmount should be increased by the amount of accrued interest allocated to this tranche. From above example it’s 1500 for Tranche2 and 500 for Tranche1
  • Number of shares minted to reserve in tranche should be calculated as solution of one of these equations
1) (totalDepositSharesOfTranceBeforeAccruing / (totalDepositSharesOfTranceBeforeAccruing + Y)) * (totalDepositAmountBeforeAccruing + interestAmountAllocatedToThisTrance) = totalDepositSharesOfTranceBeforeAccruing + interestAmountAllocatedToThisTrance * 0.9
If we paste values from above example with Tranche 2, we get (20_000 / (20_000 + Y)) * (20_000 + 1_500) = 21_350
2) (Y / (totalDepositSharesOfTranceBeforeAccruing + Y)) * (totalDepositAmountBeforeAccruing + interestAmountAllocatedToThisTrance) = interestAmountAllocatedToThisTrance * 0.9
If we paste values from above example with Tranche 2, we get (Y / (20_000 + Y)) * (20_000 + 1_500) = 150

And also write tests to check reserveFee amounts, now they absent

cccz (Judge) commented:

I’ve noticed this issue before, the reason for it is that the reserveShare is calculated by dividing by trancheDepositAmount_ instead of trancheDepositAmount_+interestAmountProportion, so the calculated reserveShare will be larger.

                tranche.totalDepositAmount = trancheDepositAmount_ + interestAmountProportion + reserveInterestAmount;
                tranche.totalBorrowAmount = trancheBorrowAmount_ + depositInterestAmount + reserveInterestAmount;
            }

            // Pay reserve fee
            uint256 reserveShare;
            uint256 totalDepositShare_ = tranche.totalDepositShare;
            if (trancheDepositAmount_ == 0) {
                reserveShare = reserveInterestAmount;
            } else {
                reserveShare = (reserveInterestAmount * totalDepositShare_) / trancheDepositAmount_; // Cannot divide by 0
            }
            trancheAccountDepositShares[trancheIndex][reserveReceiver] += reserveShare;
            tranche.totalDepositShare = totalDepositShare_ + reserveShare;

A simple example is

Tranche 1 has 5% APR, has 10000 borrowed
ReserveFee is 10%.
interestAmount = 10000 * 5% = 500
reserveInterestAmount = 500 * 10% = 50
interestAmountProportion = 500 * 90% = 450

cur: reserveShare = 50 * 10000 / 10000 = 50

 reserveAmount = 50 * (10000+500) / (1000+50) = 52.24

fix: reserveShare = 50 * 10000 / (10000+450) = 47.847

 reserveAmount = 47.847 * (10000+500) / (10000+47.847) = 50

I previously thought this was intentional because it was to handle the case where interestAmountProportion wouldn’t be distributed when trancheDepositAmount_ = 0. But thinking about it again, it’s just a simple fix

            if (trancheDepositAmount_ == 0) {
-               reserveShare = reserveInterestAmount;
+               reserveShare = reserveInterestAmount + interestAmountProportion;
            } else {
-               reserveShare = (reserveInterestAmount * totalDepositShare_) / trancheDepositAmount_; // Cannot divide by 0
+               reserveShare = (reserveInterestAmount * totalDepositShare_) / trancheDepositAmount_ + interestAmountProportion; // Cannot divide by 0
            }

allenjlee (BetaFinance) confirmed and commented:

I believe this is a valid issue, we have resolved the issue and reserve payments are correct now. See screenshot from the PoC test case.


The original report as published on Code4rena, by T1MOH. 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.

How it worked

Root cause

Likely the reserve share is calculated by dividing by the tranche deposit amount before interest is added, rather than after, so the share is too large.

Impact

Lenders receive slightly less than they should because the reserve takes a larger fee than defined; the excess is small but real.

What the attacker needed

Interest accrues in a tranche; reserve fee is non-zero

The fix

The reserve share calculation should divide by the deposit amount plus the interest proportion, and tests should verify reserve fee amounts.

How it was found

  1. Set up two tranches with different interest rates and borrowed amounts, and a reserve fee.
  2. Let interest accrue over a period.
  3. Observe that the reserve receives more shares than the correct amount, and the lender's balance is lower than expected.

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.

When interest is earned, a small cut goes to the protocol's reserve. The code gives the reserve a bit too many shares, so the reserve ends up with more money than it should. The extra comes out of the lenders' pockets.

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.

More by T1MOH

View all

11 more disclosed reports, best first.

More from Beta Finance Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.