Frequency-dependent `TaxCalculator.sol::calculateCompoundedFactor` leads to interest loss either for users or for protocol
The interest calculation uses a discrete compounding formula that depends on how often collection operations occur, so users can be charged up to 71% more interest or the protocol can receive up to 42% less interest than expected. An attacker can also deliberately trigger frequent operations to force excessive interest on other users of the same collection.
Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/117
Found by
kuprum
Summary
TaxCalculator.sol::calculateCompoundedFactor uses discrete formula for calculating the compounded factor. Combined with the wrong divisor (1000 instead of 10_000, as I outline in another finding), when collection's utilization factor is moderately high (e.g. 90%), and the operations with the collection happen relatively frequently (e.g. every day), this leads to charging users excessive interest rates: for this example, 3200% more than expected.
If the divisor is fixed to the correct value (10_000), then the effects from using the discrete formula are less severe, but still quite substantial: namely for a 100% collection utilization, depending of the frequency, either the user will be charged up to $e-2 \approx$ 71% more interest per year compared to the non-compound interest, or the protocol will receive up to $1- 1/(e-1) \approx$ 42% less interest per year compared to the compound interest.
It's worth noting that calculateCompoundedFactor is called whenever a new checkpoint is created for the collection, which happens e.g. when any user creates a listing, cancels a listing, or fills a listing. This is neither enough to reach the desired precision, nor is it gas efficient.
Depending on the frequency of collection operations, either:
- If non-compound interest is expected, but the frequency is high, then the users will be charged up to
71%more interest than they expect; - If compound interest is expected, but the frequency is low, then the protocol will receive up to
42%less interest than it expects.
Root Cause
TaxCalculator.sol::calculateCompoundedFactor employs the following formula:
compoundedFactor_ = _previousCompoundedFactor * (1e18 + (perSecondRate / 1000 * _timePeriod)) / 1e18;
As I explain in another finding, the divisor 1000 is incorrect, and has to be fixed to 10_000. Provided this is fixed, the resulting formula is the correct discrete formula for calculating the compounded interest. The problem is that the formula will give vastly different results depending on the frequency of operations which have nothing to do with the user who holds the protected listing.
Internal pre-conditions
Varying frequency of collection operations.
External pre-conditions
none
Attack Path
No attack is necessary. The interest rates will be wrongly calculated in most cases.
Impact
Either users are charged up to 71% more interest than they expect, or the protocol receives up to 42% less interest than it expects.
PoC
Drop this test to TaxCalculator.t.sol, and execute with forge test --match-test test_WrongInterestCalculation
// This test uses the unmodified source code, with the wrong divisor of 1000
function test_WrongInterestCalculation() public view {
// We fix collection utilization to 90%
uint utilization = 0.9 ether;
// New checkpoints with the updated compoundedFactor are created
// and stored whenever there is activity wrt. the collection
// The expected interest multiplier after 1 year
uint expectedFactor =
taxCalculator.calculateCompoundedFactor(1 ether, utilization, 365 days);
// The resulting interest multiplier if some activity happens every day
uint compoundedFactor = 1 ether;
for (uint time = 0; time < 365 days; time += 1 days) {
compoundedFactor =
taxCalculator.calculateCompoundedFactor(compoundedFactor, utilization, 1 days);
}
// The user loss due to the activity which doesn't concern them is 3200%
assertApproxEqRel(
33 ether * expectedFactor / 1 ether,
compoundedFactor,
0.01 ether);
}
Mitigation
Variant 1: If non-compound interest is desired, apply this diff:
diff --git a/flayer/src/contracts/TaxCalculator.sol b/flayer/src/contracts/TaxCalculator.sol
index 915c0ff..4031aba 100644
--- a/flayer/src/contracts/TaxCalculator.sol
+++ b/flayer/src/contracts/TaxCalculator.sol
@@ -87,7 +87,7 @@ contract TaxCalculator is ITaxCalculator {
uint perSecondRate = (interestRate * 1e18) / (365 * 24 * 60 * 60);
// Calculate new compounded factor
- compoundedFactor_ = _previousCompoundedFactor * (1e18 + (perSecondRate / 1000 * _timePeriod)) / 1e18;
+ compoundedFactor_ = _previousCompoundedFactor + (perSecondRate * _timePeriod / 1000);
}
/**
Note: in the above the divisor is still unfixed, as this belongs to a different finding.
Variant 2: If compound interest is desired, employ either periodic per-second compounding, or continuous compounding with exponentiation. Any of these approaches are precise enough and much more gas efficient than the current one, but require substantial refactoring.
Discussion
kuprumxyz
For clarification of this finding impact (for future reference), I add here a some more data that I've demonstrated during the judging period.
Texted PoC: Unfair interest rate can be enforced on users
Suppose there are two collections, A and B, with the same utilization rate, i.e. the interest rate charged for them should be the same. A malicious user wants to inflict excess interest rate on the users of collection B, and repeatedly does createListings / cancelListings for an NFT from collection B. The only thing a malicious user loses is paying gas fees which are negligible; but the effect of the attack is that all users of collection B pay up to 71\% more interest rate than users of collection A, though they should pay the same.
Interest rate and compounded factor are calculated _per collection_, and all the logic described in this finding applies _per collection_. I.e. only the activity (or inactivity) per collection is relevant for the frequency-dependent calculation of interest rates. It may well be the case that some collections are used frequently, and the protocol may be perfectly healthy and well-working, but for infrequently used collections either users or the protocol suffer the losses described.
## The updated coded PoC
Some Watsons have raised a concern that the PoC supplied with the finding shows only the loss of 11% if the wrong divisor vulnerability is fixed. The point is that the PoC supplied with this finding is made specifically for the case when it's unfixed. It also employs 90% utilization rate, and compares the principal sum + interest, instead of only the interest without principal sum, how it should be. Below we supply the updated PoC which addresses this.
Please fix the wrong divisor vulnerability as described, place the below PoC to TaxCalculator.t.sol, and execute with forge test --match-test test\_WrongInterestCalculation.
function test\_WrongInterestCalculation() public {
// We fix the utilization to 100\%
uint utilization = 1 ether;
// New checkpoints with the updated compoundedFactor are created
// and stored whenever there is activity wrt. the collection
// The expected interest multiplier after 1 year
uint expectedFactor =
taxCalculator.calculateCompoundedFactor(1 ether, utilization, 365 days);
// The resulting interest multiplier if some activity happens every day
uint compoundedFactor = 1 ether;
for (uint time = 0; time < 365 days; time += 1 days) {
compoundedFactor =
taxCalculator.calculateCompoundedFactor(compoundedFactor, utilization, 1 days);
}
// User interest loss due to the activity which doesn't concern them is 71\%
// We subtract the principal (1 ether) as only the interests must to be compared.
assertApproxEqRel(
1.71 ether * (expectedFactor - 1 ether) / 1 ether,
compoundedFactor - 1 ether,
0.01 ether);
}
## Interest losses calculations
In order to settle the numerical questions once and for all, I've created this spreadsheet, which calculates both user and protocol losses depending on the frequency (the number of compounding intervals per year). The spreadsheet contains more data, but here is the excerpt from it, for 100% interest rate:
| Intervals per year | Interest | Max interest | User interest loss | Protocol interest loss |
|---|---|---|---|---|
| 1 (yearly) | 100.00% | 171.83% | 71.83% | 41.80% |
| 4 (quarterly) | 144.14% | 171.83% | 19.21% | 16.11% |
| 12 (monthly) | 161.30% | 171.83% | 6.52% | 6.13% |
| 52 (weekly) | 169.26% | 171.83% | 1.52% | 1.49% |
| 365 (daily) | 171.46% | 171.83% | 0.22% | 0.22% |
User losses are calculated under the assumption of the PoC: Unfair interest rate can be enforced on users supplied before, i.e. when a malicious user creates additional activity for the collection. Protocol losses don't require any attack, and happen by themselves, due to interest rates being frequency-dependent, and the relatively low collection activity.
It can be seen that with weekly activity _within a specific collection_ (which is not an excessive limitation), the losses exceed 1% which qualifies this finding to be High severity:
Definite loss of funds without (extensive) limitations of external conditions. The loss of the affected party must exceed 1%.
Let me stress this again: Within a _specific protocol_ (one of many), with relevant NFT operations from a _specific NFT collection_ (this finding applies to each NFT collection separately, and only a small subset of NFTs from that collection will be employed within the protocol), moreover, concerning _only listings_ (which is a subset of the protocol functionality), _weekly activity_ (i.e. a listing for that collection being created or filled) is not an excessive limitation. NFTs are not the same market as fungible tokens (e.g. USDC, DAI, etc.): operations with them don't happen frequently.
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 use of a discrete compounding formula that is applied at irregular, activity-dependent intervals instead of a continuous or periodic compounding method, causing the result to vary with the frequency of checkpoints.
Impact
Users may be charged up to 71% more interest than expected, or the protocol may receive up to 42% less interest than expected; a malicious user can force the higher interest on others by repeatedly creating and cancelling listings.
What the attacker needed
None for protocol loss; attacker pays gas to force user loss
The fix
If non-compound interest is desired, change the formula to add simple interest; if compound interest is desired, use periodic per-second compounding or continuous compounding with exponentiation.
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.
Interest on a loan can be calculated in different ways. This code recalculates the interest every time someone does something with the collection, like listing or cancelling an item. Because the formula is not precise, the final interest depends on how often those actions happen, which is unfair and can be manipulated.
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.