Interest rate is updated before updating the debt when repaying debt
The repay function updates interest rates before reducing the debt, so the new rate is calculated using the old debt amount. This causes suppliers to keep accruing interest based on the previous debt even after all borrows are repaid.
000000 Follow with 0xweebad, A2-security and 11 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/413
Found by
000000, 0xweebad, A2-security, JCN, Obsidian, Tendency, TessKimy, Varun_05, almurhasan, ether_sky, imsrybr0, lemonmon, stuart_the_minion, trachev
Summary
Interest rate is updated before updating the debt when repaying debt in BorrowLogic@executeRepay leading to an incorrect total debt being used when calculating the new interest rates and causing suppliers to keep accruing interest based on the previous debt and even if there are no ongoing borrows anymore.
Root Cause
function executeRepay(
DataTypes.ReserveData storage reserve,
DataTypes.PositionBalance storage balances,
DataTypes.ReserveSupplies storage totalSupplies,
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ExecuteRepayParams memory params
) external returns (DataTypes.SharesType memory payback) {
DataTypes.ReserveCache memory cache = reserve.cache(totalSupplies);
reserve.updateState(params.reserveFactor, cache);
payback.assets = balances.getDebtBalance(cache.nextBorrowIndex);
// Allows a user to max repay without leaving dust from interest.
if (params.amount == type(uint256).max) {
params.amount = payback.assets;
}
ValidationLogic.validateRepay(params.amount, payback.assets);
// If paybackAmount is more than what the user wants to payback, the set it to the
// user input (ie params.amount)
if (params.amount < payback.assets) payback.assets = params.amount;
reserve.updateInterestRates( // <==== Audit
totalSupplies,
cache,
params.asset,
IPool(params.pool).getReserveFactor(),
payback.assets,
0,
params.position,
params.data.interestRateData
);
// update balances and total supplies
payback.shares = balances.repayDebt(totalSupplies, payback.assets, cache.nextBorrowIndex); // <==== Audit
cache.nextDebtShares = totalSupplies.debtShares; // <==== Audit
if (balances.getDebtBalance(cache.nextBorrowIndex) == 0) {
userConfig.setBorrowing(reserve.id, false);
}
IERC20(params.asset).safeTransferFrom(msg.sender, address(this), payback.assets);
emit PoolEventsLib.Repay(params.asset, params.position, msg.sender, payback.assets);
}
function updateInterestRates(
DataTypes.ReserveData storage _reserve,
DataTypes.ReserveSupplies storage totalSupplies,
DataTypes.ReserveCache memory _cache,
address _reserveAddress,
uint256 _reserveFactor,
uint256 _liquidityAdded,
uint256 _liquidityTaken,
bytes32 _position,
bytes memory _data
) internal {
UpdateInterestRatesLocalVars memory vars;
vars.totalDebt = _cache.nextDebtShares.rayMul(_cache.nextBorrowIndex); // <==== Audit
(vars.nextLiquidityRate, vars.nextBorrowRate) = IReserveInterestRateStrategy(_reserve.interestRateStrategyAddress)
.calculateInterestRates(
_position,
_data,
DataTypes.CalculateInterestRatesParams({
liquidityAdded: _liquidityAdded, // <==== Audit
liquidityTaken: _liquidityTaken,
totalDebt: vars.totalDebt, // <==== Audit
reserveFactor: _reserveFactor,
reserve: _reserveAddress
})
);
_reserve.liquidityRate = vars.nextLiquidityRate.toUint128();
_reserve.borrowRate = vars.nextBorrowRate.toUint128();
if (_liquidityAdded > 0) totalSupplies.underlyingBalance += _liquidityAdded.toUint128();
else if (_liquidityTaken > 0) totalSupplies.underlyingBalance -= _liquidityTaken.toUint128();
emit PoolEventsLib.ReserveDataUpdated(
_reserveAddress, vars.nextLiquidityRate, vars.nextBorrowRate, _cache.nextLiquidityIndex, _cache.nextBorrowIndex
);
}
Interest rate is updated before repaying the debt and updating the cached nextDebtShares which is then used in the interest rate calculation causing it to return a wrong interest rate as it behaves like liquidity was just supplied by the borrower without a change in debt.
Internal pre-conditions
N/A
External pre-conditions
N/A
Attack Path
- Bob supplies
tokenB - Alice supplies
tokenA - Alice borrows
tokenBcausing the utilization goes up and interest rate is updated - Bob starts accruing interest
- Alice fully repays
tokenBbut the interest rate is not updated correctly - Bob keeps accruing interest
Impact
Bob keeps accruing interest rate based on the previous debt and even if there are no ongoing borrows and can withdraw it at the expense of other suppliers.
PoC
function testRepay() external {
_mintAndApprove(alice, tokenA, 3000 ether, address(pool));
_mintAndApprove(alice, tokenB, 1000 ether, address(pool));
_mintAndApprove(bob, tokenB, 5000 ether, address(pool));
vm.startPrank(bob);
pool.supplySimple(address(tokenB), bob, 500 ether, 0);
skip(12);
vm.startPrank(alice);
pool.supplySimple(address(tokenA), alice, 1000 ether, 0);
skip(12);
oracleA.updateRoundTimestamp();
oracleB.updateRoundTimestamp();
pool.borrowSimple(address(tokenB), alice, 375 ether, 0);
skip(12);
pool.repaySimple(address(tokenB), type(uint256).max, 0);
vm.stopPrank();
bytes32 bobPos = keccak256(abi.encodePacked(bob, 'index', uint256(0)));
uint256 bobSupplyAssetsBefore = pool.supplyAssets(address(tokenB), bobPos);
skip(24 * 30 * 60 * 60);
pool.forceUpdateReserve(address(tokenB));
assertGt(pool.supplyAssets(address(tokenB), bobPos), bobSupplyAssetsBefore); // Bob accrued interest even if there are no borrows anymore
vm.startPrank(bob);
// Reverts because Bob shares with the accrued interest exceed the pool balance but
// succeed if there were other suppliers.
vm.expectRevert();
pool.withdrawSimple(address(tokenB), bob, type(uint256).max, 0);
vm.stopPrank();
}
Mitigation
diff --git a/zerolend-one/contracts/core/pool/logic/BorrowLogic.sol b/zerolend-one/contracts/core/pool/logic/BorrowLogic.sol
index 92806b1..c070fb1 100644
--- a/zerolend-one/contracts/core/pool/logic/BorrowLogic.sol
+++ b/zerolend-one/contracts/core/pool/logic/BorrowLogic.sol
@@ -136,6 +136,14 @@ library BorrowLogic {
// user input (ie params.amount)
if (params.amount < payback.assets) payback.assets = params.amount;
+ // update balances and total supplies
+ payback.shares = balances.repayDebt(totalSupplies, payback.assets, cache.nextBorrowIndex);
+ cache.nextDebtShares = totalSupplies.debtShares;
+
+ if (balances.getDebtBalance(cache.nextBorrowIndex) == 0) {
+ userConfig.setBorrowing(reserve.id, false);
+ }
+
reserve.updateInterestRates(
totalSupplies,
cache,
@@ -147,14 +155,6 @@ library BorrowLogic {
params.data.interestRateData
);
- // update balances and total supplies
- payback.shares = balances.repayDebt(totalSupplies, payback.assets, cache.nextBorrowIndex);
- cache.nextDebtShares = totalSupplies.debtShares;
-
- if (balances.getDebtBalance(cache.nextBorrowIndex) == 0) {
- userConfig.setBorrowing(reserve.id, false);
- }
-
IERC20(params.asset).safeTransferFrom(msg.sender, address(this), payback.assets);
emit PoolEventsLib.Repay(params.asset, params.position, msg.sender, payback.assets);
}
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
The repay function calls updateInterestRates before reducing the debt balance, so the interest rate calculation uses the old debt amount.
Impact
Suppliers keep accruing interest based on the previous debt even after all borrows are repaid, and can withdraw it at the expense of other suppliers.
What the attacker needed
A lending pool with borrowers and suppliers; a borrower fully repays their debt
The fix
The fix is to update the debt balances and total debt shares before calling updateInterestRates, so the interest rate is calculated with the correct post-repayment debt.
How it was found
- Bob supplies tokenB to the pool.
- Alice supplies tokenA to the pool.
- Alice borrows tokenB, increasing utilisation and updating the interest rate.
- Bob starts accruing interest on his supplied tokenB.
- Alice fully repays tokenB, but the interest rate is not updated correctly.
- Bob continues to accrue interest based on the previous debt even though there are no ongoing borrows.
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.
In a lending pool, interest rates depend on how much is borrowed. When someone repays, the debt should go down and the interest rate should drop. Here, the code recalculates the interest rate before reducing the debt, so the rate stays high and suppliers earn interest that isn't backed by any borrower.
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.