Skip to content
Audit finding Intermediate

OCC_Modular::applyCombine will round APR down

The APR calculation in OCC_Modular::applyCombine rounds down, allowing a user to reduce the APR on a combined loan by one basis point. This results in a small loss of interest for the protocol.

cergyk Follow with denzi_ and jasonxiale

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/304

The protocol has acknowledged this issue.

Found by

cergyk, denzi_, jasonxiale

Summary

OCC_Modular::applyCombine calculation of APR APR = APR / notional rounds in defavor of the protocol, and a user can game this feature to shave of a point of APR from one of his loans.

Vulnerability Detail

OCC_Modular::applyCombine is used to combine multiple loans into a single loan.

We can see that the APR for the new loan is computed as a weighted average of combined APRs. However since the division rounds down, the APR can be underestimated by 1 point. Since the APR is expressed as BIPS, a point represents a significant amount of interest.

OCC_Modular.sol#L781

Solidity
        APR = APR / notional;

Please note that even in the case where an underwriter has verified off-chain that the APR would not be rounded down before approving a combination, a user can make sure it rounds down by making some payments on his loans

Impact

Loss of funds for the borrowers of the protocol, since a user can reduce APR on his loans by 1 point

Code Snippet

Tool used

Manual Review

Recommendation

Ensure that there is no rounding error in the APR calculation's result by adding a check such as:

Diff
+ require(APR % notional == 0, "rounding");
APR = APR / notional;

Discussion

pseudonaut

Not relevant, also not of concern

sherlock-admin3

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

panprog commented:

invalid, the impact is insignificant and impossible to avoid rounding anyway, so it has to happen anyway

CergyK

Escalate

Issue should have the same severity as #416, since it has exactly the same impact (rounding down on a BIPS precision number).

This has the impact of causing a loss of approx 100$ on a 1M$ loan, which is not negligible. A reasonable fix recommendation is provided to avoid rounding down.

sherlock-admin3

Escalate

Issue should have the same severity as #416, since it has exactly the same impact (rounding down on a BIPS precision number).

This has the impact of causing a loss of approx 100$ on a 1M$ loan, which is not negligible. A reasonable fix recommendation is provided to avoid rounding down.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

panprog

This issue is different from #416, because here it's impossible to avoid rounding due to low precision of APR - so it's either rounded up (user loses funds at the expense of protocol) or rounded down as now (protocol loses funds at the expense of the user). Since it's impossible to avoid funds loss due to APR precision (which is apparently protocol choice), the rounding direction is also a protocol choice. There are no security concerns with either rounding direction here (as opposed to, say, ERC4626, where incorrect rounding direction might cause critical issues in extreme situations).

In #416 the issue is in the order of multiplication and division, which makes it round to the same 0.01% as here, but with the correct order of operations it's possible to avoid this rounding and increase precision. So this is different as it can be fixed and thus can not be considered protocol decision.

omar-ahsan

@panprog with all due respect, every line of code written by devs is a protocol decision on how they want the protocol to act. Watsons have identified a line of code which makes the protocol lose funds. The decision that they have made makes the protocol lose funds, this is a major concern in my opinion.

If it was known to the protocol that rounding down here makes the protocol lose funds and it is acceptable for the protocol to bear this loss then it should have been mentioned in known issues.

Furthermore rounding up in the favor of the protocol is the right decision, the protocol's task is to offer combinations. It's the borrower's decision to accept it or let it expire, Users who do not want to overpay (in case it is rounded up) can simply keep on paying the interests per each payment as before.

The loss of funds is always going to occur here for any magnitude of loans. I believe this should be judged according to the best interest of the protocol as its simply the case of protocol losing funds which they should not after an action.

WangSecurity

Need clarification on this and this comments. LSW says it's a loss of $100 on a $1M loan, which is only 0.0001%, when the lead judge says it's 0.01% as well as on #416. So what is the correct percentage here?

panprog

@WangSecurity

100/1000000=0.0001 = 0.01% APR is in BIPS, 1 BIPS = 1/10000 (0.01%)

WangSecurity

I agree that loss is quite low (0.01%), but I believe it exceeds small and finite amounts, therefore sufficient for medium severity. Planning to accept the escalation and validate the report with medium severity.

Are there other issues which identified this issue?

marchev

@WangSecurity Unfortunately, I don't think I can escalate one of my reports #671. It is not the same issue however it is an issue that identifies an attack path that results in a loss of 0.17% APR for some loans. Given that a loss of 0.01% warrants a valid issue can I please ask you to take a look at #671? 🙏

panprog

@WangSecurity My main point of why it's invalid is because rounding is inevitable and either choice is valid. If this issue is fixed (by rounding up), a new valid issue may be raised that user loses the same amount of funds due to APR rounding up. Why do you think that user must lose this amount, and not protocol (like it is now)? I believe this is the design choice and thus not valid issue.

I can understand that low precision might be the core reason, but this, again, is a design choice to limit APR to 1/10000 precision.

So I disagree because I think this is simply design choice, not because the amount lost is too small.

If you do decide to validate this as medium, here are dups: #681, #684

panprog

To add on - this issue recommends to revert if there is any rounding error at all (only make it work if it divides exactly), but I believe that this is, again, design decision (and in my opinion not the best one - why limit user to only specific percentages?).

Since combinations are approved by admin, he should simply not approve combines which might lead to bad combined loan APR. So if the recommendation of this issue is implemented, then this means that this issue is simply admin mistake who approves the loan combine he it trusted to not approve.

CergyK

To add on - this issue recommends to revert if there is any rounding error at all (only make it work if it divides exactly), but I believe that this is, again, design decision (and in my opinion not the best one - why limit user to only specific percentages?).

Since combinations are approved by admin, he should simply not approve combines which might lead to bad combined loan APR. So if the recommendation of this issue is implemented, then this means that this issue is simply admin mistake who approves the loan combine he it trusted to not approve.

That's an interesting point, however please notice that even if underwriter approves a combine which does not lead to precision loss, a user can still put it into rounding down state by making a repayment on a loan

omar-ahsan

If this issue is fixed (by rounding up), a new valid issue may be raised that user loses the same amount of funds due to APR rounding up. Why do you think that user must lose this amount, and not protocol (like it is now)? I believe this is the design choice and thus not valid issue.

The new valid issue which has been mentioned can be considered design decision as the protocol should not be losing funds via interests which they were receiving before combination is applied. Further more in situations like these, rounding up is considered in favor of the protocol.

The protocol should not be taking accruing losses per every approved combination, Consider 100 approved combinations and 100 borrowers accepts them, the protocol is now taking a loss per every combination, in the case of user, it is one single user overpaying a small amount per their combination. Borrowers will always accept the combination if they know they will have to pay less. The protocol suffers more here than each single user so this issue should be judged in the best interest of the protocol.

Its a two step process, The underwriter has to approve and then the user has to accept, If the APR is rounded up and if the borrower does not want to overpay in exchange of combining their loan then they can let the combination expire and keep on paying in separate transactions.

WangSecurity

Very insightful comments and I think I confused you a bit with my wording. Firstly, I don't think the mitigation should effect the validity of the report. Secondly, great point about admin mistake, but as LSW said here can make it round down. But I actually would like to clarify if it can be done intentionally and asking to give a small numeric example? Thirdly, I agree that it's design to make such low precision, but if it can be done intentionally, leading to other users lose funds. Hence, I may reconsider my decision after getting the answer if it can be done intentionally.

CergyK

Very insightful comments and I think I confused you a bit with my wording. Firstly, I don't think the mitigation should effect the validity of the report. Secondly, great point about admin mistake, but as LSW said here can make it round down. But I actually would like to clarify if it can be done intentionally and asking to give a small numeric example? Thirdly, I agree that it's design to make such low precision, but if it can be done intentionally, leading to other users lose funds. Hence, I may reconsider my decision after getting the answer if it can be done intentionally.

Yes a user can make a few repayments intentionally to make the combine round down again. The case in which the combine does not round down is extremely specific, so any small deviation of the values will lead it to round down again

Let's take an example:

Loan A: 100 repayments left 100 principal 20 APR

Loan B: 100 repayments left 100 principal 50 APR

a combination of these loans would yield an APR: (50x100+20x100)/(100+100)=35

without losing precision, because the division is exact

However once the combine is approved, the user can repay one payment on loan B (always can do this action):

Loan B: 99 repayments left 99 principal 50 APR

now the combination yields: (50x99+20x100)/(100+99) = 34

Whereas the parameters of the loans have barely changed

WangSecurity

Therefore, I agree that this is the design decision to have small precision, but with the fact that it allows a user to intentionally make APR round down causing a loss of funds. Hence, I believe it's suitable for medium severity. Planning to accept the escalation and validate the report with medium severity.

panprog

Agree that as the user can influence this rounding in his favor, then this is a valid medium.

Evert0x

Result: Medium Unique

sherlock-admin4

Escalations have been resolved successfully!

Escalation status:

omar-ahsan

@Evert0x I think you have mistakenly set this as unique, The lead judge has identified #681 and #684 as duplicates in his comment here

The original report as published on Sherlock, by cergyk. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 APR calculation uses integer division that truncates, and the code does not round up or check for exact division.

Impact

The protocol could lose a small amount of interest (about 0.01% of the loan amount) because the APR is rounded down.

What the attacker needed

Two or more loans; underwriter approval of combination; user can make repayments

The fix

The code should be fixed to avoid rounding down, for example by rounding up or requiring exact division.

How it was found

  1. Have two loans with different APRs and principals.
  2. Request to combine them; the APR is computed as a weighted average and rounded down.
  3. After approval, make a repayment on one loan to change the principal slightly.
  4. The combination now rounds down again, reducing the APR by one basis point.

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 combining loans, the new interest rate is calculated by dividing a total by the loan amount. Because the result is rounded down, the borrower can pay slightly less interest than they should. This means the protocol loses a tiny amount of money.

More by cergyk

View all

29 more disclosed reports, best first.

More from Zivoe

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.