There is a calculation error inside the calculateCompoundedFactor() function, causing users to overpay interest
A calculation error in the interest compounding function causes users to be charged ten times the intended interest. An attacker could exploit this to cause financial loss to users who overpay interest.
Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/227
Found by
0x37, Sentryx, Tendency, ZeroTrust, dany.armstrong90, dimulski, kuprum, merlinboii, robertodf, stuart_the_minion
Summary
There is a calculation error inside the calculateCompoundedFactor() function, causing users to overpay interest.
Vulnerability Detail
function calculateProtectedInterest(uint _utilizationRate) public pure returns (uint interestRate_) {
// If we haven't reached our kink, then we can just return the base fee
if (_utilizationRate <= UTILIZATION_KINK) {
// Calculate percentage increase for input range 0 to 0.8 ether (2% to 8%)
@>> interestRate_ = 200 + (_utilizationRate * 600) / UTILIZATION_KINK;
}
// If we have passed our kink value, then we need to calculate our additional fee
else {
// Convert value in the range 0.8 to 1 to the respective percentage between 8% and
// 100% and make it accurate to 2 decimal places.
interestRate_ = (((_utilizationRate - UTILIZATION_KINK) * (100 - 8)) / (1 ether - UTILIZATION_KINK) + 8) * 100;
}
}
function calculateCompoundedFactor(uint _previousCompoundedFactor, uint _utilizationRate, uint _timePeriod) public view returns (uint compoundedFactor_) {
// Get our interest rate from our utilization rate
@>> uint interestRate = this.calculateProtectedInterest(_utilizationRate);
// Ensure we calculate the compounded factor with correct precision. `interestRate` is
// in basis points per annum with 1e2 precision and we convert the annual rate to per
// second rate.
uint perSecondRate = (interestRate * 1e18) / (365 * 24 * 60 * 60);
// Calculate new compounded factor
@>> compoundedFactor_ = _previousCompoundedFactor * (1e18 + (perSecondRate / 1000 * _timePeriod)) / 1e18;
}
Through the calculateProtectedInterest() function, which calculates the annual interest rate, we know that 200 represents 2% and 800 represents 8%, so the decimal precision is 4. When the principal is 100 and the annual interest rate is 2% (200), the yearly interest should be calculated as 100 * 200 / 10000 = 2.
However, in the calculateCompoundedFactor function, there is a clear error when calculating compound interest, as it only divides by 1000, leading to the interest being multiplied by a factor of 10.
Impact
The user overpaid interest, resulting in financial loss.
Code Snippet
Tool used
Manual Review
Recommendation
function calculateCompoundedFactor(uint _previousCompoundedFactor, uint _utilizationRate, uint _timePeriod) public view returns (uint compoundedFactor_) {
// Get our interest rate from our utilization rate
uint interestRate = this.calculateProtectedInterest(_utilizationRate);
// Ensure we calculate the compounded factor with correct precision. `interestRate` is
// in basis points per annum with 1e2 precision and we convert the annual rate to per
// second rate.
uint perSecondRate = (interestRate * 1e18) / (365 * 24 * 60 * 60);
// Calculate new compounded factor
- compoundedFactor_ = _previousCompoundedFactor * (1e18 + (perSecondRate / 1000 * _timePeriod)) / 1e18;
+ compoundedFactor_ = _previousCompoundedFactor * (1e18 + (perSecondRate / 10000 * _timePeriod)) / 1e18;
}
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 interest rate is stored with four decimal places (basis points with 1e2 precision), but the compounding calculation divides by 1000 instead of 10000, scaling the interest by a factor of ten.
Impact
Users overpay interest, resulting in financial loss.
What the attacker needed
None
The fix
The calculation should divide by 10000 instead of 1000 to correctly account for the interest rate's precision.
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.
This is a smart contract bug where the code that calculates interest for borrowers has a mistake in the maths. Instead of dividing by the correct number to get the right percentage, it divides by a smaller number, making the interest ten times too high. So borrowers end up paying much more interest than they should.
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.