Penalty rate is used for pre-maturity date as well
When interest is added after the maturity date, the penalty rate is applied to the entire period since the last interest update, including the time before maturity. This causes borrowers to be charged more interest than intended.
Submitted by 0xA5DF, also found by 0x52 and rbserver
When _addInterest() is called post maturity, the penalty rate is applied since the last time _addInterest() was called, including the period before maturity.
Impact
Borrowers will be charged with higher interest rates, when maturity date passes. The longer the time since _addInterest() was last called before maturity date the more extra-interest they will be charged.
Sidenote: I think this should be considered High, since it comes at the cost of the assets of the borrowers (even though it stems from unnecessary fees being charged, similar to this Putty bug).
Proof of Concept
The test compares 2 scenarios, the 1st one where addInterest() isn’t called for 30 days before maturity date, and in the 2nd it isn’t called just for 1 day before.
The debt (not interest) in the first scenario would be ~50% higher than the second, see test output below.
At src/test/e2e/BorrowPairTest.t.sol I’ve modified _fuzzySetupBorrowToken() to this and then ran source .env && forge test --fork-url $MAINNET_URL --fork-block-number $DEFAULT_FORK_BLOCK -m testFuzzyMaxLTVBorrowToken -vvv. And I’ve also modified src/test/e2e/BasePairTest.sol a bit so that each pair can have a different name for salt (see diff below).
function _fuzzySetupBorrowToken(
uint256 _minInterest,
uint256 _vertexInterest,
uint256 _maxInterest,
uint256 _vertexUtilization,
uint256 _maxLTV,
uint256 _liquidationFee,
uint256 _priceTop,
uint256 _priceDiv
) public {
asset = IERC20(FIL_ERC20);
collateral = IERC20(MKR_ERC20);
oracleMultiply = AggregatorV3Interface(CHAINLINK_MKR_ETH);
oracleMultiply.setPrice(_priceTop, 1e8, vm);
oracleDivide = AggregatorV3Interface(CHAINLINK_FIL_ETH);
oracleDivide.setPrice(_priceDiv, 1e8, vm);
uint256 _oracleNormalization = 1e18;
(
address _rateContract,
bytes memory _rateInitData
) = fuzzyRateCalculator(
2,
_minInterest,
_vertexInterest,
_maxInterest,
_vertexUtilization
);
startHoax(COMPTROLLER_ADDRESS);
setWhitelistTrue();
vm.stopPrank();
address[] memory _borrowerWhitelist = _maxLTV >= LTV_PRECISION
? users
: new address[](0);
address[] memory _lenderWhitelist = _maxLTV >= LTV_PRECISION
? users
: new address[](0);
uint256 borrowerDebtWrong;
{
// wrong calculation, when addInterest isn't called before maturity date (30 days)
deployFraxlendCustom(
_oracleNormalization,
_rateContract,
_rateInitData,
_maxLTV,
_liquidationFee,
block.timestamp + 30 days,
1000 * DEFAULT_INT,
_borrowerWhitelist,
_lenderWhitelist
);
pair.updateExchangeRate();
_borrowTest(_maxLTV, 15e20, 15e23);
vm.roll(block.number + 1);
vm.warp(block.timestamp + 29 days);
vm.roll(block.number + 1);
vm.warp(block.timestamp + 2 days);
pair.addInterest();
address borrower = users[2];
uint256 borrowerShares = pair.userBorrowShares(borrower);
borrowerDebtWrong = pair.toBorrowAmount(borrowerShares, false);
}
deploySaltName = "asdf2";
uint256 borrowerDebtRight;
{
// relatively correct calculation, when addInterest is called close to maturity date
deployFraxlendCustom(
_oracleNormalization,
_rateContract,
_rateInitData,
_maxLTV,
_liquidationFee,
block.timestamp + 30 days,
1000 * DEFAULT_INT,
_borrowerWhitelist,
_lenderWhitelist
);
pair.updateExchangeRate();
_borrowTest(_maxLTV, 15e20, 15e23);
vm.roll(block.number + 1);
vm.warp(block.timestamp + 29 days);
pair.addInterest();
vm.roll(block.number + 1);
vm.warp(block.timestamp + 2 days);
pair.addInterest();
address borrower = users[2];
uint256 borrowerShares = pair.userBorrowShares(borrower);
borrowerDebtRight = pair.toBorrowAmount(borrowerShares, false);
}
// compare final debt between both scenarios
assertEq(borrowerDebtRight, borrowerDebtWrong);
}
diff --git a/src/test/e2e/BasePairTest.sol b/src/test/e2e/BasePairTest.sol
index 25114d9..27321dc 100644
--- a/src/test/e2e/BasePairTest.sol
+++ b/src/test/e2e/BasePairTest.sol
@@ -67,6 +67,8 @@ contract BasePairTest is FraxlendPairConstants, Scenarios, Test {
PairAccounting public initial;
PairAccounting public final_;
PairAccounting public net;
+ string public deploySaltName = "asdf";
+
// Users
address[] public users = [vm.addr(1), vm.addr(2), vm.addr(3), vm.addr(4), vm.addr(5)];
@@ -284,10 +286,12 @@ contract BasePairTest is FraxlendPairConstants, Scenarios, Test {
address[] memory _approvedLenders
) public {
startHoax(COMPTROLLER_ADDRESS);
+
+
{
pair = FraxlendPair(
deployer.deployCustom(
- "testname",
+ deploySaltName,
_encodeConfigData(_normalization, _rateContractAddress, _initRateData),
_maxLTV,
_liquidationFee,
Output:
Error: a == b not satisfied [uint]
Expected: 2135773332009600000000
Actual: 1541017987253789777725
Recommended Mitigation Steps
When maturity date passes and the last time _addInterest() was called was before maturity date, calculate first the interest rate for that interval as normal interest rate, and then calculate penalty rate for the remaining time.
DrakeEvans (Frax) disputed, disagreed with severity and commented:
This is mitigated by borrower calling
addInterest()this is the purpose of the public addInterest function. Alternatively they can repay their loan on time as intended.
Generally speaking, I think we should asses severity based on expected user behavior if the issue hasn’t been reported. Since there’s no warning to users to call
addInterest()before maturity date, users would simply not do that and will be charged extra interest.Alternatively they can repay their loan on time as intended.
It’s indeed a fine charged for not paying on time, but I don’t think you can wave off charging a higher fine than intended as non significant. A user might be a few minutes late and be charged a penalty rate for a few days/weeks.
DrakeEvans (Frax) acknowledged
gititGoro (judge) decreased severity to Medium and commented:
It will create an unexpected user experience to be penalized for periods in which a penalty shouldn’t apply, especially during high gas eras. If this is communicated to the user, then an acknowledged label is reasonable.
If there was an incentive for keeper type users to addInterest such as issuing a small % of shares to users who call the public addInterest then this issue can be marked invalid.
As for the severity, the existence of addInterest function as an intended mechanism to avoid this means that steps can be taken to avoid excessive interest charging. Indeed third parties can build on safe wrappers of these contracts in a downstream convex-like service. In other words, high interest charging on late debt is by no means an inevitable outcome.
For these two reasons, I’m downgrading severity to 2 and NOT marking invalid.
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 calculation applies the penalty rate to the entire interval since the last interest update without splitting it at the maturity date.
Impact
Borrowers could be charged higher interest than intended when the maturity date passes, with the extra amount growing the longer interest was not updated before maturity.
What the attacker needed
A borrower has an outstanding loan; interest is not updated before maturity; interest is updated after maturity
The fix
The interest for the period before maturity should be calculated at the normal rate, and the penalty rate applied only to the time after maturity.
How it was found
- Deploy a pair with a maturity date 30 days in the future.
- Borrow tokens and then advance time by 29 days without calling addInterest.
- Advance time by 2 more days (past maturity) and call addInterest.
- Compare the resulting debt with a scenario where addInterest was called just before maturity.
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.
A smart contract charges extra interest (a penalty) if a loan is not repaid by its due date. The bug is that when the contract updates the interest after the due date, it applies the penalty rate to the whole time since the last update, even the days before the due date. So borrowers pay a higher penalty 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.